1 | package org.greenstone.gsdl3.gs3build.notifier;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 | import java.net.*;
|
---|
5 | import java.sql.*;
|
---|
6 | import java.text.SimpleDateFormat;
|
---|
7 | import java.util.Date;
|
---|
8 |
|
---|
9 | import org.greenstone.gsdl3.gs3build.CollectionManager;
|
---|
10 | import org.greenstone.gsdl3.gs3build.database.GS3SQLConnection;
|
---|
11 |
|
---|
12 | public class NotifierManager {
|
---|
13 |
|
---|
14 | public NotifierManager() {
|
---|
15 | }
|
---|
16 |
|
---|
17 | public void detectEvents(CollectionManager collManager) {
|
---|
18 | String notifyHost = collManager.getNotifyHost();
|
---|
19 | if (notifyHost == null)
|
---|
20 | return;
|
---|
21 |
|
---|
22 | System.out.println("detecting events");
|
---|
23 |
|
---|
24 | String collectionName = collManager.getCollectionName();
|
---|
25 | String host = "http://localhost:8080/soap/servlet/rpcrouter";
|
---|
26 |
|
---|
27 | String lastBuild = dateToSqlString(collManager.getBuildDate());
|
---|
28 | System.out.println("last build was " + lastBuild);
|
---|
29 | try {
|
---|
30 | Statement statement = collManager.getDatabase().createStatement();
|
---|
31 | // detect all new documents. A document is new if and only if
|
---|
32 | // AccessionDate >= CollectionLastRebuiltDate
|
---|
33 | ResultSet results = statement.executeQuery("SELECT DocID FROM document WHERE DocType != 'GSMETADATA' AND AccessionDate >= " + lastBuild);
|
---|
34 | notify(collectionName, host, notifyHost, results, collManager.getDatabase(), collManager.getBuildDate(), "new_document");
|
---|
35 |
|
---|
36 | // detect modified documents. A document is modified if and only if
|
---|
37 | // AccessionDate < CollectionLastRebuiltDate (ie, it is not new) and
|
---|
38 | // IndexedDate >= CollectionLastRebuiltDate
|
---|
39 | results = statement.executeQuery("SELECT DocID FROM document WHERE DocType != 'GSMETADATA' AND AccessionDate < " + lastBuild + " AND IndexedDate >= " + lastBuild);
|
---|
40 | notify(collectionName, host, notifyHost, results, collManager.getDatabase(), collManager.getBuildDate(), "document_modified");
|
---|
41 |
|
---|
42 | statement.close();
|
---|
43 | // TODO deleted docs?
|
---|
44 | } catch (SQLException e) {
|
---|
45 | // TODO Auto-generated catch block
|
---|
46 | e.printStackTrace();
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @param collName
|
---|
52 | * @param hostURL
|
---|
53 | * @param results
|
---|
54 | * @param date
|
---|
55 | * @param eventType TODO
|
---|
56 | * @throws MalformedURLException
|
---|
57 | * @throws IOException
|
---|
58 | * @throws ProtocolException
|
---|
59 | * @throws SQLException
|
---|
60 | */
|
---|
61 | private void notify(String collName, String hostURL, String notifyHost, ResultSet results, GS3SQLConnection database, Date date, String eventType) {
|
---|
62 | try {
|
---|
63 | System.out.println("notifyHost is " + notifyHost);
|
---|
64 | URL url = new URL("http://" + notifyHost + "/alerting/service");
|
---|
65 | System.out.println("trying to send to " + url);
|
---|
66 |
|
---|
67 | while(results.next()) {
|
---|
68 | System.out.println(eventType + ": " + results.getString("DocID"));
|
---|
69 |
|
---|
70 | String documentID = results.getString("DocID");
|
---|
71 |
|
---|
72 | try {
|
---|
73 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // TODO what about proxies?
|
---|
74 | conn.setUseCaches(false);
|
---|
75 | conn.setDoInput(true);
|
---|
76 | conn.setDoOutput(true);
|
---|
77 | conn.setRequestMethod("POST");
|
---|
78 | conn.connect();
|
---|
79 | // Construct data
|
---|
80 | OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
|
---|
81 |
|
---|
82 | writer.write("action=receiveEvent&");
|
---|
83 | writer.write("documentID=");
|
---|
84 | writer.write(URLEncoder.encode(documentID, "UTF-8"));
|
---|
85 | writer.write("&type=");
|
---|
86 | writer.write(URLEncoder.encode(eventType, "UTF-8"));
|
---|
87 | writer.write("&collectionID=");
|
---|
88 | writer.write(URLEncoder.encode(collName, "UTF-8"));
|
---|
89 | writer.write("&hostID=");
|
---|
90 | writer.write(URLEncoder.encode(hostURL, "UTF-8"));
|
---|
91 | writer.write("&document_title=");
|
---|
92 | writer.write(URLEncoder.encode(getDocumentTitle(documentID, database), "UTF-8"));
|
---|
93 | writer.write("&document_url=");
|
---|
94 | writer.write(URLEncoder.encode("http://localhost:8080/gsdl3/library?a=d&c=" + collName + "&d=" + documentID, "UTF-8"));
|
---|
95 | writer.write("×tamp=");
|
---|
96 | writer.write(URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm z").format(date), "UTF-8"));
|
---|
97 | writer.flush();
|
---|
98 | System.out.println(conn.getResponseCode());
|
---|
99 | InputStream fromReceiver = conn.getInputStream();
|
---|
100 | int i;
|
---|
101 | while ((i = fromReceiver.read()) != -1) {
|
---|
102 | System.out.write(i);
|
---|
103 | }
|
---|
104 | conn.disconnect();
|
---|
105 | } catch (ProtocolException e1) {
|
---|
106 | // TODO Auto-generated catch block
|
---|
107 | e1.printStackTrace();
|
---|
108 | } catch (UnsupportedEncodingException e1) {
|
---|
109 | // TODO Auto-generated catch block
|
---|
110 | e1.printStackTrace();
|
---|
111 | } catch (IOException e1) {
|
---|
112 | // TODO Auto-generated catch block
|
---|
113 | e1.printStackTrace();
|
---|
114 | }
|
---|
115 | }
|
---|
116 | } catch (SQLException e) {
|
---|
117 | // TODO Auto-generated catch block
|
---|
118 | e.printStackTrace();
|
---|
119 | } catch (MalformedURLException e) {
|
---|
120 | // TODO Auto-generated catch block
|
---|
121 | e.printStackTrace();
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * @param documentID
|
---|
127 | * @return
|
---|
128 | * @throws SQLException
|
---|
129 | */
|
---|
130 | private String getDocumentTitle(String documentID, GS3SQLConnection database) {
|
---|
131 | System.out.println("getting title for document " + documentID + " from database");
|
---|
132 |
|
---|
133 | try {
|
---|
134 | Statement statement = database.createStatement();
|
---|
135 | String sqlString = "SELECT mdvalues.value " +
|
---|
136 | "FROM structure s JOIN divisions d ON (s.structureref = d.parentref) " +
|
---|
137 | " JOIN divisionmetarefs USING (divisionref) " +
|
---|
138 | " JOIN metadata m USING (metaid) " +
|
---|
139 | " JOIN namespaces USING (metadataref) " +
|
---|
140 | " JOIN mdvalues USING (namespaceref) " +
|
---|
141 | " WHERE mdvalues.label = 'Title' AND s.docid = '" + documentID + "'" +
|
---|
142 | " AND s.structureType = 'Whole Document' " +
|
---|
143 | " AND d.parenttype = 'Structure' " +
|
---|
144 | " AND m.docID = s.docid;";
|
---|
145 | ResultSet results = statement.executeQuery(sqlString);
|
---|
146 | if (results.first()) {
|
---|
147 | String title = results.getString("value");
|
---|
148 | System.out.println("title is " + title);
|
---|
149 | return title;
|
---|
150 | }
|
---|
151 | statement.close();
|
---|
152 | } catch (SQLException e) {
|
---|
153 | e.printStackTrace();
|
---|
154 | }
|
---|
155 | return "";
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * @param date
|
---|
160 | * @return
|
---|
161 | */
|
---|
162 | private String dateToSqlString(Date date) {
|
---|
163 | return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|