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

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

talk to alerting service via SOAP

  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1package org.greenstone.gsdl3.gs3build.notifier;
2
3
4
5import java.io.*;
6import java.net.*;
7import java.sql.*;
8import java.text.SimpleDateFormat;
9import java.util.*;
10import java.util.Date;
11import java.util.TreeMap;
12
13import org.apache.soap.Constants;
14import org.apache.soap.SOAPException;
15import org.apache.soap.rpc.*;
16import org.apache.soap.rpc.Call;
17import org.apache.soap.rpc.Parameter;
18import org.greenstone.gsdl3.gs3build.CollectionManager;
19import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
20
21public class NotifierManager {
22
23 public NotifierManager() {
24 }
25
26 public void detectEvents(CollectionManager collManager) {
27
28 String collectionName = collManager.getCollectionName();
29 String host = "http://localhost:8080/soap/servlet/rpcrouter";
30
31 String lastBuild = dateToSqlString(collManager.getBuildDate());
32 Statement statement = collManager.getDatabase().createStatement();
33 try {
34 // detect all new documents. A document is new if and only if
35 // AccessionDate >= CollectionLastRebuiltDate
36 ResultSet results = statement.executeQuery("SELECT DocID FROM document WHERE DocType != 'GSMETADATA' AND AccessionDate >= " + lastBuild);
37 notify(collectionName, host, results, collManager.getDatabase(), "new_document");
38
39 // detect modified documents. A document is modified if and only if
40 // AccessionDate < CollectionLastRebuiltDate (ie, it is not new) and
41 // ModifiedDate >= IndexedDate
42 results = statement.executeQuery("SELECT DocID FROM document WHERE DocType != 'GSMETADATA' AND AccessionDate < " + lastBuild + " AND ModifiedDate >= IndexedDate");
43 notify(collectionName, host, results, collManager.getDatabase(), "document_modified");
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 eventType TODO
55 * @throws MalformedURLException
56 * @throws IOException
57 * @throws ProtocolException
58 * @throws SQLException
59 */
60 private void notify(String collName, String hostURL, ResultSet results, GS3SQLConnection database, String eventType) {
61 try {
62 URL url = new URL(hostURL);
63 System.out.println("trying to send (via SOAP) to " + url);
64
65 while(results.next()) {
66 System.out.println("new document: " + results.getString("DocID"));
67 Map event = new TreeMap();
68
69 String documentID = results.getString("DocID");
70
71 event.put("documentID", documentID);
72 event.put("type", eventType);
73 event.put("collectionID", collName);
74 event.put("host_url", hostURL);
75 event.put("document_title", getDocumentTitle(documentID, database));
76
77 Call call = new Call();
78 call.setTargetObjectURI("alerting");
79 call.setMethodName("receiveEvent");
80 call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
81 Vector params = new Vector();
82 params.addElement(new Parameter("event", Map.class, event, null));
83 call.setParams(params);
84 try {
85 Response resp = call.invoke(url, "");
86 if (!resp.generatedFault()) {
87 System.out.println("successfully posted event");
88 return;
89 }
90 System.err.println("Posting event was unsuccessful:");
91 System.err.println(resp.getFault().getFaultString());
92 } catch (SOAPException e1) {
93 System.err.println("Exception while posting event: ");
94 e1.printStackTrace();
95 }
96 }
97 } catch (IOException e) {
98 // TODO Auto-generated catch block
99 e.printStackTrace();
100 } catch (SQLException e) {
101 // TODO Auto-generated catch block
102 e.printStackTrace();
103 }
104 }
105
106 /**
107 * @param documentID
108 * @return
109 * @throws SQLException
110 */
111 private String getDocumentTitle(String documentID, GS3SQLConnection database) throws SQLException {
112 Statement statement = database.createStatement();
113 ResultSet results = statement.executeQuery("select mdvalues.value " +
114 "from mdvalues join namespaces on (mdvalues.namespaceref = namespaces.namespaceid) " +
115 "join metadata on (metadata.metadataref = namespaces.metadataref) " +
116 "where label like 'Title' and metadata.docID = '" + documentID +"';");
117 return results.getString(0);
118 }
119
120 /**
121 * @param date
122 * @return
123 */
124 private String dateToSqlString(Date date) {
125 return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
126 }
127}
128
Note: See TracBrowser for help on using the repository browser.