Project

General

Profile

1 2094 jones
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 University of New Mexico and the
4
 *                  Regents of the University of California
5 2022 costa
 *
6 2094 jones
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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 2022 costa
 */
24
25
package edu.ucsb.nceas.metacat.harvesterClient;
26
27 2155 costa
import edu.ucsb.nceas.utilities.Options;
28 2086 costa
import java.io.PrintStream;
29 2031 costa
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 2108 costa
import java.util.StringTokenizer;
37 2031 costa
38
39 2022 costa
/**
40
 * Manages log entries to be inserted to the HARVEST_LOG table.
41
 *
42
 * @author  costa
43
 */
44
public class HarvestLog {
45 2139 costa
46
  private Connection conn;
47 2031 costa
  private Harvester harvester;              // The parent Harvester object
48
  private int harvestLogID;
49
  private Date harvestDate;
50
  private int status;
51 2036 costa
  private final String marker =
52
"*****************************************************************************";
53 2031 costa
  private String message;
54 2022 costa
  private String harvestOperationCode;
55
  private int siteScheduleID;
56 2105 costa
  private String explanation;
57
  private String harvestOperationCodeLevel;
58 2031 costa
  private String timestamp;
59
  private HarvestDetailLog harvestDetailLog;  // Associated detail log, if any
60 2022 costa
61 2031 costa
62 2022 costa
  /**
63 2031 costa
   * 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 2139 costa
   * @param  conn            the database connection
69
   * @param  harvestLogID    the primary key in the HARVEST_LOG table
70 2031 costa
   * @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 2139 costa
                    Connection conn,
80
                    int        harvestLogID,
81 2031 costa
                    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 2139 costa
    this.conn = conn;
92
    this.harvestLogID = harvestLogID;
93 2031 costa
    this.harvestDate = harvestDate;
94
    this.status = status;
95
    this.message = message;
96
    this.harvestOperationCode = harvestOperationCode;
97
    this.siteScheduleID = siteScheduleID;
98
99 2105 costa
    harvestOperationCodeLevel =
100 2108 costa
            getHarvestOperationCodeLevel(harvestOperationCode);
101
    explanation = getExplanation(harvestOperationCode);
102 2031 costa
    dbInsertHarvestLogEntry();   // Insert this entry to the HARVEST_LOG table
103 2022 costa
  }
104
105
106 2031 costa
  /**
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 2139 costa
   * @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 2031 costa
   * @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 2105 costa
   * @param  harvestDocument the HarvestDocument involved in this operation
123
   * @param  errorMessage    the error message generated by this operation
124 2031 costa
   */
125
  public HarvestLog(Harvester  harvester,
126 2139 costa
                    Connection conn,
127
                    int        harvestLogID,
128
                    int        detailLogID,
129 2031 costa
                    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 2139 costa
    this.conn = conn;
142
    this.harvestLogID = harvestLogID;
143 2031 costa
    this.harvestDate = harvestDate;
144
    this.status = status;
145
    this.message = message;
146
    this.harvestOperationCode = harvestOperationCode;
147
    this.siteScheduleID = siteScheduleID;
148 2139 costa
    this.harvestDetailLog = new HarvestDetailLog(harvester, conn, detailLogID,
149
                                                 harvestLogID, harvestDocument,
150
                                                 errorMessage);
151 2105 costa
    harvestOperationCodeLevel =
152 2108 costa
            getHarvestOperationCodeLevel(harvestOperationCode);
153
    explanation = getExplanation(harvestOperationCode);
154 2031 costa
    dbInsertHarvestLogEntry();               // Insert to the HARVEST_LOG table
155 2139 costa
    harvestDetailLog.dbInsertHarvestDetailLogEntry(); //  HARVEST_DETAIL_LOG
156 2031 costa
  }
157
158
159 2022 costa
  /**
160
   * Inserts a new entry into the HARVEST_LOG table, based on the contents of
161
   * this HarvestLog object. Not yet implemented.
162
   */
163 2031 costa
  void dbInsertHarvestLogEntry() {
164 2036 costa
    String dequotedMessage = harvester.dequoteText(message);
165 2031 costa
    String insertString;
166
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
167 2386 costa
    Statement stmt;
168 2031 costa
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 2036 costa
                   "'" + timestamp + ": " + dequotedMessage + "', " +
177 2031 costa
                   "'" + harvestOperationCode + "', " +
178
                   siteScheduleID +
179
                   ")";
180 2386 costa
    try {
181
      stmt = conn.createStatement();
182
      stmt.executeUpdate(insertString);
183
      stmt.close();
184
    }
185 2031 costa
    catch(SQLException e) {
186 2386 costa
      System.out.println("SQLException: " + e.getMessage());
187
    }
188 2022 costa
  }
189 2031 costa
190 2022 costa
191 2031 costa
  /**
192 2108 costa
   * 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 2139 costa
  public int getCodeLevelValue(String codeLevel) {
199 2108 costa
    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 2133 costa
   * 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 2108 costa
   *
226
   * @param  harvestOperationCode  string value of the harvest operation code
227
   * @return the explanation for this harvest operation, a String
228
   */
229 2139 costa
  public String getExplanation(String harvestOperationCode) {
230 2108 costa
    String explanation;
231
    String fieldName = "EXPLANATION";
232
233
    explanation = getHarvestOperation(fieldName, harvestOperationCode);
234
235
    return explanation;
236
  }
237
238
239
  /**
240 2133 costa
   * 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 2155 costa
   * the metacat.properties file. For example, the HarvesterStartup
244 2133 costa
   * harvest operation code stores its explanation and code level like so:
245
   *
246
   *            HarvesterStartup=Harvester start up,Info
247 2031 costa
   *
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 2108 costa
  String getHarvestOperation(String fieldName, String harvestOperationCode) {
253 2031 costa
    String explanation = "No explanation available";
254
    String harvestOperationCodeLevel = "debug";
255 2155 costa
    Options options = Harvester.options;
256 2108 costa
    String propertyValue;
257 2031 costa
    String returnString = "";
258 2108 costa
    StringTokenizer stringTokenizer;
259 2031 costa
260 2155 costa
    propertyValue = options.getOption(harvestOperationCode);
261 2108 costa
    stringTokenizer = new StringTokenizer(propertyValue, ",");
262
263
    explanation = (String) stringTokenizer.nextElement();
264
    harvestOperationCodeLevel = (String) stringTokenizer.nextElement();
265
266 2031 costa
    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 2105 costa
276 2031 costa
277 2105 costa
  /**
278 2133 costa
   * 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 2105 costa
   *
282 2108 costa
   * @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 2105 costa
   */
286 2139 costa
  public String getHarvestOperationCodeLevel(String harvestOperationCode) {
287 2108 costa
    String harvestOperationCodeLevel;
288
    String fieldName = "HARVEST_OPERATION_CODE_LEVEL";
289 2105 costa
290 2108 costa
    harvestOperationCodeLevel =
291
            getHarvestOperation(fieldName, harvestOperationCode);
292
293
    return harvestOperationCodeLevel;
294 2105 costa
  }
295
296 2031 costa
297
  /**
298 2105 costa
   * 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 2031 costa
   * Prints the contents of this HarvestLog object. Used in generating reports.
324 2086 costa
   *
325 2105 costa
   * @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 2031 costa
   */
332 2139 costa
  public void printOutput(PrintStream out, String maxLevel) {
333 2105 costa
    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 2036 costa
347 2139 costa
      if (harvestOperationCode.equals("GetHarvestListSuccess") ||
348
        harvestOperationCode.equals("GetHarvestListError")) {
349 2105 costa
        if (siteScheduleID != 0) {
350
          harvester.printHarvestSiteSchedule(out, siteScheduleID);
351
        }
352 2036 costa
      }
353 2031 costa
354 2105 costa
      if (harvestDetailLog != null) {
355
        harvestDetailLog.printOutput(out);
356
      }
357
358
      out.println("*");
359
      out.println(marker);
360 2031 costa
    }
361
  }
362
363 2022 costa
}