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