Changeset 8738 for trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java
- Timestamp:
- 2004-12-06T11:44:03+13:00 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java
r8719 r8738 9 9 package org.greenstone.gsdlas.profiles; 10 10 11 import java.sql.*; 12 import java.sql.Connection; 13 import java.sql.Statement; 11 14 import java.util.*; 12 15 import java.util.Map; 13 16 import java.util.TreeMap; 17 18 import org.greenstone.gsdlas.database.DatabaseException; 19 import org.greenstone.gsdlas.database.DatabaseManager; 14 20 15 21 /** … … 21 27 public class Subscription implements Comparable { 22 28 private Map map; 29 23 30 private int id; 31 private String username; 32 private String name; 33 private String email; 34 private boolean rssNotification; 35 private boolean eventsSincePageNotification; 36 24 37 private int numOfNonEqualsPredicates; 25 38 26 private static int highestId = 0; 27 28 public Subscription(Map valueMap) throws ParseException { 39 public Subscription(Map valueMap) throws DatabaseException, SQLException { 29 40 map = new TreeMap(); 30 41 for (Iterator iter = valueMap.keySet().iterator(); iter.hasNext();) { 31 42 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 } 35 54 36 55 if (predicate != null && !(predicate instanceof IdEqualsPredicate)) { … … 38 57 } 39 58 } 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 } 41 75 } 42 76 … … 95 129 return new Integer(id).compareTo(new Integer(other.id)); 96 130 } 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 } 97 172 }
Note:
See TracChangeset
for help on using the changeset viewer.