source: trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java@ 8888

Last change on this file since 8888 was 8888, checked in by schweer, 19 years ago

notifications

  • Property svn:keywords set to Author Date Id Revision
File size: 15.2 KB
Line 
1/*
2 * Created on Oct 27, 2004
3 * Copyright (C) Andrea Schweer, 2004
4 *
5 * This file is part of the Greenstone Alerting Service.
6 * Refer to the COPYING file in the base directory of this package
7 * for licensing information.
8 */
9package org.greenstone.gsdlas;
10
11import java.sql.*;
12import java.sql.Connection;
13import java.sql.Statement;
14import java.util.*;
15
16import javax.servlet.http.HttpSession;
17
18import org.greenstone.gsdlas.database.DatabaseException;
19import org.greenstone.gsdlas.database.DatabaseManager;
20import org.greenstone.gsdlas.profiles.*;
21
22/**
23 * Storage for all Profiles. Singleton.
24 *
25 * @author schweer
26 *
27 */
28public class ProfileStore {
29
30 private static ProfileStore instance;
31
32 private Set noEqualsSubscriptions = new TreeSet();
33 private Map dontCareSubscriptions = new TreeMap();
34 private Map subscriptions = new TreeMap();
35
36
37 private ProfileStore() {
38 // hide default constructor
39 }
40
41 /**
42 * @return the one existing instance of this class.
43 */
44 public static ProfileStore getInstance() {
45 if (instance == null) {
46 instance = new ProfileStore();
47 }
48 return instance;
49 }
50
51 /**
52 * @param subscription
53 * the subscription to add to the profile store.
54 */
55 private void addSubscription(Subscription subscription) {
56 List documentIdPredicate = subscription.getPredicateList(Constants.DOCUMENT_ID_FIELD);
57 List collectionIdPredicate = subscription.getPredicateList(Constants.COLLECTION_ID_FIELD);
58 List hostIdPredicate = subscription.getPredicateList(Constants.HOST_ID_FIELD);
59 List typePredicate = subscription.getPredicateList(Constants.TYPE_FIELD);
60
61 Integer subId = new Integer(subscription.getId());
62
63 if (documentIdPredicate.isEmpty()) {
64 if (!dontCareSubscriptions.containsKey(Constants.DOCUMENT_ID_FIELD)) {
65 dontCareSubscriptions.put(Constants.DOCUMENT_ID_FIELD, new TreeSet());
66 }
67 Set list = (Set) dontCareSubscriptions.get(Constants.DOCUMENT_ID_FIELD);
68 list.add(subId);
69 }
70 if (hostIdPredicate.isEmpty()) {
71 if (collectionIdPredicate.isEmpty()) {
72 if (!dontCareSubscriptions.containsKey(Constants.COLLECTION_ID_FIELD)) {
73 dontCareSubscriptions.put(Constants.COLLECTION_ID_FIELD, new TreeSet());
74 }
75 Set list = (Set) dontCareSubscriptions.get(Constants.COLLECTION_ID_FIELD);
76 list.add(subId);
77 }
78 }
79 if (typePredicate.isEmpty()) {
80 if (!dontCareSubscriptions.containsKey(Constants.TYPE_FIELD)) {
81 dontCareSubscriptions.put(Constants.TYPE_FIELD, new TreeSet());
82 }
83 Set list = (Set) dontCareSubscriptions.get(Constants.TYPE_FIELD);
84 list.add(subId);
85 }
86
87 if (documentIdPredicate.isEmpty() && collectionIdPredicate.isEmpty()
88 && hostIdPredicate.isEmpty() && typePredicate.isEmpty()) {
89 noEqualsSubscriptions.add(subId);
90 }
91 subscriptions.put(subId, subscription);
92 }
93
94 /**
95 * @return
96 */
97 public Set getSubscriptionsWithoutEqualsPredicates() {
98 return Collections.unmodifiableSet(noEqualsSubscriptions);
99 }
100
101 /**
102 * Computes the list of equality subscriptions satisfied by the event.
103 * @param event
104 * @return
105 */
106 public Set getPartiallyMatchedSubscriptions(Map event) {
107 Set result = new TreeSet();
108
109 EqualsPredicate predicate;
110
111 // all subscriptions with matching type...
112 predicate = PredicateFactory.getEqualsPredicate(Constants.TYPE_FIELD,
113 (String)event.get(Constants.TYPE_FIELD));
114 if (predicate != null)
115 result.addAll(predicate.getSubscriptionIDs());
116
117 // ...but only those subscriptions where the doc id matches...
118 Set docIDSubs = new TreeSet();
119 predicate = PredicateFactory.getEqualsPredicate(Constants.DOCUMENT_ID_FIELD,
120 (String)event.get(Constants.DOCUMENT_ID_FIELD));
121 if (predicate != null)
122 docIDSubs.addAll(predicate.getSubscriptionIDs());
123 docIDSubs.addAll(getDontCareSubscriptions(Constants.DOCUMENT_ID_FIELD));
124 result.retainAll(docIDSubs);
125
126 // ...and only those subscriptions where the host/collection id matches:
127 Set hostCollIDSubs = new TreeSet();
128 // i.e.: host|coll is "don't care"...
129 hostCollIDSubs.addAll(getDontCareSubscriptions(Constants.COLLECTION_ID_FIELD));
130 // ...or host matches (-> coll is "don't care" for this host)...
131 predicate = PredicateFactory.getEqualsPredicate(Constants.HOST_ID_FIELD,
132 (String)event.get(Constants.HOST_ID_FIELD));
133 if (predicate != null)
134 hostCollIDSubs.addAll(predicate.getSubscriptionIDs());
135 // ...or host|coll matches
136 predicate = PredicateFactory.getEqualsPredicate(Constants.COLLECTION_ID_FIELD,
137 (String)event.get(Constants.COLLECTION_ID_FIELD));
138 if (predicate != null)
139 hostCollIDSubs.addAll(predicate.getSubscriptionIDs());
140
141 result.retainAll(hostCollIDSubs);
142
143 return result;
144 }
145
146 /**
147 *
148 * @param gsComm
149 * @return
150 * @throws UnsupportedFieldException
151 */
152 private Set findMatchedQueryPredicates(Map event, GreenstoneCommunicator gsComm) {
153 Set matchedPreds = new TreeSet();
154
155 // iterate through all document content predicates
156 // execute the query stored in the predicate
157 // predicate is matched iff the docID occurs in the query result
158
159 for (Iterator iter = PredicateFactory.getQueryPredicates(Constants.DOCUMENT_CONTENT_FIELD).iterator(); iter.hasNext();) {
160 Predicate predicate = (Predicate) iter.next();
161 String collection = (String) event.get(Constants.COLLECTION_ID_FIELD);
162 String query = predicate.getValue();
163 Set results;
164 try {
165 results = gsComm.fullTextSearch(collection, query);
166 if (results.contains(event.get(Constants.DOCUMENT_ID_FIELD))) {
167 matchedPreds.add(predicate);
168 }
169 } catch (Exception e) {
170 }
171 }
172
173 return matchedPreds;
174 }
175
176 /**
177 * Computes the set of SubstringMatchPredicates satisfied by the event.
178 * @param event
179 *
180 * @return
181 */
182 private Set findMatchedSubstringMatchPredicates(Map event) {
183 Set result = new TreeSet();
184
185 for (Iterator iter = PredicateFactory.getAllSubstringMatchPredicates().iterator(); iter.hasNext();) {
186 Predicate predicate = (Predicate) iter.next();
187 if (predicate.isSatisfied(event)) {
188 result.add(predicate);
189 }
190 }
191 return result;
192 }
193
194 /**
195 * Filters the event against the profiles. It uses the equality-preferred
196 * algorithm as described in Fabret, Llirbat, Pereira and Shasha:
197 * <em>Efficient Matching for Content-based Publish/Subscribe
198 * Systems</em>.
199 * @param event
200 * @param gsComm
201 */
202 public Set filter(Map event, GreenstoneCommunicator gsComm) {
203 Set matchedSubscriptions = new TreeSet();
204
205 Set partiallyMatchedSubscriptions
206 = getPartiallyMatchedSubscriptions(event);
207
208 Set matchedPredicates = findMatchedSubstringMatchPredicates(event);
209 matchedPredicates.addAll(findMatchedQueryPredicates(event, gsComm));
210
211 Map numMatchedPredicates = new TreeMap();
212 for (Iterator iter = matchedPredicates.iterator(); iter.hasNext();) {
213 Predicate pred = (Predicate) iter.next();
214 for (Iterator iterator = pred.getSubscriptionIDs().iterator(); iterator
215 .hasNext();) {
216 Integer subId = (Integer) iterator.next();
217 Subscription sub = (Subscription) subscriptions.get(subId);
218 Integer count = (Integer) numMatchedPredicates.get(subId);
219 int newCountValue = (count == null ? 1 : count.intValue() + 1);
220 numMatchedPredicates.put(subId,
221 new Integer(newCountValue));
222 }
223 }
224
225 Set notUnmatchedSubscriptions = partiallyMatchedSubscriptions;
226 notUnmatchedSubscriptions.addAll(noEqualsSubscriptions);
227 for (Iterator iter = notUnmatchedSubscriptions.iterator(); iter
228 .hasNext();) {
229 Integer subId = (Integer) iter.next();
230 Subscription sub = (Subscription) subscriptions.get(subId);
231 int matchedPredicatesCount = 0;
232 if (numMatchedPredicates.get(subId) != null) {
233 matchedPredicatesCount = ((Integer) numMatchedPredicates.get(subId)).intValue();
234 }
235 if (matchedPredicatesCount == sub.getNumOfNonEqualsPredicates()) {
236 matchedSubscriptions.add(sub);
237 }
238 }
239 return matchedSubscriptions;
240 }
241
242
243 /**
244 * @param arguments
245 * @throws Exception
246 */
247 public void createSubscription(Map arguments) throws Exception {
248 Subscription sub = new Subscription(arguments);
249 addSubscription(sub);
250 }
251
252
253 /**
254 * @param subscriptionID
255 * @throws DatabaseException
256 * @throws SQLException
257 */
258 public void deleteSubscription(String subscriptionID) throws DatabaseException, SQLException {
259 Integer subID = new Integer(subscriptionID);
260 System.out.println("deleting subscription " + subscriptionID);
261 if (!subscriptions.containsKey(subID)) {
262 return;
263 }
264 Subscription sub = (Subscription) subscriptions.get(subID);
265 for (Iterator iter = dontCareSubscriptions.values().iterator(); iter.hasNext();) {
266 Set entry = (Set) iter.next();
267 if (entry.contains(subID))
268 entry.remove(subID);
269 }
270 if (noEqualsSubscriptions.contains(subID))
271 noEqualsSubscriptions.remove(subID);
272 for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {
273 Predicate pred = (Predicate) iter.next();
274 pred.removeSubscription(subID);
275 }
276
277 subscriptions.remove(subID);
278
279
280 Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
281
282 Statement statement = conn.createStatement();
283
284 String sqlString = "DELETE FROM subscriptions WHERE id = " + subscriptionID;
285 statement.executeUpdate(sqlString);
286
287 sqlString = "SELECT predicate FROM subs_to_predicates " +
288 "WHERE subscription = " + subscriptionID;
289 ResultSet predicates = statement.executeQuery(sqlString);
290 while (predicates.next()) {
291 // if there aren't any other subscriptions using this predicate, delete predicate
292 int predicateID = predicates.getInt("predicate");
293
294 }
295
296
297 }
298
299 /**
300 * @param arguments
301 * @param session
302 */
303 public void changeSubscription(Map arguments, HttpSession session) {
304 String subscriptionID = (String) arguments.get("subscriptionID");
305 // BTS 14 really edit the subscription
306
307 }
308
309 /**
310 * @param username
311 * @return
312 * @throws DatabaseException
313 */
314 public Set getAllSubscriptionsFor(String username) throws DatabaseException {
315 Set result = new TreeSet();
316 try {
317 Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
318 Statement statement = conn.createStatement();
319 String sqlString = "SELECT id FROM subscriptions " +
320 "WHERE user like '" + username + "';";
321 ResultSet idList = statement.executeQuery(sqlString);
322 while (idList.next()) {
323 int id = idList.getInt("id");
324 result.add(subscriptions.get(new Integer(id)));
325 }
326 } catch (SQLException e) {
327 e.printStackTrace();
328 }
329
330 return result;
331 }
332
333 /**
334 * @param string
335 * @return
336 */
337 private Set getDontCareSubscriptions(String field) {
338 Set result = new TreeSet();
339 if (dontCareSubscriptions.containsKey(field)) {
340 result = (Set) dontCareSubscriptions.get(field);
341 }
342 return result;
343 }
344
345 /**
346 *
347 */
348 public void restoreFromDatabase() {
349 try {
350 Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
351 Statement statement = conn.createStatement();
352 String sqlString = "SELECT * FROM subscriptions";
353 ResultSet subs = statement.executeQuery(sqlString);
354 while (subs.next()) {
355 // construct map for all predicates
356 int id = subs.getInt("id");
357 Map map = new TreeMap();
358 map.put("username", subs.getString("user"));
359 map.put("email", subs.getString("email"));
360 map.put("subscription_name", subs.getString("name"));
361 Vector ways = new Vector();
362 if (subs.getInt("rss") != 0) {
363 ways.add("rss");
364 }
365 if (subs.getInt("page") != 0) {
366 ways.add("page");
367 }
368 map.put("way", ways);
369 Statement stmnt = conn.createStatement();
370 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 + ";";
371 ResultSet predicates = stmnt.executeQuery(sqlString);
372 while (predicates.next()) {
373 String type = predicates.getString(Constants.TYPE_FIELD);
374 String field = predicates.getString("field");
375 if (type.equals(SubstringMatchPredicate.class.getName())) {
376 if (field.equals(Constants.COLLECTION_ID_FIELD)) {
377 field = Constants.COLLECTION_QUERY_FIELD;
378 } else if (field.equals(Constants.HOST_ID_FIELD)) {
379 field = Constants.HOST_QUERY_FIELD;
380 }
381 }
382 String value = predicates.getString("value");
383 if (type.equals(EqualsPredicate.class.getName())) {
384 if (!map.containsKey(field)) {
385 map.put(field, new Vector());
386 }
387 ((List) map.get(field)).add(value);
388 } else {
389 map.put(field, value);
390 }
391 }
392 try {
393 createSubscription(map);
394 } catch (Exception e1) {
395 // TODO Auto-generated catch block
396 e1.printStackTrace();
397 }
398 }
399 } catch (SQLException e) {
400 // TODO Auto-generated catch block
401 e.printStackTrace();
402 } catch (DatabaseException e) {
403 // TODO Auto-generated catch block
404 e.printStackTrace();
405 }
406 }
407
408 /**
409 * @param i
410 * @return
411 */
412 public Subscription getSubscription(int id) {
413 return (Subscription) subscriptions.get(new Integer(id));
414 }
415
416}
Note: See TracBrowser for help on using the repository browser.