Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 University of New Mexico and the 
4
 *                  Regents of the University of California
5
 *
6
 *   '$Author: jones $'
7
 *     '$Date: 2004-04-01 16:41:58 -0800 (Thu, 01 Apr 2004) $'
8
 * '$Revision: 2094 $'
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

    
36

    
37
/**
38
 * Manages log entries to be inserted to the HARVEST_LOG table.
39
 *
40
 * @author  costa
41
 */
42
public class HarvestLog {
43
    
44
  private Harvester harvester;              // The parent Harvester object
45
  private int harvestLogID;
46
  private Date harvestDate;
47
  private int status;
48
  private final String marker =
49
"*****************************************************************************";
50
  private String message;
51
  private String harvestOperationCode;
52
  private int siteScheduleID;
53
  private String timestamp;
54
  private HarvestDetailLog harvestDetailLog;  // Associated detail log, if any
55
    
56

    
57
  /** 
58
   * Creates a new instance of HarvestLog. This constructor is used when
59
   * creating log entries that do not involve an error on a harvest document.
60
   * (For that type of log entry, use the alternate constructor below.)
61
   *
62
   * @param  harvester       the parent Harvester object
63
   * @param  harvestDate     the date of this harvest
64
   * @param  status          the status of the harvest operation
65
   * @param  message         the message text of the harvest operation
66
   * @param  harvestOperationCode  the harvest operation code
67
   * @param  siteScheduleID  the siteScheduleID for which this operation was
68
   *                         performed. 0 indicates that the operation did not
69
   *                         involve a particular harvest site.
70
   */
71
  public HarvestLog(Harvester  harvester,
72
                    Date       harvestDate,
73
                    int        status,
74
                    String     message, 
75
                    String     harvestOperationCode,
76
                    int        siteScheduleID
77
                   ) {
78
    Date now = new Date();
79
    timestamp = now.toString();
80

    
81
    this.harvester = harvester;
82
    this.harvestLogID = harvester.getHarvestLogID();
83
    this.harvestDate = harvestDate;
84
    this.status = status;
85
    this.message = message;
86
    this.harvestOperationCode = harvestOperationCode;
87
    this.siteScheduleID = siteScheduleID;
88
    
89
    dbInsertHarvestLogEntry();   // Insert this entry to the HARVEST_LOG table
90
  }
91
    
92

    
93
  /** 
94
   * Creates a new instance of HarvestLog and inserts this entry to the
95
   * HARVEST_LOG table. This version of the constructor also instantiates a 
96
   * HarvestDetailLog object and inserts it to the HARVEST_DETAIL_LOG table.
97
   *
98
   * @param  harvester       the parent Harvester object
99
   * @param  harvestDate     the date of this harvest
100
   * @param  status          the status of the harvest operation
101
   * @param  message         the message text of the harvest operation
102
   * @param  harvestOperationCode  the harvest operation code
103
   * @param  siteScheduleID  the siteScheduleID for which this operation was
104
   *                         performed. 0 indicates that the operation did not
105
   *                         involve a particular harvest site.
106
   */
107
  public HarvestLog(Harvester  harvester,
108
                    Date       harvestDate,
109
                    int        status,
110
                    String     message, 
111
                    String     harvestOperationCode,
112
                    int        siteScheduleID,
113
                    HarvestDocument harvestDocument,
114
                    String     errorMessage
115
                   ) {
116
    Date now = new Date();
117
    timestamp = now.toString();
118

    
119
    this.harvester = harvester;
120
    this.harvestLogID = harvester.getHarvestLogID();
121
    this.harvestDate = harvestDate;
122
    this.status = status;
123
    this.message = message;
124
    this.harvestOperationCode = harvestOperationCode;
125
    this.siteScheduleID = siteScheduleID;
126
    this.harvestDetailLog = new HarvestDetailLog(harvester, harvestLogID, 
127
                                                 harvestDocument, errorMessage);
128
    
129
    dbInsertHarvestLogEntry();               // Insert to the HARVEST_LOG table
130
    harvestDetailLog.dbInsertHarvestDetailLogEntry(); // and HARVEST_DETAIL_LOG
131
  }
132
    
133

    
134
  /**
135
   * Retrieves the value of the EXPLANATION field of the HARVEST_OPERATION
136
   * table based on the value of the HARVEST_OPERATION_CODE field. 
137
   * 
138
   * @param  harvestOperationCode  string value of the harvest operation code
139
   * @return the explanation for this harvest operation, a String
140
   */
141
  String dbGetExplanation(String harvestOperationCode) {
142
    String explanation;
143
    String fieldName = "EXPLANATION";
144
    
145
    explanation = dbQueryHarvestOperation(fieldName, harvestOperationCode);
146
        
147
    return explanation;
148
  }
149
  
150

    
151
  /**
152
   * Retrieves the value of the HARVEST_OPERATION_CODE_LEVEL field of the
153
   * HARVEST_OPERATION table based on the value of the HARVEST_OPERATION_CODE 
154
   * field.
155
   * 
156
   * @param  harvestOperationCode  string value of the harvest operation code
157
   * @return the code level value, a String, one of the following:
158
   *         "error", "warning", "notice", "info", or "debug"
159
   */
160
  String dbGetHarvestOperationCodeLevel(String harvestOperationCode) {
161
    String harvestOperationCodeLevel;
162
    String fieldName = "HARVEST_OPERATION_CODE_LEVEL";
163
    
164
    harvestOperationCodeLevel = 
165
            dbQueryHarvestOperation(fieldName, harvestOperationCode);
166
        
167
    return harvestOperationCodeLevel;
168
  }
169
  
170

    
171
  /**
172
   * Inserts a new entry into the HARVEST_LOG table, based on the contents of
173
   * this HarvestLog object. Not yet implemented.
174
   */
175
  void dbInsertHarvestLogEntry() {
176
    String dequotedMessage = harvester.dequoteText(message);
177
    String insertString;
178
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
179
		Statement stmt;
180

    
181
    insertString = "INSERT INTO HARVEST_LOG " +
182
                   "(HARVEST_LOG_ID, HARVEST_DATE, STATUS, MESSAGE," +
183
                   " HARVEST_OPERATION_CODE, SITE_SCHEDULE_ID) " +
184
                   "values(" +
185
                   harvestLogID + ", " +
186
                   "'" + simpleDateFormat.format(harvestDate) + "', " +
187
                   status + ", " +
188
                   "'" + timestamp + ": " + dequotedMessage + "', " +
189
                   "'" + harvestOperationCode + "', " +
190
                   siteScheduleID +
191
                   ")";
192
                   
193
		try {
194
			stmt = harvester.conn.createStatement();							
195
			stmt.executeUpdate(insertString);
196
			stmt.close();
197
		}
198
    catch(SQLException e) {
199
			System.out.println("SQLException: " + e.getMessage());
200
		}
201
  }
202
  
203

    
204
  /**
205
   * Retrieves the value of the either the EXPLANATION field or the
206
   * HARVEST_OPERATION_CODE_LEVEL field of the HARVEST_OPERATION table based on
207
   * the value of the HARVEST_OPERATION_CODE field.
208
   *
209
   * @param  fieldName  the field name to match, e.g. "EXPLANATION"
210
   * @param  harvestOperationCode  string value of the harvest operation code
211
   * @return the explanation string or the harvestOperationCodeLevel string
212
   */
213
  String dbQueryHarvestOperation(String fieldName,
214
                                 String harvestOperationCode
215
                                ) {
216
    String explanation = "No explanation available";
217
    String harvestOperationCodeLevel = "debug";
218
    String queryString;
219
    String returnString = "";
220
    ResultSet rs;
221
    Statement stmt;
222
    SQLWarning warn;
223
    
224
    queryString = "SELECT EXPLANATION, HARVEST_OPERATION_CODE_LEVEL " + 
225
                  "FROM HARVEST_OPERATION " +
226
                  "WHERE HARVEST_OPERATION_CODE=" +
227
                  "'" + harvestOperationCode + "'";
228
        
229
    try {                           // Query the HARVEST_OPERATION table
230
      stmt = harvester.conn.createStatement();
231
      rs = stmt.executeQuery(queryString);
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
      while (rs.next()) {
248
        explanation = rs.getString("EXPLANATION");
249
        harvestOperationCodeLevel = rs.getString("HARVEST_OPERATION_CODE_LEVEL");
250
        warn = rs.getWarnings();
251

    
252
        if (warn != null) {
253
          System.out.println("\n---Warning---\n");
254
      
255
          while (warn != null) {
256
            System.out.println("Message: " + warn.getMessage());
257
            System.out.println("SQLState: " + warn.getSQLState());
258
            System.out.print("Vendor error code: ");
259
            System.out.println(warn.getErrorCode());
260
            System.out.println("");
261
            warn = warn.getNextWarning();
262
          }
263
        }
264
      }
265
      
266
      rs.close();
267
      stmt.close();
268
    }
269
    catch (SQLException e) {
270
      System.out.println("SQLException: Database access failed: " + e);
271
    }
272
 
273
    if (fieldName.equals("EXPLANATION")) {
274
      returnString = explanation;
275
    }
276
    else if (fieldName.equals("HARVEST_OPERATION_CODE_LEVEL")) {
277
      returnString = harvestOperationCodeLevel;
278
    }
279
    
280
    return returnString;
281
  }
282

    
283

    
284
  /**
285
   * Prints the contents of this HarvestLog object. Used in generating reports.
286
   * 
287
   * @param out   the PrintStream to write to
288
   */
289
  void printOutput(PrintStream out) {
290
    out.println("");
291
    out.println(marker);
292
    out.println("*");
293
    out.println("* harvestLogID:         " + harvestLogID);
294
    out.println("* harvestDate:          " + harvestDate);
295
    out.println("* status:               " + status);
296
    out.println("* message:              " + message);
297
    out.println("* harvestOperationCode: " + harvestOperationCode);
298

    
299
    if (harvestOperationCode.equals("GetDocListSuccess") ||
300
        harvestOperationCode.equals("GetDocListError")) {
301
      if (siteScheduleID != 0) {
302
        harvester.printHarvestSiteSchedule(out, siteScheduleID);
303
      }
304
    }
305
    
306
    if (harvestDetailLog != null) {
307
      harvestDetailLog.printOutput(out);
308
    }
309

    
310
    out.println("*");
311
    out.println(marker);
312
  }
313

    
314
}
(3-3/9)