source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/notifier/NotifierManager.java@ 8869

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

notifications will only be sent if the collectionConfig.xml of the collection has an entry <nofiy host=hostId/>, with hostId being the name and port of the host the notifications should be sent to (for most cases, this will be localhost:8080). note that the alerting service (/research/schweer/gsdl3/packages/gsdl-as) has to be deployed at /alerting for this to work, and soap for localsite has to be enabled.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1package org.greenstone.gsdl3.gs3build.notifier;
2
3import java.io.*;
4import java.net.*;
5import java.sql.*;
6import java.text.SimpleDateFormat;
7import java.util.Date;
8
9import org.greenstone.gsdl3.gs3build.CollectionManager;
10import org.greenstone.gsdl3.gs3build.database.GS3SQLConnection;
11
12public 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 Statement statement = collManager.getDatabase().createStatement();
30 try {
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 // TODO deleted docs?
43 } catch (SQLException e) {
44 // TODO Auto-generated catch block
45 e.printStackTrace();
46 }
47 }
48
49 /**
50 * @param collName
51 * @param hostURL
52 * @param results
53 * @param date
54 * @param eventType TODO
55 * @throws MalformedURLException
56 * @throws IOException
57 * @throws ProtocolException
58 * @throws SQLException
59 */
60 private void notify(String collName, String hostURL, String notifyHost, ResultSet results, GS3SQLConnection database, Date date, String eventType) {
61 try {
62 System.out.println("notifyHost is " + notifyHost);
63 URL url = new URL("http://" + notifyHost + "/alerting/service");
64 System.out.println("trying to send to " + url);
65
66 while(results.next()) {
67 System.out.println(eventType + ": " + results.getString("DocID"));
68
69 String documentID = results.getString("DocID");
70
71 try {
72 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // TODO what about proxies?
73 conn.setUseCaches(false);
74 conn.setDoInput(true);
75 conn.setDoOutput(true);
76 conn.setRequestMethod("POST");
77 conn.connect();
78 // Construct data
79 OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
80
81 writer.write("action=receiveEvent&");
82 writer.write("documentID=");
83 writer.write(URLEncoder.encode(documentID, "UTF-8"));
84 writer.write("&type=");
85 writer.write(URLEncoder.encode(eventType, "UTF-8"));
86 writer.write("&collectionID=");
87 writer.write(URLEncoder.encode(collName, "UTF-8"));
88 writer.write("&hostID=");
89 writer.write(URLEncoder.encode(hostURL, "UTF-8"));
90 writer.write("&document_title=");
91 writer.write(URLEncoder.encode(getDocumentTitle(documentID, database), "UTF-8"));
92 writer.write("&document_url=");
93 writer.write(URLEncoder.encode("http://localhost:8080/gsdl3/library?a=d&c=" + collName + "&d=" + documentID, "UTF-8"));
94 writer.write("&timestamp=");
95 writer.write(URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm z").format(date), "UTF-8"));
96 writer.flush();
97 System.out.println(conn.getResponseCode());
98 InputStream fromReceiver = conn.getInputStream();
99 int i;
100 while ((i = fromReceiver.read()) != -1) {
101 System.out.write(i);
102 }
103 conn.disconnect();
104 } catch (ProtocolException e1) {
105 // TODO Auto-generated catch block
106 e1.printStackTrace();
107 } catch (UnsupportedEncodingException e1) {
108 // TODO Auto-generated catch block
109 e1.printStackTrace();
110 } catch (IOException e1) {
111 // TODO Auto-generated catch block
112 e1.printStackTrace();
113 }
114 }
115 } catch (SQLException e) {
116 // TODO Auto-generated catch block
117 e.printStackTrace();
118 } catch (MalformedURLException e) {
119 // TODO Auto-generated catch block
120 e.printStackTrace();
121 }
122 }
123
124 /**
125 * @param documentID
126 * @return
127 * @throws SQLException
128 */
129 private String getDocumentTitle(String documentID, GS3SQLConnection database) {
130 System.out.println("getting title for document " + documentID + " from database");
131 Statement statement = database.createStatement();
132
133 ResultSet results;
134 try {
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 results = statement.executeQuery(sqlString);
146 if (results.next()) {
147 String title = results.getString("value");
148 System.out.println("title is " + title);
149 return title;
150 }
151 } catch (SQLException e) {
152 e.printStackTrace();
153 }
154 return "";
155 }
156
157 /**
158 * @param date
159 * @return
160 */
161 private String dateToSqlString(Date date) {
162 return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
163 }
164}
165
Note: See TracBrowser for help on using the repository browser.