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 | return "Subscription id " + id + ": " + map;
|
---|
191 | }
|
---|
192 |
|
---|
193 | public boolean equals(Object other) {
|
---|
194 | if (other == null || !(other instanceof Subscription)) return false;
|
---|
195 | return id == ((Subscription)other).id;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /* (non-Javadoc)
|
---|
199 | * @see java.lang.Comparable#compareTo(java.lang.Object)
|
---|
200 | */
|
---|
201 | public int compareTo(Object arg0) {
|
---|
202 | Subscription other = (Subscription) arg0;
|
---|
203 | return new Integer(id).compareTo(new Integer(other.id));
|
---|
204 | }
|
---|
205 |
|
---|
206 | private int saveToDatabase() throws DatabaseException, SQLException {
|
---|
207 | Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
|
---|
208 | Statement statement = conn.createStatement();
|
---|
209 | String sqlString;
|
---|
210 | boolean initial = true;
|
---|
211 |
|
---|
212 | sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
|
---|
213 | "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
|
---|
214 | + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
|
---|
215 | username + "';";
|
---|
216 | ResultSet result = statement.executeQuery(sqlString);
|
---|
217 | if (result.next()) {
|
---|
218 | initial = false;
|
---|
219 | this.id = result.getInt("id");
|
---|
220 | }
|
---|
221 |
|
---|
222 | if (initial) {
|
---|
223 | sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " +
|
---|
224 | "VALUES ('" + name + "','" + email + "'," + (rssNotification ? 1 : 0) +
|
---|
225 | "," + (eventsSincePageNotification ? 1: 0)+ ", '" + username + "');";
|
---|
226 | } else {
|
---|
227 | sqlString = "UPDATE subscriptions SET name='" + name + "', email='" +
|
---|
228 | email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" +
|
---|
229 | (eventsSincePageNotification ? 1 : 0)+ " WHERE id=" + this.id + ";";
|
---|
230 | // cannot change user
|
---|
231 | }
|
---|
232 | statement.executeUpdate(sqlString);
|
---|
233 |
|
---|
234 | sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
|
---|
235 | "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
|
---|
236 | + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
|
---|
237 | username + "';";
|
---|
238 | System.out.println(sqlString);
|
---|
239 | result = statement.executeQuery(sqlString);
|
---|
240 |
|
---|
241 | if (result.next()) {
|
---|
242 | id = result.getInt("id");
|
---|
243 | } else {
|
---|
244 | throw new DatabaseException("Couldn't save subscription");
|
---|
245 | }
|
---|
246 |
|
---|
247 | if (initial) {
|
---|
248 | for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
|
---|
249 | Predicate predicate = (Predicate) iter.next();
|
---|
250 | sqlString = "INSERT INTO subs_to_predicates (subscription, predicate) " +
|
---|
251 | "VALUES (" + id + "," + predicate.getID() + ");";
|
---|
252 | statement.executeUpdate(sqlString);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | return id;
|
---|
256 | }
|
---|
257 |
|
---|
258 | } |
---|