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

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

filtering should work now (including multivalues); changed database based on Annika's suggestions

  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 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(true);
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(boolean initial) throws DatabaseException, SQLException {
160 Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
161 Statement statement = conn.createStatement();
162 String sqlString;
163 if (initial) {
164 sqlString = "INSERT INTO subscriptions (name, email, rss, page, user) " +
165 "VALUES ('" + name + "','" + email + "'," + (rssNotification ? 1 : 0) +
166 "," + (eventsSincePageNotification ? 1: 0)+ ", '" + username + "');";
167 } else {
168 sqlString = "UPDATE subscriptions SET name='" + name + "', email='" +
169 email + "', rss=" + (rssNotification ? 1 : 0) + ",page=" +
170 (eventsSincePageNotification ? 1 : 0)+ "WHERE id=" + id + ";";
171 // cannot change user
172 }
173 statement.executeUpdate(sqlString);
174
175 sqlString = "SELECT id FROM subscriptions WHERE name like '" + name +
176 "' AND email like '" + email + "' AND rss=" + (rssNotification ? 1 : 0)
177 + " AND page=" + (eventsSincePageNotification ? 1 : 0) + " AND user like '" +
178 username + "';";
179 System.out.println(sqlString);
180 ResultSet result = statement.executeQuery(sqlString);
181 int id;
182 if (result.next()) {
183 id = result.getInt("id");
184 } else {
185 throw new DatabaseException("Couldn't save subscription");
186 }
187
188 if (initial) {
189 for (Iterator iter = getPredicates().iterator(); iter.hasNext();) {
190 Predicate predicate = (Predicate) iter.next();
191 sqlString = "INSERT INTO subs_to_predicates (subscription, predicate) " +
192 "VALUES (" + id + "," + predicate.getID() + ");";
193 statement.executeUpdate(sqlString);
194 }
195 }
196 return id;
197 }
198
199}
Note: See TracBrowser for help on using the repository browser.