Project

General

Profile

« Previous | Next » 

Revision 2031

Additional development of Harvester implementation

View differences:

HarvestLog.java
6 6

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

  
9
import java.sql.Connection;
10
import java.sql.ResultSet;
11
import java.sql.SQLException;
12
import java.sql.SQLWarning;
13
import java.sql.Statement;
14
import java.text.SimpleDateFormat;
15
import java.util.Date;
16

  
17

  
9 18
/**
10 19
 * Manages log entries to be inserted to the HARVEST_LOG table.
11 20
 *
......
13 22
 */
14 23
public class HarvestLog {
15 24
    
16
  private Object harvestDate;
25
  private Harvester harvester;              // The parent Harvester object
26
  private int harvestLogID;
27
  private Date harvestDate;
28
  private int status;
29
  private String message;
17 30
  private String harvestOperationCode;
18
  private String message;
19 31
  private int siteScheduleID;
20
  private String status;
32
  private String timestamp;
33
  private HarvestDetailLog harvestDetailLog;  // Associated detail log, if any
21 34
    
35

  
22 36
  /** 
23
    * Creates a new instance of HarvestLog.
24
    */
25
  public HarvestLog() {
37
   * Creates a new instance of HarvestLog. This constructor is used when
38
   * creating log entries that do not involve an error on a harvest document.
39
   * (For that type of log entry, use the alternate constructor below.)
40
   *
41
   * @param  harvester       the parent Harvester object
42
   * @param  harvestDate     the date of this harvest
43
   * @param  status          the status of the harvest operation
44
   * @param  message         the message text of the harvest operation
45
   * @param  harvestOperationCode  the harvest operation code
46
   * @param  siteScheduleID  the siteScheduleID for which this operation was
47
   *                         performed. 0 indicates that the operation did not
48
   *                         involve a particular harvest site.
49
   */
50
  public HarvestLog(Harvester  harvester,
51
                    Date       harvestDate,
52
                    int        status,
53
                    String     message, 
54
                    String     harvestOperationCode,
55
                    int        siteScheduleID
56
                   ) {
57
    Date now = new Date();
58
    timestamp = now.toString();
59

  
60
    this.harvester = harvester;
61
    this.harvestLogID = harvester.getHarvestLogID();
62
    this.harvestDate = harvestDate;
63
    this.status = status;
64
    this.message = message;
65
    this.harvestOperationCode = harvestOperationCode;
66
    this.siteScheduleID = siteScheduleID;
67
    
68
    dbInsertHarvestLogEntry();   // Insert this entry to the HARVEST_LOG table
26 69
  }
27 70
    
28 71

  
72
  /** 
73
   * Creates a new instance of HarvestLog and inserts this entry to the
74
   * HARVEST_LOG table. This version of the constructor also instantiates a 
75
   * HarvestDetailLog object and inserts it to the HARVEST_DETAIL_LOG table.
76
   *
77
   * @param  harvester       the parent Harvester object
78
   * @param  harvestDate     the date of this harvest
79
   * @param  status          the status of the harvest operation
80
   * @param  message         the message text of the harvest operation
81
   * @param  harvestOperationCode  the harvest operation code
82
   * @param  siteScheduleID  the siteScheduleID for which this operation was
83
   *                         performed. 0 indicates that the operation did not
84
   *                         involve a particular harvest site.
85
   */
86
  public HarvestLog(Harvester  harvester,
87
                    Date       harvestDate,
88
                    int        status,
89
                    String     message, 
90
                    String     harvestOperationCode,
91
                    int        siteScheduleID,
92
                    HarvestDocument harvestDocument,
93
                    String     errorMessage
94
                   ) {
95
    Date now = new Date();
96
    timestamp = now.toString();
97

  
98
    this.harvester = harvester;
99
    this.harvestLogID = harvester.getHarvestLogID();
100
    this.harvestDate = harvestDate;
101
    this.status = status;
102
    this.message = message;
103
    this.harvestOperationCode = harvestOperationCode;
104
    this.siteScheduleID = siteScheduleID;
105
    this.harvestDetailLog = new HarvestDetailLog(harvester, harvestLogID, 
106
                                                 harvestDocument, errorMessage);
107
    
108
    dbInsertHarvestLogEntry();               // Insert to the HARVEST_LOG table
109
    harvestDetailLog.dbInsertHarvestDetailLogEntry(); // and HARVEST_DETAIL_LOG
110
  }
111
    
112

  
29 113
  /**
30 114
   * Retrieves the value of the EXPLANATION field of the HARVEST_OPERATION
31 115
   * table based on the value of the HARVEST_OPERATION_CODE field. 
32
   * Not yet implemented.
33 116
   * 
34
   * @param harvestOperationCode  string value of the harvest operation code
35
   * @return           the explanation for this harvest operation, a String
117
   * @param  harvestOperationCode  string value of the harvest operation code
118
   * @return the explanation for this harvest operation, a String
36 119
   */
37
  public String dbGetExplanation(String harvestOperationCode) {
38
    String explanation = "";
120
  String dbGetExplanation(String harvestOperationCode) {
121
    String explanation;
122
    String fieldName = "EXPLANATION";
123
    
124
    explanation = dbQueryHarvestOperation(fieldName, harvestOperationCode);
39 125
        
40 126
    return explanation;
41 127
  }
......
44 130
  /**
45 131
   * Retrieves the value of the HARVEST_OPERATION_CODE_LEVEL field of the
46 132
   * HARVEST_OPERATION table based on the value of the HARVEST_OPERATION_CODE 
47
   * field. Not yet implemented.
133
   * field.
48 134
   * 
49
   * @param harvestOperationCode  string value of the harvest operation code
50
   * @return          the code level value, an int
135
   * @param  harvestOperationCode  string value of the harvest operation code
136
   * @return the code level value, a String, one of the following:
137
   *         "error", "warning", "notice", "info", or "debug"
51 138
   */
52
  public int dbGetHarvestOperationCodeLevel(String harvestOperationCode) {
53
    int codeLevel = 0;
139
  String dbGetHarvestOperationCodeLevel(String harvestOperationCode) {
140
    String harvestOperationCodeLevel;
141
    String fieldName = "HARVEST_OPERATION_CODE_LEVEL";
142
    
143
    harvestOperationCodeLevel = 
144
            dbQueryHarvestOperation(fieldName, harvestOperationCode);
54 145
        
55
    return codeLevel;
146
    return harvestOperationCodeLevel;
56 147
  }
57
    
148
  
58 149

  
59 150
  /**
60 151
   * Inserts a new entry into the HARVEST_LOG table, based on the contents of
61 152
   * this HarvestLog object. Not yet implemented.
62 153
   */
63
  public void dbInsertHarvestLogEntry() {
154
  void dbInsertHarvestLogEntry() {
155
    String insertString;
156
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
157
		Statement stmt;
158

  
159
    insertString = "INSERT INTO HARVEST_LOG " +
160
                   "(HARVEST_LOG_ID, HARVEST_DATE, STATUS, MESSAGE," +
161
                   " HARVEST_OPERATION_CODE, SITE_SCHEDULE_ID) " +
162
                   "values(" +
163
                   harvestLogID + ", " +
164
                   "'" + simpleDateFormat.format(harvestDate) + "', " +
165
                   status + ", " +
166
                   "'" + timestamp + ": " + message + "', " +
167
                   "'" + harvestOperationCode + "', " +
168
                   siteScheduleID +
169
                   ")";
170
                   
171
		try {
172
			stmt = harvester.conn.createStatement();							
173
			stmt.executeUpdate(insertString);
174
			stmt.close();
175
		}
176
    catch(SQLException e) {
177
			System.out.println("SQLException: " + e.getMessage());
178
		}
64 179
  }
180
  
65 181

  
182
  /**
183
   * Retrieves the value of the either the EXPLANATION field or the
184
   * HARVEST_OPERATION_CODE_LEVEL field of the HARVEST_OPERATION table based on
185
   * the value of the HARVEST_OPERATION_CODE field.
186
   *
187
   * @param  fieldName  the field name to match, e.g. "EXPLANATION"
188
   * @param  harvestOperationCode  string value of the harvest operation code
189
   * @return the explanation string or the harvestOperationCodeLevel string
190
   */
191
  String dbQueryHarvestOperation(String fieldName,
192
                                 String harvestOperationCode
193
                                ) {
194
    String explanation = "No explanation available";
195
    String harvestOperationCodeLevel = "debug";
196
    String queryString;
197
    String returnString = "";
198
    ResultSet rs;
199
    Statement stmt;
200
    SQLWarning warn;
201
    
202
    queryString = "SELECT EXPLANATION, HARVEST_OPERATION_CODE_LEVEL " + 
203
                  "FROM HARVEST_OPERATION " +
204
                  "WHERE HARVEST_OPERATION_CODE=" +
205
                  "'" + harvestOperationCode + "'";
206
        
207
    try {                           // Query the HARVEST_OPERATION table
208
      stmt = harvester.conn.createStatement();
209
      rs = stmt.executeQuery(queryString);
210
      warn = rs.getWarnings();
211

  
212
      if (warn != null) {
213
        System.out.println("\n---Warning---\n");
214

  
215
        while (warn != null) {
216
          System.out.println("Message: " + warn.getMessage());
217
          System.out.println("SQLState: " + warn.getSQLState());
218
          System.out.print("Vendor error code: ");
219
          System.out.println(warn.getErrorCode());
220
          System.out.println("");
221
          warn = warn.getNextWarning();
222
        }
223
      }
224
     
225
      while (rs.next()) {
226
        explanation = rs.getString("EXPLANATION");
227
        harvestOperationCodeLevel = rs.getString("HARVEST_OPERATION_CODE_LEVEL");
228
        warn = rs.getWarnings();
229

  
230
        if (warn != null) {
231
          System.out.println("\n---Warning---\n");
232
      
233
          while (warn != null) {
234
            System.out.println("Message: " + warn.getMessage());
235
            System.out.println("SQLState: " + warn.getSQLState());
236
            System.out.print("Vendor error code: ");
237
            System.out.println(warn.getErrorCode());
238
            System.out.println("");
239
            warn = warn.getNextWarning();
240
          }
241
        }
242
      }
243
      
244
      rs.close();
245
      stmt.close();
246
    }
247
    catch (SQLException e) {
248
      System.out.println("SQLException: Database access failed: " + e);
249
    }
250
 
251
    if (fieldName.equals("EXPLANATION")) {
252
      returnString = explanation;
253
    }
254
    else if (fieldName.equals("HARVEST_OPERATION_CODE_LEVEL")) {
255
      returnString = harvestOperationCodeLevel;
256
    }
257
    
258
    return returnString;
259
  }
260

  
261

  
262
  /**
263
   * Prints the contents of this HarvestLog object. Used in generating reports.
264
   */
265
  void printOutput() {
266
    System.out.println("");
267
    System.out.println("harvestLogID:         " + harvestLogID);
268
    System.out.println("harvestDate:          " + harvestDate);
269
    System.out.println("status:               " + status);
270
    System.out.println("message:              " + message);
271
    System.out.println("harvestOperationCode: " + harvestOperationCode);
272
    
273
    if (siteScheduleID != 0) {
274
      harvester.printHarvestSiteSchedule(siteScheduleID);
275
    }
276
    
277
    if (harvestDetailLog != null) {
278
      harvestDetailLog.printOutput();
279
    }
280
  }
281

  
66 282
}

Also available in: Unified diff