1 | package org.greenstone.gsdl3.gs3build.notifier;
|
---|
2 |
|
---|
3 |
|
---|
4 |
|
---|
5 | import java.io.*;
|
---|
6 | import java.net.*;
|
---|
7 | import java.sql.*;
|
---|
8 | import java.util.Calendar;
|
---|
9 | import java.util.Date;
|
---|
10 | import java.text.DateFormat;
|
---|
11 | import java.text.SimpleDateFormat;
|
---|
12 |
|
---|
13 | import org.greenstone.gsdl3.gs3build.CollectionManager;
|
---|
14 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
|
---|
15 |
|
---|
16 | public class NotifierManager {
|
---|
17 |
|
---|
18 | public NotifierManager() {
|
---|
19 | }
|
---|
20 |
|
---|
21 | public void detectEvents(CollectionManager collManager) {
|
---|
22 |
|
---|
23 | String collectionName = collManager.getCollectionName();
|
---|
24 | String host = "http://localhost:8080/soap/servlet/rpcrouter";
|
---|
25 |
|
---|
26 | String lastBuild = dateToSqlString(collManager.getBuildDate());
|
---|
27 | Statement statement = collManager.getDatabase().createStatement();
|
---|
28 | try {
|
---|
29 | // detect all new documents. A document is new if and only if
|
---|
30 | // AccessionDate >= CollectionLastRebuiltDate
|
---|
31 | ResultSet results = statement.executeQuery("SELECT DocID FROM document WHERE DocType != 'GSMETADATA' AND AccessionDate >= " + lastBuild);
|
---|
32 | notify(collectionName, host, results, collManager.getDatabase());
|
---|
33 |
|
---|
34 | // detect modified documents. A document is modified if and only if
|
---|
35 | // AccessionDate < CollectionLastRebuiltDate (ie, it is not new) and
|
---|
36 | // ModifiedDate >= IndexedDate
|
---|
37 | results = statement.executeQuery("SELECT DocID FROM document WHERE DocType != 'GSMETADATA' AND AccessionDate < " + lastBuild + " AND ModifiedDate >= IndexedDate");
|
---|
38 | notify(collectionName, host, results, collManager.getDatabase());
|
---|
39 | } catch (SQLException e) {
|
---|
40 | // TODO Auto-generated catch block
|
---|
41 | e.printStackTrace();
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * @param collName
|
---|
47 | * @param hostURL
|
---|
48 | * @param results
|
---|
49 | * @throws MalformedURLException
|
---|
50 | * @throws IOException
|
---|
51 | * @throws ProtocolException
|
---|
52 | * @throws SQLException
|
---|
53 | */
|
---|
54 | private void notify(String collName, String hostURL, ResultSet results, GS3SQLConnection database) {
|
---|
55 | try {
|
---|
56 | URL url = new URL("http://localhost:8080/alerting/service?receive");
|
---|
57 | System.out.println("trying to POST to " + url);
|
---|
58 | HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
---|
59 | conn.setDoOutput(true);
|
---|
60 | conn.setRequestMethod("POST");
|
---|
61 | conn.setUseCaches(false);
|
---|
62 | while(results.next()) {
|
---|
63 | System.out.println("new document: " + results.getString("DocID"));
|
---|
64 | try {
|
---|
65 | conn.connect();
|
---|
66 | OutputStream toReceiver = conn.getOutputStream();
|
---|
67 | OutputStreamWriter writeToReceiver = new OutputStreamWriter(toReceiver);
|
---|
68 |
|
---|
69 | // TODO HTTP Post -> SOAP
|
---|
70 | String documentID = results.getString("DocID");
|
---|
71 |
|
---|
72 | writeToReceiver.write("type=new_document&");
|
---|
73 | writeToReceiver.write("collectionID=" + collName + "&");
|
---|
74 | writeToReceiver.write("host_url=" + hostURL + "&");
|
---|
75 | writeToReceiver.write("document_title=" + getDocumentTitle(documentID, database) + "&");
|
---|
76 | writeToReceiver.write("documentID=" + documentID + "\n");
|
---|
77 | writeToReceiver.close();
|
---|
78 |
|
---|
79 | System.out.println(conn.getResponseCode());
|
---|
80 | InputStream fromReceiver = conn.getInputStream();
|
---|
81 | int i;
|
---|
82 | while ((i = fromReceiver.read()) != -1) System.out.write(i);
|
---|
83 | conn.disconnect();
|
---|
84 | } catch (IOException e1) {
|
---|
85 | // TODO Auto-generated catch block
|
---|
86 | System.out.println("Error talking to server.");
|
---|
87 | e1.printStackTrace();
|
---|
88 | }
|
---|
89 | }
|
---|
90 | } catch (IOException e) {
|
---|
91 | // TODO Auto-generated catch block
|
---|
92 | e.printStackTrace();
|
---|
93 | } catch (SQLException e) {
|
---|
94 | // TODO Auto-generated catch block
|
---|
95 | e.printStackTrace();
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * @param documentID
|
---|
101 | * @return
|
---|
102 | * @throws SQLException
|
---|
103 | */
|
---|
104 | private String getDocumentTitle(String documentID, GS3SQLConnection database) throws SQLException {
|
---|
105 | Statement statement = database.createStatement();
|
---|
106 | ResultSet results = statement.executeQuery("select mdvalues.value " +
|
---|
107 | "from mdvalues join namespaces on (mdvalues.namespaceref = namespaces.namespaceid) " +
|
---|
108 | "join metadata on (metadata.metadataref = namespaces.metadataref) " +
|
---|
109 | "where label like 'Title' and metadata.docID = '" + documentID +"';");
|
---|
110 | return results.getString(0);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * @param date
|
---|
115 | * @return
|
---|
116 | */
|
---|
117 | private String dateToSqlString(Date date) {
|
---|
118 | return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|