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

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

user authentication works; user information and subscriptions/predicates are stored to thedatabase

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