Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 University of New Mexico and the 
4
 *                  Regents of the University of California
5
 *
6
 *   '$Author: costa $'
7
 *     '$Date: 2005-01-25 15:14:26 -0800 (Tue, 25 Jan 2005) $'
8
 * '$Revision: 2386 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

    
25
package edu.ucsb.nceas.metacat.harvesterClient;
26

    
27
import edu.ucsb.nceas.utilities.Options;
28
import java.io.PrintStream;
29
import java.sql.Connection;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.sql.SQLWarning;
33
import java.sql.Statement;
34
import java.text.SimpleDateFormat;
35
import java.util.Date;
36
import java.util.StringTokenizer;
37

    
38

    
39
/**
40
 * Manages log entries to be inserted to the HARVEST_LOG table.
41
 *
42
 * @author  costa
43
 */
44
public class HarvestLog {
45

    
46
  private Connection conn;    
47
  private Harvester harvester;              // The parent Harvester object
48
  private int harvestLogID;
49
  private Date harvestDate;
50
  private int status;
51
  private final String marker =
52
"*****************************************************************************";
53
  private String message;
54
  private String harvestOperationCode;
55
  private int siteScheduleID;
56
  private String explanation;
57
  private String harvestOperationCodeLevel;
58
  private String timestamp;
59
  private HarvestDetailLog harvestDetailLog;  // Associated detail log, if any
60
    
61

    
62
  /** 
63
   * Creates a new instance of HarvestLog. This constructor is used when
64
   * creating log entries that do not involve an error on a harvest document.
65
   * (For that type of log entry, use the alternate constructor below.)
66
   *
67
   * @param  harvester       the parent Harvester object
68
   * @param  conn            the database connection
69
   * @param  harvestLogID    the primary key in the HARVEST_LOG table
70
   * @param  harvestDate     the date of this harvest
71
   * @param  status          the status of the harvest operation
72
   * @param  message         the message text of the harvest operation
73
   * @param  harvestOperationCode  the harvest operation code
74
   * @param  siteScheduleID  the siteScheduleID for which this operation was
75
   *                         performed. 0 indicates that the operation did not
76
   *                         involve a particular harvest site.
77
   */
78
  public HarvestLog(Harvester  harvester,
79
                    Connection conn,
80
                    int        harvestLogID,
81
                    Date       harvestDate,
82
                    int        status,
83
                    String     message, 
84
                    String     harvestOperationCode,
85
                    int        siteScheduleID
86
                   ) {
87
    Date now = new Date();
88
    timestamp = now.toString();
89

    
90
    this.harvester = harvester;
91
    this.conn = conn;
92
    this.harvestLogID = harvestLogID;
93
    this.harvestDate = harvestDate;
94
    this.status = status;
95
    this.message = message;
96
    this.harvestOperationCode = harvestOperationCode;
97
    this.siteScheduleID = siteScheduleID;
98
    
99
    harvestOperationCodeLevel = 
100
            getHarvestOperationCodeLevel(harvestOperationCode);
101
    explanation = getExplanation(harvestOperationCode);
102
    dbInsertHarvestLogEntry();   // Insert this entry to the HARVEST_LOG table
103
  }
104
    
105

    
106
  /** 
107
   * Creates a new instance of HarvestLog and inserts this entry to the
108
   * HARVEST_LOG table. This version of the constructor also instantiates a 
109
   * HarvestDetailLog object and inserts it to the HARVEST_DETAIL_LOG table.
110
   *
111
   * @param  harvester       the parent Harvester object
112
   * @param  conn            the database connection
113
   * @param  harvestLogID    the primary key in the HARVEST_LOG table
114
   * @param  detailLogID     the primary key in the HARVEST_DETAIL_LOG table
115
   * @param  harvestDate     the date of this harvest
116
   * @param  status          the status of the harvest operation
117
   * @param  message         the message text of the harvest operation
118
   * @param  harvestOperationCode  the harvest operation code
119
   * @param  siteScheduleID  the siteScheduleID for which this operation was
120
   *                         performed. 0 indicates that the operation did not
121
   *                         involve a particular harvest site.
122
   * @param  harvestDocument the HarvestDocument involved in this operation
123
   * @param  errorMessage    the error message generated by this operation
124
   */
125
  public HarvestLog(Harvester  harvester,
126
                    Connection conn,
127
                    int        harvestLogID,
128
                    int        detailLogID,
129
                    Date       harvestDate,
130
                    int        status,
131
                    String     message, 
132
                    String     harvestOperationCode,
133
                    int        siteScheduleID,
134
                    HarvestDocument harvestDocument,
135
                    String     errorMessage
136
                   ) {
137
    Date now = new Date();
138
    timestamp = now.toString();
139

    
140
    this.harvester = harvester;
141
    this.conn = conn;
142
    this.harvestLogID = harvestLogID;
143
    this.harvestDate = harvestDate;
144
    this.status = status;
145
    this.message = message;
146
    this.harvestOperationCode = harvestOperationCode;
147
    this.siteScheduleID = siteScheduleID;
148
    this.harvestDetailLog = new HarvestDetailLog(harvester, conn, detailLogID,
149
                                                 harvestLogID, harvestDocument,
150
                                                 errorMessage);
151
    harvestOperationCodeLevel = 
152
            getHarvestOperationCodeLevel(harvestOperationCode);
153
    explanation = getExplanation(harvestOperationCode);
154
    dbInsertHarvestLogEntry();               // Insert to the HARVEST_LOG table
155
    harvestDetailLog.dbInsertHarvestDetailLogEntry(); //  HARVEST_DETAIL_LOG
156
  }
157
    
158

    
159
  /**
160
   * Inserts a new entry into the HARVEST_LOG table, based on the contents of
161
   * this HarvestLog object. Not yet implemented.
162
   */
163
  void dbInsertHarvestLogEntry() {
164
    String dequotedMessage = harvester.dequoteText(message);
165
    String insertString;
166
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
167
    Statement stmt;
168

    
169
    insertString = "INSERT INTO HARVEST_LOG " +
170
                   "(HARVEST_LOG_ID, HARVEST_DATE, STATUS, MESSAGE," +
171
                   " HARVEST_OPERATION_CODE, SITE_SCHEDULE_ID) " +
172
                   "values(" +
173
                   harvestLogID + ", " +
174
                   "'" + simpleDateFormat.format(harvestDate) + "', " +
175
                   status + ", " +
176
                   "'" + timestamp + ": " + dequotedMessage + "', " +
177
                   "'" + harvestOperationCode + "', " +
178
                   siteScheduleID +
179
                   ")";
180
    try {
181
      stmt = conn.createStatement();
182
      stmt.executeUpdate(insertString);
183
      stmt.close();
184
    }
185
    catch(SQLException e) {
186
      System.out.println("SQLException: " + e.getMessage());
187
    }
188
  }
189
  
190

    
191
  /**
192
   * Maps each code level to an integer value.
193
   * 
194
   * @param codeLevel        the code level: "error", "warning", "notice",
195
   *                         "info", or "debug"
196
   * @return codeLevelValue  the corresponding code level value
197
   */
198
  public int getCodeLevelValue(String codeLevel) {
199
    int codeLevelValue = 0;
200
    
201
    if (codeLevel.equalsIgnoreCase("error")) {
202
      codeLevelValue = 1;
203
    }
204
    else if (codeLevel.equalsIgnoreCase("warning")) {
205
      codeLevelValue = 2;
206
    }
207
    else if (codeLevel.equalsIgnoreCase("notice")) {
208
      codeLevelValue = 3;
209
    }
210
    else if (codeLevel.equalsIgnoreCase("info")) {
211
      codeLevelValue = 4;
212
    }
213
    else if (codeLevel.equalsIgnoreCase("debug")) {
214
      codeLevelValue = 5;
215
    }
216
    
217
    return codeLevelValue;
218
  }
219
  
220

    
221
  /**
222
   * Returns an explanation string based on the value of a
223
   * harvestOperationCode string. The explanation string is a description
224
   * of the harvest operation specified by the harvestOperationCode.
225
   * 
226
   * @param  harvestOperationCode  string value of the harvest operation code
227
   * @return the explanation for this harvest operation, a String
228
   */
229
  public String getExplanation(String harvestOperationCode) {
230
    String explanation;
231
    String fieldName = "EXPLANATION";
232
    
233
    explanation = getHarvestOperation(fieldName, harvestOperationCode);
234
        
235
    return explanation;
236
  }
237
  
238

    
239
  /**
240
   * Returns either an explanation string or a harvest operation code level 
241
   * string based on the value of a harvest operation code property. The
242
   * explanation and the code level are stored as comma-separated strings in
243
   * the metacat.properties file. For example, the HarvesterStartup
244
   * harvest operation code stores its explanation and code level like so:
245
   * 
246
   *            HarvesterStartup=Harvester start up,Info
247
   *
248
   * @param  fieldName  the field name to match, e.g. "EXPLANATION"
249
   * @param  harvestOperationCode  string value of the harvest operation code
250
   * @return the explanation string or the harvestOperationCodeLevel string
251
   */
252
  String getHarvestOperation(String fieldName, String harvestOperationCode) {
253
    String explanation = "No explanation available";
254
    String harvestOperationCodeLevel = "debug";
255
    Options options = Harvester.options;
256
    String propertyValue;
257
    String returnString = "";
258
    StringTokenizer stringTokenizer;
259
    
260
    propertyValue = options.getOption(harvestOperationCode);
261
    stringTokenizer = new StringTokenizer(propertyValue, ",");
262
    
263
    explanation = (String) stringTokenizer.nextElement();
264
    harvestOperationCodeLevel = (String) stringTokenizer.nextElement();
265
                                           
266
    if (fieldName.equals("EXPLANATION")) {
267
      returnString = explanation;
268
    }
269
    else if (fieldName.equals("HARVEST_OPERATION_CODE_LEVEL")) {
270
      returnString = harvestOperationCodeLevel;
271
    }
272
    
273
    return returnString;
274
  }
275
  
276

    
277
  /**
278
   * Returns a code level string based on a harvestOperationCode string.
279
   * The code level string is one of a set of possible code levels:
280
   * "error", "warning", "notice", "info", or "debug".
281
   * 
282
   * @param  harvestOperationCode  string value of the harvest operation code
283
   * @return the code level value, a String, one of the following:
284
   *         "error", "warning", "notice", "info", or "debug"
285
   */
286
  public String getHarvestOperationCodeLevel(String harvestOperationCode) {
287
    String harvestOperationCodeLevel;
288
    String fieldName = "HARVEST_OPERATION_CODE_LEVEL";
289
    
290
    harvestOperationCodeLevel = 
291
            getHarvestOperation(fieldName, harvestOperationCode);
292
        
293
    return harvestOperationCodeLevel;
294
  }
295
  
296

    
297
  /**
298
   * Access function for the siteScheduleID field.
299
   * 
300
   * @return  siteScheduleID, an int. If 0, indicates that this log entry does
301
   *          not pertain to a particular site.
302
   */
303
  int getSiteScheduleID() {
304
    return siteScheduleID;
305
  }
306
  
307

    
308
  /**
309
   * Determines whether this log entry had an error status.
310
   * 
311
   * @return  isError  true if this log entry had an error status, else false 
312
   */
313
  boolean isErrorEntry () {
314
    boolean isError;
315
    
316
    isError = (status != 0);
317
    
318
    return isError;
319
  }
320

    
321

    
322
  /**
323
   * Prints the contents of this HarvestLog object. Used in generating reports.
324
   * 
325
   * @param out        the PrintStream to write to
326
   * @param maxLevel  the maximum code level to output. If this log entry has a
327
   *                  higher code level than the maxLevel, no output
328
   *                  is issued. For example, if the maxLevel is "error"
329
   *                  (level 1), then anything lower ("warning", "notice", etc.)
330
   *                  will not generate any output.
331
   */
332
  public void printOutput(PrintStream out, String maxLevel) {
333
    int codeLevelValue = getCodeLevelValue(harvestOperationCodeLevel);
334
    int maxLevelValue = getCodeLevelValue(maxLevel);
335
    
336
    if (codeLevelValue <= maxLevelValue) {    
337
      out.println("");
338
      out.println(marker);
339
      out.println("*");
340
      out.println("* harvestLogID:         " + harvestLogID);
341
      out.println("* harvestDate:          " + harvestDate);
342
      out.println("* status:               " + status);
343
      out.println("* message:              " + message);
344
      out.println("* harvestOperationCode: " + harvestOperationCode);
345
      out.println("* description:          " + explanation);
346

    
347
      if (harvestOperationCode.equals("GetHarvestListSuccess") ||
348
        harvestOperationCode.equals("GetHarvestListError")) {
349
        if (siteScheduleID != 0) {
350
          harvester.printHarvestSiteSchedule(out, siteScheduleID);
351
        }
352
      }
353
    
354
      if (harvestDetailLog != null) {
355
        harvestDetailLog.printOutput(out);
356
      }
357

    
358
      out.println("*");
359
      out.println(marker);
360
    }
361
  }
362

    
363
}
(4-4/11)