Project

General

Profile

« Previous | Next » 

Revision 2086

Implement harvester scheduing capabilities and sending email reports to the Harvester Administrator

View differences:

src/edu/ucsb/nceas/metacat/harvesterClient/HarvestDetailLog.java
6 6

  
7 7
package edu.ucsb.nceas.metacat.harvesterClient;
8 8

  
9
import java.io.PrintStream;
9 10
import java.sql.Connection;
10 11
import java.sql.SQLException;
11 12
import java.sql.Statement;
......
88 89

  
89 90
  /**
90 91
   * Prints the contents of this HarvestLog object. Used in generating reports.
92
   * 
93
   * @param out   the PrintStream to write to
91 94
   */
92
  void printOutput() {
93
    System.out.println("* detailLogID:          " + detailLogID);
94
    System.out.println("* errorMessage:         " + errorMessage);
95
    harvestDocument.printOutput();
95
  void printOutput(PrintStream out) {
96
    out.println("* detailLogID:          " + detailLogID);
97
    out.println("* errorMessage:         " + errorMessage);
98
    harvestDocument.printOutput(out);
96 99
  }
97 100

  
98 101
}
src/edu/ucsb/nceas/metacat/harvesterClient/Harvester.java
6 6

  
7 7
package edu.ucsb.nceas.metacat.harvesterClient;
8 8

  
9
import com.oreilly.servlet.MailMessage;
9 10
import java.io.File;
10 11
import java.io.FileInputStream;
11 12
import java.io.IOException;
13
import java.io.PrintStream;
12 14
import java.sql.Connection;
13 15
import java.sql.DriverManager;
14 16
import java.sql.ResultSet;
......
235 237
  /** Metacat client object */
236 238
  Metacat metacat;
237 239
  
240
  /** SMTP server for sending mail messages */
241
  String smtpServer;
242
  
238 243

  
239 244
  /*
240 245
   * Object methods
......
450 455
  
451 456

  
452 457
  /**
458
   * Prints all harvest log entries for this harvest run.
459
   * 
460
   * @param out    the PrintStream object to write to
461
   */
462
  private void printHarvestLog(PrintStream out) {
463
    HarvestLog harvestLog;
464
    
465
    for (int i = 0; i < harvestLogList.size(); i++) {
466
      harvestLog = (HarvestLog) harvestLogList.get(i);
467
      harvestLog.printOutput(out);
468
    }
469
  }
470
    
471

  
472
  /**
453 473
   * Prints the site schedule data for a given site.
454 474
   * 
475
   * @param out              the PrintStream to write to
455 476
   * @param siteScheduleID   the primary key in the HARVEST_SITE_SCHEDULE table
456 477
   */
457
  void printHarvestSiteSchedule(int siteScheduleID) {
478
  void printHarvestSiteSchedule(PrintStream out, int siteScheduleID) {
458 479
     HarvestSiteSchedule harvestSiteSchedule;
459 480

  
460 481
    for (int i = 0; i < harvestSiteScheduleList.size(); i++) {
461 482
      harvestSiteSchedule = (HarvestSiteSchedule)harvestSiteScheduleList.get(i);
462 483
      if (harvestSiteSchedule.siteScheduleID == siteScheduleID) {
463
        harvestSiteSchedule.printOutput();
484
        harvestSiteSchedule.printOutput(out);
464 485
      }
465 486
    }
466 487
  }
......
622 643
    
623 644

  
624 645
  /**
625
   * Sends a report to the Harvester administrator.
646
   * Sends a report to the Harvester administrator. The report prints each log
647
   * entry pertaining to this harvest run.
626 648
   */
627 649
  void reportToAdministrator() {
628
    System.out.println("\nSending report to administrator.");
650
    PrintStream body;
651
    String from = harvesterAdministrator;
652
    MailMessage msg;
653
    String subject = "Report from Metacat Harvester";
654
    String to = harvesterAdministrator;
655
    
656
    if (!to.equals("")) {
657
      System.out.println("Sending report to Harvester Administrator at address "
658
                         + harvesterAdministrator);
659
      
660
      try {
661
        msg = new MailMessage(smtpServer);
662
        msg.from(from);
663
        msg.to(to);
664
        msg.setSubject(subject);
665
        body = msg.getPrintStream();
666
        printHarvestLog(body);
667
        msg.sendAndClose();
668
      }
669
      catch (IOException e) {
670
        System.out.println("There was a problem sending email to " + to);
671
        System.out.println("IOException: " + e.getMessage());
672
      }
629 673
  }
674
}
630 675
    
631 676

  
632 677
  /**
......
649 694
      System.out.println("Database access failed " + e);
650 695
    }
651 696
    
652
    writeHarvestLog();
653
    reportToAdministrator();
697
    printHarvestLog(System.out);  // Print log to standard output
698
    reportToAdministrator();      // Send a copy of the log to harvester admin
654 699
  }
655 700
    
656 701

  
......
702 747

  
703 748
    metacatURL = properties.getProperty("metacatURL");
704 749
    password = properties.getProperty("password");
750
    smtpServer = properties.getProperty("smtpServer", "localhost");
705 751
    url = properties.getProperty("url");
706 752
    user = properties.getProperty("user");
707 753

  
......
757 803
  }
758 804

  
759 805

  
760
  /**
761
   * Writes one or more log entries to the HARVEST_LOG table.
762
   */
763
  private void writeHarvestLog() {
764
    HarvestLog harvestLog;
765
    
766
    for (int i = 0; i < harvestLogList.size(); i++) {
767
      harvestLog = (HarvestLog) harvestLogList.get(i);
768
      harvestLog.printOutput();
769
    }
770
  }
771
    
772 806
}
src/edu/ucsb/nceas/metacat/harvesterClient/HarvestDocument.java
9 9
import java.io.InputStream;
10 10
import java.io.InputStreamReader;
11 11
import java.io.IOException;
12
import java.io.PrintStream;
12 13
import java.io.StringReader;
13 14
import java.net.MalformedURLException;
14 15
import java.net.URL;
......
230 231
  
231 232
  /**
232 233
   * Print the data fields and values in this HarvestDocument object.
234
   * 
235
   * @param out   the PrintStream to write to
233 236
   */
234
  void printOutput() {
235
    System.out.println("* scope:                " + scope);
236
    System.out.println("* identifier:           " + identifier);
237
    System.out.println("* revision:             " + revision);
238
    System.out.println("* documentType:         " + documentType);
239
    System.out.println("* documentURL:          " + documentURL);
237
  void printOutput(PrintStream out) {
238
    out.println("* scope:                " + scope);
239
    out.println("* identifier:           " + identifier);
240
    out.println("* revision:             " + revision);
241
    out.println("* documentType:         " + documentType);
242
    out.println("* documentURL:          " + documentURL);
240 243
  }
241 244
 
242 245
 
src/edu/ucsb/nceas/metacat/harvesterClient/HarvestLog.java
6 6

  
7 7
package edu.ucsb.nceas.metacat.harvesterClient;
8 8

  
9
import java.io.PrintStream;
9 10
import java.sql.Connection;
10 11
import java.sql.ResultSet;
11 12
import java.sql.SQLException;
......
264 265

  
265 266
  /**
266 267
   * Prints the contents of this HarvestLog object. Used in generating reports.
268
   * 
269
   * @param out   the PrintStream to write to
267 270
   */
268
  void printOutput() {
269
    System.out.println("");
270
    System.out.println(marker);
271
    System.out.println("*");
272
    System.out.println("* harvestLogID:         " + harvestLogID);
273
    System.out.println("* harvestDate:          " + harvestDate);
274
    System.out.println("* status:               " + status);
275
    System.out.println("* message:              " + message);
276
    System.out.println("* harvestOperationCode: " + harvestOperationCode);
271
  void printOutput(PrintStream out) {
272
    out.println("");
273
    out.println(marker);
274
    out.println("*");
275
    out.println("* harvestLogID:         " + harvestLogID);
276
    out.println("* harvestDate:          " + harvestDate);
277
    out.println("* status:               " + status);
278
    out.println("* message:              " + message);
279
    out.println("* harvestOperationCode: " + harvestOperationCode);
277 280

  
278 281
    if (harvestOperationCode.equals("GetDocListSuccess") ||
279 282
        harvestOperationCode.equals("GetDocListError")) {
280 283
      if (siteScheduleID != 0) {
281
        harvester.printHarvestSiteSchedule(siteScheduleID);
284
        harvester.printHarvestSiteSchedule(out, siteScheduleID);
282 285
      }
283 286
    }
284 287
    
285 288
    if (harvestDetailLog != null) {
286
      harvestDetailLog.printOutput();
289
      harvestDetailLog.printOutput(out);
287 290
    }
288 291

  
289
    System.out.println("*");
290
    System.out.println(marker);
292
    out.println("*");
293
    out.println(marker);
291 294
  }
292 295

  
293 296
}
src/edu/ucsb/nceas/metacat/harvesterClient/HarvestSiteSchedule.java
6 6

  
7 7
package edu.ucsb.nceas.metacat.harvesterClient;
8 8

  
9
import com.oreilly.servlet.MailMessage;
9 10
import java.io.FileNotFoundException;
10 11
import java.io.IOException;
11 12
import java.io.InputStream;
12 13
import java.io.InputStreamReader;
14
import java.io.PrintStream;
13 15
import java.io.Reader;
14 16
import java.net.MalformedURLException;
15 17
import java.net.URL;
......
338 340

  
339 341
  /**
340 342
   * Prints the data that is stored in this HarvestSiteSchedule object.
343
   * 
344
   * @param out   the PrintStream to write to
341 345
   */
342
  void printOutput() {
343
    System.out.println("* siteScheduleID:       " + siteScheduleID);
344
    System.out.println("* documentListURL:      " + documentListURL);
345
    System.out.println("* ldapDN:               " + ldapDN);
346
    System.out.println("* dateNextHarvest:      " + dateNextHarvest);
347
    System.out.println("* dateLastHarvest:      " + dateLastHarvest);
348
    System.out.println("* updateFrequency:      " + updateFrequency);
349
    System.out.println("* unit:                 " + unit);
350
    System.out.println("* contactEmail:         " + contactEmail);
346
  void printOutput(PrintStream out) {
347
    out.println("* siteScheduleID:       " + siteScheduleID);
348
    out.println("* documentListURL:      " + documentListURL);
349
    out.println("* ldapDN:               " + ldapDN);
350
    out.println("* dateNextHarvest:      " + dateNextHarvest);
351
    out.println("* dateLastHarvest:      " + dateLastHarvest);
352
    out.println("* updateFrequency:      " + updateFrequency);
353
    out.println("* unit:                 " + unit);
354
    out.println("* contactEmail:         " + contactEmail);
351 355
  }
352 356
  
353 357

  
......
356 360
   * operation.
357 361
   */
358 362
  void reportToSite() {
359
    System.out.println("Sending report to site: " + contactEmail);
363
    PrintStream body;
364
    String from = "Metacat Harvester";
365
    MailMessage msg;
366
    String subject = "Report from Metacat Harvester";
367
    String to = contactEmail;
368
    
369
    if (!to.equals("")) {
370
      System.out.println("Sending report to siteScheduleID=" + siteScheduleID +
371
                         " at address: " + contactEmail);
372
      
373
      try {
374
        msg = new MailMessage();
375
        msg.from(from);
376
        msg.to(to);
377
        msg.setSubject(subject);
378
        body = msg.getPrintStream();
379
        
380
      }
381
      catch (IOException e) {
382
        System.out.println("There was a problem sending email to " + to);
383
        System.out.println("IOException: " + e.getMessage());
384
      }
385
      
386
    }
360 387
  }
361 388
    
362 389

  

Also available in: Unified diff