Project

General

Profile

« Previous | Next » 

Revision 2086

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

View differences:

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
}

Also available in: Unified diff