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 | // TODO restore subscriptions from database
|
---|
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 | // TODO change to lists
|
---|
57 | List documentIdPredicate = subscription.getPredicateList("documentID");
|
---|
58 | List collectionIdPredicate = subscription.getPredicateList("collectionID");
|
---|
59 | List hostIdPredicate = subscription.getPredicateList("hostID");
|
---|
60 | List typePredicate = subscription.getPredicateList("type");
|
---|
61 |
|
---|
62 | Integer subId = new Integer(subscription.getId());
|
---|
63 |
|
---|
64 | if (documentIdPredicate.isEmpty()) {
|
---|
65 | if (!dontCareSubscriptions.containsKey("documentID")) {
|
---|
66 | dontCareSubscriptions.put("documentID", new TreeSet());
|
---|
67 | }
|
---|
68 | Set list = (Set) dontCareSubscriptions.get("documentID");
|
---|
69 | list.add(subId);
|
---|
70 | }
|
---|
71 | if (collectionIdPredicate.isEmpty()) {
|
---|
72 | if (!dontCareSubscriptions.containsKey("collectionID")) {
|
---|
73 | dontCareSubscriptions.put("collectionID", new TreeSet());
|
---|
74 | }
|
---|
75 | Set list = (Set) dontCareSubscriptions.get("collectionID");
|
---|
76 | list.add(subId);
|
---|
77 | }
|
---|
78 | if (hostIdPredicate.isEmpty()) {
|
---|
79 | if (!dontCareSubscriptions.containsKey("hostID")) {
|
---|
80 | dontCareSubscriptions.put("hostID", new TreeSet());
|
---|
81 | }
|
---|
82 | Set list = (Set) dontCareSubscriptions.get("hostID");
|
---|
83 | list.add(subId);
|
---|
84 | }
|
---|
85 | if (typePredicate.isEmpty()) {
|
---|
86 | if (!dontCareSubscriptions.containsKey("type")) {
|
---|
87 | dontCareSubscriptions.put("type", new TreeSet());
|
---|
88 | }
|
---|
89 | Set list = (Set) dontCareSubscriptions.get("type");
|
---|
90 | list.add(subId);
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (documentIdPredicate.isEmpty() && collectionIdPredicate.isEmpty()
|
---|
94 | && hostIdPredicate.isEmpty() && typePredicate.isEmpty()) {
|
---|
95 | noEqualsSubscriptions.add(subId);
|
---|
96 | }
|
---|
97 | subscriptions.put(subId, subscription);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * @return
|
---|
102 | */
|
---|
103 | public Set getSubscriptionsWithoutEqualsPredicates() {
|
---|
104 | return Collections.unmodifiableSet(noEqualsSubscriptions);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Computes the list of equality subscriptions satisfied by the event.
|
---|
109 | * @param event
|
---|
110 | * @return
|
---|
111 | */
|
---|
112 | public Set getPartiallyMatchedSubscriptions(Map event) {
|
---|
113 | Set result = new TreeSet();
|
---|
114 | // TODO rework algo for getting partially matched subs
|
---|
115 | IdEqualsPredicate predicate;
|
---|
116 | predicate = PredicateFactory.getIdEqualsPredicate("type", (String)event.get("type"));
|
---|
117 | if (predicate != null)
|
---|
118 | result.addAll(predicate.getSubscriptionIDs());
|
---|
119 |
|
---|
120 | Set docIDSubs = new TreeSet();
|
---|
121 | predicate = PredicateFactory.getIdEqualsPredicate("documentID", (String)event.get("documentID"));
|
---|
122 | if (predicate != null)
|
---|
123 | docIDSubs.addAll(predicate.getSubscriptionIDs());
|
---|
124 | docIDSubs.addAll(getDontCareSubscriptions("documentID"));
|
---|
125 | result.retainAll(docIDSubs);
|
---|
126 |
|
---|
127 | Set collIDSubs = new TreeSet();
|
---|
128 | predicate = PredicateFactory.getIdEqualsPredicate("collectionID", (String)event.get("collectionID"));
|
---|
129 | if (predicate != null)
|
---|
130 | collIDSubs.addAll(predicate.getSubscriptionIDs());
|
---|
131 | collIDSubs.addAll(getDontCareSubscriptions("collectionID"));
|
---|
132 | result.retainAll(collIDSubs);
|
---|
133 |
|
---|
134 | Set hostIDSubs = new TreeSet();
|
---|
135 | predicate = PredicateFactory.getIdEqualsPredicate("hostID", (String)event.get("hostID"));
|
---|
136 | if (predicate != null)
|
---|
137 | hostIDSubs.addAll(predicate.getSubscriptionIDs());
|
---|
138 | hostIDSubs.addAll(getDontCareSubscriptions("hostID"));
|
---|
139 | result.retainAll(hostIDSubs);
|
---|
140 |
|
---|
141 | return result;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | *
|
---|
146 | * @param gsComm
|
---|
147 | * @return
|
---|
148 | * @throws UnsupportedFieldException
|
---|
149 | */
|
---|
150 | private Set findMatchedQueryPredicates(Map event, GreenstoneCommunicator gsComm) {
|
---|
151 | Set matchedPreds = new TreeSet();
|
---|
152 |
|
---|
153 | // iterate through all document content predicates
|
---|
154 | // execute the query stored in the predicate
|
---|
155 | // predicate is matched iff the docID occurs in the query result
|
---|
156 |
|
---|
157 | for (Iterator iter = PredicateFactory.getQueryPredicates("document_content").iterator(); iter.hasNext();) {
|
---|
158 | Predicate predicate = (Predicate) iter.next();
|
---|
159 | String collection = (String) event.get("collectionID");
|
---|
160 | String query = predicate.getValue();
|
---|
161 | Set results;
|
---|
162 | try {
|
---|
163 | results = gsComm.fullTextSearch(collection, query);
|
---|
164 | if (results.contains(event.get("documentID"))) {
|
---|
165 | matchedPreds.add(predicate);
|
---|
166 | }
|
---|
167 | } catch (Exception e) {
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | return matchedPreds;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Computes the set of SubstringMatchPredicates satisfied by the event.
|
---|
176 | * @param event
|
---|
177 | *
|
---|
178 | * @return
|
---|
179 | */
|
---|
180 | private Set findMatchedSubstringMatchPredicates(Map event) {
|
---|
181 | Set result = new TreeSet();
|
---|
182 |
|
---|
183 | for (Iterator iter = PredicateFactory.getAllSubstringMatchPredicates().iterator(); iter.hasNext();) {
|
---|
184 | Predicate predicate = (Predicate) iter.next();
|
---|
185 | if (predicate.isSatisfied(event)) {
|
---|
186 | result.add(predicate);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | return result;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Filters the event against the profiles. It uses the equality-preferred
|
---|
194 | * algorithm as described in Fabret, Llirbat, Pereira and Shasha:
|
---|
195 | * <em>Efficient Matching for Content-based Publish/Subscribe
|
---|
196 | * Systems</em>.
|
---|
197 | * @param event
|
---|
198 | * @param gsComm
|
---|
199 | */
|
---|
200 | public Set filter(Map event, GreenstoneCommunicator gsComm) {
|
---|
201 | Set matchedSubscriptions = new TreeSet();
|
---|
202 |
|
---|
203 | Set partiallyMatchedSubscriptions
|
---|
204 | = getPartiallyMatchedSubscriptions(event);
|
---|
205 |
|
---|
206 | Set matchedPredicates = findMatchedSubstringMatchPredicates(event);
|
---|
207 | matchedPredicates.addAll(findMatchedQueryPredicates(event, gsComm));
|
---|
208 |
|
---|
209 | Map numMatchedPredicates = new TreeMap();
|
---|
210 | for (Iterator iter = matchedPredicates.iterator(); iter.hasNext();) {
|
---|
211 | Predicate pred = (Predicate) iter.next();
|
---|
212 | for (Iterator iterator = pred.getSubscriptionIDs().iterator(); iterator
|
---|
213 | .hasNext();) {
|
---|
214 | Integer subId = (Integer) iterator.next();
|
---|
215 | Subscription sub = (Subscription) subscriptions.get(subId);
|
---|
216 | Integer count = (Integer) numMatchedPredicates.get(subId);
|
---|
217 | int newCountValue = (count == null ? 1 : count.intValue() + 1);
|
---|
218 | numMatchedPredicates.put(subId,
|
---|
219 | new Integer(newCountValue));
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | Set notUnmatchedSubscriptions = partiallyMatchedSubscriptions;
|
---|
224 | notUnmatchedSubscriptions.addAll(noEqualsSubscriptions);
|
---|
225 | for (Iterator iter = notUnmatchedSubscriptions.iterator(); iter
|
---|
226 | .hasNext();) {
|
---|
227 | Integer subId = (Integer) iter.next();
|
---|
228 | Subscription sub = (Subscription) subscriptions.get(subId);
|
---|
229 | int matchedPredicatesCount = 0;
|
---|
230 | if (numMatchedPredicates.get(subId) != null) {
|
---|
231 | matchedPredicatesCount = ((Integer) numMatchedPredicates.get(subId)).intValue();
|
---|
232 | }
|
---|
233 | if (matchedPredicatesCount == sub.getNumOfNonEqualsPredicates()) {
|
---|
234 | matchedSubscriptions.add(sub);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | return matchedSubscriptions;
|
---|
238 | }
|
---|
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 | // * @return
|
---|
256 | // */
|
---|
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 |
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * @param arguments
|
---|
281 | * @throws Exception
|
---|
282 | */
|
---|
283 | public void createSubscription(Map arguments) throws Exception {
|
---|
284 | Subscription sub = new Subscription(arguments);
|
---|
285 | addSubscription(sub);
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * @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
|
---|
298 |
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * @param arguments
|
---|
303 | * @param session
|
---|
304 | */
|
---|
305 | public void changeSubscription(Map arguments, HttpSession session) {
|
---|
306 | String subscriptionID = (String) arguments.get("subscriptionID");
|
---|
307 | // TODO Auto-generated method stub
|
---|
308 |
|
---|
309 | }
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * @param username
|
---|
313 | * @return
|
---|
314 | * @throws DatabaseException
|
---|
315 | */
|
---|
316 | public Set getAllSubscriptionsFor(String username) throws DatabaseException {
|
---|
317 | Set result = new TreeSet();
|
---|
318 | try {
|
---|
319 | Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
|
---|
320 | Statement statement = conn.createStatement();
|
---|
321 | String sqlString = "SELECT id FROM subscriptions " +
|
---|
322 | "WHERE user like '" + username + "';";
|
---|
323 | ResultSet idList = statement.executeQuery(sqlString);
|
---|
324 | while (idList.next()) {
|
---|
325 | int id = idList.getInt("id");
|
---|
326 | result.add(subscriptions.get(new Integer(id)));
|
---|
327 | }
|
---|
328 | } catch (SQLException e) {
|
---|
329 | e.printStackTrace();
|
---|
330 | }
|
---|
331 |
|
---|
332 | return result;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * @param string
|
---|
337 | * @return
|
---|
338 | */
|
---|
339 | private Set getDontCareSubscriptions(String field) {
|
---|
340 | Set result = new TreeSet();
|
---|
341 | if (dontCareSubscriptions.containsKey(field)) {
|
---|
342 | result = (Set) dontCareSubscriptions.get(field);
|
---|
343 | }
|
---|
344 | return result;
|
---|
345 | }
|
---|
346 |
|
---|
347 | } |
---|