Changeset 8867 for trunk/greenstone3-extensions
- Timestamp:
- 2004-12-21T12:11:00+13:00 (19 years ago)
- Location:
- trunk/greenstone3-extensions/gsdl-as
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/greenstone3-extensions/gsdl-as/create-tables.sql
r8799 r8867 19 19 field varchar(128) not null, 20 20 value varchar(255), 21 type enum("org.greenstone.gsdlas.profiles.IdEqualsPredicate", "org.greenstone.gsdlas.profiles.SubstringMatchPredicate", "org.greenstone.gsdlas.profiles.QueryPredicate") not null, 21 22 22 unique(field, value)23 unique(field,type,value) 23 24 ); 24 25 -
trunk/greenstone3-extensions/gsdl-as/lib/templates/list.vm
r8720 r8867 14 14 <div class="subscription"> 15 15 <p><span class="sub_id">Subscription $sub.Id</span><br/> 16 <a href="service?action=showFeed&subscriptionID=$sub.Id">show matching events</a> | <a href="service?action=editSubscription&subscriptionID=$sub.Id">edit subscription</a> </p>16 <a href="service?action=showFeed&subscriptionID=$sub.Id">show matching events</a> | <a href="service?action=editSubscription&subscriptionID=$sub.Id">edit subscription</a> | <a href="service?action=deleteSubscription&subscriptionID=$sub.Id">delete subscription</a></p> 17 17 <table class="subscription"> 18 18 <thead> -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java
r8847 r8867 12 12 import java.net.MalformedURLException; 13 13 import java.net.URL; 14 import java.sql.SQLException; 14 15 import java.util.*; 15 16 17 import javax.servlet.ServletConfig; 18 import javax.servlet.ServletException; 16 19 import javax.servlet.http.*; 17 20 … … 33 36 public class AlertingService extends VelocityServlet { 34 37 38 /* (non-Javadoc) 39 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) 40 */ 41 public void init(ServletConfig config) throws ServletException { 42 super.init(config); 43 System.out.println("reloading subs"); 44 ProfileStore.getInstance().restoreFromDatabase(); 45 try { 46 Set subs = ProfileStore.getInstance().getAllSubscriptionsFor("andrea"); 47 System.out.println("reloaded " + subs.size() + " subs for andrea: "); 48 for (Iterator iter = subs.iterator(); iter.hasNext();) { 49 System.out.println(iter.next()); 50 } 51 } catch (DatabaseException e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } 55 } 35 56 public static final String[] actions = new String[] { 36 57 "createSubscription", … … 129 150 } 130 151 String subscriptionID = (String) arguments.get("subscriptionID"); 131 ProfileStore.getInstance().deleteSubscription(subscriptionID); 152 try { 153 ProfileStore.getInstance().deleteSubscription(subscriptionID); 154 } catch (DatabaseException e) { 155 // TODO Auto-generated catch block 156 e.printStackTrace(); 157 } catch (SQLException e) { 158 // TODO Auto-generated catch block 159 e.printStackTrace(); 160 } 132 161 return listSubscriptions(arguments, context); 133 162 } -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java
r8798 r8867 54 54 */ 55 55 private void addSubscription(Subscription subscription) { 56 // TODO change to lists57 56 List documentIdPredicate = subscription.getPredicateList("documentID"); 58 57 List collectionIdPredicate = subscription.getPredicateList("collectionID"); … … 237 236 return matchedSubscriptions; 238 237 } 239 //240 // public String toString() {241 // Set allSubscriptions = getAllSubscriptions();242 //243 // StringBuffer buffer = new StringBuffer();244 //245 // for (Iterator iter = allSubscriptions.iterator(); iter.hasNext();) {246 // Subscription sub = (Subscription) iter.next();247 // buffer.append(sub);248 // buffer.append("\n");249 // }250 //251 // return buffer.toString();252 // }253 //254 // /**255 // * @return256 // */257 // public Set getAllSubscriptions() {258 // Set allSubscriptions = new TreeSet();259 // allSubscriptions.addAll(noEqualsSubscriptions);260 //261 // for(Iterator iter = docIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {262 // Set values = (Set) iter.next();263 // allSubscriptions.addAll(values);264 // }265 //266 // for(Iterator iter = docCollectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {267 // Set values = (Set) iter.next();268 // allSubscriptions.addAll(values);269 // }270 //271 // for(Iterator iter = collectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {272 // Set values = (Set) iter.next();273 // allSubscriptions.addAll(values);274 // }275 // return Collections.unmodifiableSet(allSubscriptions);276 // }277 238 278 239 … … 289 250 /** 290 251 * @param subscriptionID 291 */ 292 public void deleteSubscription(String subscriptionID) { 293 // delete row from subscriptions table 294 // for each predicate: 295 // delete row from subs_to_predicates 296 // if there aren't any other subscriptions using this predicate, delete predicate 297 // TODO Auto-generated method stub 252 * @throws DatabaseException 253 * @throws SQLException 254 */ 255 public void deleteSubscription(String subscriptionID) throws DatabaseException, SQLException { 256 Integer subID = new Integer(subscriptionID); 257 System.out.println("deleting subscription " + subscriptionID); 258 if (!subscriptions.containsKey(subID)) { 259 return; 260 } 261 Subscription sub = (Subscription) subscriptions.get(subID); 262 for (Iterator iter = dontCareSubscriptions.values().iterator(); iter.hasNext();) { 263 Set entry = (Set) iter.next(); 264 if (entry.contains(subID)) 265 entry.remove(subID); 266 } 267 if (noEqualsSubscriptions.contains(subID)) 268 noEqualsSubscriptions.remove(subID); 269 for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) { 270 Predicate pred = (Predicate) iter.next(); 271 pred.removeSubscription(subID); 272 } 273 274 subscriptions.remove(subID); 275 276 277 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 278 279 Statement statement = conn.createStatement(); 280 281 String sqlString = "DELETE FROM subscriptions WHERE id = " + subscriptionID; 282 statement.executeUpdate(sqlString); 283 284 sqlString = "SELECT predicate FROM subs_to_predicates " + 285 "WHERE subscription = " + subscriptionID; 286 ResultSet predicates = statement.executeQuery(sqlString); 287 while (predicates.next()) { 288 // if there aren't any other subscriptions using this predicate, delete predicate 289 int predicateID = predicates.getInt("predicate"); 290 291 } 292 298 293 299 294 } … … 344 339 return result; 345 340 } 341 342 /** 343 * 344 */ 345 public void restoreFromDatabase() { 346 try { 347 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 348 Statement statement = conn.createStatement(); 349 String sqlString = "SELECT * FROM subscriptions"; 350 ResultSet subs = statement.executeQuery(sqlString); 351 while (subs.next()) { 352 // construct map for all predicates 353 int id = subs.getInt("id"); 354 Map map = new TreeMap(); 355 map.put("username", subs.getString("user")); 356 map.put("email", subs.getString("email")); 357 map.put("subscription_name", subs.getString("name")); 358 Vector ways = new Vector(); 359 if (subs.getInt("rss") != 0) { 360 ways.add("rss"); 361 } 362 if (subs.getInt("page") != 0) { 363 ways.add("page"); 364 } 365 map.put("way", ways); 366 Statement stmnt = conn.createStatement(); 367 sqlString = "SELECT p.type, p.field, p.value from subs_to_predicates stp join predicates p on(stp.predicate = p.id) where stp.subscription = " + id + ";"; 368 ResultSet predicates = stmnt.executeQuery(sqlString); 369 while (predicates.next()) { 370 String type = predicates.getString("type"); 371 String field = predicates.getString("field"); 372 if (type.equals(SubstringMatchPredicate.class.getName())) { 373 if (field.equals("colletionID")) { 374 field = "collection_query"; 375 } else if (field.equals("hostID")) { 376 field = "host_query"; 377 } 378 } 379 String value = predicates.getString("value"); 380 if (type.equals(IdEqualsPredicate.class.getName())) { 381 if (!map.containsKey(field)) { 382 map.put(field, new Vector()); 383 } 384 ((List) map.get(field)).add(value); 385 } else { 386 map.put(field, value); 387 } 388 } 389 try { 390 createSubscription(map); 391 } catch (Exception e1) { 392 // TODO Auto-generated catch block 393 e1.printStackTrace(); 394 } 395 } 396 } catch (SQLException e) { 397 // TODO Auto-generated catch block 398 e.printStackTrace(); 399 } catch (DatabaseException e) { 400 // TODO Auto-generated catch block 401 e.printStackTrace(); 402 } 403 } 346 404 347 405 } -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java
r8798 r8867 119 119 String query = "SELECT id FROM predicates " + 120 120 "WHERE field = '" + field + 121 "' AND value = '" + value + "';"; 121 "' AND value = '" + value + 122 "' AND type = '" + this.getClass().getName() + "';"; 122 123 Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement(); 123 124 System.out.println(query); 124 125 ResultSet result = statement.executeQuery(query); 125 126 if (result.next()) { // predicate already exists in database 126 System.out.println("predicate " + id + " already exists"); 127 return result.getInt("id"); 127 int id = result.getInt("id"); 128 System.out.println("predicate already exists with id " + id); 129 return id; 128 130 } 129 131 // predicate has been newly created 130 String insert = "INSERT INTO predicates (field, value) " +131 "VALUES ('" + field + "', '" + value + "');";132 String insert = "INSERT INTO predicates (field, type, value) " + 133 "VALUES ('" + field + "', '" + this.getClass().getName() + "', '" + value + "');"; 132 134 System.out.println(insert); 133 135 statement.execute(insert); … … 141 143 142 144 /** 143 * @return144 */145 public List getValueList() {146 throw new UnsupportedOperationException("you can only call this method for multi-valued predicates");147 }148 149 /**150 145 * @param key 151 146 * @return … … 154 149 return ArrayHelper.contains(singleValueFields, key) || ArrayHelper.contains(multiValueFields, key); 155 150 } 151 152 /** 153 * @param subID 154 * @throws DatabaseException 155 * @throws SQLException 156 */ 157 public void removeSubscription(Integer subID) throws SQLException, DatabaseException { 158 subscriptions.remove(subID); 159 160 Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement(); 161 String sqlString = "DELETE FROM subs_to_predicates " + 162 "WHERE subscription = " + subID + 163 " AND predicate = " + id; 164 statement.executeUpdate(sqlString); 165 166 if (subscriptions.isEmpty()) { 167 PredicateFactory.deletePredicate(this); 168 sqlString = "DELETE FROM predicates WHERE id = " + id; 169 statement.executeUpdate(sqlString); 170 } 171 } 156 172 157 173 } -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/PredicateFactory.java
r8847 r8867 208 208 return result; 209 209 } 210 211 /** 212 * @param predicate 213 */ 214 public static void deletePredicate(Predicate predicate) { 215 String field = predicate.getField(); 216 String value = predicate.getValue(); 217 218 if (predicate instanceof IdEqualsPredicate) { 219 Map map = (Map) idEqualsPredicates.get(field); 220 if (map.containsKey(value)) { 221 map.remove(value); 222 } 223 } else if (predicate instanceof QueryPredicate) { 224 if (field.equals("document_title")) { 225 documentTitleQueryPredicates.remove(value); 226 } else if (field.equals("document_content")) { 227 documentContentQueryPredicates.remove(value); 228 } else { 229 System.err.println("problem while removing: unknown field " + field); 230 } 231 } else if (predicate instanceof SubstringMatchPredicate) { 232 if (field.equals("hostID")) { 233 hostNameMatchPredicates.remove(value); 234 } else if (field.equals("collectionID")) { 235 collectionNameMatchPredicates.remove(value); 236 } else { 237 System.err.println("problem while removing: unknown field " + field); 238 } 239 } else { 240 System.err.println("problem while removing: unknown class " + predicate.getClass().getName()); 241 } 242 } 210 243 211 244 } -
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java
r8798 r8867 78 78 rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss"); 79 79 eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page"); 80 id = saveToDatabase( true);80 id = saveToDatabase(); 81 81 82 82 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) { … … 157 157 } 158 158 159 private int saveToDatabase( boolean initial) throws DatabaseException, SQLException {159 private int saveToDatabase() throws DatabaseException, SQLException { 160 160 Connection conn = DatabaseManager.getInstance().getDatabaseConnection(); 161 161 Statement statement = conn.createStatement(); 162 162 String sqlString; 163 boolean initial = true; 164 165 sqlString = "SELECT id FROM subscriptions WHERE name like '" + name + 166 "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0) 167 + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" + 168 username + "';"; 169 ResultSet result = statement.executeQuery(sqlString); 170 if (result.next()) { 171 initial = false; 172 this.id = result.getInt("id"); 173 } 174 163 175 if (initial) { 164 176 sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " + … … 168 180 sqlString = "UPDATE subscriptions SET name='" + name + "', email='" + 169 181 email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" + 170 (eventsSincePageNotification ? 1 : 0)+ " WHERE id=" +id + ";";182 (eventsSincePageNotification ? 1 : 0)+ " WHERE id=" + this.id + ";"; 171 183 // cannot change user 172 184 } … … 178 190 username + "';"; 179 191 System.out.println(sqlString); 180 ResultSetresult = statement.executeQuery(sqlString);181 int id;192 result = statement.executeQuery(sqlString); 193 182 194 if (result.next()) { 183 195 id = result.getInt("id");
Note:
See TracChangeset
for help on using the changeset viewer.