Changeset 8867


Ignore:
Timestamp:
2004-12-21T12:11:00+13:00 (19 years ago)
Author:
schweer
Message:

fixed BTS 12: restore subscriptions/predicates from database

Location:
trunk
Files:
21 edited

Legend:

Unmodified
Added
Removed
  • trunk/greenstone3-extensions/gsdl-as/create-tables.sql

    r8799 r8867  
    1919    field varchar(128) not null,
    2020    value varchar(255),
     21    type enum("org.greenstone.gsdlas.profiles.IdEqualsPredicate", "org.greenstone.gsdlas.profiles.SubstringMatchPredicate", "org.greenstone.gsdlas.profiles.QueryPredicate") not null,
    2122   
    22     unique(field,value)
     23    unique(field,type,value)
    2324);
    2425
  • trunk/greenstone3-extensions/gsdl-as/lib/templates/list.vm

    r8720 r8867  
    1414    <div class="subscription">
    1515        <p><span class="sub_id">Subscription $sub.Id</span><br/>
    16         <a href="service?action=showFeed&subscriptionID=$sub.Id">show matching events</a> | <a href="service?action=editSubscription&subscriptionID=$sub.Id">edit subscription</a></p>
     16        <a href="service?action=showFeed&subscriptionID=$sub.Id">show matching events</a> | <a href="service?action=editSubscription&subscriptionID=$sub.Id">edit subscription</a> | <a href="service?action=deleteSubscription&subscriptionID=$sub.Id">delete subscription</a></p>
    1717        <table class="subscription">
    1818            <thead>
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java

    r8847 r8867  
    1212import java.net.MalformedURLException;
    1313import java.net.URL;
     14import java.sql.SQLException;
    1415import java.util.*;
    1516
     17import javax.servlet.ServletConfig;
     18import javax.servlet.ServletException;
    1619import javax.servlet.http.*;
    1720
     
    3336public class AlertingService extends VelocityServlet {
    3437   
     38    /* (non-Javadoc)
     39     * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
     40     */
     41    public void init(ServletConfig config) throws ServletException {
     42        super.init(config);
     43        System.out.println("reloading subs");
     44        ProfileStore.getInstance().restoreFromDatabase();
     45        try {
     46            Set subs = ProfileStore.getInstance().getAllSubscriptionsFor("andrea");
     47            System.out.println("reloaded " + subs.size() + " subs for andrea: ");
     48            for (Iterator iter = subs.iterator(); iter.hasNext();) {
     49                System.out.println(iter.next());
     50            }
     51        } catch (DatabaseException e) {
     52            // TODO Auto-generated catch block
     53            e.printStackTrace();
     54        }
     55    }
    3556    public static final String[] actions = new String[] {
    3657            "createSubscription",
     
    129150        }
    130151        String subscriptionID = (String) arguments.get("subscriptionID");
    131         ProfileStore.getInstance().deleteSubscription(subscriptionID);
     152        try {
     153            ProfileStore.getInstance().deleteSubscription(subscriptionID);
     154        } catch (DatabaseException e) {
     155            // TODO Auto-generated catch block
     156            e.printStackTrace();
     157        } catch (SQLException e) {
     158            // TODO Auto-generated catch block
     159            e.printStackTrace();
     160        }
    132161        return listSubscriptions(arguments, context);
    133162    }
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java

    r8798 r8867  
    5454     */
    5555    private void addSubscription(Subscription subscription) {
    56         // TODO change to lists
    5756        List documentIdPredicate = subscription.getPredicateList("documentID");
    5857        List collectionIdPredicate = subscription.getPredicateList("collectionID");
     
    237236        return matchedSubscriptions;
    238237    }
    239 //
    240 //    public String toString() {
    241 //        Set allSubscriptions = getAllSubscriptions();
    242 //       
    243 //        StringBuffer buffer = new StringBuffer();
    244 //       
    245 //        for (Iterator iter = allSubscriptions.iterator(); iter.hasNext();) {
    246 //            Subscription sub = (Subscription) iter.next();
    247 //            buffer.append(sub);
    248 //            buffer.append("\n");
    249 //        }
    250 //       
    251 //        return buffer.toString();
    252 //    }
    253 //
    254 //    /**
    255 //     * @return
    256 //     */
    257 //    public Set getAllSubscriptions() {
    258 //        Set allSubscriptions = new TreeSet();
    259 //        allSubscriptions.addAll(noEqualsSubscriptions);
    260 //       
    261 //        for(Iterator iter = docIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    262 //            Set values = (Set) iter.next();
    263 //            allSubscriptions.addAll(values);
    264 //        }
    265 //       
    266 //        for(Iterator iter = docCollectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    267 //            Set values = (Set) iter.next();
    268 //            allSubscriptions.addAll(values);
    269 //        }
    270 //       
    271 //        for(Iterator iter = collectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    272 //            Set values = (Set) iter.next();
    273 //            allSubscriptions.addAll(values);
    274 //        }
    275 //        return Collections.unmodifiableSet(allSubscriptions);
    276 //    }
    277238
    278239
     
    289250    /**
    290251     * @param subscriptionID
    291      */
    292     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
    297         // TODO Auto-generated method stub
     252     * @throws DatabaseException
     253     * @throws SQLException
     254     */
     255    public void deleteSubscription(String subscriptionID) throws DatabaseException, SQLException {
     256        Integer subID = new Integer(subscriptionID);
     257        System.out.println("deleting subscription " + subscriptionID);
     258        if (!subscriptions.containsKey(subID)) {
     259            return;
     260        }
     261        Subscription sub = (Subscription) subscriptions.get(subID);
     262        for (Iterator iter = dontCareSubscriptions.values().iterator(); iter.hasNext();) {
     263            Set entry = (Set) iter.next();
     264            if (entry.contains(subID))
     265                entry.remove(subID);
     266        }
     267        if (noEqualsSubscriptions.contains(subID))
     268            noEqualsSubscriptions.remove(subID);
     269        for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {
     270            Predicate pred = (Predicate) iter.next();
     271            pred.removeSubscription(subID);
     272        }
     273
     274        subscriptions.remove(subID);
     275       
     276       
     277        Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
     278       
     279        Statement statement = conn.createStatement();
     280       
     281        String sqlString = "DELETE FROM subscriptions WHERE id = " + subscriptionID;
     282        statement.executeUpdate(sqlString);
     283       
     284        sqlString = "SELECT predicate FROM subs_to_predicates " +
     285                "WHERE subscription = " + subscriptionID;
     286        ResultSet predicates = statement.executeQuery(sqlString);
     287        while (predicates.next()) {
     288            // if there aren't any other subscriptions using this predicate, delete predicate
     289            int predicateID = predicates.getInt("predicate");
     290           
     291        }
     292       
    298293       
    299294    }
     
    344339        return result;
    345340    }
     341
     342    /**
     343     *
     344     */
     345    public void restoreFromDatabase() {
     346        try {
     347            Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
     348            Statement statement = conn.createStatement();
     349            String sqlString = "SELECT * FROM subscriptions";
     350            ResultSet subs = statement.executeQuery(sqlString);
     351            while (subs.next()) {
     352                // construct map for all predicates
     353                int id = subs.getInt("id");
     354                Map map = new TreeMap();
     355                map.put("username", subs.getString("user"));
     356                map.put("email", subs.getString("email"));
     357                map.put("subscription_name", subs.getString("name"));
     358                Vector ways = new Vector();
     359                if (subs.getInt("rss") != 0) {
     360                    ways.add("rss");
     361                }
     362                if (subs.getInt("page") != 0) {
     363                    ways.add("page");
     364                }
     365                map.put("way", ways);
     366                Statement stmnt = conn.createStatement();
     367                sqlString = "SELECT p.type, p.field, p.value from subs_to_predicates stp join predicates p on(stp.predicate = p.id) where stp.subscription = " + id + ";";
     368                ResultSet predicates = stmnt.executeQuery(sqlString);
     369                while (predicates.next()) {
     370                    String type = predicates.getString("type");
     371                    String field = predicates.getString("field");
     372                    if (type.equals(SubstringMatchPredicate.class.getName())) {
     373                        if (field.equals("colletionID")) {
     374                            field = "collection_query";
     375                        } else if (field.equals("hostID")) {
     376                            field = "host_query";
     377                        }
     378                    }
     379                    String value = predicates.getString("value");
     380                    if (type.equals(IdEqualsPredicate.class.getName())) {
     381                        if (!map.containsKey(field)) {
     382                            map.put(field, new Vector());
     383                        }
     384                        ((List) map.get(field)).add(value);
     385                    } else {
     386                        map.put(field, value);
     387                    }
     388                }
     389                try {
     390                    createSubscription(map);
     391                } catch (Exception e1) {
     392                    // TODO Auto-generated catch block
     393                    e1.printStackTrace();
     394                }
     395            }
     396        } catch (SQLException e) {
     397            // TODO Auto-generated catch block
     398            e.printStackTrace();
     399        } catch (DatabaseException e) {
     400            // TODO Auto-generated catch block
     401            e.printStackTrace();
     402        }
     403    }
    346404   
    347405}
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java

    r8798 r8867  
    119119        String query = "SELECT id FROM predicates " +
    120120                "WHERE field = '" + field +
    121                 "' AND value = '" + value + "';";
     121                "' AND value = '" + value +
     122                "' AND type = '" + this.getClass().getName() + "';";
    122123        Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
    123124        System.out.println(query);
    124125        ResultSet result = statement.executeQuery(query);
    125126        if (result.next()) { // predicate already exists in database
    126             System.out.println("predicate " + id + " already exists");
    127             return result.getInt("id");
     127            int id = result.getInt("id");
     128            System.out.println("predicate already exists with id " + id);
     129            return id;
    128130        }
    129131        // predicate has been newly created
    130         String insert = "INSERT INTO predicates (field, value) " +
    131         "VALUES ('" + field + "', '" + value + "');";
     132        String insert = "INSERT INTO predicates (field, type, value) " +
     133        "VALUES ('" + field + "', '" + this.getClass().getName() + "', '" + value + "');";
    132134        System.out.println(insert);
    133135        statement.execute(insert);
     
    141143
    142144    /**
    143      * @return
    144      */
    145     public List getValueList() {
    146         throw new UnsupportedOperationException("you can only call this method for multi-valued predicates");
    147     }
    148 
    149     /**
    150145     * @param key
    151146     * @return
     
    154149        return ArrayHelper.contains(singleValueFields, key) || ArrayHelper.contains(multiValueFields, key);
    155150    }
     151
     152    /**
     153     * @param subID
     154     * @throws DatabaseException
     155     * @throws SQLException
     156     */
     157    public void removeSubscription(Integer subID) throws SQLException, DatabaseException {
     158        subscriptions.remove(subID);
     159
     160        Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
     161        String sqlString = "DELETE FROM subs_to_predicates " +
     162            "WHERE subscription = " + subID +
     163            " AND predicate = " + id;
     164        statement.executeUpdate(sqlString);
     165       
     166        if (subscriptions.isEmpty()) {
     167            PredicateFactory.deletePredicate(this);
     168            sqlString = "DELETE FROM predicates WHERE id = " + id;
     169            statement.executeUpdate(sqlString);
     170        }
     171    }
    156172   
    157173}
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/PredicateFactory.java

    r8847 r8867  
    208208        return result;
    209209    }
     210
     211    /**
     212     * @param predicate
     213     */
     214    public static void deletePredicate(Predicate predicate) {
     215        String field = predicate.getField();
     216        String value = predicate.getValue();
     217       
     218        if (predicate instanceof IdEqualsPredicate) {
     219            Map map = (Map) idEqualsPredicates.get(field);
     220            if (map.containsKey(value)) {
     221                map.remove(value);
     222            }
     223        } else if (predicate instanceof QueryPredicate) {
     224            if (field.equals("document_title")) {
     225                documentTitleQueryPredicates.remove(value);
     226            } else if (field.equals("document_content")) {
     227                documentContentQueryPredicates.remove(value);
     228            } else {
     229                System.err.println("problem while removing: unknown field " + field);
     230            }
     231        } else if (predicate instanceof SubstringMatchPredicate) {
     232            if (field.equals("hostID")) {
     233                hostNameMatchPredicates.remove(value);
     234            } else if (field.equals("collectionID")) {
     235                collectionNameMatchPredicates.remove(value);
     236            } else {
     237                System.err.println("problem while removing: unknown field " + field);
     238            }
     239        } else {
     240            System.err.println("problem while removing: unknown class " + predicate.getClass().getName());
     241        }
     242    }
    210243   
    211244}
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java

    r8798 r8867  
    7878        rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss");
    7979        eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page");
    80         id = saveToDatabase(true);
     80        id = saveToDatabase();
    8181       
    8282        for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
     
    157157    }
    158158   
    159     private int saveToDatabase(boolean initial) throws DatabaseException, SQLException {
     159    private int saveToDatabase() throws DatabaseException, SQLException {
    160160        Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
    161161        Statement statement = conn.createStatement();
    162162        String sqlString;
     163        boolean initial = true;
     164       
     165        sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
     166                    "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
     167                    + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
     168                    username + "';";
     169        ResultSet result = statement.executeQuery(sqlString);
     170        if (result.next()) {
     171            initial = false;
     172            this.id = result.getInt("id");
     173        }
     174       
    163175        if (initial) {
    164176            sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " +
     
    168180            sqlString = "UPDATE subscriptions SET name='" + name + "', email='" +
    169181                    email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" +
    170                     (eventsSincePageNotification ? 1 : 0)+ "WHERE id=" + id + ";";
     182                    (eventsSincePageNotification ? 1 : 0)+ " WHERE id=" + this.id + ";";
    171183            // cannot change user
    172184        }
     
    178190                    username + "';";
    179191        System.out.println(sqlString);
    180         ResultSet result = statement.executeQuery(sqlString);
    181         int id;
     192        result = statement.executeQuery(sqlString);
     193       
    182194        if (result.next()) {
    183195            id = result.getInt("id");
  • trunk/gsdl3/extensions/gsdl-as/create-tables.sql

    r8799 r8867  
    1919    field varchar(128) not null,
    2020    value varchar(255),
     21    type enum("org.greenstone.gsdlas.profiles.IdEqualsPredicate", "org.greenstone.gsdlas.profiles.SubstringMatchPredicate", "org.greenstone.gsdlas.profiles.QueryPredicate") not null,
    2122   
    22     unique(field,value)
     23    unique(field,type,value)
    2324);
    2425
  • trunk/gsdl3/extensions/gsdl-as/lib/templates/list.vm

    r8720 r8867  
    1414    <div class="subscription">
    1515        <p><span class="sub_id">Subscription $sub.Id</span><br/>
    16         <a href="service?action=showFeed&subscriptionID=$sub.Id">show matching events</a> | <a href="service?action=editSubscription&subscriptionID=$sub.Id">edit subscription</a></p>
     16        <a href="service?action=showFeed&subscriptionID=$sub.Id">show matching events</a> | <a href="service?action=editSubscription&subscriptionID=$sub.Id">edit subscription</a> | <a href="service?action=deleteSubscription&subscriptionID=$sub.Id">delete subscription</a></p>
    1717        <table class="subscription">
    1818            <thead>
  • trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java

    r8847 r8867  
    1212import java.net.MalformedURLException;
    1313import java.net.URL;
     14import java.sql.SQLException;
    1415import java.util.*;
    1516
     17import javax.servlet.ServletConfig;
     18import javax.servlet.ServletException;
    1619import javax.servlet.http.*;
    1720
     
    3336public class AlertingService extends VelocityServlet {
    3437   
     38    /* (non-Javadoc)
     39     * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
     40     */
     41    public void init(ServletConfig config) throws ServletException {
     42        super.init(config);
     43        System.out.println("reloading subs");
     44        ProfileStore.getInstance().restoreFromDatabase();
     45        try {
     46            Set subs = ProfileStore.getInstance().getAllSubscriptionsFor("andrea");
     47            System.out.println("reloaded " + subs.size() + " subs for andrea: ");
     48            for (Iterator iter = subs.iterator(); iter.hasNext();) {
     49                System.out.println(iter.next());
     50            }
     51        } catch (DatabaseException e) {
     52            // TODO Auto-generated catch block
     53            e.printStackTrace();
     54        }
     55    }
    3556    public static final String[] actions = new String[] {
    3657            "createSubscription",
     
    129150        }
    130151        String subscriptionID = (String) arguments.get("subscriptionID");
    131         ProfileStore.getInstance().deleteSubscription(subscriptionID);
     152        try {
     153            ProfileStore.getInstance().deleteSubscription(subscriptionID);
     154        } catch (DatabaseException e) {
     155            // TODO Auto-generated catch block
     156            e.printStackTrace();
     157        } catch (SQLException e) {
     158            // TODO Auto-generated catch block
     159            e.printStackTrace();
     160        }
    132161        return listSubscriptions(arguments, context);
    133162    }
  • trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java

    r8798 r8867  
    5454     */
    5555    private void addSubscription(Subscription subscription) {
    56         // TODO change to lists
    5756        List documentIdPredicate = subscription.getPredicateList("documentID");
    5857        List collectionIdPredicate = subscription.getPredicateList("collectionID");
     
    237236        return matchedSubscriptions;
    238237    }
    239 //
    240 //    public String toString() {
    241 //        Set allSubscriptions = getAllSubscriptions();
    242 //       
    243 //        StringBuffer buffer = new StringBuffer();
    244 //       
    245 //        for (Iterator iter = allSubscriptions.iterator(); iter.hasNext();) {
    246 //            Subscription sub = (Subscription) iter.next();
    247 //            buffer.append(sub);
    248 //            buffer.append("\n");
    249 //        }
    250 //       
    251 //        return buffer.toString();
    252 //    }
    253 //
    254 //    /**
    255 //     * @return
    256 //     */
    257 //    public Set getAllSubscriptions() {
    258 //        Set allSubscriptions = new TreeSet();
    259 //        allSubscriptions.addAll(noEqualsSubscriptions);
    260 //       
    261 //        for(Iterator iter = docIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    262 //            Set values = (Set) iter.next();
    263 //            allSubscriptions.addAll(values);
    264 //        }
    265 //       
    266 //        for(Iterator iter = docCollectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    267 //            Set values = (Set) iter.next();
    268 //            allSubscriptions.addAll(values);
    269 //        }
    270 //       
    271 //        for(Iterator iter = collectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    272 //            Set values = (Set) iter.next();
    273 //            allSubscriptions.addAll(values);
    274 //        }
    275 //        return Collections.unmodifiableSet(allSubscriptions);
    276 //    }
    277238
    278239
     
    289250    /**
    290251     * @param subscriptionID
    291      */
    292     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
    297         // TODO Auto-generated method stub
     252     * @throws DatabaseException
     253     * @throws SQLException
     254     */
     255    public void deleteSubscription(String subscriptionID) throws DatabaseException, SQLException {
     256        Integer subID = new Integer(subscriptionID);
     257        System.out.println("deleting subscription " + subscriptionID);
     258        if (!subscriptions.containsKey(subID)) {
     259            return;
     260        }
     261        Subscription sub = (Subscription) subscriptions.get(subID);
     262        for (Iterator iter = dontCareSubscriptions.values().iterator(); iter.hasNext();) {
     263            Set entry = (Set) iter.next();
     264            if (entry.contains(subID))
     265                entry.remove(subID);
     266        }
     267        if (noEqualsSubscriptions.contains(subID))
     268            noEqualsSubscriptions.remove(subID);
     269        for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {
     270            Predicate pred = (Predicate) iter.next();
     271            pred.removeSubscription(subID);
     272        }
     273
     274        subscriptions.remove(subID);
     275       
     276       
     277        Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
     278       
     279        Statement statement = conn.createStatement();
     280       
     281        String sqlString = "DELETE FROM subscriptions WHERE id = " + subscriptionID;
     282        statement.executeUpdate(sqlString);
     283       
     284        sqlString = "SELECT predicate FROM subs_to_predicates " +
     285                "WHERE subscription = " + subscriptionID;
     286        ResultSet predicates = statement.executeQuery(sqlString);
     287        while (predicates.next()) {
     288            // if there aren't any other subscriptions using this predicate, delete predicate
     289            int predicateID = predicates.getInt("predicate");
     290           
     291        }
     292       
    298293       
    299294    }
     
    344339        return result;
    345340    }
     341
     342    /**
     343     *
     344     */
     345    public void restoreFromDatabase() {
     346        try {
     347            Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
     348            Statement statement = conn.createStatement();
     349            String sqlString = "SELECT * FROM subscriptions";
     350            ResultSet subs = statement.executeQuery(sqlString);
     351            while (subs.next()) {
     352                // construct map for all predicates
     353                int id = subs.getInt("id");
     354                Map map = new TreeMap();
     355                map.put("username", subs.getString("user"));
     356                map.put("email", subs.getString("email"));
     357                map.put("subscription_name", subs.getString("name"));
     358                Vector ways = new Vector();
     359                if (subs.getInt("rss") != 0) {
     360                    ways.add("rss");
     361                }
     362                if (subs.getInt("page") != 0) {
     363                    ways.add("page");
     364                }
     365                map.put("way", ways);
     366                Statement stmnt = conn.createStatement();
     367                sqlString = "SELECT p.type, p.field, p.value from subs_to_predicates stp join predicates p on(stp.predicate = p.id) where stp.subscription = " + id + ";";
     368                ResultSet predicates = stmnt.executeQuery(sqlString);
     369                while (predicates.next()) {
     370                    String type = predicates.getString("type");
     371                    String field = predicates.getString("field");
     372                    if (type.equals(SubstringMatchPredicate.class.getName())) {
     373                        if (field.equals("colletionID")) {
     374                            field = "collection_query";
     375                        } else if (field.equals("hostID")) {
     376                            field = "host_query";
     377                        }
     378                    }
     379                    String value = predicates.getString("value");
     380                    if (type.equals(IdEqualsPredicate.class.getName())) {
     381                        if (!map.containsKey(field)) {
     382                            map.put(field, new Vector());
     383                        }
     384                        ((List) map.get(field)).add(value);
     385                    } else {
     386                        map.put(field, value);
     387                    }
     388                }
     389                try {
     390                    createSubscription(map);
     391                } catch (Exception e1) {
     392                    // TODO Auto-generated catch block
     393                    e1.printStackTrace();
     394                }
     395            }
     396        } catch (SQLException e) {
     397            // TODO Auto-generated catch block
     398            e.printStackTrace();
     399        } catch (DatabaseException e) {
     400            // TODO Auto-generated catch block
     401            e.printStackTrace();
     402        }
     403    }
    346404   
    347405}
  • trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java

    r8798 r8867  
    119119        String query = "SELECT id FROM predicates " +
    120120                "WHERE field = '" + field +
    121                 "' AND value = '" + value + "';";
     121                "' AND value = '" + value +
     122                "' AND type = '" + this.getClass().getName() + "';";
    122123        Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
    123124        System.out.println(query);
    124125        ResultSet result = statement.executeQuery(query);
    125126        if (result.next()) { // predicate already exists in database
    126             System.out.println("predicate " + id + " already exists");
    127             return result.getInt("id");
     127            int id = result.getInt("id");
     128            System.out.println("predicate already exists with id " + id);
     129            return id;
    128130        }
    129131        // predicate has been newly created
    130         String insert = "INSERT INTO predicates (field, value) " +
    131         "VALUES ('" + field + "', '" + value + "');";
     132        String insert = "INSERT INTO predicates (field, type, value) " +
     133        "VALUES ('" + field + "', '" + this.getClass().getName() + "', '" + value + "');";
    132134        System.out.println(insert);
    133135        statement.execute(insert);
     
    141143
    142144    /**
    143      * @return
    144      */
    145     public List getValueList() {
    146         throw new UnsupportedOperationException("you can only call this method for multi-valued predicates");
    147     }
    148 
    149     /**
    150145     * @param key
    151146     * @return
     
    154149        return ArrayHelper.contains(singleValueFields, key) || ArrayHelper.contains(multiValueFields, key);
    155150    }
     151
     152    /**
     153     * @param subID
     154     * @throws DatabaseException
     155     * @throws SQLException
     156     */
     157    public void removeSubscription(Integer subID) throws SQLException, DatabaseException {
     158        subscriptions.remove(subID);
     159
     160        Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
     161        String sqlString = "DELETE FROM subs_to_predicates " +
     162            "WHERE subscription = " + subID +
     163            " AND predicate = " + id;
     164        statement.executeUpdate(sqlString);
     165       
     166        if (subscriptions.isEmpty()) {
     167            PredicateFactory.deletePredicate(this);
     168            sqlString = "DELETE FROM predicates WHERE id = " + id;
     169            statement.executeUpdate(sqlString);
     170        }
     171    }
    156172   
    157173}
  • trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/PredicateFactory.java

    r8847 r8867  
    208208        return result;
    209209    }
     210
     211    /**
     212     * @param predicate
     213     */
     214    public static void deletePredicate(Predicate predicate) {
     215        String field = predicate.getField();
     216        String value = predicate.getValue();
     217       
     218        if (predicate instanceof IdEqualsPredicate) {
     219            Map map = (Map) idEqualsPredicates.get(field);
     220            if (map.containsKey(value)) {
     221                map.remove(value);
     222            }
     223        } else if (predicate instanceof QueryPredicate) {
     224            if (field.equals("document_title")) {
     225                documentTitleQueryPredicates.remove(value);
     226            } else if (field.equals("document_content")) {
     227                documentContentQueryPredicates.remove(value);
     228            } else {
     229                System.err.println("problem while removing: unknown field " + field);
     230            }
     231        } else if (predicate instanceof SubstringMatchPredicate) {
     232            if (field.equals("hostID")) {
     233                hostNameMatchPredicates.remove(value);
     234            } else if (field.equals("collectionID")) {
     235                collectionNameMatchPredicates.remove(value);
     236            } else {
     237                System.err.println("problem while removing: unknown field " + field);
     238            }
     239        } else {
     240            System.err.println("problem while removing: unknown class " + predicate.getClass().getName());
     241        }
     242    }
    210243   
    211244}
  • trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java

    r8798 r8867  
    7878        rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss");
    7979        eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page");
    80         id = saveToDatabase(true);
     80        id = saveToDatabase();
    8181       
    8282        for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
     
    157157    }
    158158   
    159     private int saveToDatabase(boolean initial) throws DatabaseException, SQLException {
     159    private int saveToDatabase() throws DatabaseException, SQLException {
    160160        Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
    161161        Statement statement = conn.createStatement();
    162162        String sqlString;
     163        boolean initial = true;
     164       
     165        sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
     166                    "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
     167                    + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
     168                    username + "';";
     169        ResultSet result = statement.executeQuery(sqlString);
     170        if (result.next()) {
     171            initial = false;
     172            this.id = result.getInt("id");
     173        }
     174       
    163175        if (initial) {
    164176            sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " +
     
    168180            sqlString = "UPDATE subscriptions SET name='" + name + "', email='" +
    169181                    email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" +
    170                     (eventsSincePageNotification ? 1 : 0)+ "WHERE id=" + id + ";";
     182                    (eventsSincePageNotification ? 1 : 0)+ " WHERE id=" + this.id + ";";
    171183            // cannot change user
    172184        }
     
    178190                    username + "';";
    179191        System.out.println(sqlString);
    180         ResultSet result = statement.executeQuery(sqlString);
    181         int id;
     192        result = statement.executeQuery(sqlString);
     193       
    182194        if (result.next()) {
    183195            id = result.getInt("id");
  • trunk/gsdl3/packages/gsdl-as/create-tables.sql

    r8799 r8867  
    1919    field varchar(128) not null,
    2020    value varchar(255),
     21    type enum("org.greenstone.gsdlas.profiles.IdEqualsPredicate", "org.greenstone.gsdlas.profiles.SubstringMatchPredicate", "org.greenstone.gsdlas.profiles.QueryPredicate") not null,
    2122   
    22     unique(field,value)
     23    unique(field,type,value)
    2324);
    2425
  • trunk/gsdl3/packages/gsdl-as/lib/templates/list.vm

    r8720 r8867  
    1414    <div class="subscription">
    1515        <p><span class="sub_id">Subscription $sub.Id</span><br/>
    16         <a href="service?action=showFeed&subscriptionID=$sub.Id">show matching events</a> | <a href="service?action=editSubscription&subscriptionID=$sub.Id">edit subscription</a></p>
     16        <a href="service?action=showFeed&subscriptionID=$sub.Id">show matching events</a> | <a href="service?action=editSubscription&subscriptionID=$sub.Id">edit subscription</a> | <a href="service?action=deleteSubscription&subscriptionID=$sub.Id">delete subscription</a></p>
    1717        <table class="subscription">
    1818            <thead>
  • trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java

    r8847 r8867  
    1212import java.net.MalformedURLException;
    1313import java.net.URL;
     14import java.sql.SQLException;
    1415import java.util.*;
    1516
     17import javax.servlet.ServletConfig;
     18import javax.servlet.ServletException;
    1619import javax.servlet.http.*;
    1720
     
    3336public class AlertingService extends VelocityServlet {
    3437   
     38    /* (non-Javadoc)
     39     * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
     40     */
     41    public void init(ServletConfig config) throws ServletException {
     42        super.init(config);
     43        System.out.println("reloading subs");
     44        ProfileStore.getInstance().restoreFromDatabase();
     45        try {
     46            Set subs = ProfileStore.getInstance().getAllSubscriptionsFor("andrea");
     47            System.out.println("reloaded " + subs.size() + " subs for andrea: ");
     48            for (Iterator iter = subs.iterator(); iter.hasNext();) {
     49                System.out.println(iter.next());
     50            }
     51        } catch (DatabaseException e) {
     52            // TODO Auto-generated catch block
     53            e.printStackTrace();
     54        }
     55    }
    3556    public static final String[] actions = new String[] {
    3657            "createSubscription",
     
    129150        }
    130151        String subscriptionID = (String) arguments.get("subscriptionID");
    131         ProfileStore.getInstance().deleteSubscription(subscriptionID);
     152        try {
     153            ProfileStore.getInstance().deleteSubscription(subscriptionID);
     154        } catch (DatabaseException e) {
     155            // TODO Auto-generated catch block
     156            e.printStackTrace();
     157        } catch (SQLException e) {
     158            // TODO Auto-generated catch block
     159            e.printStackTrace();
     160        }
    132161        return listSubscriptions(arguments, context);
    133162    }
  • trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java

    r8798 r8867  
    5454     */
    5555    private void addSubscription(Subscription subscription) {
    56         // TODO change to lists
    5756        List documentIdPredicate = subscription.getPredicateList("documentID");
    5857        List collectionIdPredicate = subscription.getPredicateList("collectionID");
     
    237236        return matchedSubscriptions;
    238237    }
    239 //
    240 //    public String toString() {
    241 //        Set allSubscriptions = getAllSubscriptions();
    242 //       
    243 //        StringBuffer buffer = new StringBuffer();
    244 //       
    245 //        for (Iterator iter = allSubscriptions.iterator(); iter.hasNext();) {
    246 //            Subscription sub = (Subscription) iter.next();
    247 //            buffer.append(sub);
    248 //            buffer.append("\n");
    249 //        }
    250 //       
    251 //        return buffer.toString();
    252 //    }
    253 //
    254 //    /**
    255 //     * @return
    256 //     */
    257 //    public Set getAllSubscriptions() {
    258 //        Set allSubscriptions = new TreeSet();
    259 //        allSubscriptions.addAll(noEqualsSubscriptions);
    260 //       
    261 //        for(Iterator iter = docIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    262 //            Set values = (Set) iter.next();
    263 //            allSubscriptions.addAll(values);
    264 //        }
    265 //       
    266 //        for(Iterator iter = docCollectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    267 //            Set values = (Set) iter.next();
    268 //            allSubscriptions.addAll(values);
    269 //        }
    270 //       
    271 //        for(Iterator iter = collectionIdEqualsSubscriptions.values().iterator(); iter.hasNext();) {
    272 //            Set values = (Set) iter.next();
    273 //            allSubscriptions.addAll(values);
    274 //        }
    275 //        return Collections.unmodifiableSet(allSubscriptions);
    276 //    }
    277238
    278239
     
    289250    /**
    290251     * @param subscriptionID
    291      */
    292     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
    297         // TODO Auto-generated method stub
     252     * @throws DatabaseException
     253     * @throws SQLException
     254     */
     255    public void deleteSubscription(String subscriptionID) throws DatabaseException, SQLException {
     256        Integer subID = new Integer(subscriptionID);
     257        System.out.println("deleting subscription " + subscriptionID);
     258        if (!subscriptions.containsKey(subID)) {
     259            return;
     260        }
     261        Subscription sub = (Subscription) subscriptions.get(subID);
     262        for (Iterator iter = dontCareSubscriptions.values().iterator(); iter.hasNext();) {
     263            Set entry = (Set) iter.next();
     264            if (entry.contains(subID))
     265                entry.remove(subID);
     266        }
     267        if (noEqualsSubscriptions.contains(subID))
     268            noEqualsSubscriptions.remove(subID);
     269        for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {
     270            Predicate pred = (Predicate) iter.next();
     271            pred.removeSubscription(subID);
     272        }
     273
     274        subscriptions.remove(subID);
     275       
     276       
     277        Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
     278       
     279        Statement statement = conn.createStatement();
     280       
     281        String sqlString = "DELETE FROM subscriptions WHERE id = " + subscriptionID;
     282        statement.executeUpdate(sqlString);
     283       
     284        sqlString = "SELECT predicate FROM subs_to_predicates " +
     285                "WHERE subscription = " + subscriptionID;
     286        ResultSet predicates = statement.executeQuery(sqlString);
     287        while (predicates.next()) {
     288            // if there aren't any other subscriptions using this predicate, delete predicate
     289            int predicateID = predicates.getInt("predicate");
     290           
     291        }
     292       
    298293       
    299294    }
     
    344339        return result;
    345340    }
     341
     342    /**
     343     *
     344     */
     345    public void restoreFromDatabase() {
     346        try {
     347            Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
     348            Statement statement = conn.createStatement();
     349            String sqlString = "SELECT * FROM subscriptions";
     350            ResultSet subs = statement.executeQuery(sqlString);
     351            while (subs.next()) {
     352                // construct map for all predicates
     353                int id = subs.getInt("id");
     354                Map map = new TreeMap();
     355                map.put("username", subs.getString("user"));
     356                map.put("email", subs.getString("email"));
     357                map.put("subscription_name", subs.getString("name"));
     358                Vector ways = new Vector();
     359                if (subs.getInt("rss") != 0) {
     360                    ways.add("rss");
     361                }
     362                if (subs.getInt("page") != 0) {
     363                    ways.add("page");
     364                }
     365                map.put("way", ways);
     366                Statement stmnt = conn.createStatement();
     367                sqlString = "SELECT p.type, p.field, p.value from subs_to_predicates stp join predicates p on(stp.predicate = p.id) where stp.subscription = " + id + ";";
     368                ResultSet predicates = stmnt.executeQuery(sqlString);
     369                while (predicates.next()) {
     370                    String type = predicates.getString("type");
     371                    String field = predicates.getString("field");
     372                    if (type.equals(SubstringMatchPredicate.class.getName())) {
     373                        if (field.equals("colletionID")) {
     374                            field = "collection_query";
     375                        } else if (field.equals("hostID")) {
     376                            field = "host_query";
     377                        }
     378                    }
     379                    String value = predicates.getString("value");
     380                    if (type.equals(IdEqualsPredicate.class.getName())) {
     381                        if (!map.containsKey(field)) {
     382                            map.put(field, new Vector());
     383                        }
     384                        ((List) map.get(field)).add(value);
     385                    } else {
     386                        map.put(field, value);
     387                    }
     388                }
     389                try {
     390                    createSubscription(map);
     391                } catch (Exception e1) {
     392                    // TODO Auto-generated catch block
     393                    e1.printStackTrace();
     394                }
     395            }
     396        } catch (SQLException e) {
     397            // TODO Auto-generated catch block
     398            e.printStackTrace();
     399        } catch (DatabaseException e) {
     400            // TODO Auto-generated catch block
     401            e.printStackTrace();
     402        }
     403    }
    346404   
    347405}
  • trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java

    r8798 r8867  
    119119        String query = "SELECT id FROM predicates " +
    120120                "WHERE field = '" + field +
    121                 "' AND value = '" + value + "';";
     121                "' AND value = '" + value +
     122                "' AND type = '" + this.getClass().getName() + "';";
    122123        Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
    123124        System.out.println(query);
    124125        ResultSet result = statement.executeQuery(query);
    125126        if (result.next()) { // predicate already exists in database
    126             System.out.println("predicate " + id + " already exists");
    127             return result.getInt("id");
     127            int id = result.getInt("id");
     128            System.out.println("predicate already exists with id " + id);
     129            return id;
    128130        }
    129131        // predicate has been newly created
    130         String insert = "INSERT INTO predicates (field, value) " +
    131         "VALUES ('" + field + "', '" + value + "');";
     132        String insert = "INSERT INTO predicates (field, type, value) " +
     133        "VALUES ('" + field + "', '" + this.getClass().getName() + "', '" + value + "');";
    132134        System.out.println(insert);
    133135        statement.execute(insert);
     
    141143
    142144    /**
    143      * @return
    144      */
    145     public List getValueList() {
    146         throw new UnsupportedOperationException("you can only call this method for multi-valued predicates");
    147     }
    148 
    149     /**
    150145     * @param key
    151146     * @return
     
    154149        return ArrayHelper.contains(singleValueFields, key) || ArrayHelper.contains(multiValueFields, key);
    155150    }
     151
     152    /**
     153     * @param subID
     154     * @throws DatabaseException
     155     * @throws SQLException
     156     */
     157    public void removeSubscription(Integer subID) throws SQLException, DatabaseException {
     158        subscriptions.remove(subID);
     159
     160        Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
     161        String sqlString = "DELETE FROM subs_to_predicates " +
     162            "WHERE subscription = " + subID +
     163            " AND predicate = " + id;
     164        statement.executeUpdate(sqlString);
     165       
     166        if (subscriptions.isEmpty()) {
     167            PredicateFactory.deletePredicate(this);
     168            sqlString = "DELETE FROM predicates WHERE id = " + id;
     169            statement.executeUpdate(sqlString);
     170        }
     171    }
    156172   
    157173}
  • trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/profiles/PredicateFactory.java

    r8847 r8867  
    208208        return result;
    209209    }
     210
     211    /**
     212     * @param predicate
     213     */
     214    public static void deletePredicate(Predicate predicate) {
     215        String field = predicate.getField();
     216        String value = predicate.getValue();
     217       
     218        if (predicate instanceof IdEqualsPredicate) {
     219            Map map = (Map) idEqualsPredicates.get(field);
     220            if (map.containsKey(value)) {
     221                map.remove(value);
     222            }
     223        } else if (predicate instanceof QueryPredicate) {
     224            if (field.equals("document_title")) {
     225                documentTitleQueryPredicates.remove(value);
     226            } else if (field.equals("document_content")) {
     227                documentContentQueryPredicates.remove(value);
     228            } else {
     229                System.err.println("problem while removing: unknown field " + field);
     230            }
     231        } else if (predicate instanceof SubstringMatchPredicate) {
     232            if (field.equals("hostID")) {
     233                hostNameMatchPredicates.remove(value);
     234            } else if (field.equals("collectionID")) {
     235                collectionNameMatchPredicates.remove(value);
     236            } else {
     237                System.err.println("problem while removing: unknown field " + field);
     238            }
     239        } else {
     240            System.err.println("problem while removing: unknown class " + predicate.getClass().getName());
     241        }
     242    }
    210243   
    211244}
  • trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java

    r8798 r8867  
    7878        rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss");
    7979        eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page");
    80         id = saveToDatabase(true);
     80        id = saveToDatabase();
    8181       
    8282        for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
     
    157157    }
    158158   
    159     private int saveToDatabase(boolean initial) throws DatabaseException, SQLException {
     159    private int saveToDatabase() throws DatabaseException, SQLException {
    160160        Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
    161161        Statement statement = conn.createStatement();
    162162        String sqlString;
     163        boolean initial = true;
     164       
     165        sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
     166                    "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
     167                    + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
     168                    username + "';";
     169        ResultSet result = statement.executeQuery(sqlString);
     170        if (result.next()) {
     171            initial = false;
     172            this.id = result.getInt("id");
     173        }
     174       
    163175        if (initial) {
    164176            sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " +
     
    168180            sqlString = "UPDATE subscriptions SET name='" + name + "', email='" +
    169181                    email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" +
    170                     (eventsSincePageNotification ? 1 : 0)+ "WHERE id=" + id + ";";
     182                    (eventsSincePageNotification ? 1 : 0)+ " WHERE id=" + this.id + ";";
    171183            // cannot change user
    172184        }
     
    178190                    username + "';";
    179191        System.out.println(sqlString);
    180         ResultSet result = statement.executeQuery(sqlString);
    181         int id;
     192        result = statement.executeQuery(sqlString);
     193       
    182194        if (result.next()) {
    183195            id = result.getInt("id");
Note: See TracChangeset for help on using the changeset viewer.