source: trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Subscription.java@ 8888

Last change on this file since 8888 was 8888, checked in by schweer, 19 years ago

notifications

  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
RevLine 
[8609]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 */
9package org.greenstone.gsdlas.profiles;
10
[8738]11import java.sql.*;
12import java.sql.Connection;
13import java.sql.Statement;
[8609]14import java.util.*;
15import java.util.Map;
16import java.util.TreeMap;
[8875]17import java.util.regex.Matcher;
18import java.util.regex.Pattern;
[8609]19
[8875]20import org.greenstone.gsdlas.Constants;
[8738]21import org.greenstone.gsdlas.database.DatabaseException;
22import org.greenstone.gsdlas.database.DatabaseManager;
23
[8609]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 */
30public class Subscription implements Comparable {
31 private Map map;
[8738]32
[8609]33 private int id;
[8738]34 private String username;
35 private String name;
36 private String email;
37 private boolean rssNotification;
38 private boolean eventsSincePageNotification;
39
[8609]40 private int numOfNonEqualsPredicates;
41
[8738]42 public Subscription(Map valueMap) throws DatabaseException, SQLException {
[8875]43 valueMap = cleanHostCollectionIDs(valueMap);
[8609]44 map = new TreeMap();
45 for (Iterator iter = valueMap.keySet().iterator(); iter.hasNext();) {
46 String key = (String) iter.next();
[8781]47
48 if (!Predicate.isFieldName(key)) {
49 continue;
50 }
51
[8738]52 Object value = valueMap.get(key);
[8609]53
[8738]54 if (value instanceof String) {
[8798]55 Predicate predicate = PredicateFactory.createPredicate(key, (String) value);
[8738]56 map.put(key, predicate);
[8798]57
[8875]58 if (predicate != null && !(predicate instanceof EqualsPredicate)) {
[8798]59 numOfNonEqualsPredicates++;
60 }
[8738]61 } else if (value instanceof List) {
62 List values = (List)value;
[8798]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
[8875]69 if (predicate != null && !(predicate instanceof EqualsPredicate)) {
[8798]70 numOfNonEqualsPredicates++;
71 }
72 }
[8738]73 }
74
[8609]75 }
[8738]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");
[8867]84 id = saveToDatabase();
[8738]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 }
[8609]92 }
93
[8875]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
[8609]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) {
[8798]146 if (map.get(field) instanceof Predicate) {
147 return (Predicate) map.get(field);
148 }
149 return null;
[8609]150 }
151
[8798]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
[8609]159 public Set keySet() {
160 return map.keySet();
161 }
162
[8719]163 public Set getPredicates() {
164 Set result = new HashSet();
165 for (Iterator iter = map.values().iterator(); iter.hasNext();) {
[8798]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);
[8719]173 }
174 }
175 return result;
[8609]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() {
[8888]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();
[8609]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 }
[8738]229
[8867]230 private int saveToDatabase() throws DatabaseException, SQLException {
[8738]231 Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
232 Statement statement = conn.createStatement();
233 String sqlString;
[8867]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
[8738]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=" +
[8867]253 (eventsSincePageNotification ? 1 : 0)+ " WHERE id=" + this.id + ";";
[8738]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);
[8867]263 result = statement.executeQuery(sqlString);
264
[8738]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 }
[8888]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 }
[8777]295
[8888]296 public String getName() {
297 return name;
298 }
299
[8609]300}
Note: See TracBrowser for help on using the repository browser.