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.util.*;
|
---|
12 |
|
---|
13 | import javax.servlet.http.HttpSession;
|
---|
14 |
|
---|
15 | import org.greenstone.gsdlas.profiles.*;
|
---|
16 | import org.greenstone.gsdlas.profiles.IdEqualsPredicate;
|
---|
17 | import org.greenstone.gsdlas.profiles.Subscription;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Storage for all Profiles. Singleton.
|
---|
21 | *
|
---|
22 | * @author schweer
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class ProfileStore {
|
---|
26 |
|
---|
27 | private static ProfileStore instance;
|
---|
28 |
|
---|
29 | private Map docIdEqualsSubscriptions = new TreeMap();
|
---|
30 | private Map collectionIdEqualsSubscriptions = new TreeMap();
|
---|
31 | private Map docCollectionIdEqualsSubscriptions = new TreeMap();
|
---|
32 | private Set noEqualsSubscriptions = new TreeSet();
|
---|
33 |
|
---|
34 | private ProfileStore() {
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * @return the one existing instance of this class.
|
---|
40 | */
|
---|
41 | public static ProfileStore getInstance() {
|
---|
42 | if (instance == null) {
|
---|
43 | instance = new ProfileStore();
|
---|
44 | }
|
---|
45 | return instance;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * @param subscription
|
---|
50 | * the subscription to add to the profile store.
|
---|
51 | */
|
---|
52 | private void addSubscription(Subscription subscription) {
|
---|
53 | Predicate documentIdPredicate = subscription.getPredicate("documentID");
|
---|
54 | Predicate collectionIdPredicate = subscription.getPredicate("collectionID");
|
---|
55 |
|
---|
56 | if (documentIdPredicate != null) {
|
---|
57 | IdEqualsPredicate docIdPredicate
|
---|
58 | = (IdEqualsPredicate) documentIdPredicate;
|
---|
59 | if (collectionIdPredicate != null) {
|
---|
60 | IdEqualsPredicate collIdPredicate
|
---|
61 | = (IdEqualsPredicate) collectionIdPredicate;
|
---|
62 |
|
---|
63 | String[] values = new String[2];
|
---|
64 | values[0] = docIdPredicate.getValue();
|
---|
65 | values[1] = collIdPredicate.getValue();
|
---|
66 |
|
---|
67 | Set subList = (Set) docCollectionIdEqualsSubscriptions.get(values);
|
---|
68 | if (subList == null) {
|
---|
69 | subList = new TreeSet();
|
---|
70 | docCollectionIdEqualsSubscriptions.put(values, subList);
|
---|
71 | }
|
---|
72 | subList.add(subscription);
|
---|
73 | } else {
|
---|
74 | String value = docIdPredicate.getValue();
|
---|
75 | Set subList = (Set) docIdEqualsSubscriptions.get(value);
|
---|
76 | if (subList == null) {
|
---|
77 | subList = new TreeSet();
|
---|
78 | docIdEqualsSubscriptions.put(value, subList);
|
---|
79 | }
|
---|
80 | subList.add(subscription);
|
---|
81 | }
|
---|
82 | } else if (collectionIdPredicate != null) {
|
---|
83 | IdEqualsPredicate collIdPredicate
|
---|
84 | = (IdEqualsPredicate) collectionIdPredicate;
|
---|
85 |
|
---|
86 | String value = collIdPredicate.getValue();
|
---|
87 | Set subList = (Set) collectionIdEqualsSubscriptions.get(value);
|
---|
88 | if (subList == null) {
|
---|
89 | subList = new TreeSet();
|
---|
90 | collectionIdEqualsSubscriptions.put(value, subList);
|
---|
91 | }
|
---|
92 | subList.add(subscription);
|
---|
93 | } else {
|
---|
94 | noEqualsSubscriptions.add(subscription);
|
---|
95 | }
|
---|
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 | if (event.get("documentID") != null) {
|
---|
113 | String documentID = (String) event.get("documentID");
|
---|
114 | if (event.get("collectionID") != null) {
|
---|
115 | String collectionID = (String) event.get("collectionID");
|
---|
116 | String[] values = new String[] {documentID, collectionID};
|
---|
117 | Set fromDocAndCollectionId = (Set) docCollectionIdEqualsSubscriptions.get(values);
|
---|
118 | if (fromDocAndCollectionId != null) {
|
---|
119 | result.addAll(fromDocAndCollectionId);
|
---|
120 | }
|
---|
121 | Set fromCollectionId = (Set) collectionIdEqualsSubscriptions.get(collectionID);
|
---|
122 | if (fromCollectionId != null) {
|
---|
123 | result.addAll(fromCollectionId);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | Set fromDocId = (Set) collectionIdEqualsSubscriptions.get(documentID);
|
---|
127 | if (fromDocId != null) {
|
---|
128 | result.addAll(fromDocId);
|
---|
129 | }
|
---|
130 | } else if (event.get("collectionID") != null){
|
---|
131 | String collectionID = (String) event.get("collectionID");
|
---|
132 | Set fromCollectionId = (Set) collectionIdEqualsSubscriptions.get(collectionID);
|
---|
133 | if (fromCollectionId != null) {
|
---|
134 | result.addAll(fromCollectionId);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | return result;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | *
|
---|
142 | * @param gsComm TODO
|
---|
143 | * @return
|
---|
144 | * @throws UnsupportedFieldException
|
---|
145 | */
|
---|
146 | private Set findMatchedQueryPredicates(Map event, GreenstoneCommunicator gsComm) {
|
---|
147 | Set matchedPreds = new TreeSet();
|
---|
148 |
|
---|
149 | // iterate through all document content predicates
|
---|
150 | // execute the query stored in the predicate
|
---|
151 | // predicate is matched iff the docID occurs in the query result
|
---|
152 |
|
---|
153 | for (Iterator iter = PredicateFactory.getQueryPredicates("document_content").iterator(); iter.hasNext();) {
|
---|
154 | Predicate predicate = (Predicate) iter.next();
|
---|
155 | String collection = (String) event.get("collectionID");
|
---|
156 | String query = predicate.getValue();
|
---|
157 | Set results;
|
---|
158 | try {
|
---|
159 | results = gsComm.fullTextSearch(collection, query);
|
---|
160 | if (results.contains(event.get("documentID"))) {
|
---|
161 | matchedPreds.add(predicate);
|
---|
162 | }
|
---|
163 | } catch (Exception e) {
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | return matchedPreds;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Computes the set of SubstringMatchPredicates satisfied by the event.
|
---|
172 | * @param event
|
---|
173 | *
|
---|
174 | * @return
|
---|
175 | */
|
---|
176 | private Set findMatchedSubstringMatchPredicates(Map event) {
|
---|
177 | Set result = new TreeSet();
|
---|
178 |
|
---|
179 | for (Iterator iter = PredicateFactory.getAllSubstringMatchPredicates().iterator(); iter.hasNext();) {
|
---|
180 | Predicate predicate = (Predicate) iter.next();
|
---|
181 | if (predicate.isSatisfied(event)) {
|
---|
182 | result.add(predicate);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | return result;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Filters the event against the profiles. It uses the equality-preferred
|
---|
190 | * algorithm as described in Fabret, Llirbat, Pereira and Shasha:
|
---|
191 | * <em>Efficient Matching for Content-based Publish/Subscribe
|
---|
192 | * Systems</em>.
|
---|
193 | * @param event
|
---|
194 | * @param gsComm TODO
|
---|
195 | */
|
---|
196 | public Set filter(Map event, GreenstoneCommunicator gsComm) {
|
---|
197 | Set matchedSubscriptions = new TreeSet();
|
---|
198 |
|
---|
199 | Set partiallyMatchedSubscriptions
|
---|
200 | = getPartiallyMatchedSubscriptions(event);
|
---|
201 |
|
---|
202 | Set matchedPredicates = findMatchedSubstringMatchPredicates(event);
|
---|
203 | matchedPredicates.addAll(findMatchedQueryPredicates(event, gsComm));
|
---|
204 |
|
---|
205 | Map numMatchedPredicates = new TreeMap();
|
---|
206 | for (Iterator iter = matchedPredicates.iterator(); iter.hasNext();) {
|
---|
207 | Predicate pred = (Predicate) iter.next();
|
---|
208 | for (Iterator iterator = pred.getSubscriptions().iterator(); iterator
|
---|
209 | .hasNext();) {
|
---|
210 | Subscription sub = (Subscription) iterator.next();
|
---|
211 | Integer count = (Integer) numMatchedPredicates.get(sub);
|
---|
212 | int newCountValue = (count == null ? 1 : count.intValue() + 1);
|
---|
213 | numMatchedPredicates.put(sub,
|
---|
214 | new Integer(newCountValue));
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | Set notUnmatchedSubscriptions = partiallyMatchedSubscriptions;
|
---|
219 | notUnmatchedSubscriptions.addAll(noEqualsSubscriptions);
|
---|
220 | for (Iterator iter = notUnmatchedSubscriptions.iterator(); iter
|
---|
221 | .hasNext();) {
|
---|
222 | Subscription sub = (Subscription) iter.next();
|
---|
223 | int matchedPredicatesCount = 0;
|
---|
224 | if (numMatchedPredicates.get(sub) != null) {
|
---|
225 | matchedPredicatesCount = ((Integer) numMatchedPredicates.get(sub)).intValue();
|
---|
226 | }
|
---|
227 | if (matchedPredicatesCount == sub.getNumOfNonEqualsPredicates()) {
|
---|
228 | matchedSubscriptions.add(sub);
|
---|
229 | }
|
---|
230 | }
|
---|
231 | return matchedSubscriptions;
|
---|
232 | }
|
---|
233 |
|
---|
234 | public String toString() {
|
---|
235 | Set allSubscriptions = getAllSubscriptions();
|
---|
236 |
|
---|
237 | StringBuffer buffer = new StringBuffer();
|
---|
238 |
|
---|
239 | for (Iterator iter = allSubscriptions.iterator(); iter.hasNext();) {
|
---|
240 | Subscription sub = (Subscription) iter.next();
|
---|
241 | buffer.append(sub);
|
---|
242 | buffer.append("\n");
|
---|
243 | }
|
---|
244 |
|
---|
245 | return buffer.toString();
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * @return
|
---|
250 | */
|
---|
251 | public Set getAllSubscriptions() {
|
---|
252 | Set allSubscriptions = new TreeSet();
|
---|
253 | allSubscriptions.addAll(noEqualsSubscriptions);
|
---|
254 |
|
---|
255 | for(Iterator iter = docIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
|
---|
256 | Set values = (Set) iter.next();
|
---|
257 | allSubscriptions.addAll(values);
|
---|
258 | }
|
---|
259 |
|
---|
260 | for(Iterator iter = docCollectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
|
---|
261 | Set values = (Set) iter.next();
|
---|
262 | allSubscriptions.addAll(values);
|
---|
263 | }
|
---|
264 |
|
---|
265 | for(Iterator iter = collectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
|
---|
266 | Set values = (Set) iter.next();
|
---|
267 | allSubscriptions.addAll(values);
|
---|
268 | }
|
---|
269 | return Collections.unmodifiableSet(allSubscriptions);
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * @param arguments
|
---|
275 | * @throws ParseException
|
---|
276 | */
|
---|
277 | public void createSubscription(Map arguments) throws ParseException {
|
---|
278 | // TODO pass user information to profile store
|
---|
279 | Subscription sub = new Subscription(arguments);
|
---|
280 | // TODO this should probably be somewhere else
|
---|
281 | for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {
|
---|
282 | Predicate predicate = (Predicate) iter.next();
|
---|
283 | if (predicate != null) {
|
---|
284 | predicate.addSubscription(sub);
|
---|
285 | }
|
---|
286 | }
|
---|
287 | addSubscription(sub);
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * @param subscriptionID
|
---|
293 | */
|
---|
294 | public void deleteSubscription(String subscriptionID) {
|
---|
295 | // TODO Auto-generated method stub
|
---|
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 | // TODO Auto-generated method stub
|
---|
306 |
|
---|
307 | }
|
---|
308 |
|
---|
309 | } |
---|