Ignore:
Timestamp:
2004-12-14T14:28:42+13:00 (19 years ago)
Author:
schweer
Message:

filtering should work now (including multivalues); changed database based on Annika's suggestions

Location:
trunk/greenstone3-extensions/gsdl-as
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/greenstone3-extensions/gsdl-as/lib/templates/sub_collection.vm

    r8738 r8798  
    3737                    <ol>
    3838                    <li>You can choose one or more collections from the list below. Only events from the collections(s) you have chosen will be sent to you<br/>
    39                     <select multiple="multiple" name="collection" size="7" style="margin:5px 0px;">
     39                    <select multiple="multiple" name="collectionID" size="7" style="margin:5px 0px;">
    4040                        #foreach($host in $hostnames)
    4141                        <optgroup label="$host">
  • trunk/greenstone3-extensions/gsdl-as/lib/templates/sub_host.vm

    r8738 r8798  
    3737                    <ol>
    3838                    <li>You can choose one or more hosts from the list below. Only events from the host(s) you have chosen will be sent to you.<br/>
    39                     <select multiple="multiple" name="host" size="3" style="margin:5px 0px;">
     39                    <select multiple="multiple" name="hostID" size="3" style="margin:5px 0px;">
    4040                    #set($firstoption = true);
    4141                    #foreach($host in $hostnames)
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java

    r8778 r8798  
    1919import org.apache.velocity.context.Context;
    2020import org.apache.velocity.servlet.VelocityServlet;
     21import org.greenstone.gsdlas.database.DatabaseException;
     22import org.greenstone.gsdlas.profiles.Predicate;
    2123import org.greenstone.gsdlas.users.UserManagementException;
    2224import org.greenstone.gsdlas.users.UserManager;
     
    112114        if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) {
    113115            arguments.putAll(getPageArgsFromSession(session));
     116            arguments.put("username", session.getAttribute("username"));
    114117            ProfileStore.getInstance().createSubscription(arguments);
    115118            return listSubscriptions(arguments, context);
     
    165168        String username = (String) session.getAttribute("username");
    166169        context.put("title", "List of Subscriptions for " + username);
    167         context.put("list", ProfileStore.getInstance().getAllSubscriptionsFor(username));
    168         return "list.vm";
     170        try {
     171            Collection subscriptions = ProfileStore.getInstance().getAllSubscriptionsFor(username);
     172            context.put("list", subscriptions);
     173            return "list.vm";
     174        } catch (DatabaseException de) {
     175            context.put("message", "couldn't get list of subscriptions for " + username);
     176            context.put("details", de.getMessage());
     177            return "error.vm";
     178        }
    169179    }
    170180   
     
    218228        GreenstoneCommunicator gsComm = null;
    219229        try {
    220             gsComm = new GreenstoneCommunicator(new URL((String) event.get("host_url")));
     230            gsComm = new GreenstoneCommunicator(new URL((String) event.get("hostID")));
    221231        } catch (MalformedURLException e) {
    222232            // TODO Auto-generated catch block
     
    238248        for (Iterator iter = args.keySet().iterator(); iter.hasNext();) {
    239249            String key = (String) iter.next();
    240             if (key.equals("type") || key.equals("host")
    241                     || key.equals("collection") || key.equals("way")) {
     250            if (Predicate.isMultiValued(key) || key.equals("way")) {
    242251                // multi-valued attributes
    243252                String[] values = ((String[]) args.get(key));
     
    320329        } else if (nextPage.equals("collection")) {
    321330            // TODO might be query instead of just name
    322             List hostNames = (List) arguments.get("host");
     331            List hostNames = (List) arguments.get("hostID");
    323332            GreenstoneCommunicator gsComm = null;
    324333            Map collNames = new TreeMap();
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/GreenstoneCommunicator.java

    r8779 r8798  
    146146            // TODO what about the nodeType
    147147            // make sure to pass IDs of whole documents, not of parts of docs
    148             Matcher matcher = Pattern.compile("(\\d{8}:\\d+)*").matcher(docID);
     148            Matcher matcher = Pattern.compile("(\\d{8}:\\d+).*").matcher(docID);
    149149            if (matcher.matches()) {
    150150                docID = matcher.group(1);
     
    159159        Call call = new Call();
    160160        // TODO get the uri from a configuration file
    161         call.setTargetObjectURI("teevee.localsite");
     161        call.setTargetObjectURI("localsite");
    162162        call.setMethodName("process");
    163163        // set Encoding Style to use literal XML
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java

    r8780 r8798  
    99package org.greenstone.gsdlas;
    1010
     11import java.sql.*;
     12import java.sql.Connection;
     13import java.sql.Statement;
    1114import java.util.*;
    1215
    1316import javax.servlet.http.HttpSession;
    1417
     18import org.greenstone.gsdlas.database.DatabaseException;
     19import org.greenstone.gsdlas.database.DatabaseManager;
    1520import org.greenstone.gsdlas.profiles.*;
    1621
     
    2530    private static ProfileStore instance;
    2631
    27     private Set noEqualsSubscriptions = new TreeSet();
     32    private Set noEqualsSubscriptions = new TreeSet();
     33    private Map dontCareSubscriptions = new TreeMap();
    2834    private Map subscriptions = new TreeMap();
     35   
    2936
    3037    private ProfileStore() {
     38        // TODO restore subscriptions from database
    3139    }
    3240
     
    4654     */
    4755    private void addSubscription(Subscription subscription) {
    48         Predicate documentIdPredicate = subscription.getPredicate("documentID");
    49         Predicate collectionIdPredicate = subscription.getPredicate("collectionID");
    50         Predicate hostIdPredicate = subscription.getPredicate("host");
    51         Predicate typePredicate = subscription.getPredicate("type");
     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");
    5261       
    5362        Integer subId = new Integer(subscription.getId());
    5463       
    55         if (documentIdPredicate == null && collectionIdPredicate == null
    56                 && hostIdPredicate == null && typePredicate == null) {
     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()) {
    5795            noEqualsSubscriptions.add(subId);
    5896        }
     
    84122        if (predicate != null)
    85123            docIDSubs.addAll(predicate.getSubscriptionIDs());
    86         predicate = PredicateFactory.getIdEqualsPredicate("documentID", null);
    87         if (predicate != null)
    88             docIDSubs.addAll(predicate.getSubscriptionIDs());
     124        docIDSubs.addAll(getDontCareSubscriptions("documentID"));
    89125        result.retainAll(docIDSubs);
    90126       
     
    93129        if (predicate != null)
    94130            collIDSubs.addAll(predicate.getSubscriptionIDs());
    95         predicate = PredicateFactory.getIdEqualsPredicate("collectionID", null);
    96         if (predicate != null)
    97             collIDSubs.addAll(predicate.getSubscriptionIDs());
     131        collIDSubs.addAll(getDontCareSubscriptions("collectionID"));
    98132        result.retainAll(collIDSubs);
    99133       
    100134        Set hostIDSubs = new TreeSet();
    101         predicate = PredicateFactory.getIdEqualsPredicate("host", (String)event.get("host"));
     135        predicate = PredicateFactory.getIdEqualsPredicate("hostID", (String)event.get("hostID"));
    102136        if (predicate != null)
    103137            hostIDSubs.addAll(predicate.getSubscriptionIDs());
    104         predicate = PredicateFactory.getIdEqualsPredicate("host", null);
    105         if (predicate != null)
    106             hostIDSubs.addAll(predicate.getSubscriptionIDs());
     138        hostIDSubs.addAll(getDontCareSubscriptions("hostID"));
    107139        result.retainAll(hostIDSubs);
    108140       
     
    180212            for (Iterator iterator = pred.getSubscriptionIDs().iterator(); iterator
    181213                    .hasNext();) {
    182                 // TODO change to subscription IDs
    183214                Integer subId = (Integer) iterator.next();
    184215                Subscription sub = (Subscription) subscriptions.get(subId);
     
    260291     */
    261292    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
    262297        // TODO Auto-generated method stub
    263298       
     
    277312     * @param username
    278313     * @return
    279      */
    280     public Set getAllSubscriptionsFor(String username) {
     314     * @throws DatabaseException
     315     */
     316    public Set getAllSubscriptionsFor(String username) throws DatabaseException {
    281317        Set result = new TreeSet();
    282         // TODO get subscriptions from database
     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       
    283332        return result;
    284333    }
     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    }
    285346   
    286347}
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java

    r8781 r8798  
    4242    public static final String[] multiValueFields = new String[] {
    4343            "type",
    44             "host",
    45             "collection",
     44            "hostID",
     45            "collectionID",
    4646            "documentID" };
    4747   
     
    117117     */
    118118    int saveToDatabase() throws SQLException, DatabaseException {
    119         // TODO handle multi-valued predicate (in subclass?)
    120119        String query = "SELECT id FROM predicates " +
    121                 "WHERE type='single' AND field = '" + field +
    122                 "' AND singleValue = '" + value + "';";
     120                "WHERE field = '" + field +
     121                "' AND value = '" + value + "';";
    123122        Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
    124123        System.out.println(query);
     
    129128        }
    130129        // predicate has been newly created
    131         String insert = "INSERT INTO predicates (type, field, singleValue) " +
    132         "VALUES ('single', '" + field + "', '" + value + "');";
     130        String insert = "INSERT INTO predicates (field, value) " +
     131        "VALUES ('" + field + "', '" + value + "');";
    133132        System.out.println(insert);
    134133        statement.execute(insert);
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/PredicateFactory.java

    r8781 r8798  
    5959            List list = new ArrayList();
    6060            list.add(value);
    61             return createPredicate(key, list);
     61            return (Predicate) createPredicates(key, list).get(0);
    6262        } else if (key.startsWith("document")) {
    6363            result = createQueryPredicate(key, value);
     
    7171    }
    7272   
    73     public static Predicate createPredicate(String key, List values) throws SQLException, DatabaseException {
     73    public static List createPredicates(String key, List values) throws SQLException, DatabaseException {
    7474        if (!Predicate.isMultiValued(key)) {
    7575            return null;
    7676        }
    77         Predicate result = createIdEqualsPredicate(key, values);
    78         result.setID(result.saveToDatabase());
    79         return result;
     77        List results = createIdEqualsPredicates(key, values);
     78        for (Iterator iter = results.iterator(); iter.hasNext();) {
     79            IdEqualsPredicate pred = (IdEqualsPredicate) iter.next();
     80
     81            pred.setID(pred.saveToDatabase());
     82        }
     83        return results;
    8084    }
    8185
     
    103107     * @return
    104108     */
    105     private static IdEqualsPredicate createIdEqualsPredicate(String key, List values) {
    106         IdEqualsPredicate predicate = null;
     109    private static List createIdEqualsPredicates(String key, List values) {
     110        List result = new Vector();
    107111       
    108112        Map keyToPredicates;
     
    116120            String value = (String) iter.next();
    117121
    118             if (key.equals("host") && !(key.startsWith("http://"))) {
     122            if (key.equals("hostID") && !(value.startsWith("http://"))) {
    119123                value = "http://" + value + ":8080/soap/servlet/rpcrouter";
    120124            }
    121125           
     126            IdEqualsPredicate predicate = null;
    122127            if (keyToPredicates.containsKey(value)) {
    123128                predicate = (IdEqualsPredicate) keyToPredicates.get(value);
     
    126131                keyToPredicates.put(value, predicate);
    127132            }
     133            result.add(predicate);
    128134        }
    129         return predicate;
     135        return result;
    130136    }
    131137   
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/QueryPredicate.java

    r8609 r8798  
    2727        super.field = field;
    2828        super.value = query;
    29        
    30         String[] words = query.split("\\s");
    31         for (int i = 0; i < words.length; i++) {
    32             String word = words[i];
    33             if (!word.startsWith("!")) {
    34                 containsWords.add(word);
    35             } else {
    36                 if (word.length() > 1) {
    37                     notContainsWords.add(word);
    38                 } else if (i < words.length - 1) {
    39                     notContainsWords.add(words[i++]);
    40                 }
    41             }
    42         }
    4329    }
    44 
    45     /* (non-Javadoc)
    46      * @see org.greenstone.gsdlas.profiles.Predicate#isSatisfied(java.util.Map)
    47      */
    4830    public boolean isSatisfied(Map event) {
    49         // TODO more efficient implementation
    50        
    51         String value = (String) event.get(field);
    52         if (value == null) return false;
    53        
    54        
    55         for (Iterator iter = containsWords.iterator(); iter.hasNext();) {
    56             String word = (String) iter.next();
    57             if (value.indexOf(word) == -1) {
    58                 return false;
    59             }
    60         }
    61        
    62         for (Iterator iter = notContainsWords.iterator(); iter.hasNext();) {
    63             String word = (String) iter.next();
    64             if (value.indexOf(word) >= 0) {
    65                 return false;
    66             }
    67         }
    68        
    6931        return true;
    7032    }
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java

    r8781 r8798  
    4848            Object value = valueMap.get(key);
    4949           
    50             Predicate predicate = null;
    5150            if (value instanceof String) {
    52                 predicate = PredicateFactory.createPredicate(key, (String) value); 
     51                Predicate predicate = PredicateFactory.createPredicate(key, (String) value); 
    5352                map.put(key, predicate);
     53
     54                if (predicate != null && !(predicate instanceof IdEqualsPredicate)) {
     55                    numOfNonEqualsPredicates++;
     56                }
    5457            } else if (value instanceof List) {
    5558                List values = (List)value;
    56                 predicate = PredicateFactory.createPredicate(key, values); 
    57                 map.put(key, predicate);
     59                List predicates = PredicateFactory.createPredicates(key, values);
     60                map.put(key, predicates);
     61                for (Iterator iterator = predicates.iterator(); iterator
     62                        .hasNext();) {
     63                    Predicate predicate = (Predicate) iterator.next();
     64                   
     65                    if (predicate != null && !(predicate instanceof IdEqualsPredicate)) {
     66                        numOfNonEqualsPredicates++;
     67                    }
     68                }
    5869            }
    5970           
    60             if (predicate != null && !(predicate instanceof IdEqualsPredicate)) {
    61                 numOfNonEqualsPredicates++;
    62             }
    6371        }
    6472       
     
    8997   
    9098    public Predicate getPredicate(String field) {
    91         return (Predicate) map.get(field);
     99        if (map.get(field) instanceof Predicate) {
     100            return (Predicate) map.get(field);
     101        }
     102        return null;
     103    }
     104   
     105    public List getPredicateList(String field) {
     106        if (map.get(field) instanceof List) {
     107            return (List) map.get(field);
     108        }
     109        return new Vector();
    92110    }
    93111   
     
    99117        Set result = new HashSet();
    100118        for (Iterator iter = map.values().iterator(); iter.hasNext();) {
    101             Predicate pred = (Predicate) iter.next();
    102             if (pred != null) {
    103                 result.add(pred);
     119            Object next = iter.next();
     120            if (next == null) {
     121                continue;
     122            } else if (next instanceof Predicate) {
     123                result.add(next);
     124            } else if (next instanceof List) {
     125                result.addAll((Collection) next);
    104126            }
    105127        }
     
    149171            // cannot change user
    150172        }
    151         System.out.println(sqlString);
    152173        statement.executeUpdate(sqlString);
    153174       
  • trunk/greenstone3-extensions/gsdl-as/test-src/org/greenstone/gsdlas/ProfileStoreTest.java

    r8738 r8798  
    4646            event.put("documentID", "20041118:5");
    4747            event.put("collectionID", "gs3mgdemo");
    48             event.put("host", "http://localhost:8080/soap/servlet/rpcrouter");
     48            event.put("hostID", "http://localhost:8080/soap/servlet/rpcrouter");
    4949           
    5050            Set result = ProfileStore.getInstance().filter(event, gsComm);
Note: See TracChangeset for help on using the changeset viewer.