Ignore:
Timestamp:
2004-12-10T15:48:39+13:00 (19 years ago)
Author:
schweer
Message:

started to work on multi-valued predicates; started to rework the filter algorithm

Location:
trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java

    r8775 r8777  
    2121import org.greenstone.gsdlas.users.UserManagementException;
    2222import org.greenstone.gsdlas.users.UserManager;
     23import org.greenstone.gsdlas.util.ArrayHelper;
    2324
    2425/**
     
    3031public class AlertingService extends VelocityServlet {
    3132   
    32     public static final Set actions;
    33     static {
    34         Set set = new TreeSet();
    35         set.add("createSubscription");
    36         set.add("deleteSubscription");
    37         set.add("editSubscription");
    38         set.add("showFeed");
    39         set.add("listSubscriptions");
    40        
    41         set.add("login");
    42         set.add("register");
    43         set.add("logout");
    44         set.add("showLoginForm");
    45         set.add("showRegistrationForm");
    46         actions = Collections.unmodifiableSet(set);
    47     }
     33    public static final String[] actions = new String[] {
     34            "createSubscription",
     35            "deleteSubscription",
     36            "editSubscription",
     37            "showFeed",
     38            "listSubscriptions",
     39            "login",
     40            "register",
     41            "logout",
     42            "showLoginForm",
     43            "showRegistrationForm"
     44    };
    4845   
    4946   
     
    5249       
    5350        String action = req.getParameter("action");
    54        
    55         if (action == null || !actions.contains(action)) {
    56             String title = "Unknown action";
    57             String message = "I don't know how to " + action;
    58             String details = "The only actions I know are " + actions;
    59             return showError(context, message, details);
    60         }
    61        
    6251
    6352        Map args = req.getParameterMap();
    6453        // TODO stop this, we need multi-valued stuff
    6554        args = normalise(args);
    66        
     55
     56        if (action != null && action.equals("receiveEvent")) {
     57            receiveEvent(args);
     58            return null;
     59        }
     60       
     61        if (action == null || !ArrayHelper.contains(actions, action)) {
     62            String title = "Unknown action";
     63            String message = "I don't know how to " + action;
     64            String details = "The only actions I know are " + Arrays.toString(actions);
     65            return showError(context, message, details);
     66        }
     67               
    6768        String templateString = "";
    6869       
     
    163164        String username = (String) session.getAttribute("username");
    164165        context.put("title", "List of Subscriptions for " + username);
    165         context.put("list", ProfileStore.getInstance().getAllSubscriptions());
     166        context.put("list", ProfileStore.getInstance().getAllSubscriptionsFor(username));
    166167        return "list.vm";
    167168    }
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java

    r8753 r8777  
    2525    private static ProfileStore instance;
    2626
    27     private Map docIdEqualsSubscriptions = new TreeMap();
    28     private Map collectionIdEqualsSubscriptions = new TreeMap();
    29     private Map docCollectionIdEqualsSubscriptions = new TreeMap();
    3027    private Set noEqualsSubscriptions = new TreeSet();
     28    private Map subscriptions = new TreeMap();
    3129
    3230    private ProfileStore() {
     
    5048        Predicate documentIdPredicate = subscription.getPredicate("documentID");
    5149        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         }
     50        Predicate hostIdPredicate = subscription.getPredicate("host");
     51        Predicate typePredicate = subscription.getPredicate("type");
     52       
     53        Integer subId = new Integer(subscription.getId());
     54       
     55        if (documentIdPredicate == null && collectionIdPredicate == null
     56                && hostIdPredicate == null && typePredicate == null) {
     57            noEqualsSubscriptions.add(subId);
     58        }
     59        subscriptions.put(subId, subscription);
    9360    }
    9461
     
    10774    public Set getPartiallyMatchedSubscriptions(Map event) {
    10875        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         }
     76        // TODO rework algo for getting partially matched subs
     77        result.addAll(PredicateFactory.getIdEqualsPredicate("type", (String)event.get("type")).getSubscriptionIDs());
     78       
     79        Set docIDSubs = new TreeSet();
     80        docIDSubs.addAll(PredicateFactory.getIdEqualsPredicate("documentID", (String)event.get("documentID")).getSubscriptionIDs());
     81        docIDSubs.addAll(PredicateFactory.getIdEqualsPredicate("documentID", null).getSubscriptionIDs());
     82        result.retainAll(docIDSubs);
     83       
     84        Set collIDSubs = new TreeSet();
     85        collIDSubs.addAll(PredicateFactory.getIdEqualsPredicate("collectionID", (String)event.get("collectionID")).getSubscriptionIDs());
     86        collIDSubs.addAll(PredicateFactory.getIdEqualsPredicate("collectionID", null).getSubscriptionIDs());
     87        result.retainAll(collIDSubs);
     88       
     89        Set hostIDSubs = new TreeSet();
     90        hostIDSubs.addAll(PredicateFactory.getIdEqualsPredicate("host", (String)event.get("host")).getSubscriptionIDs());
     91        hostIDSubs.addAll(PredicateFactory.getIdEqualsPredicate("host", null).getSubscriptionIDs());
     92        result.retainAll(hostIDSubs);
     93       
    13494        return result;
    13595    }
     
    206166                    .hasNext();) {
    207167                // TODO change to subscription IDs
    208                 Subscription sub = (Subscription) iterator.next();
    209                 Integer count = (Integer) numMatchedPredicates.get(sub);
     168                Integer subId = (Integer) iterator.next();
     169                Subscription sub = (Subscription) subscriptions.get(subId);
     170                Integer count = (Integer) numMatchedPredicates.get(subId);
    210171                int newCountValue = (count == null ? 1 : count.intValue() + 1);
    211                 numMatchedPredicates.put(sub,
     172                numMatchedPredicates.put(subId,
    212173                        new Integer(newCountValue));
    213174            }
     
    218179        for (Iterator iter = notUnmatchedSubscriptions.iterator(); iter
    219180                .hasNext();) {
    220             Subscription sub = (Subscription) iter.next();
     181            Integer subId = (Integer) iter.next();
     182            Subscription sub = (Subscription) subscriptions.get(subId);
    221183            int matchedPredicatesCount = 0;
    222             if (numMatchedPredicates.get(sub) != null) {
    223                 matchedPredicatesCount = ((Integer) numMatchedPredicates.get(sub)).intValue();
     184            if (numMatchedPredicates.get(subId) != null) {
     185                matchedPredicatesCount = ((Integer) numMatchedPredicates.get(subId)).intValue();
    224186            }
    225187            if (matchedPredicatesCount == sub.getNumOfNonEqualsPredicates()) {
     
    229191        return matchedSubscriptions;
    230192    }
    231 
    232     public String toString() {
    233         Set allSubscriptions = getAllSubscriptions();
    234        
    235         StringBuffer buffer = new StringBuffer();
    236        
    237         for (Iterator iter = allSubscriptions.iterator(); iter.hasNext();) {
    238             Subscription sub = (Subscription) iter.next();
    239             buffer.append(sub);
    240             buffer.append("\n");
    241         }
    242        
    243         return buffer.toString();
    244     }
    245 
    246     /**
    247      * @return
    248      */
    249     public Set getAllSubscriptions() {
    250         Set allSubscriptions = new TreeSet();
    251         allSubscriptions.addAll(noEqualsSubscriptions);
    252        
    253         for(Iterator iter = docIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    254             Set values = (Set) iter.next();
    255             allSubscriptions.addAll(values);
    256         }
    257        
    258         for(Iterator iter = docCollectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    259             Set values = (Set) iter.next();
    260             allSubscriptions.addAll(values);
    261         }
    262        
    263         for(Iterator iter = collectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    264             Set values = (Set) iter.next();
    265             allSubscriptions.addAll(values);
    266         }
    267         return Collections.unmodifiableSet(allSubscriptions);
    268     }
     193//
     194//    public String toString() {
     195//        Set allSubscriptions = getAllSubscriptions();
     196//       
     197//        StringBuffer buffer = new StringBuffer();
     198//       
     199//        for (Iterator iter = allSubscriptions.iterator(); iter.hasNext();) {
     200//            Subscription sub = (Subscription) iter.next();
     201//            buffer.append(sub);
     202//            buffer.append("\n");
     203//        }
     204//       
     205//        return buffer.toString();
     206//    }
     207//
     208//    /**
     209//     * @return
     210//     */
     211//    public Set getAllSubscriptions() {
     212//        Set allSubscriptions = new TreeSet();
     213//        allSubscriptions.addAll(noEqualsSubscriptions);
     214//       
     215//        for(Iterator iter = docIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
     216//            Set values = (Set) iter.next();
     217//            allSubscriptions.addAll(values);
     218//        }
     219//       
     220//        for(Iterator iter = docCollectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
     221//            Set values = (Set) iter.next();
     222//            allSubscriptions.addAll(values);
     223//        }
     224//       
     225//        for(Iterator iter = collectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
     226//            Set values = (Set) iter.next();
     227//            allSubscriptions.addAll(values);
     228//        }
     229//        return Collections.unmodifiableSet(allSubscriptions);
     230//    }
    269231
    270232
     
    296258       
    297259    }
     260
     261    /**
     262     * @param username
     263     * @return
     264     */
     265    public Set getAllSubscriptionsFor(String username) {
     266        Set result = new TreeSet();
     267        // TODO get subscriptions from database
     268        return result;
     269    }
    298270   
    299271}
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java

    r8775 r8777  
    4343            "type",
    4444            "host",
    45             "collection" };
     45            "collection",
     46            "documentID" };
    4647   
    4748    public void addSubscription(int subscriptionID) {
     
    139140        }
    140141    }
     142
     143    /**
     144     * @return
     145     */
     146    public List getValueList() {
     147        throw new UnsupportedOperationException("you can only call this method for multi-valued predicates");
     148    }
    141149   
    142150}
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/PredicateFactory.java

    r8738 r8777  
    2727    private static int id = 0;
    2828   
    29     // documentID -> IdEqualsPredicate referring to it
    30     private static Map documentIdEqualsPredicates = new TreeMap();
    31     // collectionID -> IdEqualsPredicate referring to it
    32     private static Map collectionIdEqualsPredicates = new TreeMap();
     29    private static Map idEqualsPredicates = new TreeMap();
    3330   
    34     // substring for collection_name -> SubstringMatchPredicate referring to it
     31    // substring for collection_name -> SubstringMatchPredicates referring to it
    3532    private static Map collectionNameMatchPredicates = new TreeMap();
    3633   
     
    5956        }
    6057       
    61         if (key.equals("type")) {
    62             return result;
    63         } else if (key.endsWith("ID")) {
    64             result = createIdEqualsPredicate(key, value);
     58        if (Predicate.isMultiValued(key)) {
     59            List list = new ArrayList();
     60            list.add(value);
     61            return createPredicate(key, list);
    6562        } else if (key.startsWith("document")) {
    6663            result = createQueryPredicate(key, value);
     
    7875            return null;
    7976        }
    80         // TODO properly handle multi-valued predicates
    81         return createPredicate(key, (String) values.get(0));
     77        Predicate result = createIdEqualsPredicate(key, values);
     78        result.setID(result.saveToDatabase());
     79        return result;
    8280    }
    8381
     
    102100    /**
    103101     * @param key
    104      * @param value
     102     * @param values
    105103     * @return
    106104     */
    107     private static IdEqualsPredicate createIdEqualsPredicate(String key, String value) {
     105    private static IdEqualsPredicate createIdEqualsPredicate(String key, List values) {
    108106        IdEqualsPredicate predicate = null;
    109         if (key.equals("documentID")) {
    110             if (documentIdEqualsPredicates.containsKey(value)) {
    111                 predicate = (IdEqualsPredicate) (documentIdEqualsPredicates.get(value));
     107       
     108        Map keyToPredicates;
     109        if (!idEqualsPredicates.containsKey(key)) {
     110            keyToPredicates = new HashMap();
     111            idEqualsPredicates.put(key, keyToPredicates);
     112        }
     113        keyToPredicates = (Map) idEqualsPredicates.get(key);
     114       
     115        for (Iterator iter = values.iterator(); iter.hasNext();) {
     116            String value = (String) iter.next();
     117            if (keyToPredicates.containsKey(value)) {
     118                predicate = (IdEqualsPredicate) keyToPredicates.get(value);
    112119            } else {
    113120                predicate = new IdEqualsPredicate(key, value);
    114                 documentIdEqualsPredicates.put(value, predicate);
     121                keyToPredicates.put(value, predicate);
    115122            }
    116         } else if (key.equals("collectionID")) {
    117             if (collectionIdEqualsPredicates.containsKey(value)) {
    118                 predicate = (IdEqualsPredicate) collectionIdEqualsPredicates.get(value);
    119             } else {
    120                 predicate = new IdEqualsPredicate(key, value);
    121                 collectionIdEqualsPredicates.put(value, predicate);
    122             }
    123         } // TODO other IDs
     123        }
    124124        return predicate;
    125125    }
     
    161161        return Collections.unmodifiableCollection(collectionNameMatchPredicates.values());
    162162    }
     163
     164    /**
     165     * @param string
     166     * @param string2
     167     * @return
     168     */
     169    public static Predicate getIdEqualsPredicate(String key, String value) {
     170        return (Predicate) ((Map)idEqualsPredicates.get(key)).get(value);
     171    }
    163172   
    164173}
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java

    r8738 r8777  
    170170        return id;
    171171    }
     172   
    172173}
Note: See TracChangeset for help on using the changeset viewer.