Changeset 8770


Ignore:
Timestamp:
2004-12-09T16:26:32+13:00 (19 years ago)
Author:
schweer
Message:

have to use http post because of classpath problems with soap; use correct sql query for finding out the title of the document

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/notifier/NotifierManager.java

    r8742 r8770  
    3333            // AccessionDate >= CollectionLastRebuiltDate
    3434            ResultSet results = statement.executeQuery("SELECT DocID FROM document WHERE DocType != 'GSMETADATA' AND AccessionDate >= " + lastBuild);
    35             notify(collectionName, host, results, collManager.getDatabase(), "new_document");
     35            notify(collectionName, host, results, collManager.getDatabase(), collManager.getBuildDate(), "new_document");
    3636           
    3737            // detect modified documents. A document is modified if and only if
     
    3939            // ModifiedDate >= IndexedDate
    4040            results = statement.executeQuery("SELECT DocID FROM document WHERE DocType != 'GSMETADATA' AND AccessionDate < " + lastBuild + " AND ModifiedDate >= IndexedDate");
    41             notify(collectionName, host, results, collManager.getDatabase(), "document_modified");
     41            notify(collectionName, host, results, collManager.getDatabase(), collManager.getBuildDate(), "document_modified");
    4242        } catch (SQLException e) {
    4343            // TODO Auto-generated catch block
     
    5050     * @param hostURL
    5151     * @param results
     52     * @param date
    5253     * @param eventType TODO
    5354     * @throws MalformedURLException
     
    5657     * @throws SQLException
    5758     */
    58     private void notify(String collName, String hostURL, ResultSet results, GS3SQLConnection database, String eventType) {
     59    private void notify(String collName, String hostURL, ResultSet results, GS3SQLConnection database, Date date, String eventType) {
    5960        try {
    60             URL url = new URL(hostURL);
    61             System.out.println("trying to send (via SOAP) to " + url);
     61            URL url = new URL("http://localhost:8080/alerting/service");
     62            System.out.println("trying to send to " + url);
    6263           
    6364            while(results.next()) {
    6465                System.out.println("new document: " + results.getString("DocID"));
    65                 Map event = new TreeMap();
    6666               
    6767                String documentID = results.getString("DocID");
    6868               
    69                 event.put("documentID", documentID);
    70                 event.put("type", eventType);
    71                 event.put("collectionID", collName);
    72                 event.put("host_url", hostURL);
    73                 event.put("document_title", getDocumentTitle(documentID, database));
    74                
    75                 Call call = new Call();
    76                 call.setTargetObjectURI("alerting");
    77                 call.setMethodName("receiveEvent");
    78                 call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
    79                 Vector params = new Vector();
    80                 params.addElement(new Parameter("event", Map.class, event, null));
    81                 call.setParams(params);
    8269                try {
    83                     Response resp = call.invoke(url, "");
    84                     if (!resp.generatedFault()) {
    85                         System.out.println("successfully posted event");
    86                         return;
     70                    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // TODO what about proxies?
     71                    conn.setUseCaches(false);
     72                    conn.setDoInput(true);
     73                    conn.setDoOutput(true);
     74                    conn.setRequestMethod("POST");
     75                    conn.connect();
     76                    // Construct data
     77                    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
     78                   
     79                    writer.write("action=receiveEvent&");
     80                    writer.write("documentID=");
     81                    writer.write(URLEncoder.encode(documentID, "UTF-8"));
     82                    writer.write("&type=");
     83                    writer.write(URLEncoder.encode(eventType, "UTF-8"));
     84                    writer.write("&collectionID=");
     85                    writer.write(URLEncoder.encode(collName, "UTF-8"));
     86                    writer.write("&host_url=");
     87                    writer.write(URLEncoder.encode(hostURL, "UTF-8"));
     88                    writer.write("&document_title=");
     89                    writer.write(URLEncoder.encode(getDocumentTitle(documentID, database), "UTF-8"));
     90                    writer.write("&document_url=");
     91                    writer.write(URLEncoder.encode("http://localhost:8080/gsdl3/library?a=d&c=" + collName + "&d=" + documentID, "UTF-8"));
     92                    writer.write("&timestamp=");
     93                    writer.write(URLEncoder.encode(new SimpleDateFormat("yyyy-MM-dd HH:mm z").format(date), "UTF-8"));
     94                    writer.flush();
     95                    System.out.println(conn.getResponseCode());
     96                    InputStream fromReceiver = conn.getInputStream();
     97                    int i;
     98                    while ((i = fromReceiver.read()) != -1) {
     99                        System.out.write(i);
    87100                    }
    88                     System.err.println("Posting event was unsuccessful:");
    89                     System.err.println(resp.getFault().getFaultString());
    90                 } catch (SOAPException e1) {
    91                     System.err.println("Exception while posting event: ");
     101                    conn.disconnect();
     102                } catch (ProtocolException e1) {
     103                    // TODO Auto-generated catch block
     104                    e1.printStackTrace();
     105                } catch (UnsupportedEncodingException e1) {
     106                    // TODO Auto-generated catch block
     107                    e1.printStackTrace();
     108                } catch (IOException e1) {
     109                    // TODO Auto-generated catch block
    92110                    e1.printStackTrace();
    93111                }
     112//             
     113//              Call call = new Call();
     114//              call.setTargetObjectURI("alerting");
     115//              call.setMethodName("receiveEvent");
     116//              call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
     117//              Vector params = new Vector();
     118//              params.addElement(new Parameter("event", Map.class, event, null));
     119//              call.setParams(params);
     120//              try {
     121//                    Response resp = call.invoke(url, "");
     122//                    if (!resp.generatedFault()) {
     123//                        System.out.println("successfully posted event");
     124//                        return;
     125//                    }
     126//                    System.err.println("Posting event was unsuccessful:");
     127//                    System.err.println(resp.getFault().getFaultString());
     128//                   
     129//                    Vector entries = resp.getFault().getDetailEntries();
     130//                    for (Iterator i = entries.iterator(); i.hasNext(); ) {
     131//                      org.w3c.dom.Element entry = (org.w3c.dom.Element)i.next( );
     132//                      System.err.println(entry.getFirstChild().getNodeValue( ));
     133//                    }
     134//                } catch (SOAPException e1) {
     135//                    System.err.println("Exception while posting event: ");
     136//                    e1.printStackTrace();
     137//                }
    94138            }
    95         } catch (IOException e) {
    96             // TODO Auto-generated catch block
    97             e.printStackTrace();
    98139        } catch (SQLException e) {
    99140            // TODO Auto-generated catch block
    100141            e.printStackTrace();
    101         }
     142        } catch (MalformedURLException e) {
     143            // TODO Auto-generated catch block
     144            e.printStackTrace();
     145        }
    102146    }
    103147
     
    107151     * @throws SQLException
    108152     */
    109     private String getDocumentTitle(String documentID, GS3SQLConnection database) throws SQLException {
     153    private String getDocumentTitle(String documentID, GS3SQLConnection database) {
    110154        Statement statement = database.createStatement();
    111         ResultSet results = statement.executeQuery("select mdvalues.value " +
    112                 "from mdvalues join namespaces on (mdvalues.namespaceref = namespaces.namespaceid) " +
    113                 "join metadata on (metadata.metadataref = namespaces.metadataref) " +
    114                 "where label like 'Title' and metadata.docID = '" + documentID +"';");
    115         return results.getString(0);
     155        ResultSet results;
     156        try {
     157            String sqlString = "SELECT mdvalues.value " +
     158                    "FROM structure s JOIN divisions d ON (s.structureref = d.parentref) " +
     159                    "                 JOIN divisionmetarefs USING (divisionref) " +
     160                    "                 JOIN metadata m USING (metaid) " +
     161                    "                 JOIN namespaces USING (metadataref) " +
     162                    "                 JOIN mdvalues USING (namespaceref) " +
     163                    "  WHERE mdvalues.label = 'Title' AND s.docid = '" + documentID + "'" +
     164                    "                                 AND s.structureType = 'Whole Document' " +
     165                    "                                 AND d.parenttype = 'Structure' " +
     166                    "                                 AND m.docID = s.docid;";
     167            results = statement.executeQuery(sqlString);
     168            return results.getString(0);
     169        } catch (SQLException e) {
     170            return "";
     171        }
    116172    }
    117173
Note: See TracChangeset for help on using the changeset viewer.