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
		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 2036 costa
                   "'" + timestamp + ": " + dequotedMessage + "', " +
177 2031 costa
                   "'" + harvestOperationCode + "', " +
178
                   siteScheduleID +
179
                   ")";
180
181
		try {
182 2139 costa
			stmt = conn.createStatement();
183 2031 costa
			stmt.executeUpdate(insertString);
184
			stmt.close();
185
		}
186
    catch(SQLException e) {
187
			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 2155 costa
    Options options = Harvester.options;
257 2108 costa
    String propertyValue;
258 2031 costa
    String returnString = "";
259 2108 costa
    StringTokenizer stringTokenizer;
260 2031 costa
261 2155 costa
    propertyValue = options.getOption(harvestOperationCode);
262 2108 costa
    stringTokenizer = new StringTokenizer(propertyValue, ",");
263
264
    explanation = (String) stringTokenizer.nextElement();
265
    harvestOperationCodeLevel = (String) stringTokenizer.nextElement();
266
267 2031 costa
    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 2105 costa
277 2031 costa
278 2105 costa
  /**
279 2133 costa
   * 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 2105 costa
   *
283 2108 costa
   * @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 2105 costa
   */
287 2139 costa
  public String getHarvestOperationCodeLevel(String harvestOperationCode) {
288 2108 costa
    String harvestOperationCodeLevel;
289
    String fieldName = "HARVEST_OPERATION_CODE_LEVEL";
290 2105 costa
291 2108 costa
    harvestOperationCodeLevel =
292
            getHarvestOperation(fieldName, harvestOperationCode);
293
294
    return harvestOperationCodeLevel;
295 2105 costa
  }
296
297 2031 costa
298
  /**
299 2105 costa
   * 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 2031 costa
   * Prints the contents of this HarvestLog object. Used in generating reports.
325 2086 costa
   *
326 2105 costa
   * @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 2031 costa
   */
333 2139 costa
  public void printOutput(PrintStream out, String maxLevel) {
334 2105 costa
    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 2036 costa
348 2139 costa
      if (harvestOperationCode.equals("GetHarvestListSuccess") ||
349
        harvestOperationCode.equals("GetHarvestListError")) {
350 2105 costa
        if (siteScheduleID != 0) {
351
          harvester.printHarvestSiteSchedule(out, siteScheduleID);
352
        }
353 2036 costa
      }
354 2031 costa
355 2105 costa
      if (harvestDetailLog != null) {
356
        harvestDetailLog.printOutput(out);
357
      }
358
359
      out.println("*");
360
      out.println(marker);
361 2031 costa
    }
362
  }
363
364 2022 costa
}