source: trunk/gsdl3/extensions/gsdl-as/src/org/greenstone/gsdlas/EventStore.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: 5.8 KB
Line 
1/*
2 * Created on Nov 23, 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;
10
11import java.sql.*;
12import java.text.ParseException;
13import java.text.SimpleDateFormat;
14import java.util.*;
15import java.util.Date;
16
17import org.greenstone.gsdlas.database.DatabaseException;
18import org.greenstone.gsdlas.database.DatabaseManager;
19import org.greenstone.gsdlas.profiles.Subscription;
20
21/**
22 * @author schweer
23 *
24 * TODO To change the template for this generated type comment go to
25 * Window - Preferences - Java - Code Style - Code Templates
26 */
27public class EventStore {
28
29 private static EventStore instance;
30
31 private EventStore() {
32 }
33
34 /**
35 * @return
36 */
37 public static EventStore getInstance() {
38 if (instance == null) instance = new EventStore();
39 return instance;
40 }
41
42 public void add(Map event, Set subscriptions) throws DatabaseException, SQLException, ParseException {
43 Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
44 Statement statement = conn.createStatement();
45
46 int eventID = saveEventToDatabase(event);
47
48 for (Iterator iter = subscriptions.iterator(); iter.hasNext();) {
49 Subscription sub = (Subscription) iter.next();
50 int subID = sub.getId();
51 String sqlString = "INSERT INTO events_to_subs (event,subscription) " +
52 "VALUES (" + eventID + "," + subID + ");";
53 statement.executeUpdate(sqlString);
54 }
55 }
56
57 /**
58 * @param event
59 * @return
60 * @throws DatabaseException
61 * @throws SQLException
62 * @throws ParseException
63 */
64 private int saveEventToDatabase(Map event) throws DatabaseException, SQLException, ParseException {
65 String eventString = mapAsString(event);
66 String timestamp = (String) event.get("timestamp");
67 timestamp = convertToSQLDatetime(timestamp);
68
69 Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
70 Statement statement = conn.createStatement();
71 String query = "SELECT id FROM events WHERE timestamp = " + timestamp +
72 " AND content = '" + eventString + "';";
73 ResultSet results = statement.executeQuery(query);
74 if (results.next()) {
75 return results.getInt("id");
76 }
77
78 String insert = "INSERT INTO events (timestamp,content) " +
79 "VALUES (" + timestamp + ",'" + eventString + "');";
80 statement.executeUpdate(insert);
81 results = statement.executeQuery(query);
82 if (results.next()) {
83 return results.getInt("id");
84 }
85 throw new DatabaseException("couldn't save event to database");
86 }
87
88 /**
89 * converts a timestamp from an event to a format recognised by MySQL
90 * @param timestamp
91 * @return
92 * @throws ParseException
93 */
94 private String convertToSQLDatetime(String timestamp) throws ParseException {
95 Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(timestamp);
96 return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
97 }
98
99 /**
100 * @param event
101 */
102 private String encodeSpecialChars(String string) {
103 String encodedString = string.replace("&", "&");
104 encodedString = encodedString.replace("<", "&lt;");
105 encodedString = encodedString.replace(">", "&gt;");
106 encodedString = encodedString.replace("'", "&apos;");
107 encodedString = encodedString.replace("\"", "&quot;");
108 return encodedString;
109 }
110
111 /**
112 * @param subscriptionID
113 * @return
114 * @throws DatabaseException
115 * @throws SQLException
116 */
117 public Set getEvents(Integer subscriptionID) throws DatabaseException, SQLException {
118 Set result = new HashSet(); // can't use TreeSet, because we put Maps in there, and Map isn't instanceof Comparable
119 Connection conn = DatabaseManager.getInstance().getDatabaseConnection();
120 Statement statement = conn.createStatement();
121 String query = "SELECT content " +
122 "FROM events e JOIN events_to_subs ets ON (e.id = ets.event) " +
123 "WHERE ets.subscription = " + subscriptionID.intValue() + ";";
124 ResultSet results = statement.executeQuery(query);
125 while (results.next()) {
126 Map event = stringAsMap(results.getString("content"));
127 result.add(event);
128 }
129 return result;
130 }
131
132 private String mapAsString(Map map) {
133 StringBuffer buffer = new StringBuffer();
134 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
135 String key = (String) iter.next();
136 String value = (String) map.get(key);
137 String encodedValue = encodeSpecialChars(value);
138 buffer.append(key);
139 buffer.append(">"); // > has been encoded to &gt; in the original String
140 buffer.append(encodedValue);
141 if (iter.hasNext()) {
142 buffer.append(">>");
143 }
144 }
145 return buffer.toString();
146 }
147
148 private Map stringAsMap(String string) {
149 Map map = new HashMap();
150 String[] lines = string.split(">>");
151 for (int i = 0; i < lines.length; i++) {
152 String line = lines[i];
153 String[] columns = line.split(">");
154 String key = columns[0];
155 String value = null;
156 if (columns.length > 1) {
157 value = columns[1];
158 }
159 map.put(key, value);
160 }
161 return map;
162 }
163
164 public void cleanUp() {
165 // TODO implement method
166 // delete all events that are too old (?)
167 // delete all events that don't match any subscriptions
168 }
169}
Note: See TracBrowser for help on using the repository browser.