Project

General

Profile

Revision 2105

Implement sending email reports to harvest administrator and site administrator

View differences:

src/edu/ucsb/nceas/metacat/harvesterClient/Harvester.java
177 177
        else {
178 178
          System.out.println(" hours.");
179 179
        }
180
        //Thread.sleep(delay * oneHour);
181
        Thread.sleep(delay * oneSecond);
180
        Thread.sleep(delay * oneHour);
182 181
      }
183 182
      catch (InterruptedException e) {
184 183
          System.err.println("InterruptedException: " + e.getMessage());
......
232 231
  private int detailLogID;
233 232
  
234 233
  /** Email address of the Harvester Administrator */
235
  private String harvesterAdministrator;
234
  String harvesterAdministrator;
236 235
  
237 236
  /** Highest HARVEST_LOG_ID primary key in the HARVEST_LOG table */
238 237
  private int harvestLogID;
......
473 472
  
474 473

  
475 474
  /**
476
   * Prints all harvest log entries for this harvest run.
475
   * Prints harvest log entries for this harvest run. Entries may be filtered
476
   * for a particular site, or all entries may be printed.
477 477
   * 
478
   * @param out    the PrintStream object to write to
478
   * @param out            the PrintStream object to write to
479
   * @param maxCodeLevel   the maximum code level that should be printed,
480
   *                       e.g. "warning". Any log entries higher than this
481
   *                       level will not be printed.
482
   * @param siteScheduleID if greater than 0, indicates that the log
483
   *                       entry should only be printed for a particular site
484
   *                       as identified by its siteScheduleID. if 0, then
485
   *                       print output for all sites.
479 486
   */
480
  private void printHarvestLog(PrintStream out) {
487
  void printHarvestLog(PrintStream out, 
488
                       String maxCodeLevel, 
489
                       int siteScheduleID
490
                      ) {
481 491
    HarvestLog harvestLog;
492
    int logSiteScheduleID;
493
    int nErrors = 0;
494
    String phrase;
482 495
    
483 496
    for (int i = 0; i < harvestLogList.size(); i++) {
484 497
      harvestLog = (HarvestLog) harvestLogList.get(i);
485
      harvestLog.printOutput(out);
498
      logSiteScheduleID = harvestLog.getSiteScheduleID();
499
      
500
      if ((siteScheduleID == 0) || (siteScheduleID == logSiteScheduleID)) {
501
        if (harvestLog.isErrorEntry()) {
502
          nErrors++;
503
        }
504
      }      
486 505
    }
506

  
507
    out.println(marker);
508
    out.println("*");
509
    out.println("* METACAT HARVESTER REPORT");
510
    out.println("*");
511

  
512
    if (nErrors > 0) {
513
      phrase = (nErrors == 1) ? " ERROR WAS " : " ERRORS WERE ";
514
      out.println("* A TOTAL OF " + nErrors + phrase + "DETECTED.");
515
      out.println("* Please see the log entries below for additonal details.");
516
    }
517
    else {
518
      out.println("* NO ERRORS WERE DETECTED DURING THIS HARVEST.");
519
    }
520
    
521
    out.println("*");
522
    out.println(marker);
523

  
524
    for (int i = 0; i < harvestLogList.size(); i++) {
525
      harvestLog = (HarvestLog) harvestLogList.get(i);
526
      logSiteScheduleID = harvestLog.getSiteScheduleID();
527
      if ((siteScheduleID == 0) || (siteScheduleID == logSiteScheduleID)) {
528
        harvestLog.printOutput(out, maxCodeLevel);
529
      }
530
    }
487 531
  }
488 532
    
489 533

  
......
663 707
  /**
664 708
   * Sends a report to the Harvester administrator. The report prints each log
665 709
   * entry pertaining to this harvest run.
710
   *
711
   * @param maxCodeLevel  the maximum code level that should be printed,
712
   *                      e.g. "warning". Any log entries higher than this
713
   *                      level will not be printed.
666 714
   */
667
  void reportToAdministrator() {
715
  void reportToAdministrator(String maxCodeLevel) {
668 716
    PrintStream body;
669 717
    String from = harvesterAdministrator;
670 718
    MailMessage msg;
719
    int siteScheduleID = 0;
671 720
    String subject = "Report from Metacat Harvester";
672 721
    String to = harvesterAdministrator;
673 722
    
......
681 730
        msg.to(to);
682 731
        msg.setSubject(subject);
683 732
        body = msg.getPrintStream();
684
        printHarvestLog(body);
733
        printHarvestLog(body, maxCodeLevel, siteScheduleID);
685 734
        msg.sendAndClose();
686 735
      }
687 736
      catch (IOException e) {
......
697 746
   * of Metacat and disconnecting from the database.
698 747
   */
699 748
  private void shutdown() {
749
    String maxCodeLevel = "debug";  // Print all log entries from level 1
750
                                    // ("error") to level 5 ("debug")
751
    int siteScheduleID = 0;
752

  
700 753
    // Log shutdown operation
701 754
    System.out.println("Shutting Down Harvester");
702 755
    addLogEntry(0, "Shutting Down Harvester", "HarvesterShutdown", 0, null, "");
......
712 765
      System.out.println("Database access failed " + e);
713 766
    }
714 767
    
715
    printHarvestLog(System.out);  // Print log to standard output
716
    reportToAdministrator();      // Send a copy of the log to harvester admin
768
    // Print log to standard output and then email the Harvester administrator
769
    printHarvestLog(System.out, maxCodeLevel, siteScheduleID);
770
    reportToAdministrator(maxCodeLevel);      // Send a copy to harvester admin
717 771
  }
718 772
    
719 773

  
src/edu/ucsb/nceas/metacat/harvesterClient/HarvestLog.java
50 50
  private String message;
51 51
  private String harvestOperationCode;
52 52
  private int siteScheduleID;
53
  private String explanation;
54
  private String harvestOperationCodeLevel;
53 55
  private String timestamp;
54 56
  private HarvestDetailLog harvestDetailLog;  // Associated detail log, if any
55 57
    
......
86 88
    this.harvestOperationCode = harvestOperationCode;
87 89
    this.siteScheduleID = siteScheduleID;
88 90
    
91
    harvestOperationCodeLevel = 
92
            dbGetHarvestOperationCodeLevel(harvestOperationCode);
93
    explanation = dbGetExplanation(harvestOperationCode);
89 94
    dbInsertHarvestLogEntry();   // Insert this entry to the HARVEST_LOG table
90 95
  }
91 96
    
......
103 108
   * @param  siteScheduleID  the siteScheduleID for which this operation was
104 109
   *                         performed. 0 indicates that the operation did not
105 110
   *                         involve a particular harvest site.
111
   * @param  harvestDocument the HarvestDocument involved in this operation
112
   * @param  errorMessage    the error message generated by this operation
106 113
   */
107 114
  public HarvestLog(Harvester  harvester,
108 115
                    Date       harvestDate,
......
125 132
    this.siteScheduleID = siteScheduleID;
126 133
    this.harvestDetailLog = new HarvestDetailLog(harvester, harvestLogID, 
127 134
                                                 harvestDocument, errorMessage);
128
    
135
    harvestOperationCodeLevel = 
136
            dbGetHarvestOperationCodeLevel(harvestOperationCode);
137
    explanation = dbGetExplanation(harvestOperationCode);
129 138
    dbInsertHarvestLogEntry();               // Insert to the HARVEST_LOG table
130 139
    harvestDetailLog.dbInsertHarvestDetailLogEntry(); // and HARVEST_DETAIL_LOG
131 140
  }
......
279 288
    
280 289
    return returnString;
281 290
  }
291
  
282 292

  
293
  /**
294
   * Maps each code level to an integer value.
295
   * 
296
   * @param codeLevel        the code level: "error", "warning", "notice",
297
   *                         "info", or "debug"
298
   * @return codeLevelValue  the corresponding code level value
299
   */
300
  int getCodeLevelValue(String codeLevel) {
301
    int codeLevelValue = 0;
302
    
303
    if (codeLevel.equalsIgnoreCase("error")) {
304
      codeLevelValue = 1;
305
    }
306
    else if (codeLevel.equalsIgnoreCase("warning")) {
307
      codeLevelValue = 2;
308
    }
309
    else if (codeLevel.equalsIgnoreCase("notice")) {
310
      codeLevelValue = 3;
311
    }
312
    else if (codeLevel.equalsIgnoreCase("info")) {
313
      codeLevelValue = 4;
314
    }
315
    else if (codeLevel.equalsIgnoreCase("debug")) {
316
      codeLevelValue = 5;
317
    }
318
    
319
    return codeLevelValue;
320
  }
321
  
283 322

  
284 323
  /**
324
   * Access function for the siteScheduleID field.
325
   * 
326
   * @return  siteScheduleID, an int. If 0, indicates that this log entry does
327
   *          not pertain to a particular site.
328
   */
329
  int getSiteScheduleID() {
330
    return siteScheduleID;
331
  }
332
  
333

  
334
  /**
335
   * Determines whether this log entry had an error status.
336
   * 
337
   * @return  isError  true if this log entry had an error status, else false 
338
   */
339
  boolean isErrorEntry () {
340
    boolean isError;
341
    
342
    isError = (status != 0);
343
    
344
    return isError;
345
  }
346

  
347

  
348
  /**
285 349
   * Prints the contents of this HarvestLog object. Used in generating reports.
286 350
   * 
287
   * @param out   the PrintStream to write to
351
   * @param out        the PrintStream to write to
352
   * @param maxLevel  the maximum code level to output. If this log entry has a
353
   *                  higher code level than the maxLevel, no output
354
   *                  is issued. For example, if the maxLevel is "error"
355
   *                  (level 1), then anything lower ("warning", "notice", etc.)
356
   *                  will not generate any output.
288 357
   */
289
  void printOutput(PrintStream out) {
290
    out.println("");
291
    out.println(marker);
292
    out.println("*");
293
    out.println("* harvestLogID:         " + harvestLogID);
294
    out.println("* harvestDate:          " + harvestDate);
295
    out.println("* status:               " + status);
296
    out.println("* message:              " + message);
297
    out.println("* harvestOperationCode: " + harvestOperationCode);
358
  void printOutput(PrintStream out, String maxLevel) {
359
    int codeLevelValue = getCodeLevelValue(harvestOperationCodeLevel);
360
    int maxLevelValue = getCodeLevelValue(maxLevel);
361
    
362
    if (codeLevelValue <= maxLevelValue) {    
363
      out.println("");
364
      out.println(marker);
365
      out.println("*");
366
      out.println("* harvestLogID:         " + harvestLogID);
367
      out.println("* harvestDate:          " + harvestDate);
368
      out.println("* status:               " + status);
369
      out.println("* message:              " + message);
370
      out.println("* harvestOperationCode: " + harvestOperationCode);
371
      out.println("* description:          " + explanation);
298 372

  
299
    if (harvestOperationCode.equals("GetDocListSuccess") ||
373
      if (harvestOperationCode.equals("GetDocListSuccess") ||
300 374
        harvestOperationCode.equals("GetDocListError")) {
301
      if (siteScheduleID != 0) {
302
        harvester.printHarvestSiteSchedule(out, siteScheduleID);
375
        if (siteScheduleID != 0) {
376
          harvester.printHarvestSiteSchedule(out, siteScheduleID);
377
        }
303 378
      }
304
    }
305 379
    
306
    if (harvestDetailLog != null) {
307
      harvestDetailLog.printOutput(out);
380
      if (harvestDetailLog != null) {
381
        harvestDetailLog.printOutput(out);
382
      }
383

  
384
      out.println("*");
385
      out.println(marker);
308 386
    }
309

  
310
    out.println("*");
311
    out.println(marker);
312 387
  }
313 388

  
314 389
}
src/edu/ucsb/nceas/metacat/harvesterClient/HarvestSiteSchedule.java
374 374
  
375 375

  
376 376
  /**
377
   * Sends a report to the site summarizing the results of the harvest
378
   * operation.
377
   * Sends a report to the site summarizing the results of the harvest at
378
   * that site.
379 379
   */
380 380
  void reportToSite() {
381 381
    PrintStream body;
382
    String from = "Metacat Harvester";
382
    String from = harvester.harvesterAdministrator;
383
    String maxCodeLevel = "info";
383 384
    MailMessage msg;
385
    int nErrors = 0;
384 386
    String subject = "Report from Metacat Harvester";
385 387
    String to = contactEmail;
386 388
    
387 389
    if (!to.equals("")) {
388 390
      System.out.println("Sending report to siteScheduleID=" + siteScheduleID +
389 391
                         " at address: " + contactEmail);
390
      
391 392
      try {
392
        msg = new MailMessage();
393
        msg = new MailMessage(harvester.smtpServer);
393 394
        msg.from(from);
394 395
        msg.to(to);
395 396
        msg.setSubject(subject);
396 397
        body = msg.getPrintStream();
397
        
398
        harvester.printHarvestLog(body, maxCodeLevel, siteScheduleID);
399
        msg.sendAndClose();        
398 400
      }
399 401
      catch (IOException e) {
400 402
        System.out.println("There was a problem sending email to " + to);
401 403
        System.out.println("IOException: " + e.getMessage());
402 404
      }
403
      
404 405
    }
405 406
  }
406 407
    

Also available in: Unified diff