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.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 final String marker =
30
"*****************************************************************************";
31
  private String message;
32
  private String harvestOperationCode;
33
  private int siteScheduleID;
34
  private String timestamp;
35
  private HarvestDetailLog harvestDetailLog;  // Associated detail log, if any
36
    
37

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

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

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

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

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

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

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

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

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

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

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

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

    
264

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

    
278
    if (harvestOperationCode.equals("GetDocListSuccess") ||
279
        harvestOperationCode.equals("GetDocListError")) {
280
      if (siteScheduleID != 0) {
281
        harvester.printHarvestSiteSchedule(siteScheduleID);
282
      }
283
    }
284
    
285
    if (harvestDetailLog != null) {
286
      harvestDetailLog.printOutput();
287
    }
288

    
289
    System.out.println("*");
290
    System.out.println(marker);
291
  }
292

    
293
}
(3-3/7)