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-04-22 10:35:37 -0700 (Thu, 22 Apr 2004) $'
|
8
|
* '$Revision: 2133 $'
|
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.Properties;
|
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 Harvester harvester; // The parent Harvester object
|
47
|
private int harvestLogID;
|
48
|
private Date harvestDate;
|
49
|
private int status;
|
50
|
private final String marker =
|
51
|
"*****************************************************************************";
|
52
|
private String message;
|
53
|
private String harvestOperationCode;
|
54
|
private int siteScheduleID;
|
55
|
private String explanation;
|
56
|
private String harvestOperationCodeLevel;
|
57
|
private String timestamp;
|
58
|
private HarvestDetailLog harvestDetailLog; // Associated detail log, if any
|
59
|
|
60
|
|
61
|
/**
|
62
|
* Creates a new instance of HarvestLog. This constructor is used when
|
63
|
* creating log entries that do not involve an error on a harvest document.
|
64
|
* (For that type of log entry, use the alternate constructor below.)
|
65
|
*
|
66
|
* @param harvester the parent Harvester object
|
67
|
* @param harvestDate the date of this harvest
|
68
|
* @param status the status of the harvest operation
|
69
|
* @param message the message text of the harvest operation
|
70
|
* @param harvestOperationCode the harvest operation code
|
71
|
* @param siteScheduleID the siteScheduleID for which this operation was
|
72
|
* performed. 0 indicates that the operation did not
|
73
|
* involve a particular harvest site.
|
74
|
*/
|
75
|
public HarvestLog(Harvester harvester,
|
76
|
Date harvestDate,
|
77
|
int status,
|
78
|
String message,
|
79
|
String harvestOperationCode,
|
80
|
int siteScheduleID
|
81
|
) {
|
82
|
Date now = new Date();
|
83
|
timestamp = now.toString();
|
84
|
|
85
|
this.harvester = harvester;
|
86
|
this.harvestLogID = harvester.getHarvestLogID();
|
87
|
this.harvestDate = harvestDate;
|
88
|
this.status = status;
|
89
|
this.message = message;
|
90
|
this.harvestOperationCode = harvestOperationCode;
|
91
|
this.siteScheduleID = siteScheduleID;
|
92
|
|
93
|
harvestOperationCodeLevel =
|
94
|
getHarvestOperationCodeLevel(harvestOperationCode);
|
95
|
explanation = getExplanation(harvestOperationCode);
|
96
|
dbInsertHarvestLogEntry(); // Insert this entry to the HARVEST_LOG table
|
97
|
}
|
98
|
|
99
|
|
100
|
/**
|
101
|
* Creates a new instance of HarvestLog and inserts this entry to the
|
102
|
* HARVEST_LOG table. This version of the constructor also instantiates a
|
103
|
* HarvestDetailLog object and inserts it to the HARVEST_DETAIL_LOG table.
|
104
|
*
|
105
|
* @param harvester the parent Harvester object
|
106
|
* @param harvestDate the date of this harvest
|
107
|
* @param status the status of the harvest operation
|
108
|
* @param message the message text of the harvest operation
|
109
|
* @param harvestOperationCode the harvest operation code
|
110
|
* @param siteScheduleID the siteScheduleID for which this operation was
|
111
|
* performed. 0 indicates that the operation did not
|
112
|
* involve a particular harvest site.
|
113
|
* @param harvestDocument the HarvestDocument involved in this operation
|
114
|
* @param errorMessage the error message generated by this operation
|
115
|
*/
|
116
|
public HarvestLog(Harvester harvester,
|
117
|
Date harvestDate,
|
118
|
int status,
|
119
|
String message,
|
120
|
String harvestOperationCode,
|
121
|
int siteScheduleID,
|
122
|
HarvestDocument harvestDocument,
|
123
|
String errorMessage
|
124
|
) {
|
125
|
Date now = new Date();
|
126
|
timestamp = now.toString();
|
127
|
|
128
|
this.harvester = harvester;
|
129
|
this.harvestLogID = harvester.getHarvestLogID();
|
130
|
this.harvestDate = harvestDate;
|
131
|
this.status = status;
|
132
|
this.message = message;
|
133
|
this.harvestOperationCode = harvestOperationCode;
|
134
|
this.siteScheduleID = siteScheduleID;
|
135
|
this.harvestDetailLog = new HarvestDetailLog(harvester, harvestLogID,
|
136
|
harvestDocument, errorMessage);
|
137
|
harvestOperationCodeLevel =
|
138
|
getHarvestOperationCodeLevel(harvestOperationCode);
|
139
|
explanation = getExplanation(harvestOperationCode);
|
140
|
dbInsertHarvestLogEntry(); // Insert to the HARVEST_LOG table
|
141
|
harvestDetailLog.dbInsertHarvestDetailLogEntry(); // and HARVEST_DETAIL_LOG
|
142
|
}
|
143
|
|
144
|
|
145
|
/**
|
146
|
* Inserts a new entry into the HARVEST_LOG table, based on the contents of
|
147
|
* this HarvestLog object. Not yet implemented.
|
148
|
*/
|
149
|
void dbInsertHarvestLogEntry() {
|
150
|
String dequotedMessage = harvester.dequoteText(message);
|
151
|
String insertString;
|
152
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
|
153
|
Statement stmt;
|
154
|
|
155
|
insertString = "INSERT INTO HARVEST_LOG " +
|
156
|
"(HARVEST_LOG_ID, HARVEST_DATE, STATUS, MESSAGE," +
|
157
|
" HARVEST_OPERATION_CODE, SITE_SCHEDULE_ID) " +
|
158
|
"values(" +
|
159
|
harvestLogID + ", " +
|
160
|
"'" + simpleDateFormat.format(harvestDate) + "', " +
|
161
|
status + ", " +
|
162
|
"'" + timestamp + ": " + dequotedMessage + "', " +
|
163
|
"'" + harvestOperationCode + "', " +
|
164
|
siteScheduleID +
|
165
|
")";
|
166
|
|
167
|
try {
|
168
|
stmt = harvester.conn.createStatement();
|
169
|
stmt.executeUpdate(insertString);
|
170
|
stmt.close();
|
171
|
}
|
172
|
catch(SQLException e) {
|
173
|
System.out.println("SQLException: " + e.getMessage());
|
174
|
}
|
175
|
}
|
176
|
|
177
|
|
178
|
/**
|
179
|
* Maps each code level to an integer value.
|
180
|
*
|
181
|
* @param codeLevel the code level: "error", "warning", "notice",
|
182
|
* "info", or "debug"
|
183
|
* @return codeLevelValue the corresponding code level value
|
184
|
*/
|
185
|
int getCodeLevelValue(String codeLevel) {
|
186
|
int codeLevelValue = 0;
|
187
|
|
188
|
if (codeLevel.equalsIgnoreCase("error")) {
|
189
|
codeLevelValue = 1;
|
190
|
}
|
191
|
else if (codeLevel.equalsIgnoreCase("warning")) {
|
192
|
codeLevelValue = 2;
|
193
|
}
|
194
|
else if (codeLevel.equalsIgnoreCase("notice")) {
|
195
|
codeLevelValue = 3;
|
196
|
}
|
197
|
else if (codeLevel.equalsIgnoreCase("info")) {
|
198
|
codeLevelValue = 4;
|
199
|
}
|
200
|
else if (codeLevel.equalsIgnoreCase("debug")) {
|
201
|
codeLevelValue = 5;
|
202
|
}
|
203
|
|
204
|
return codeLevelValue;
|
205
|
}
|
206
|
|
207
|
|
208
|
/**
|
209
|
* Returns an explanation string based on the value of a
|
210
|
* harvestOperationCode string. The explanation string is a description
|
211
|
* of the harvest operation specified by the harvestOperationCode.
|
212
|
*
|
213
|
* @param harvestOperationCode string value of the harvest operation code
|
214
|
* @return the explanation for this harvest operation, a String
|
215
|
*/
|
216
|
String getExplanation(String harvestOperationCode) {
|
217
|
String explanation;
|
218
|
String fieldName = "EXPLANATION";
|
219
|
|
220
|
explanation = getHarvestOperation(fieldName, harvestOperationCode);
|
221
|
|
222
|
return explanation;
|
223
|
}
|
224
|
|
225
|
|
226
|
/**
|
227
|
* Returns either an explanation string or a harvest operation code level
|
228
|
* string based on the value of a harvest operation code property. The
|
229
|
* explanation and the code level are stored as comma-separated strings in
|
230
|
* the harvester.properties file. For example, the HarvesterStartup
|
231
|
* harvest operation code stores its explanation and code level like so:
|
232
|
*
|
233
|
* HarvesterStartup=Harvester start up,Info
|
234
|
*
|
235
|
* @param fieldName the field name to match, e.g. "EXPLANATION"
|
236
|
* @param harvestOperationCode string value of the harvest operation code
|
237
|
* @return the explanation string or the harvestOperationCodeLevel string
|
238
|
*/
|
239
|
String getHarvestOperation(String fieldName, String harvestOperationCode) {
|
240
|
String explanation = "No explanation available";
|
241
|
String harvestOperationCodeLevel = "debug";
|
242
|
Properties properties = Harvester.properties;
|
243
|
String propertyValue;
|
244
|
String returnString = "";
|
245
|
StringTokenizer stringTokenizer;
|
246
|
|
247
|
propertyValue = properties.getProperty(harvestOperationCode);
|
248
|
stringTokenizer = new StringTokenizer(propertyValue, ",");
|
249
|
|
250
|
explanation = (String) stringTokenizer.nextElement();
|
251
|
harvestOperationCodeLevel = (String) stringTokenizer.nextElement();
|
252
|
|
253
|
if (fieldName.equals("EXPLANATION")) {
|
254
|
returnString = explanation;
|
255
|
}
|
256
|
else if (fieldName.equals("HARVEST_OPERATION_CODE_LEVEL")) {
|
257
|
returnString = harvestOperationCodeLevel;
|
258
|
}
|
259
|
|
260
|
return returnString;
|
261
|
}
|
262
|
|
263
|
|
264
|
/**
|
265
|
* Returns a code level string based on a harvestOperationCode string.
|
266
|
* The code level string is one of a set of possible code levels:
|
267
|
* "error", "warning", "notice", "info", or "debug".
|
268
|
*
|
269
|
* @param harvestOperationCode string value of the harvest operation code
|
270
|
* @return the code level value, a String, one of the following:
|
271
|
* "error", "warning", "notice", "info", or "debug"
|
272
|
*/
|
273
|
String getHarvestOperationCodeLevel(String harvestOperationCode) {
|
274
|
String harvestOperationCodeLevel;
|
275
|
String fieldName = "HARVEST_OPERATION_CODE_LEVEL";
|
276
|
|
277
|
harvestOperationCodeLevel =
|
278
|
getHarvestOperation(fieldName, harvestOperationCode);
|
279
|
|
280
|
return harvestOperationCodeLevel;
|
281
|
}
|
282
|
|
283
|
|
284
|
/**
|
285
|
* Access function for the siteScheduleID field.
|
286
|
*
|
287
|
* @return siteScheduleID, an int. If 0, indicates that this log entry does
|
288
|
* not pertain to a particular site.
|
289
|
*/
|
290
|
int getSiteScheduleID() {
|
291
|
return siteScheduleID;
|
292
|
}
|
293
|
|
294
|
|
295
|
/**
|
296
|
* Determines whether this log entry had an error status.
|
297
|
*
|
298
|
* @return isError true if this log entry had an error status, else false
|
299
|
*/
|
300
|
boolean isErrorEntry () {
|
301
|
boolean isError;
|
302
|
|
303
|
isError = (status != 0);
|
304
|
|
305
|
return isError;
|
306
|
}
|
307
|
|
308
|
|
309
|
/**
|
310
|
* Prints the contents of this HarvestLog object. Used in generating reports.
|
311
|
*
|
312
|
* @param out the PrintStream to write to
|
313
|
* @param maxLevel the maximum code level to output. If this log entry has a
|
314
|
* higher code level than the maxLevel, no output
|
315
|
* is issued. For example, if the maxLevel is "error"
|
316
|
* (level 1), then anything lower ("warning", "notice", etc.)
|
317
|
* will not generate any output.
|
318
|
*/
|
319
|
void printOutput(PrintStream out, String maxLevel) {
|
320
|
int codeLevelValue = getCodeLevelValue(harvestOperationCodeLevel);
|
321
|
int maxLevelValue = getCodeLevelValue(maxLevel);
|
322
|
|
323
|
if (codeLevelValue <= maxLevelValue) {
|
324
|
out.println("");
|
325
|
out.println(marker);
|
326
|
out.println("*");
|
327
|
out.println("* harvestLogID: " + harvestLogID);
|
328
|
out.println("* harvestDate: " + harvestDate);
|
329
|
out.println("* status: " + status);
|
330
|
out.println("* message: " + message);
|
331
|
out.println("* harvestOperationCode: " + harvestOperationCode);
|
332
|
out.println("* description: " + explanation);
|
333
|
|
334
|
if (harvestOperationCode.equals("GetDocListSuccess") ||
|
335
|
harvestOperationCode.equals("GetDocListError")) {
|
336
|
if (siteScheduleID != 0) {
|
337
|
harvester.printHarvestSiteSchedule(out, siteScheduleID);
|
338
|
}
|
339
|
}
|
340
|
|
341
|
if (harvestDetailLog != null) {
|
342
|
harvestDetailLog.printOutput(out);
|
343
|
}
|
344
|
|
345
|
out.println("*");
|
346
|
out.println(marker);
|
347
|
}
|
348
|
}
|
349
|
|
350
|
}
|