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: 2004-05-03 15:59:10 -0700 (Mon, 03 May 2004) $'
8
 * '$Revision: 2155 $'
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
                   
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
    Options options = Harvester.options;
257
    String propertyValue;
258
    String returnString = "";
259
    StringTokenizer stringTokenizer;
260
    
261
    propertyValue = options.getOption(harvestOperationCode);
262
    stringTokenizer = new StringTokenizer(propertyValue, ",");
263
    
264
    explanation = (String) stringTokenizer.nextElement();
265
    harvestOperationCodeLevel = (String) stringTokenizer.nextElement();
266
                                           
267
    if (fieldName.equals("EXPLANATION")) {
268
      returnString = explanation;
269
    }
270
    else if (fieldName.equals("HARVEST_OPERATION_CODE_LEVEL")) {
271
      returnString = harvestOperationCodeLevel;
272
    }
273
    
274
    return returnString;
275
  }
276
  
277

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

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

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

    
322

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

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

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

    
364
}
(4-4/11)