Ignore:
Timestamp:
2004-12-06T11:44:03+13:00 (19 years ago)
Author:
schweer
Message:

user authentication works; user information and subscriptions/predicates are stored to thedatabase

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java

    r8719 r8738  
    99package org.greenstone.gsdlas.profiles;
    1010
     11import java.sql.*;
     12import java.sql.Connection;
     13import java.sql.Statement;
    1114import java.util.*;
    1215import java.util.Map;
    1316import java.util.TreeMap;
     17
     18import org.greenstone.gsdlas.database.DatabaseException;
     19import org.greenstone.gsdlas.database.DatabaseManager;
    1420
    1521/**
     
    2127public class Subscription implements Comparable {
    2228    private Map map;
     29   
    2330    private int id;
     31    private String username;
     32    private String name;
     33    private String email;
     34    private boolean rssNotification;
     35    private boolean eventsSincePageNotification;
     36   
    2437    private int numOfNonEqualsPredicates;
    2538   
    26     private static int highestId = 0;
    27 
    28     public Subscription(Map valueMap) throws ParseException {
     39    public Subscription(Map valueMap) throws DatabaseException, SQLException {
    2940        map = new TreeMap();
    3041        for (Iterator iter = valueMap.keySet().iterator(); iter.hasNext();) {
    3142            String key = (String) iter.next();
    32             String value = (String) valueMap.get(key);
    33             Predicate predicate = PredicateFactory.createPredicate(key, value);
    34             map.put(key, predicate);
     43            Object value = valueMap.get(key);
     44           
     45            Predicate predicate = null;
     46            if (value instanceof String) {
     47                predicate = PredicateFactory.createPredicate(key, (String) value); 
     48                map.put(key, predicate);
     49            } else if (value instanceof List) {
     50                List values = (List)value;
     51                predicate = PredicateFactory.createPredicate(key, values); 
     52                map.put(key, predicate);
     53            }
    3554           
    3655            if (predicate != null && !(predicate instanceof IdEqualsPredicate)) {
     
    3857            }
    3958        }
    40         id = highestId++;
     59       
     60        System.out.println("finished creating predicates");
     61       
     62        username = (String) valueMap.get("username");
     63        name = (String) valueMap.get("subscription_name");
     64        email = (String) valueMap.get("email");
     65        rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss");
     66        eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page");
     67        id = saveToDatabase(true);
     68       
     69        for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
     70            Predicate predicate = (Predicate) iter.next();
     71            if (predicate != null) {
     72                predicate.addSubscription(id);
     73            }
     74        }
    4175    }
    4276   
     
    95129        return new Integer(id).compareTo(new Integer(other.id));
    96130    }
     131   
     132    private int saveToDatabase(boolean initial) throws DatabaseException, SQLException {
     133        Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
     134        Statement statement = conn.createStatement();
     135        String sqlString;
     136        if (initial) {
     137            sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " +
     138                    "VALUES ('" + name + "','" + email + "'," + (rssNotification ? 1 : 0) +
     139                    "," + (eventsSincePageNotification ? 1: 0)+ ", '" + username + "');";
     140        } else {
     141            sqlString = "UPDATE subscriptions SET name='" + name + "', email='" +
     142                    email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" +
     143                    (eventsSincePageNotification ? 1 : 0)+ "WHERE id=" + id + ";";
     144            // cannot change user
     145        }
     146        System.out.println(sqlString);
     147        statement.executeUpdate(sqlString);
     148       
     149        sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
     150                    "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
     151                    + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
     152                    username + "';";
     153        System.out.println(sqlString);
     154        ResultSet result = statement.executeQuery(sqlString);
     155        int id;
     156        if (result.next()) {
     157            id = result.getInt("id");
     158        } else {
     159            throw new DatabaseException("Couldn't save subscription");
     160        }
     161       
     162        if (initial) {
     163           for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
     164               Predicate predicate = (Predicate) iter.next();
     165               sqlString = "INSERT INTO subs_to_predicates (subscription, predicate) " +
     166                    "VALUES (" + id + "," + predicate.getID() + ");";
     167               statement.executeUpdate(sqlString);
     168           }
     169        }
     170        return id;
     171    }
    97172}
Note: See TracChangeset for help on using the changeset viewer.