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 2086 costa
import java.io.PrintStream;
28 2031 costa
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 2108 costa
import java.util.StringTokenizer;
36 2031 costa
37 4125 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
38
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39 2031 costa
40 2022 costa
/**
41
 * Manages log entries to be inserted to the HARVEST_LOG table.
42
 *
43
 * @author  costa
44
 */
45
public class HarvestLog {
46 2139 costa
47
  private Connection conn;
48 2031 costa
  private Harvester harvester;              // The parent Harvester object
49
  private int harvestLogID;
50
  private Date harvestDate;
51
  private int status;
52 2036 costa
  private final String marker =
53
"*****************************************************************************";
54 2031 costa
  private String message;
55 2022 costa
  private String harvestOperationCode;
56
  private int siteScheduleID;
57 2105 costa
  private String explanation;
58
  private String harvestOperationCodeLevel;
59 2031 costa
  private String timestamp;
60
  private HarvestDetailLog harvestDetailLog;  // Associated detail log, if any
61 2022 costa
62 2031 costa
63 2022 costa
  /**
64 2031 costa
   * 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 2139 costa
   * @param  conn            the database connection
70
   * @param  harvestLogID    the primary key in the HARVEST_LOG table
71 2031 costa
   * @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 2139 costa
                    Connection conn,
81
                    int        harvestLogID,
82 2031 costa
                    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 2139 costa
    this.conn = conn;
93
    this.harvestLogID = harvestLogID;
94 2031 costa
    this.harvestDate = harvestDate;
95
    this.status = status;
96
    this.message = message;
97
    this.harvestOperationCode = harvestOperationCode;
98
    this.siteScheduleID = siteScheduleID;
99
100 2105 costa
    harvestOperationCodeLevel =
101 2108 costa
            getHarvestOperationCodeLevel(harvestOperationCode);
102
    explanation = getExplanation(harvestOperationCode);
103 2031 costa
    dbInsertHarvestLogEntry();   // Insert this entry to the HARVEST_LOG table
104 2022 costa
  }
105
106
107 2031 costa
  /**
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 2139 costa
   * @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 2031 costa
   * @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 2105 costa
   * @param  harvestDocument the HarvestDocument involved in this operation
124
   * @param  errorMessage    the error message generated by this operation
125 2031 costa
   */
126
  public HarvestLog(Harvester  harvester,
127 2139 costa
                    Connection conn,
128
                    int        harvestLogID,
129
                    int        detailLogID,
130 2031 costa
                    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 2139 costa
    this.conn = conn;
143
    this.harvestLogID = harvestLogID;
144 2031 costa
    this.harvestDate = harvestDate;
145
    this.status = status;
146
    this.message = message;
147
    this.harvestOperationCode = harvestOperationCode;
148
    this.siteScheduleID = siteScheduleID;
149 2139 costa
    this.harvestDetailLog = new HarvestDetailLog(harvester, conn, detailLogID,
150
                                                 harvestLogID, harvestDocument,
151
                                                 errorMessage);
152 2105 costa
    harvestOperationCodeLevel =
153 2108 costa
            getHarvestOperationCodeLevel(harvestOperationCode);
154
    explanation = getExplanation(harvestOperationCode);
155 2031 costa
    dbInsertHarvestLogEntry();               // Insert to the HARVEST_LOG table
156 2139 costa
    harvestDetailLog.dbInsertHarvestDetailLogEntry(); //  HARVEST_DETAIL_LOG
157 2031 costa
  }
158
159
160 2022 costa
  /**
161
   * Inserts a new entry into the HARVEST_LOG table, based on the contents of
162
   * this HarvestLog object. Not yet implemented.
163
   */
164 2031 costa
  void dbInsertHarvestLogEntry() {
165 2036 costa
    String dequotedMessage = harvester.dequoteText(message);
166 2031 costa
    String insertString;
167
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
168 2386 costa
    Statement stmt;
169 2031 costa
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 2036 costa
                   "'" + timestamp + ": " + dequotedMessage + "', " +
178 2031 costa
                   "'" + harvestOperationCode + "', " +
179
                   siteScheduleID +
180
                   ")";
181 2386 costa
    try {
182
      stmt = conn.createStatement();
183
      stmt.executeUpdate(insertString);
184
      stmt.close();
185
    }
186 2031 costa
    catch(SQLException e) {
187 2386 costa
      System.out.println("SQLException: " + e.getMessage());
188
    }
189 2022 costa
  }
190 2031 costa
191 2022 costa
192 2031 costa
  /**
193 2108 costa
   * 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 2139 costa
  public int getCodeLevelValue(String codeLevel) {
200 2108 costa
    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 2133 costa
   * 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 2108 costa
   *
227
   * @param  harvestOperationCode  string value of the harvest operation code
228
   * @return the explanation for this harvest operation, a String
229
   */
230 2139 costa
  public String getExplanation(String harvestOperationCode) {
231 2108 costa
    String explanation;
232
    String fieldName = "EXPLANATION";
233
234
    explanation = getHarvestOperation(fieldName, harvestOperationCode);
235
236
    return explanation;
237
  }
238
239
240
  /**
241 2133 costa
   * 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 2155 costa
   * the metacat.properties file. For example, the HarvesterStartup
245 2133 costa
   * harvest operation code stores its explanation and code level like so:
246
   *
247
   *            HarvesterStartup=Harvester start up,Info
248 2031 costa
   *
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 2108 costa
  String getHarvestOperation(String fieldName, String harvestOperationCode) {
254 2031 costa
    String explanation = "No explanation available";
255
    String harvestOperationCodeLevel = "debug";
256 4125 daigle
    String propertyValue = null;
257 2031 costa
    String returnString = "";
258 2108 costa
    StringTokenizer stringTokenizer;
259 2031 costa
260 4125 daigle
    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 2108 costa
268
    explanation = (String) stringTokenizer.nextElement();
269
    harvestOperationCodeLevel = (String) stringTokenizer.nextElement();
270
271 2031 costa
    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 2105 costa
281 2031 costa
282 2105 costa
  /**
283 2133 costa
   * 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 2105 costa
   *
287 2108 costa
   * @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 2105 costa
   */
291 2139 costa
  public String getHarvestOperationCodeLevel(String harvestOperationCode) {
292 2108 costa
    String harvestOperationCodeLevel;
293
    String fieldName = "HARVEST_OPERATION_CODE_LEVEL";
294 2105 costa
295 2108 costa
    harvestOperationCodeLevel =
296
            getHarvestOperation(fieldName, harvestOperationCode);
297
298
    return harvestOperationCodeLevel;
299 2105 costa
  }
300
301 2031 costa
302
  /**
303 2105 costa
   * 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 2031 costa
   * Prints the contents of this HarvestLog object. Used in generating reports.
329 2086 costa
   *
330 2105 costa
   * @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 2031 costa
   */
337 2139 costa
  public void printOutput(PrintStream out, String maxLevel) {
338 2105 costa
    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 2036 costa
352 2139 costa
      if (harvestOperationCode.equals("GetHarvestListSuccess") ||
353
        harvestOperationCode.equals("GetHarvestListError")) {
354 2105 costa
        if (siteScheduleID != 0) {
355
          harvester.printHarvestSiteSchedule(out, siteScheduleID);
356
        }
357 2036 costa
      }
358 2031 costa
359 2105 costa
      if (harvestDetailLog != null) {
360
        harvestDetailLog.printOutput(out);
361
      }
362
363
      out.println("*");
364
      out.println(marker);
365 2031 costa
    }
366
  }
367
368 2022 costa
}