1
|
/*
|
2
|
* HarvestLog.java
|
3
|
*
|
4
|
* Created on January 14, 2004, 4:55 PM
|
5
|
*/
|
6
|
|
7
|
package edu.ucsb.nceas.metacat.harvesterClient;
|
8
|
|
9
|
import java.sql.Connection;
|
10
|
import java.sql.ResultSet;
|
11
|
import java.sql.SQLException;
|
12
|
import java.sql.SQLWarning;
|
13
|
import java.sql.Statement;
|
14
|
import java.text.SimpleDateFormat;
|
15
|
import java.util.Date;
|
16
|
|
17
|
|
18
|
/**
|
19
|
* Manages log entries to be inserted to the HARVEST_LOG table.
|
20
|
*
|
21
|
* @author costa
|
22
|
*/
|
23
|
public class HarvestLog {
|
24
|
|
25
|
private Harvester harvester; // The parent Harvester object
|
26
|
private int harvestLogID;
|
27
|
private Date harvestDate;
|
28
|
private int status;
|
29
|
private String message;
|
30
|
private String harvestOperationCode;
|
31
|
private int siteScheduleID;
|
32
|
private String timestamp;
|
33
|
private HarvestDetailLog harvestDetailLog; // Associated detail log, if any
|
34
|
|
35
|
|
36
|
/**
|
37
|
* Creates a new instance of HarvestLog. This constructor is used when
|
38
|
* creating log entries that do not involve an error on a harvest document.
|
39
|
* (For that type of log entry, use the alternate constructor below.)
|
40
|
*
|
41
|
* @param harvester the parent Harvester object
|
42
|
* @param harvestDate the date of this harvest
|
43
|
* @param status the status of the harvest operation
|
44
|
* @param message the message text of the harvest operation
|
45
|
* @param harvestOperationCode the harvest operation code
|
46
|
* @param siteScheduleID the siteScheduleID for which this operation was
|
47
|
* performed. 0 indicates that the operation did not
|
48
|
* involve a particular harvest site.
|
49
|
*/
|
50
|
public HarvestLog(Harvester harvester,
|
51
|
Date harvestDate,
|
52
|
int status,
|
53
|
String message,
|
54
|
String harvestOperationCode,
|
55
|
int siteScheduleID
|
56
|
) {
|
57
|
Date now = new Date();
|
58
|
timestamp = now.toString();
|
59
|
|
60
|
this.harvester = harvester;
|
61
|
this.harvestLogID = harvester.getHarvestLogID();
|
62
|
this.harvestDate = harvestDate;
|
63
|
this.status = status;
|
64
|
this.message = message;
|
65
|
this.harvestOperationCode = harvestOperationCode;
|
66
|
this.siteScheduleID = siteScheduleID;
|
67
|
|
68
|
dbInsertHarvestLogEntry(); // Insert this entry to the HARVEST_LOG table
|
69
|
}
|
70
|
|
71
|
|
72
|
/**
|
73
|
* Creates a new instance of HarvestLog and inserts this entry to the
|
74
|
* HARVEST_LOG table. This version of the constructor also instantiates a
|
75
|
* HarvestDetailLog object and inserts it to the HARVEST_DETAIL_LOG table.
|
76
|
*
|
77
|
* @param harvester the parent Harvester object
|
78
|
* @param harvestDate the date of this harvest
|
79
|
* @param status the status of the harvest operation
|
80
|
* @param message the message text of the harvest operation
|
81
|
* @param harvestOperationCode the harvest operation code
|
82
|
* @param siteScheduleID the siteScheduleID for which this operation was
|
83
|
* performed. 0 indicates that the operation did not
|
84
|
* involve a particular harvest site.
|
85
|
*/
|
86
|
public HarvestLog(Harvester harvester,
|
87
|
Date harvestDate,
|
88
|
int status,
|
89
|
String message,
|
90
|
String harvestOperationCode,
|
91
|
int siteScheduleID,
|
92
|
HarvestDocument harvestDocument,
|
93
|
String errorMessage
|
94
|
) {
|
95
|
Date now = new Date();
|
96
|
timestamp = now.toString();
|
97
|
|
98
|
this.harvester = harvester;
|
99
|
this.harvestLogID = harvester.getHarvestLogID();
|
100
|
this.harvestDate = harvestDate;
|
101
|
this.status = status;
|
102
|
this.message = message;
|
103
|
this.harvestOperationCode = harvestOperationCode;
|
104
|
this.siteScheduleID = siteScheduleID;
|
105
|
this.harvestDetailLog = new HarvestDetailLog(harvester, harvestLogID,
|
106
|
harvestDocument, errorMessage);
|
107
|
|
108
|
dbInsertHarvestLogEntry(); // Insert to the HARVEST_LOG table
|
109
|
harvestDetailLog.dbInsertHarvestDetailLogEntry(); // and HARVEST_DETAIL_LOG
|
110
|
}
|
111
|
|
112
|
|
113
|
/**
|
114
|
* Retrieves the value of the EXPLANATION field of the HARVEST_OPERATION
|
115
|
* table based on the value of the HARVEST_OPERATION_CODE field.
|
116
|
*
|
117
|
* @param harvestOperationCode string value of the harvest operation code
|
118
|
* @return the explanation for this harvest operation, a String
|
119
|
*/
|
120
|
String dbGetExplanation(String harvestOperationCode) {
|
121
|
String explanation;
|
122
|
String fieldName = "EXPLANATION";
|
123
|
|
124
|
explanation = dbQueryHarvestOperation(fieldName, harvestOperationCode);
|
125
|
|
126
|
return explanation;
|
127
|
}
|
128
|
|
129
|
|
130
|
/**
|
131
|
* Retrieves the value of the HARVEST_OPERATION_CODE_LEVEL field of the
|
132
|
* HARVEST_OPERATION table based on the value of the HARVEST_OPERATION_CODE
|
133
|
* field.
|
134
|
*
|
135
|
* @param harvestOperationCode string value of the harvest operation code
|
136
|
* @return the code level value, a String, one of the following:
|
137
|
* "error", "warning", "notice", "info", or "debug"
|
138
|
*/
|
139
|
String dbGetHarvestOperationCodeLevel(String harvestOperationCode) {
|
140
|
String harvestOperationCodeLevel;
|
141
|
String fieldName = "HARVEST_OPERATION_CODE_LEVEL";
|
142
|
|
143
|
harvestOperationCodeLevel =
|
144
|
dbQueryHarvestOperation(fieldName, harvestOperationCode);
|
145
|
|
146
|
return harvestOperationCodeLevel;
|
147
|
}
|
148
|
|
149
|
|
150
|
/**
|
151
|
* Inserts a new entry into the HARVEST_LOG table, based on the contents of
|
152
|
* this HarvestLog object. Not yet implemented.
|
153
|
*/
|
154
|
void dbInsertHarvestLogEntry() {
|
155
|
String insertString;
|
156
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
|
157
|
Statement stmt;
|
158
|
|
159
|
insertString = "INSERT INTO HARVEST_LOG " +
|
160
|
"(HARVEST_LOG_ID, HARVEST_DATE, STATUS, MESSAGE," +
|
161
|
" HARVEST_OPERATION_CODE, SITE_SCHEDULE_ID) " +
|
162
|
"values(" +
|
163
|
harvestLogID + ", " +
|
164
|
"'" + simpleDateFormat.format(harvestDate) + "', " +
|
165
|
status + ", " +
|
166
|
"'" + timestamp + ": " + message + "', " +
|
167
|
"'" + harvestOperationCode + "', " +
|
168
|
siteScheduleID +
|
169
|
")";
|
170
|
|
171
|
try {
|
172
|
stmt = harvester.conn.createStatement();
|
173
|
stmt.executeUpdate(insertString);
|
174
|
stmt.close();
|
175
|
}
|
176
|
catch(SQLException e) {
|
177
|
System.out.println("SQLException: " + e.getMessage());
|
178
|
}
|
179
|
}
|
180
|
|
181
|
|
182
|
/**
|
183
|
* Retrieves the value of the either the EXPLANATION field or the
|
184
|
* HARVEST_OPERATION_CODE_LEVEL field of the HARVEST_OPERATION table based on
|
185
|
* the value of the HARVEST_OPERATION_CODE field.
|
186
|
*
|
187
|
* @param fieldName the field name to match, e.g. "EXPLANATION"
|
188
|
* @param harvestOperationCode string value of the harvest operation code
|
189
|
* @return the explanation string or the harvestOperationCodeLevel string
|
190
|
*/
|
191
|
String dbQueryHarvestOperation(String fieldName,
|
192
|
String harvestOperationCode
|
193
|
) {
|
194
|
String explanation = "No explanation available";
|
195
|
String harvestOperationCodeLevel = "debug";
|
196
|
String queryString;
|
197
|
String returnString = "";
|
198
|
ResultSet rs;
|
199
|
Statement stmt;
|
200
|
SQLWarning warn;
|
201
|
|
202
|
queryString = "SELECT EXPLANATION, HARVEST_OPERATION_CODE_LEVEL " +
|
203
|
"FROM HARVEST_OPERATION " +
|
204
|
"WHERE HARVEST_OPERATION_CODE=" +
|
205
|
"'" + harvestOperationCode + "'";
|
206
|
|
207
|
try { // Query the HARVEST_OPERATION table
|
208
|
stmt = harvester.conn.createStatement();
|
209
|
rs = stmt.executeQuery(queryString);
|
210
|
warn = rs.getWarnings();
|
211
|
|
212
|
if (warn != null) {
|
213
|
System.out.println("\n---Warning---\n");
|
214
|
|
215
|
while (warn != null) {
|
216
|
System.out.println("Message: " + warn.getMessage());
|
217
|
System.out.println("SQLState: " + warn.getSQLState());
|
218
|
System.out.print("Vendor error code: ");
|
219
|
System.out.println(warn.getErrorCode());
|
220
|
System.out.println("");
|
221
|
warn = warn.getNextWarning();
|
222
|
}
|
223
|
}
|
224
|
|
225
|
while (rs.next()) {
|
226
|
explanation = rs.getString("EXPLANATION");
|
227
|
harvestOperationCodeLevel = rs.getString("HARVEST_OPERATION_CODE_LEVEL");
|
228
|
warn = rs.getWarnings();
|
229
|
|
230
|
if (warn != null) {
|
231
|
System.out.println("\n---Warning---\n");
|
232
|
|
233
|
while (warn != null) {
|
234
|
System.out.println("Message: " + warn.getMessage());
|
235
|
System.out.println("SQLState: " + warn.getSQLState());
|
236
|
System.out.print("Vendor error code: ");
|
237
|
System.out.println(warn.getErrorCode());
|
238
|
System.out.println("");
|
239
|
warn = warn.getNextWarning();
|
240
|
}
|
241
|
}
|
242
|
}
|
243
|
|
244
|
rs.close();
|
245
|
stmt.close();
|
246
|
}
|
247
|
catch (SQLException e) {
|
248
|
System.out.println("SQLException: Database access failed: " + e);
|
249
|
}
|
250
|
|
251
|
if (fieldName.equals("EXPLANATION")) {
|
252
|
returnString = explanation;
|
253
|
}
|
254
|
else if (fieldName.equals("HARVEST_OPERATION_CODE_LEVEL")) {
|
255
|
returnString = harvestOperationCodeLevel;
|
256
|
}
|
257
|
|
258
|
return returnString;
|
259
|
}
|
260
|
|
261
|
|
262
|
/**
|
263
|
* Prints the contents of this HarvestLog object. Used in generating reports.
|
264
|
*/
|
265
|
void printOutput() {
|
266
|
System.out.println("");
|
267
|
System.out.println("harvestLogID: " + harvestLogID);
|
268
|
System.out.println("harvestDate: " + harvestDate);
|
269
|
System.out.println("status: " + status);
|
270
|
System.out.println("message: " + message);
|
271
|
System.out.println("harvestOperationCode: " + harvestOperationCode);
|
272
|
|
273
|
if (siteScheduleID != 0) {
|
274
|
harvester.printHarvestSiteSchedule(siteScheduleID);
|
275
|
}
|
276
|
|
277
|
if (harvestDetailLog != null) {
|
278
|
harvestDetailLog.printOutput();
|
279
|
}
|
280
|
}
|
281
|
|
282
|
}
|