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

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

fixed BTS 12: restore subscriptions/predicates from database

  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
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
11import java.sql.*;
12import java.sql.Connection;
13import java.sql.Statement;
14import java.util.*;
15import java.util.Map;
16import java.util.TreeMap;
17
18import org.greenstone.gsdlas.database.DatabaseException;
19import 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 */
27public 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
44 if (!Predicate.isFieldName(key)) {
45 continue;
46 }
47
48 Object value = valueMap.get(key);
49
50 if (value instanceof String) {
51 Predicate predicate = PredicateFactory.createPredicate(key, (String) value);
52 map.put(key, predicate);
53
54 if (predicate != null && !(predicate instanceof IdEqualsPredicate)) {
55 numOfNonEqualsPredicates++;
56 }
57 } else if (value instanceof List) {
58 List values = (List)value;
59 List predicates = PredicateFactory.createPredicates(key, values);
60 map.put(key, predicates);
61 for (Iterator iterator = predicates.iterator(); iterator
62 .hasNext();) {
63 Predicate predicate = (Predicate) iterator.next();
64
65 if (predicate != null && !(predicate instanceof IdEqualsPredicate)) {
66 numOfNonEqualsPredicates++;
67 }
68 }
69 }
70
71 }
72
73 System.out.println("finished creating predicates");
74
75 username = (String) valueMap.get("username");
76 name = (String) valueMap.get("subscription_name");
77 email = (String) valueMap.get("email");
78 rssNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("rss");
79 eventsSincePageNotification = valueMap.containsKey("way") && ((List)valueMap.get("way")).contains("page");
80 id = saveToDatabase();
81
82 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
83 Predicate predicate = (Predicate) iter.next();
84 if (predicate != null) {
85 predicate.addSubscription(id);
86 }
87 }
88 }
89
90 public boolean containsKey(Object key) {
91 return map.containsKey(key);
92 }
93
94 public boolean containsValue(Object value) {
95 return map.containsValue(value);
96 }
97
98 public Predicate getPredicate(String field) {
99 if (map.get(field) instanceof Predicate) {
100 return (Predicate) map.get(field);
101 }
102 return null;
103 }
104
105 public List getPredicateList(String field) {
106 if (map.get(field) instanceof List) {
107 return (List) map.get(field);
108 }
109 return new Vector();
110 }
111
112 public Set keySet() {
113 return map.keySet();
114 }
115
116 public Set getPredicates() {
117 Set result = new HashSet();
118 for (Iterator iter = map.values().iterator(); iter.hasNext();) {
119 Object next = iter.next();
120 if (next == null) {
121 continue;
122 } else if (next instanceof Predicate) {
123 result.add(next);
124 } else if (next instanceof List) {
125 result.addAll((Collection) next);
126 }
127 }
128 return result;
129 }
130
131 /**
132 * @return
133 */
134 public int getId() {
135 return id;
136 }
137
138 public int getNumOfNonEqualsPredicates() {
139 return numOfNonEqualsPredicates;
140 }
141
142 public String toString() {
143 return "Subscription id " + id + ": " + map;
144 }
145
146 public boolean equals(Object other) {
147 if (other == null || !(other instanceof Subscription)) return false;
148 return id == ((Subscription)other).id;
149 }
150
151 /* (non-Javadoc)
152 * @see java.lang.Comparable#compareTo(java.lang.Object)
153 */
154 public int compareTo(Object arg0) {
155 Subscription other = (Subscription) arg0;
156 return new Integer(id).compareTo(new Integer(other.id));
157 }
158
159 private int saveToDatabase() throws DatabaseException, SQLException {
160 Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
161 Statement statement = conn.createStatement();
162 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
175 if (initial) {
176 sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " +
177 "VALUES ('" + name + "','" + email + "'," + (rssNotification ? 1 : 0) +
178 "," + (eventsSincePageNotification ? 1: 0)+ ", '" + username + "');";
179 } else {
180 sqlString = "UPDATE subscriptions SET name='" + name + "', email='" +
181 email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" +
182 (eventsSincePageNotification ? 1 : 0)+ " WHERE id=" + this.id + ";";
183 // cannot change user
184 }
185 statement.executeUpdate(sqlString);
186
187 sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
188 "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
189 + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
190 username + "';";
191 System.out.println(sqlString);
192 result = statement.executeQuery(sqlString);
193
194 if (result.next()) {
195 id = result.getInt("id");
196 } else {
197 throw new DatabaseException("Couldn't save subscription");
198 }
199
200 if (initial) {
201 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
202 Predicate predicate = (Predicate) iter.next();
203 sqlString = "INSERT INTO subs_to_predicates (subscription, predicate) " +
204 "VALUES (" + id + "," + predicate.getID() + ");";
205 statement.executeUpdate(sqlString);
206 }
207 }
208 return id;
209 }
210
211}
Note: See TracBrowser for help on using the repository browser.