Project

General

Profile

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.io.PrintStream;
10
import java.sql.Connection;
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.sql.SQLWarning;
14
import java.sql.Statement;
15
import java.text.SimpleDateFormat;
16
import java.util.Date;
17

    
18

    
19
/**
20
 * Manages log entries to be inserted to the HARVEST_LOG table.
21
 *
22
 * @author  costa
23
 */
24
public class HarvestLog {
25
    
26
  private Harvester harvester;              // The parent Harvester object
27
  private int harvestLogID;
28
  private Date harvestDate;
29
  private int status;
30
  private final String marker =
31
"*****************************************************************************";
32
  private String message;
33
  private String harvestOperationCode;
34
  private int siteScheduleID;
35
  private String timestamp;
36
  private HarvestDetailLog harvestDetailLog;  // Associated detail log, if any
37
    
38

    
39
  /** 
40
   * Creates a new instance of HarvestLog. This constructor is used when
41
   * creating log entries that do not involve an error on a harvest document.
42
   * (For that type of log entry, use the alternate constructor below.)
43
   *
44
   * @param  harvester       the parent Harvester object
45
   * @param  harvestDate     the date of this harvest
46
   * @param  status          the status of the harvest operation
47
   * @param  message         the message text of the harvest operation
48
   * @param  harvestOperationCode  the harvest operation code
49
   * @param  siteScheduleID  the siteScheduleID for which this operation was
50
   *                         performed. 0 indicates that the operation did not
51
   *                         involve a particular harvest site.
52
   */
53
  public HarvestLog(Harvester  harvester,
54
                    Date       harvestDate,
55
                    int        status,
56
                    String     message, 
57
                    String     harvestOperationCode,
58
                    int        siteScheduleID
59
                   ) {
60
    Date now = new Date();
61
    timestamp = now.toString();
62

    
63
    this.harvester = harvester;
64
    this.harvestLogID = harvester.getHarvestLogID();
65
    this.harvestDate = harvestDate;
66
    this.status = status;
67
    this.message = message;
68
    this.harvestOperationCode = harvestOperationCode;
69
    this.siteScheduleID = siteScheduleID;
70
    
71
    dbInsertHarvestLogEntry();   // Insert this entry to the HARVEST_LOG table
72
  }
73
    
74

    
75
  /** 
76
   * Creates a new instance of HarvestLog and inserts this entry to the
77
   * HARVEST_LOG table. This version of the constructor also instantiates a 
78
   * HarvestDetailLog object and inserts it to the HARVEST_DETAIL_LOG table.
79
   *
80
   * @param  harvester       the parent Harvester object
81
   * @param  harvestDate     the date of this harvest
82
   * @param  status          the status of the harvest operation
83
   * @param  message         the message text of the harvest operation
84
   * @param  harvestOperationCode  the harvest operation code
85
   * @param  siteScheduleID  the siteScheduleID for which this operation was
86
   *                         performed. 0 indicates that the operation did not
87
   *                         involve a particular harvest site.
88
   */
89
  public HarvestLog(Harvester  harvester,
90
                    Date       harvestDate,
91
                    int        status,
92
                    String     message, 
93
                    String     harvestOperationCode,
94
                    int        siteScheduleID,
95
                    HarvestDocument harvestDocument,
96
                    String     errorMessage
97
                   ) {
98
    Date now = new Date();
99
    timestamp = now.toString();
100

    
101
    this.harvester = harvester;
102
    this.harvestLogID = harvester.getHarvestLogID();
103
    this.harvestDate = harvestDate;
104
    this.status = status;
105
    this.message = message;
106
    this.harvestOperationCode = harvestOperationCode;
107
    this.siteScheduleID = siteScheduleID;
108
    this.harvestDetailLog = new HarvestDetailLog(harvester, harvestLogID, 
109
                                                 harvestDocument, errorMessage);
110
    
111
    dbInsertHarvestLogEntry();               // Insert to the HARVEST_LOG table
112
    harvestDetailLog.dbInsertHarvestDetailLogEntry(); // and HARVEST_DETAIL_LOG
113
  }
114
    
115

    
116
  /**
117
   * Retrieves the value of the EXPLANATION field of the HARVEST_OPERATION
118
   * table based on the value of the HARVEST_OPERATION_CODE field. 
119
   * 
120
   * @param  harvestOperationCode  string value of the harvest operation code
121
   * @return the explanation for this harvest operation, a String
122
   */
123
  String dbGetExplanation(String harvestOperationCode) {
124
    String explanation;
125
    String fieldName = "EXPLANATION";
126
    
127
    explanation = dbQueryHarvestOperation(fieldName, harvestOperationCode);
128
        
129
    return explanation;
130
  }
131
  
132

    
133
  /**
134
   * Retrieves the value of the HARVEST_OPERATION_CODE_LEVEL field of the
135
   * HARVEST_OPERATION table based on the value of the HARVEST_OPERATION_CODE 
136
   * field.
137
   * 
138
   * @param  harvestOperationCode  string value of the harvest operation code
139
   * @return the code level value, a String, one of the following:
140
   *         "error", "warning", "notice", "info", or "debug"
141
   */
142
  String dbGetHarvestOperationCodeLevel(String harvestOperationCode) {
143
    String harvestOperationCodeLevel;
144
    String fieldName = "HARVEST_OPERATION_CODE_LEVEL";
145
    
146
    harvestOperationCodeLevel = 
147
            dbQueryHarvestOperation(fieldName, harvestOperationCode);
148
        
149
    return harvestOperationCodeLevel;
150
  }
151
  
152

    
153
  /**
154
   * Inserts a new entry into the HARVEST_LOG table, based on the contents of
155
   * this HarvestLog object. Not yet implemented.
156
   */
157
  void dbInsertHarvestLogEntry() {
158
    String dequotedMessage = harvester.dequoteText(message);
159
    String insertString;
160
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
161
		Statement stmt;
162

    
163
    insertString = "INSERT INTO HARVEST_LOG " +
164
                   "(HARVEST_LOG_ID, HARVEST_DATE, STATUS, MESSAGE," +
165
                   " HARVEST_OPERATION_CODE, SITE_SCHEDULE_ID) " +
166
                   "values(" +
167
                   harvestLogID + ", " +
168
                   "'" + simpleDateFormat.format(harvestDate) + "', " +
169
                   status + ", " +
170
                   "'" + timestamp + ": " + dequotedMessage + "', " +
171
                   "'" + harvestOperationCode + "', " +
172
                   siteScheduleID +
173
                   ")";
174
                   
175
		try {
176
			stmt = harvester.conn.createStatement();							
177
			stmt.executeUpdate(insertString);
178
			stmt.close();
179
		}
180
    catch(SQLException e) {
181
			System.out.println("SQLException: " + e.getMessage());
182
		}
183
  }
184
  
185

    
186
  /**
187
   * Retrieves the value of the either the EXPLANATION field or the
188
   * HARVEST_OPERATION_CODE_LEVEL field of the HARVEST_OPERATION table based on
189
   * the value of the HARVEST_OPERATION_CODE field.
190
   *
191
   * @param  fieldName  the field name to match, e.g. "EXPLANATION"
192
   * @param  harvestOperationCode  string value of the harvest operation code
193
   * @return the explanation string or the harvestOperationCodeLevel string
194
   */
195
  String dbQueryHarvestOperation(String fieldName,
196
                                 String harvestOperationCode
197
                                ) {
198
    String explanation = "No explanation available";
199
    String harvestOperationCodeLevel = "debug";
200
    String queryString;
201
    String returnString = "";
202
    ResultSet rs;
203
    Statement stmt;
204
    SQLWarning warn;
205
    
206
    queryString = "SELECT EXPLANATION, HARVEST_OPERATION_CODE_LEVEL " + 
207
                  "FROM HARVEST_OPERATION " +
208
                  "WHERE HARVEST_OPERATION_CODE=" +
209
                  "'" + harvestOperationCode + "'";
210
        
211
    try {                           // Query the HARVEST_OPERATION table
212
      stmt = harvester.conn.createStatement();
213
      rs = stmt.executeQuery(queryString);
214
      warn = rs.getWarnings();
215

    
216
      if (warn != null) {
217
        System.out.println("\n---Warning---\n");
218

    
219
        while (warn != null) {
220
          System.out.println("Message: " + warn.getMessage());
221
          System.out.println("SQLState: " + warn.getSQLState());
222
          System.out.print("Vendor error code: ");
223
          System.out.println(warn.getErrorCode());
224
          System.out.println("");
225
          warn = warn.getNextWarning();
226
        }
227
      }
228
     
229
      while (rs.next()) {
230
        explanation = rs.getString("EXPLANATION");
231
        harvestOperationCodeLevel = rs.getString("HARVEST_OPERATION_CODE_LEVEL");
232
        warn = rs.getWarnings();
233

    
234
        if (warn != null) {
235
          System.out.println("\n---Warning---\n");
236
      
237
          while (warn != null) {
238
            System.out.println("Message: " + warn.getMessage());
239
            System.out.println("SQLState: " + warn.getSQLState());
240
            System.out.print("Vendor error code: ");
241
            System.out.println(warn.getErrorCode());
242
            System.out.println("");
243
            warn = warn.getNextWarning();
244
          }
245
        }
246
      }
247
      
248
      rs.close();
249
      stmt.close();
250
    }
251
    catch (SQLException e) {
252
      System.out.println("SQLException: Database access failed: " + e);
253
    }
254
 
255
    if (fieldName.equals("EXPLANATION")) {
256
      returnString = explanation;
257
    }
258
    else if (fieldName.equals("HARVEST_OPERATION_CODE_LEVEL")) {
259
      returnString = harvestOperationCodeLevel;
260
    }
261
    
262
    return returnString;
263
  }
264

    
265

    
266
  /**
267
   * Prints the contents of this HarvestLog object. Used in generating reports.
268
   * 
269
   * @param out   the PrintStream to write to
270
   */
271
  void printOutput(PrintStream out) {
272
    out.println("");
273
    out.println(marker);
274
    out.println("*");
275
    out.println("* harvestLogID:         " + harvestLogID);
276
    out.println("* harvestDate:          " + harvestDate);
277
    out.println("* status:               " + status);
278
    out.println("* message:              " + message);
279
    out.println("* harvestOperationCode: " + harvestOperationCode);
280

    
281
    if (harvestOperationCode.equals("GetDocListSuccess") ||
282
        harvestOperationCode.equals("GetDocListError")) {
283
      if (siteScheduleID != 0) {
284
        harvester.printHarvestSiteSchedule(out, siteScheduleID);
285
      }
286
    }
287
    
288
    if (harvestDetailLog != null) {
289
      harvestDetailLog.printOutput(out);
290
    }
291

    
292
    out.println("*");
293
    out.println(marker);
294
  }
295

    
296
}
(3-3/9)