/* * Created on Oct 27, 2004 * Copyright (C) Andrea Schweer, 2004 * * This file is part of the Greenstone Alerting Service. * Refer to the COPYING file in the base directory of this package * for licensing information. */ package org.greenstone.gsdlas; import java.util.*; import javax.servlet.http.HttpSession; import org.greenstone.gsdlas.profiles.*; /** * Storage for all Profiles. Singleton. * * @author schweer * */ public class ProfileStore { private static ProfileStore instance; private Map docIdEqualsSubscriptions = new TreeMap(); private Map collectionIdEqualsSubscriptions = new TreeMap(); private Map docCollectionIdEqualsSubscriptions = new TreeMap(); private Set noEqualsSubscriptions = new TreeSet(); private ProfileStore() { } /** * @return the one existing instance of this class. */ public static ProfileStore getInstance() { if (instance == null) { instance = new ProfileStore(); } return instance; } /** * @param subscription * the subscription to add to the profile store. */ private void addSubscription(Subscription subscription) { Predicate documentIdPredicate = subscription.getPredicate("documentID"); Predicate collectionIdPredicate = subscription.getPredicate("collectionID"); if (documentIdPredicate != null) { IdEqualsPredicate docIdPredicate = (IdEqualsPredicate) documentIdPredicate; if (collectionIdPredicate != null) { IdEqualsPredicate collIdPredicate = (IdEqualsPredicate) collectionIdPredicate; String[] values = new String[2]; values[0] = docIdPredicate.getValue(); values[1] = collIdPredicate.getValue(); Set subList = (Set) docCollectionIdEqualsSubscriptions.get(values); if (subList == null) { subList = new TreeSet(); docCollectionIdEqualsSubscriptions.put(values, subList); } subList.add(subscription); } else { String value = docIdPredicate.getValue(); Set subList = (Set) docIdEqualsSubscriptions.get(value); if (subList == null) { subList = new TreeSet(); docIdEqualsSubscriptions.put(value, subList); } subList.add(subscription); } } else if (collectionIdPredicate != null) { IdEqualsPredicate collIdPredicate = (IdEqualsPredicate) collectionIdPredicate; String value = collIdPredicate.getValue(); Set subList = (Set) collectionIdEqualsSubscriptions.get(value); if (subList == null) { subList = new TreeSet(); collectionIdEqualsSubscriptions.put(value, subList); } subList.add(subscription); } else { noEqualsSubscriptions.add(subscription); } } /** * @return */ public Set getSubscriptionsWithoutEqualsPredicates() { return Collections.unmodifiableSet(noEqualsSubscriptions); } /** * Computes the list of equality subscriptions satisfied by the event. * @param event * @return */ public Set getPartiallyMatchedSubscriptions(Map event) { Set result = new TreeSet(); if (event.get("documentID") != null) { String documentID = (String) event.get("documentID"); if (event.get("collectionID") != null) { String collectionID = (String) event.get("collectionID"); String[] values = new String[] {documentID, collectionID}; Set fromDocAndCollectionId = (Set) docCollectionIdEqualsSubscriptions.get(values); if (fromDocAndCollectionId != null) { result.addAll(fromDocAndCollectionId); } Set fromCollectionId = (Set) collectionIdEqualsSubscriptions.get(collectionID); if (fromCollectionId != null) { result.addAll(fromCollectionId); } } Set fromDocId = (Set) collectionIdEqualsSubscriptions.get(documentID); if (fromDocId != null) { result.addAll(fromDocId); } } else if (event.get("collectionID") != null){ String collectionID = (String) event.get("collectionID"); Set fromCollectionId = (Set) collectionIdEqualsSubscriptions.get(collectionID); if (fromCollectionId != null) { result.addAll(fromCollectionId); } } return result; } /** * * @param gsComm * @return * @throws UnsupportedFieldException */ private Set findMatchedQueryPredicates(Map event, GreenstoneCommunicator gsComm) { Set matchedPreds = new TreeSet(); // iterate through all document content predicates // execute the query stored in the predicate // predicate is matched iff the docID occurs in the query result for (Iterator iter = PredicateFactory.getQueryPredicates("document_content").iterator(); iter.hasNext();) { Predicate predicate = (Predicate) iter.next(); String collection = (String) event.get("collectionID"); String query = predicate.getValue(); Set results; try { results = gsComm.fullTextSearch(collection, query); if (results.contains(event.get("documentID"))) { matchedPreds.add(predicate); } } catch (Exception e) { } } return matchedPreds; } /** * Computes the set of SubstringMatchPredicates satisfied by the event. * @param event * * @return */ private Set findMatchedSubstringMatchPredicates(Map event) { Set result = new TreeSet(); for (Iterator iter = PredicateFactory.getAllSubstringMatchPredicates().iterator(); iter.hasNext();) { Predicate predicate = (Predicate) iter.next(); if (predicate.isSatisfied(event)) { result.add(predicate); } } return result; } /** * Filters the event against the profiles. It uses the equality-preferred * algorithm as described in Fabret, Llirbat, Pereira and Shasha: * Efficient Matching for Content-based Publish/Subscribe * Systems. * @param event * @param gsComm */ public Set filter(Map event, GreenstoneCommunicator gsComm) { Set matchedSubscriptions = new TreeSet(); Set partiallyMatchedSubscriptions = getPartiallyMatchedSubscriptions(event); Set matchedPredicates = findMatchedSubstringMatchPredicates(event); matchedPredicates.addAll(findMatchedQueryPredicates(event, gsComm)); Map numMatchedPredicates = new TreeMap(); for (Iterator iter = matchedPredicates.iterator(); iter.hasNext();) { Predicate pred = (Predicate) iter.next(); for (Iterator iterator = pred.getSubscriptionIDs().iterator(); iterator .hasNext();) { // TODO change to subscription IDs Subscription sub = (Subscription) iterator.next(); Integer count = (Integer) numMatchedPredicates.get(sub); int newCountValue = (count == null ? 1 : count.intValue() + 1); numMatchedPredicates.put(sub, new Integer(newCountValue)); } } Set notUnmatchedSubscriptions = partiallyMatchedSubscriptions; notUnmatchedSubscriptions.addAll(noEqualsSubscriptions); for (Iterator iter = notUnmatchedSubscriptions.iterator(); iter .hasNext();) { Subscription sub = (Subscription) iter.next(); int matchedPredicatesCount = 0; if (numMatchedPredicates.get(sub) != null) { matchedPredicatesCount = ((Integer) numMatchedPredicates.get(sub)).intValue(); } if (matchedPredicatesCount == sub.getNumOfNonEqualsPredicates()) { matchedSubscriptions.add(sub); } } return matchedSubscriptions; } public String toString() { Set allSubscriptions = getAllSubscriptions(); StringBuffer buffer = new StringBuffer(); for (Iterator iter = allSubscriptions.iterator(); iter.hasNext();) { Subscription sub = (Subscription) iter.next(); buffer.append(sub); buffer.append("\n"); } return buffer.toString(); } /** * @return */ public Set getAllSubscriptions() { Set allSubscriptions = new TreeSet(); allSubscriptions.addAll(noEqualsSubscriptions); for(Iterator iter = docIdEqualsSubscriptions.values().iterator(); iter.hasNext();) { Set values = (Set) iter.next(); allSubscriptions.addAll(values); } for(Iterator iter = docCollectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) { Set values = (Set) iter.next(); allSubscriptions.addAll(values); } for(Iterator iter = collectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) { Set values = (Set) iter.next(); allSubscriptions.addAll(values); } return Collections.unmodifiableSet(allSubscriptions); } /** * @param arguments * @throws Exception */ public void createSubscription(Map arguments) throws Exception { Subscription sub = new Subscription(arguments); addSubscription(sub); } /** * @param subscriptionID */ public void deleteSubscription(String subscriptionID) { // TODO Auto-generated method stub } /** * @param arguments * @param session */ public void changeSubscription(Map arguments, HttpSession session) { String subscriptionID = (String) arguments.get("subscriptionID"); // TODO Auto-generated method stub } }