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 | import java.util.regex.Matcher;
|
---|
18 | import java.util.regex.Pattern;
|
---|
19 |
|
---|
20 | import org.greenstone.gsdlas.Constants;
|
---|
21 | import org.greenstone.gsdlas.database.DatabaseException;
|
---|
22 | import org.greenstone.gsdlas.database.DatabaseManager;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @author schweer
|
---|
26 | *
|
---|
27 | * TODO To change the template for this generated type comment go to Window -
|
---|
28 | * Preferences - Java - Code Style - Code Templates
|
---|
29 | */
|
---|
30 | public class Subscription implements Comparable {
|
---|
31 | private Map map;
|
---|
32 |
|
---|
33 | private int id;
|
---|
34 | private String username;
|
---|
35 | private String name;
|
---|
36 | private String email;
|
---|
37 | private boolean rssNotification;
|
---|
38 | private boolean eventsSincePageNotification;
|
---|
39 |
|
---|
40 | private int numOfNonEqualsPredicates;
|
---|
41 |
|
---|
42 | public Subscription(Map valueMap) throws DatabaseException, SQLException {
|
---|
43 | valueMap = cleanHostCollectionIDs(valueMap);
|
---|
44 | map = new TreeMap();
|
---|
45 | for (Iterator iter = valueMap.keySet().iterator(); iter.hasNext();) {
|
---|
46 | String key = (String) iter.next();
|
---|
47 |
|
---|
48 | if (!Predicate.isFieldName(key)) {
|
---|
49 | continue;
|
---|
50 | }
|
---|
51 |
|
---|
52 | Object value = valueMap.get(key);
|
---|
53 |
|
---|
54 | if (value instanceof String) {
|
---|
55 | Predicate predicate = PredicateFactory.createPredicate(key, (String) value);
|
---|
56 | map.put(key, predicate);
|
---|
57 |
|
---|
58 | if (predicate != null && !(predicate instanceof EqualsPredicate)) {
|
---|
59 | numOfNonEqualsPredicates++;
|
---|
60 | }
|
---|
61 | } else if (value instanceof List) {
|
---|
62 | List values = (List)value;
|
---|
63 | List predicates = PredicateFactory.createPredicates(key, values);
|
---|
64 | map.put(key, predicates);
|
---|
65 | for (Iterator iterator = predicates.iterator(); iterator
|
---|
66 | .hasNext();) {
|
---|
67 | Predicate predicate = (Predicate) iterator.next();
|
---|
68 |
|
---|
69 | if (predicate != null && !(predicate instanceof EqualsPredicate)) {
|
---|
70 | numOfNonEqualsPredicates++;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | System.out.println("finished creating predicates");
|
---|
78 |
|
---|
79 | username = (String) valueMap.get("username");
|
---|
80 | name = (String) valueMap.get("subscription_name");
|
---|
81 | email = (String) valueMap.get("email");
|
---|
82 | rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss");
|
---|
83 | eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page");
|
---|
84 | id = saveToDatabase();
|
---|
85 |
|
---|
86 | for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
|
---|
87 | Predicate predicate = (Predicate) iter.next();
|
---|
88 | if (predicate != null) {
|
---|
89 | predicate.addSubscription(id);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * @param valueMap
|
---|
96 | * @return
|
---|
97 | */
|
---|
98 | private Map cleanHostCollectionIDs(Map valueMap) {
|
---|
99 | Map result = new TreeMap();
|
---|
100 | Pattern pattern = Pattern.compile("^(.*)\\|(.*)$");
|
---|
101 | for (Iterator iter = valueMap.keySet().iterator(); iter.hasNext();) {
|
---|
102 | String key = (String) iter.next();
|
---|
103 | if (!key.equals(Constants.HOST_COLLECTION_ID_FIELD)) {
|
---|
104 | result.put(key, valueMap.get(key));
|
---|
105 | continue;
|
---|
106 | }
|
---|
107 | if (key.equals(Constants.HOST_ID_FIELD)) { continue; }
|
---|
108 |
|
---|
109 | List values = (List) valueMap.get(key);
|
---|
110 | List hostIDs = new Vector();
|
---|
111 | List collIDs = new Vector();
|
---|
112 | for (Iterator iterator = values.iterator(); iterator.hasNext();) {
|
---|
113 | String id = (String) iterator.next();
|
---|
114 | Matcher matcher = pattern.matcher(id);
|
---|
115 | if (matcher.matches()) {
|
---|
116 | String host = null;
|
---|
117 | String coll = null;
|
---|
118 | try {
|
---|
119 | host = matcher.group(1);
|
---|
120 | coll = matcher.group(2);
|
---|
121 | } catch (IndexOutOfBoundsException e) { /*ignore*/ }
|
---|
122 | if (host != null && host.length() > 0) {
|
---|
123 | if (coll != null && coll.length() > 0) {
|
---|
124 | if (!collIDs.contains(id)) collIDs.add(id);
|
---|
125 | } else {
|
---|
126 | if (!hostIDs.contains(host)) hostIDs.add(host);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | result.put(Constants.HOST_ID_FIELD, hostIDs);
|
---|
132 | result.put(Constants.COLLECTION_ID_FIELD, collIDs);
|
---|
133 | }
|
---|
134 | return result;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public boolean containsKey(Object key) {
|
---|
138 | return map.containsKey(key);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public boolean containsValue(Object value) {
|
---|
142 | return map.containsValue(value);
|
---|
143 | }
|
---|
144 |
|
---|
145 | public Predicate getPredicate(String field) {
|
---|
146 | if (map.get(field) instanceof Predicate) {
|
---|
147 | return (Predicate) map.get(field);
|
---|
148 | }
|
---|
149 | return null;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public List getPredicateList(String field) {
|
---|
153 | if (map.get(field) instanceof List) {
|
---|
154 | return (List) map.get(field);
|
---|
155 | }
|
---|
156 | return new Vector();
|
---|
157 | }
|
---|
158 |
|
---|
159 | public Set keySet() {
|
---|
160 | return map.keySet();
|
---|
161 | }
|
---|
162 |
|
---|
163 | public Set getPredicates() {
|
---|
164 | Set result = new HashSet();
|
---|
165 | for (Iterator iter = map.values().iterator(); iter.hasNext();) {
|
---|
166 | Object next = iter.next();
|
---|
167 | if (next == null) {
|
---|
168 | continue;
|
---|
169 | } else if (next instanceof Predicate) {
|
---|
170 | result.add(next);
|
---|
171 | } else if (next instanceof List) {
|
---|
172 | result.addAll((Collection) next);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | return result;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * @return
|
---|
180 | */
|
---|
181 | public int getId() {
|
---|
182 | return id;
|
---|
183 | }
|
---|
184 |
|
---|
185 | public int getNumOfNonEqualsPredicates() {
|
---|
186 | return numOfNonEqualsPredicates;
|
---|
187 | }
|
---|
188 |
|
---|
189 | public String toString() {
|
---|
190 | StringBuffer result = new StringBuffer("Subscription ");
|
---|
191 | result.append(name != null ? name : "(no name) ");
|
---|
192 | result.append(" (id ");
|
---|
193 | result.append(id);
|
---|
194 | result.append("):\n");
|
---|
195 | for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
|
---|
196 | String key = (String) iter.next();
|
---|
197 | Object value = map.get(key);
|
---|
198 | if (value instanceof Predicate) {
|
---|
199 | Predicate pred = (Predicate) value;
|
---|
200 | result.append(pred);
|
---|
201 | }
|
---|
202 | if (value instanceof List) {
|
---|
203 | List list = (List) value;
|
---|
204 | for (Iterator iterator = list.iterator(); iterator.hasNext();) {
|
---|
205 | Predicate pred = (Predicate) iterator.next();
|
---|
206 | result.append(pred);
|
---|
207 | if (iterator.hasNext()) {
|
---|
208 | result.append(" or ");
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 | result.append("\n");
|
---|
213 | }
|
---|
214 | return result.toString();
|
---|
215 | }
|
---|
216 |
|
---|
217 | public boolean equals(Object other) {
|
---|
218 | if (other == null || !(other instanceof Subscription)) return false;
|
---|
219 | return id == ((Subscription)other).id;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* (non-Javadoc)
|
---|
223 | * @see java.lang.Comparable#compareTo(java.lang.Object)
|
---|
224 | */
|
---|
225 | public int compareTo(Object arg0) {
|
---|
226 | Subscription other = (Subscription) arg0;
|
---|
227 | return new Integer(id).compareTo(new Integer(other.id));
|
---|
228 | }
|
---|
229 |
|
---|
230 | private int saveToDatabase() throws DatabaseException, SQLException {
|
---|
231 | Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
|
---|
232 | Statement statement = conn.createStatement();
|
---|
233 | String sqlString;
|
---|
234 | boolean initial = true;
|
---|
235 |
|
---|
236 | sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
|
---|
237 | "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
|
---|
238 | + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
|
---|
239 | username + "';";
|
---|
240 | ResultSet result = statement.executeQuery(sqlString);
|
---|
241 | if (result.next()) {
|
---|
242 | initial = false;
|
---|
243 | this.id = result.getInt("id");
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (initial) {
|
---|
247 | sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " +
|
---|
248 | "VALUES ('" + name + "','" + email + "'," + (rssNotification ? 1 : 0) +
|
---|
249 | "," + (eventsSincePageNotification ? 1: 0)+ ", '" + username + "');";
|
---|
250 | } else {
|
---|
251 | sqlString = "UPDATE subscriptions SET name='" + name + "', email='" +
|
---|
252 | email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" +
|
---|
253 | (eventsSincePageNotification ? 1 : 0)+ " WHERE id=" + this.id + ";";
|
---|
254 | // cannot change user
|
---|
255 | }
|
---|
256 | statement.executeUpdate(sqlString);
|
---|
257 |
|
---|
258 | sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
|
---|
259 | "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
|
---|
260 | + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
|
---|
261 | username + "';";
|
---|
262 | System.out.println(sqlString);
|
---|
263 | result = statement.executeQuery(sqlString);
|
---|
264 |
|
---|
265 | if (result.next()) {
|
---|
266 | id = result.getInt("id");
|
---|
267 | } else {
|
---|
268 | throw new DatabaseException("Couldn't save subscription");
|
---|
269 | }
|
---|
270 |
|
---|
271 | if (initial) {
|
---|
272 | for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
|
---|
273 | Predicate predicate = (Predicate) iter.next();
|
---|
274 | sqlString = "INSERT INTO subs_to_predicates (subscription, predicate) " +
|
---|
275 | "VALUES (" + id + "," + predicate.getID() + ");";
|
---|
276 | statement.executeUpdate(sqlString);
|
---|
277 | }
|
---|
278 | }
|
---|
279 | return id;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * @return
|
---|
284 | */
|
---|
285 | public boolean wantsEMailNotification() {
|
---|
286 | return email != null && email.length() > 1;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * @return
|
---|
291 | */
|
---|
292 | public String getMailAddress() {
|
---|
293 | return email;
|
---|
294 | }
|
---|
295 |
|
---|
296 | public String getName() {
|
---|
297 | return name;
|
---|
298 | }
|
---|
299 |
|
---|
300 | } |
---|