1 | /*
|
---|
2 | * Created on Oct 27, 2004
|
---|
3 | * Copyright (C) Andrea Schweer, 2004
|
---|
4 | *
|
---|
5 | * This file is part of the Greenstone Alerting Service.
|
---|
6 | * Refer to the COPYING file in the base directory of this package
|
---|
7 | * for licensing information.
|
---|
8 | */
|
---|
9 | package org.greenstone.gsdlas.profiles;
|
---|
10 |
|
---|
11 | import java.sql.*;
|
---|
12 | import java.sql.Connection;
|
---|
13 | import java.sql.Statement;
|
---|
14 | import java.util.*;
|
---|
15 | import java.util.Map;
|
---|
16 | import java.util.TreeMap;
|
---|
17 |
|
---|
18 | import org.greenstone.gsdlas.database.DatabaseException;
|
---|
19 | import org.greenstone.gsdlas.database.DatabaseManager;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @author schweer
|
---|
23 | *
|
---|
24 | * TODO To change the template for this generated type comment go to Window -
|
---|
25 | * Preferences - Java - Code Style - Code Templates
|
---|
26 | */
|
---|
27 | public class Subscription implements Comparable {
|
---|
28 | private Map map;
|
---|
29 |
|
---|
30 | private int id;
|
---|
31 | private String username;
|
---|
32 | private String name;
|
---|
33 | private String email;
|
---|
34 | private boolean rssNotification;
|
---|
35 | private boolean eventsSincePageNotification;
|
---|
36 |
|
---|
37 | private int numOfNonEqualsPredicates;
|
---|
38 |
|
---|
39 | public Subscription(Map valueMap) throws DatabaseException, SQLException {
|
---|
40 | map = new TreeMap();
|
---|
41 | for (Iterator iter = valueMap.keySet().iterator(); iter.hasNext();) {
|
---|
42 | String key = (String) iter.next();
|
---|
43 | Object value = valueMap.get(key);
|
---|
44 |
|
---|
45 | Predicate predicate = null;
|
---|
46 | if (value instanceof String) {
|
---|
47 | predicate = PredicateFactory.createPredicate(key, (String) value);
|
---|
48 | map.put(key, predicate);
|
---|
49 | } else if (value instanceof List) {
|
---|
50 | List values = (List)value;
|
---|
51 | predicate = PredicateFactory.createPredicate(key, values);
|
---|
52 | map.put(key, predicate);
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (predicate != null && !(predicate instanceof IdEqualsPredicate)) {
|
---|
56 | numOfNonEqualsPredicates++;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | System.out.println("finished creating predicates");
|
---|
61 |
|
---|
62 | username = (String) valueMap.get("username");
|
---|
63 | name = (String) valueMap.get("subscription_name");
|
---|
64 | email = (String) valueMap.get("email");
|
---|
65 | rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss");
|
---|
66 | eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page");
|
---|
67 | id = saveToDatabase(true);
|
---|
68 |
|
---|
69 | for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
|
---|
70 | Predicate predicate = (Predicate) iter.next();
|
---|
71 | if (predicate != null) {
|
---|
72 | predicate.addSubscription(id);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public boolean containsKey(Object key) {
|
---|
78 | return map.containsKey(key);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public boolean containsValue(Object value) {
|
---|
82 | return map.containsValue(value);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public Predicate getPredicate(String field) {
|
---|
86 | return (Predicate) map.get(field);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public Set keySet() {
|
---|
90 | return map.keySet();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public Set getPredicates() {
|
---|
94 | Set result = new HashSet();
|
---|
95 | for (Iterator iter = map.values().iterator(); iter.hasNext();) {
|
---|
96 | Predicate pred = (Predicate) iter.next();
|
---|
97 | if (pred != null) {
|
---|
98 | result.add(pred);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | return result;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * @return
|
---|
106 | */
|
---|
107 | public int getId() {
|
---|
108 | return id;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public int getNumOfNonEqualsPredicates() {
|
---|
112 | return numOfNonEqualsPredicates;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public String toString() {
|
---|
116 | return "Subscription id " + id + ": " + map;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public boolean equals(Object other) {
|
---|
120 | if (other == null || !(other instanceof Subscription)) return false;
|
---|
121 | return id == ((Subscription)other).id;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* (non-Javadoc)
|
---|
125 | * @see java.lang.Comparable#compareTo(java.lang.Object)
|
---|
126 | */
|
---|
127 | public int compareTo(Object arg0) {
|
---|
128 | Subscription other = (Subscription) arg0;
|
---|
129 | return new Integer(id).compareTo(new Integer(other.id));
|
---|
130 | }
|
---|
131 |
|
---|
132 | private int saveToDatabase(boolean initial) throws DatabaseException, SQLException {
|
---|
133 | Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
|
---|
134 | Statement statement = conn.createStatement();
|
---|
135 | String sqlString;
|
---|
136 | if (initial) {
|
---|
137 | sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " +
|
---|
138 | "VALUES ('" + name + "','" + email + "'," + (rssNotification ? 1 : 0) +
|
---|
139 | "," + (eventsSincePageNotification ? 1: 0)+ ", '" + username + "');";
|
---|
140 | } else {
|
---|
141 | sqlString = "UPDATE subscriptions SET name='" + name + "', email='" +
|
---|
142 | email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" +
|
---|
143 | (eventsSincePageNotification ? 1 : 0)+ "WHERE id=" + id + ";";
|
---|
144 | // cannot change user
|
---|
145 | }
|
---|
146 | System.out.println(sqlString);
|
---|
147 | statement.executeUpdate(sqlString);
|
---|
148 |
|
---|
149 | sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
|
---|
150 | "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
|
---|
151 | + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
|
---|
152 | username + "';";
|
---|
153 | System.out.println(sqlString);
|
---|
154 | ResultSet result = statement.executeQuery(sqlString);
|
---|
155 | int id;
|
---|
156 | if (result.next()) {
|
---|
157 | id = result.getInt("id");
|
---|
158 | } else {
|
---|
159 | throw new DatabaseException("Couldn't save subscription");
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (initial) {
|
---|
163 | for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
|
---|
164 | Predicate predicate = (Predicate) iter.next();
|
---|
165 | sqlString = "INSERT INTO subs_to_predicates (subscription, predicate) " +
|
---|
166 | "VALUES (" + id + "," + predicate.getID() + ");";
|
---|
167 | statement.executeUpdate(sqlString);
|
---|
168 | }
|
---|
169 | }
|
---|
170 | return id;
|
---|
171 | }
|
---|
172 | } |
---|