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: daigle $'
7
 *     '$Date: 2009-08-24 14:34:17 -0700 (Mon, 24 Aug 2009) $'
8
 * '$Revision: 5030 $'
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 java.io.PrintStream;
28
import java.sql.Connection;
29
import java.sql.ResultSet;
30
import java.sql.SQLException;
31
import java.sql.SQLWarning;
32
import java.sql.Statement;
33
import java.text.SimpleDateFormat;
34
import java.util.Date;
35
import java.util.StringTokenizer;
36

    
37
import edu.ucsb.nceas.metacat.properties.PropertyService;
38
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39

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

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

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

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

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

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

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

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

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

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

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

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

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

    
313
  /**
314
   * Determines whether this log entry had an error status.
315
   * 
316
   * @return  isError  true if this log entry had an error status, else false 
317
   */
318
  boolean isErrorEntry () {
319
    boolean isError;
320
    
321
    isError = (status != 0);
322
    
323
    return isError;
324
  }
325

    
326

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

    
352
      if (harvestOperationCode.equals("harvester.GetHarvestListSuccess") ||
353
        harvestOperationCode.equals("harvester.GetHarvestListError")) {
354
        if (siteScheduleID != 0) {
355
          harvester.printHarvestSiteSchedule(out, siteScheduleID);
356
        }
357
      }
358
    
359
      if (harvestDetailLog != null) {
360
        harvestDetailLog.printOutput(out);
361
      }
362

    
363
      out.println("*");
364
      out.println(marker);
365
    }
366
  }
367

    
368
}
(4-4/11)