1
|
/*
|
2
|
* HarvesterRegistration.java
|
3
|
*
|
4
|
* Created on March 23, 2004, 4:44 PM
|
5
|
*/
|
6
|
|
7
|
package edu.ucsb.nceas.metacat.harvesterClient;
|
8
|
|
9
|
import com.oreilly.servlet.ParameterParser;
|
10
|
import java.io.File;
|
11
|
import java.io.InputStream;
|
12
|
import java.io.IOException;
|
13
|
import java.io.PrintWriter;
|
14
|
import java.sql.Connection;
|
15
|
import java.sql.DriverManager;
|
16
|
import java.sql.ResultSet;
|
17
|
import java.sql.SQLException;
|
18
|
import java.sql.SQLWarning;
|
19
|
import java.sql.Statement;
|
20
|
import java.text.ParseException;
|
21
|
import java.text.SimpleDateFormat;
|
22
|
import java.util.Date;
|
23
|
import java.util.Enumeration;
|
24
|
import java.util.Properties;
|
25
|
import javax.servlet.ServletContext;
|
26
|
import javax.servlet.ServletException;
|
27
|
import javax.servlet.http.HttpServlet;
|
28
|
import javax.servlet.http.HttpServletRequest;
|
29
|
import javax.servlet.http.HttpServletResponse;
|
30
|
import javax.servlet.http.HttpSession;
|
31
|
|
32
|
/**
|
33
|
* HarvesterRegistration is a servlet that implements harvester registration.
|
34
|
* The servlet reads parameters that were entered in a harvester registration
|
35
|
* form, checks the validity of the values, stores the values in the database
|
36
|
* by either inserting, updating, or removing a record in the
|
37
|
* HARVEST_SITE_SCHEDULE table.
|
38
|
*
|
39
|
* @author costa
|
40
|
*
|
41
|
*/
|
42
|
public class HarvesterRegistration extends HttpServlet {
|
43
|
|
44
|
/*
|
45
|
* Object fields
|
46
|
*/
|
47
|
final private long millisecondsPerDay = (1000 * 60 * 60 * 24);
|
48
|
private String password; // database password, from properties file
|
49
|
private String url; // database url, from properties file
|
50
|
private String user; // database user, from properties file
|
51
|
|
52
|
|
53
|
/*
|
54
|
* Object methods
|
55
|
*/
|
56
|
|
57
|
|
58
|
/**
|
59
|
* Checks validity of user input values.
|
60
|
*
|
61
|
* @param out the PrintWriter output object
|
62
|
* @param documentListURL the Harvest Document List URL specified by the user
|
63
|
* @param updateFrequency the Harvest Frequency specified by the user
|
64
|
* @return validValues true if all values are valid, else false
|
65
|
*/
|
66
|
private boolean checkValues(PrintWriter out,
|
67
|
String documentListURL,
|
68
|
int updateFrequency
|
69
|
) {
|
70
|
boolean validValues = true;
|
71
|
|
72
|
// Check validity of the Harvest Document List URL field
|
73
|
if (documentListURL.equals("")) {
|
74
|
out.println(
|
75
|
"A value must be specified in the Harvest Document List URL field"
|
76
|
);
|
77
|
validValues = false;
|
78
|
}
|
79
|
|
80
|
// Check validity of the Harvest Frequency field
|
81
|
if ((updateFrequency < 1) || (updateFrequency > 99)) {
|
82
|
out.println("Harvest Frequency should be in the range 1 to 99");
|
83
|
validValues = false;
|
84
|
}
|
85
|
|
86
|
return validValues;
|
87
|
}
|
88
|
|
89
|
|
90
|
/**
|
91
|
* Closes the database connection.
|
92
|
*
|
93
|
* @param conn The connection.
|
94
|
*/
|
95
|
private void closeConnection(Connection conn) {
|
96
|
try {
|
97
|
if (conn != null) {
|
98
|
conn.close();
|
99
|
}
|
100
|
}
|
101
|
catch (SQLException e) {
|
102
|
// ignored
|
103
|
}
|
104
|
}
|
105
|
|
106
|
|
107
|
/**
|
108
|
* Inserts a record to the HARVEST_SITE_SCHEDULE table.
|
109
|
*
|
110
|
* @param out the PrintWriter output object
|
111
|
* @param conn the Connection
|
112
|
* @param siteScheduleID the primary key for the table
|
113
|
* @param contactEmail contact email address of the site user
|
114
|
* @param documentListURL the URL of the harvester document list at the site
|
115
|
* @param ldapDN the site user's LDAP DN
|
116
|
* @param ldapPwd the site user's LDAP password
|
117
|
* @param unit the update frequency unit, e.g. "days", "weeks"
|
118
|
* @param updateFrequency the update frequency, an integer in range 1-99
|
119
|
*/
|
120
|
private void dbInsert(PrintWriter out,
|
121
|
Connection conn,
|
122
|
int siteScheduleID,
|
123
|
String contactEmail,
|
124
|
String documentListURL,
|
125
|
String ldapDN,
|
126
|
String ldapPwd,
|
127
|
String unit,
|
128
|
int updateFrequency
|
129
|
) {
|
130
|
String dateNextHarvest;
|
131
|
long delta;
|
132
|
Date dnh; // Date of next harvest
|
133
|
Date now; // Today's date
|
134
|
String query;
|
135
|
Statement stmt;
|
136
|
long timeNextHarvest;
|
137
|
SimpleDateFormat writeFormat = new SimpleDateFormat("dd-MMM-yyyy");
|
138
|
|
139
|
// Calculate the value of delta, the number of milliseconds between the
|
140
|
// last harvest date and the next harvest date.
|
141
|
delta = updateFrequency * millisecondsPerDay;
|
142
|
|
143
|
if (unit.equals("weeks")) {
|
144
|
delta *= 7;
|
145
|
}
|
146
|
else if (unit.equals("months")) {
|
147
|
delta *= 30;
|
148
|
}
|
149
|
|
150
|
now = new Date();
|
151
|
timeNextHarvest = now.getTime();
|
152
|
dnh = new Date(timeNextHarvest);
|
153
|
dateNextHarvest = writeFormat.format(dnh);
|
154
|
|
155
|
try {
|
156
|
stmt = conn.createStatement();
|
157
|
query = "insert into HARVEST_SITE_SCHEDULE " +
|
158
|
"(SITE_SCHEDULE_ID, CONTACT_EMAIL, DOCUMENTLISTURL, LDAPDN, LDAPPWD, " +
|
159
|
"UNIT, UPDATEFREQUENCY, DATENEXTHARVEST) " +
|
160
|
"values(" + siteScheduleID + "," +
|
161
|
quoteString(contactEmail) + "," +
|
162
|
quoteString(documentListURL) + "," +
|
163
|
quoteString(ldapDN) + "," +
|
164
|
quoteString(ldapPwd) + "," +
|
165
|
quoteString(unit) + "," +
|
166
|
updateFrequency + "," +
|
167
|
quoteString(dateNextHarvest) + ")";
|
168
|
|
169
|
System.out.println(query);
|
170
|
stmt.executeUpdate(query);
|
171
|
stmt.close();
|
172
|
reportResults(out, ldapDN, contactEmail, documentListURL, updateFrequency,
|
173
|
unit, dateNextHarvest);
|
174
|
}
|
175
|
catch(SQLException e) {
|
176
|
System.out.println("SQLException: " + e.getMessage());
|
177
|
}
|
178
|
}
|
179
|
|
180
|
|
181
|
/**
|
182
|
* Removes a record from the HARVEST_SITE_SCHEDULE table.
|
183
|
*
|
184
|
* @param out the PrintWriter output object
|
185
|
* @param conn the Connection
|
186
|
* @param siteScheduleID the primary key for the table
|
187
|
* @param ldapDN the site user's LDAP DN
|
188
|
*/
|
189
|
private void dbRemove(PrintWriter out,
|
190
|
Connection conn,
|
191
|
int siteScheduleID,
|
192
|
String ldapDN
|
193
|
) {
|
194
|
String query = "DELETE FROM HARVEST_SITE_SCHEDULE WHERE " +
|
195
|
"SITE_SCHEDULE_ID=" + siteScheduleID;
|
196
|
int nRecords = 0;
|
197
|
Statement stmt;
|
198
|
|
199
|
try {
|
200
|
stmt = conn.createStatement();
|
201
|
System.out.println(query);
|
202
|
nRecords = stmt.executeUpdate(query);
|
203
|
stmt.close();
|
204
|
System.out.println(nRecords + " record(s) removed.");
|
205
|
|
206
|
if (nRecords > 0) {
|
207
|
out.println("Harvester registration removed for user " + ldapDN);
|
208
|
}
|
209
|
else {
|
210
|
out.println("A problem was encountered removing registration for user "
|
211
|
+ ldapDN);
|
212
|
}
|
213
|
}
|
214
|
catch(SQLException e) {
|
215
|
System.out.println("SQLException: " + e.getMessage());
|
216
|
}
|
217
|
}
|
218
|
|
219
|
|
220
|
/**
|
221
|
* Updates a record in the HARVEST_SITE_SCHEDULE table.
|
222
|
*
|
223
|
* @param out the PrintWriter output object
|
224
|
* @param conn the Connection
|
225
|
* @param siteScheduleID the primary key for the table
|
226
|
* @param contactEmail contact email address of the site user
|
227
|
* @param documentListURL the URL of the harvester document list at the site
|
228
|
* @param ldapDN the site user's LDAP DN
|
229
|
* @param ldapPwd the site user's LDAP password
|
230
|
* @param unit the update frequency unit, e.g. "days", "weeks"
|
231
|
* @param updateFrequency the update frequency, an integer in range 1-99
|
232
|
* @param dateLastHarvest the date of last harvest,
|
233
|
* e.g. "2004-04-30 00:00:00.0" or ""
|
234
|
*/
|
235
|
private void dbUpdate(PrintWriter out,
|
236
|
Connection conn,
|
237
|
int siteScheduleID,
|
238
|
String contactEmail,
|
239
|
String documentListURL,
|
240
|
String ldapDN,
|
241
|
String ldapPwd,
|
242
|
String unit,
|
243
|
int updateFrequency,
|
244
|
String dateLastHarvest
|
245
|
) {
|
246
|
String dateNextHarvest;
|
247
|
long delta;
|
248
|
Date dlh; // Date of last harvest
|
249
|
Date dnh; // Date of next harvest
|
250
|
Date now = new Date(); // Today's date
|
251
|
SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
|
252
|
SimpleDateFormat writeFormat = new SimpleDateFormat("dd-MMM-yyyy");
|
253
|
Statement stmt;
|
254
|
long timeLastHarvest;
|
255
|
long timeNextHarvest;
|
256
|
long timeNow = now.getTime();
|
257
|
|
258
|
// Calculate the value of delta, the number of milliseconds between the
|
259
|
// last harvest date and the next harvest date.
|
260
|
delta = updateFrequency * millisecondsPerDay;
|
261
|
|
262
|
if (unit.equals("weeks")) {
|
263
|
delta *= 7;
|
264
|
}
|
265
|
else if (unit.equals("months")) {
|
266
|
delta *= 30;
|
267
|
}
|
268
|
|
269
|
if (dateLastHarvest.equals("")) {
|
270
|
timeNextHarvest = timeNow;
|
271
|
}
|
272
|
else {
|
273
|
try {
|
274
|
dlh = readFormat.parse(dateLastHarvest);
|
275
|
timeLastHarvest = dlh.getTime();
|
276
|
timeNextHarvest = timeLastHarvest + delta;
|
277
|
timeNextHarvest = Math.max(timeNextHarvest, timeNow);
|
278
|
}
|
279
|
catch (ParseException e) {
|
280
|
System.out.println("Error parsing date: " + dateLastHarvest +
|
281
|
" " + e.getMessage());
|
282
|
timeNextHarvest = timeNow;
|
283
|
}
|
284
|
}
|
285
|
|
286
|
dnh = new Date(timeNextHarvest);
|
287
|
dateNextHarvest = writeFormat.format(dnh);
|
288
|
|
289
|
try {
|
290
|
stmt = conn.createStatement();
|
291
|
stmt.executeUpdate("UPDATE HARVEST_SITE_SCHEDULE SET CONTACT_EMAIL=" +
|
292
|
quoteString(contactEmail) +
|
293
|
" WHERE SITE_SCHEDULE_ID = " + siteScheduleID);
|
294
|
stmt.executeUpdate("UPDATE HARVEST_SITE_SCHEDULE SET DOCUMENTLISTURL=" +
|
295
|
quoteString(documentListURL) +
|
296
|
" WHERE SITE_SCHEDULE_ID = " + siteScheduleID);
|
297
|
stmt.executeUpdate("UPDATE HARVEST_SITE_SCHEDULE SET LDAPPWD=" +
|
298
|
quoteString(ldapPwd) +
|
299
|
" WHERE SITE_SCHEDULE_ID = " + siteScheduleID);
|
300
|
stmt.executeUpdate("UPDATE HARVEST_SITE_SCHEDULE SET UNIT=" +
|
301
|
quoteString(unit) +
|
302
|
" WHERE SITE_SCHEDULE_ID = " + siteScheduleID);
|
303
|
stmt.executeUpdate("UPDATE HARVEST_SITE_SCHEDULE SET UPDATEFREQUENCY=" +
|
304
|
updateFrequency +
|
305
|
" WHERE SITE_SCHEDULE_ID = " + siteScheduleID);
|
306
|
stmt.executeUpdate("UPDATE HARVEST_SITE_SCHEDULE SET DATENEXTHARVEST=" +
|
307
|
quoteString(dateNextHarvest) +
|
308
|
" WHERE SITE_SCHEDULE_ID = " + siteScheduleID);
|
309
|
stmt.close();
|
310
|
reportResults(out, ldapDN, contactEmail, documentListURL, updateFrequency,
|
311
|
unit, dateNextHarvest);
|
312
|
}
|
313
|
catch(SQLException e) {
|
314
|
System.out.println("SQLException: " + e.getMessage());
|
315
|
}
|
316
|
|
317
|
}
|
318
|
|
319
|
|
320
|
/**
|
321
|
* Handles GET method requests. Displays the current registration info for
|
322
|
* this user (if any), then allows the user to make changes and register or
|
323
|
* unregister.
|
324
|
*
|
325
|
* @param req the request
|
326
|
* @param res the response
|
327
|
* @throws ServletException
|
328
|
* @throws IOException
|
329
|
*/
|
330
|
public void doGet(HttpServletRequest req, HttpServletResponse res)
|
331
|
throws ServletException, IOException {
|
332
|
Connection conn = getConnection();
|
333
|
String contactEmail = "";
|
334
|
String documentListURL = "http://";
|
335
|
HttpSession httpSession;
|
336
|
String ldapDN;
|
337
|
String ldapPwd;
|
338
|
String query;
|
339
|
int siteScheduleID;
|
340
|
Statement stmt;
|
341
|
String unit = "days";
|
342
|
int updateFrequency = 1;
|
343
|
|
344
|
httpSession = req.getSession(false);
|
345
|
|
346
|
if (httpSession == null) {
|
347
|
System.out.println("User did not log in.");
|
348
|
return;
|
349
|
}
|
350
|
|
351
|
// The user name and password are stored as session attributes by the
|
352
|
// HarvesterRegistrationLogin servlet.
|
353
|
ldapDN = (String) httpSession.getAttribute("ldapDN");
|
354
|
ldapPwd = (String) httpSession.getAttribute("ldapPwd");
|
355
|
|
356
|
siteScheduleID = getSiteScheduleID(conn, ldapDN);
|
357
|
|
358
|
// If the user is already registered, query the existing values and
|
359
|
// initialize the form with them.
|
360
|
if (siteScheduleID != 0) {
|
361
|
query = "SELECT * FROM HARVEST_SITE_SCHEDULE WHERE SITE_SCHEDULE_ID=" +
|
362
|
siteScheduleID;
|
363
|
|
364
|
try {
|
365
|
stmt = conn.createStatement();
|
366
|
ResultSet rs = stmt.executeQuery(query);
|
367
|
|
368
|
while (rs.next()) {
|
369
|
contactEmail = rs.getString("CONTACT_EMAIL");
|
370
|
documentListURL = rs.getString("DOCUMENTLISTURL");
|
371
|
updateFrequency = rs.getInt("UPDATEFREQUENCY");
|
372
|
unit = rs.getString("UNIT");
|
373
|
}
|
374
|
|
375
|
stmt.close();
|
376
|
}
|
377
|
catch (SQLException ex) {
|
378
|
System.out.println("SQLException: " + ex.getMessage());
|
379
|
}
|
380
|
}
|
381
|
|
382
|
res.setContentType("text/html");
|
383
|
PrintWriter out = res.getWriter();
|
384
|
|
385
|
// Print the registration form
|
386
|
out.println("<HTML>");
|
387
|
out.println("<HEAD>");
|
388
|
out.println("<TITLE>Metacat Harvester Registration</TITLE>");
|
389
|
out.println("</HEAD>");
|
390
|
out.println("<BODY>");
|
391
|
out.println("<H2><B>Metacat Harvester Registration</B></H2>");
|
392
|
out.println("<FORM METHOD=POST>"); // posts to itself
|
393
|
out.println("To register or changes values, enter all values ");
|
394
|
out.println("below and click <B>Register</B>.<BR>");
|
395
|
out.println("To unregister, simply click <B>Unregister</B>.<BR>");
|
396
|
out.println("<BR><BR>");
|
397
|
out.println("<pre>Email address: </pre>");
|
398
|
out.println("<INPUT TYPE=TEXT NAME=contactEmail VALUE=");
|
399
|
out.println(contactEmail + ">");
|
400
|
out.println("<BR><BR>");
|
401
|
out.println("<pre>Harvest Document List URL: </pre>");
|
402
|
out.println("<INPUT TYPE=TEXT NAME=documentListURL VALUE=");
|
403
|
out.println(documentListURL + ">");
|
404
|
out.println("<BR><BR>");
|
405
|
out.println("<pre>Harvest Frequency (1-99): </pre>");
|
406
|
out.println("<INPUT TYPE=TEXT NAME=updateFrequency ");
|
407
|
out.println("MAXLENGTH=2 SIZE=2 VALUE=");
|
408
|
out.println(updateFrequency + ">");
|
409
|
out.println("<BR><BR>");
|
410
|
out.println("<pre>Unit: </pre>");
|
411
|
out.println("<INPUT TYPE=RADIO ");
|
412
|
if (unit.equals("days")) out.println("CHECKED ");
|
413
|
out.println("NAME=unit VALUE=days>day(s)");
|
414
|
out.println("<INPUT TYPE=RADIO ");
|
415
|
if (unit.equals("weeks")) out.println("CHECKED ");
|
416
|
out.println("NAME=unit VALUE=weeks>week(s)");
|
417
|
out.println("<INPUT TYPE=RADIO ");
|
418
|
if (unit.equals("months")) out.println("CHECKED ");
|
419
|
out.println("NAME=unit VALUE=months>month(s)");
|
420
|
out.println("<BR><BR><BR>");
|
421
|
out.println("<INPUT TYPE=SUBMIT NAME=register VALUE=Register>");
|
422
|
out.println("<INPUT TYPE=SUBMIT NAME=unregister VALUE=Unregister>");
|
423
|
out.println("</BODY>");
|
424
|
out.println("</HTML>");
|
425
|
}
|
426
|
|
427
|
|
428
|
/**
|
429
|
* Handles POST method requests. Reads values as entered by the user in the
|
430
|
* harvester registration form and checks them for validity. Then either
|
431
|
* inserts, updates, or removes a record in the HARVEST_SITE_SCHEDULE table.
|
432
|
*
|
433
|
* @param req the request
|
434
|
* @param res the response
|
435
|
* @throws ServletException
|
436
|
* @throws IOException
|
437
|
*/
|
438
|
public void doPost(HttpServletRequest req, HttpServletResponse res)
|
439
|
throws ServletException, IOException {
|
440
|
Connection conn = getConnection();
|
441
|
int maxValue;
|
442
|
boolean remove = false; // if true, remove record
|
443
|
int siteScheduleID;
|
444
|
String contactEmail;
|
445
|
String dateLastHarvest;
|
446
|
String dateNextHarvest;
|
447
|
String documentListURL;
|
448
|
HttpSession httpSession;
|
449
|
String ldapDN;
|
450
|
String ldapPwd;
|
451
|
PrintWriter out;
|
452
|
ParameterParser parameterParser = new ParameterParser(req);
|
453
|
String register;
|
454
|
String unit;
|
455
|
String unregister;
|
456
|
int updateFrequency;
|
457
|
boolean validValues;
|
458
|
|
459
|
httpSession = req.getSession(false);
|
460
|
|
461
|
if (httpSession == null) {
|
462
|
System.out.println("User did not log in.");
|
463
|
return;
|
464
|
}
|
465
|
|
466
|
// The user name and password are stored as session attributes by the
|
467
|
// HarvesterRegistrationLogin servlet
|
468
|
ldapDN = (String) httpSession.getAttribute("ldapDN");
|
469
|
ldapPwd = (String) httpSession.getAttribute("ldapPwd");
|
470
|
|
471
|
contactEmail = parameterParser.getStringParameter("contactEmail", "None");
|
472
|
documentListURL = parameterParser.getStringParameter("documentListURL", "");
|
473
|
unit = parameterParser.getStringParameter("unit", "days");
|
474
|
updateFrequency = parameterParser.getIntParameter("updateFrequency", 1);
|
475
|
register = parameterParser.getStringParameter("register", "");
|
476
|
unregister = parameterParser.getStringParameter("unregister", "");
|
477
|
remove = (unregister.equalsIgnoreCase("Unregister"));
|
478
|
siteScheduleID = getSiteScheduleID(conn, ldapDN);
|
479
|
dateLastHarvest = getDateLastHarvest(conn, siteScheduleID);
|
480
|
|
481
|
res.setContentType("text/plain");
|
482
|
out = res.getWriter();
|
483
|
|
484
|
if (!remove) {
|
485
|
validValues = checkValues(out, documentListURL, updateFrequency);
|
486
|
|
487
|
if (!validValues) {
|
488
|
return;
|
489
|
}
|
490
|
}
|
491
|
|
492
|
if (siteScheduleID == 0) {
|
493
|
if (remove) {
|
494
|
// The user clicked Unregister, but no existing record was found
|
495
|
System.out.println("Unable to remove record for user " + ldapDN);
|
496
|
System.out.println("No matching record found in HARVEST_SITE_SCHEDULE");
|
497
|
out.println("No record found for user " + ldapDN + ".");
|
498
|
out.println("Since you were not registered, no action was taken.");
|
499
|
}
|
500
|
else {
|
501
|
maxValue = getMaxValue(conn,
|
502
|
"HARVEST_SITE_SCHEDULE",
|
503
|
"SITE_SCHEDULE_ID");
|
504
|
siteScheduleID = maxValue + 1;
|
505
|
dbInsert(out, conn, siteScheduleID, contactEmail, documentListURL,
|
506
|
ldapDN, ldapPwd, unit, updateFrequency);
|
507
|
}
|
508
|
}
|
509
|
else {
|
510
|
// Either update or remove an existing record
|
511
|
if (remove) {
|
512
|
dbRemove(out, conn, siteScheduleID, ldapDN);
|
513
|
}
|
514
|
else {
|
515
|
dbUpdate(out, conn, siteScheduleID, contactEmail, documentListURL,
|
516
|
ldapDN, ldapPwd, unit, updateFrequency, dateLastHarvest);
|
517
|
}
|
518
|
}
|
519
|
|
520
|
closeConnection(conn);
|
521
|
}
|
522
|
|
523
|
|
524
|
/**
|
525
|
* Gets a database connection.
|
526
|
*
|
527
|
* @return conn, the Connection object
|
528
|
*/
|
529
|
private Connection getConnection() {
|
530
|
Connection conn = null;
|
531
|
SQLWarning warn;
|
532
|
|
533
|
// Make the database connection
|
534
|
try {
|
535
|
System.out.println("Getting connection to Harvester tables");
|
536
|
conn = DriverManager.getConnection(url, user, password);
|
537
|
|
538
|
// If a SQLWarning object is available, print its warning(s).
|
539
|
// There may be multiple warnings chained.
|
540
|
warn = conn.getWarnings();
|
541
|
|
542
|
if (warn != null) {
|
543
|
while (warn != null) {
|
544
|
System.out.println("SQLState: " + warn.getSQLState());
|
545
|
System.out.println("Message: " + warn.getMessage());
|
546
|
System.out.println("Vendor: " + warn.getErrorCode());
|
547
|
System.out.println("");
|
548
|
warn = warn.getNextWarning();
|
549
|
}
|
550
|
}
|
551
|
}
|
552
|
catch (SQLException e) {
|
553
|
System.out.println("Database access failed " + e);
|
554
|
}
|
555
|
|
556
|
return conn;
|
557
|
}
|
558
|
|
559
|
|
560
|
/**
|
561
|
* Gets the date of last harvest value from the HARVEST_SITE_SCHEDULE table,
|
562
|
* given a siteScheduleID value (the primary key).
|
563
|
*
|
564
|
* @param conn the connection
|
565
|
* @param siteScheduleID the primary key
|
566
|
* @return dateLastHarvest the string stored in the table, e.g.
|
567
|
* "2004-04-30 00:00:00.0" or ""
|
568
|
*/
|
569
|
private String getDateLastHarvest(Connection conn, int siteScheduleID) {
|
570
|
String dateLastHarvest = "";
|
571
|
String query = "SELECT DATELASTHARVEST FROM HARVEST_SITE_SCHEDULE " +
|
572
|
"WHERE SITE_SCHEDULE_ID=" + siteScheduleID;
|
573
|
Statement stmt;
|
574
|
|
575
|
try {
|
576
|
stmt = conn.createStatement();
|
577
|
ResultSet rs = stmt.executeQuery(query);
|
578
|
|
579
|
while (rs.next()) {
|
580
|
dateLastHarvest = rs.getString("DATELASTHARVEST");
|
581
|
if (rs.wasNull()) {
|
582
|
dateLastHarvest = ""; // Convert null value to empty string
|
583
|
}
|
584
|
}
|
585
|
|
586
|
stmt.close();
|
587
|
}
|
588
|
catch (SQLException ex) {
|
589
|
System.out.println("SQLException: " + ex.getMessage());
|
590
|
}
|
591
|
|
592
|
return dateLastHarvest;
|
593
|
}
|
594
|
|
595
|
|
596
|
/**
|
597
|
* Gets the maximum value of an integer field from a table, given the table
|
598
|
* name and the field name.
|
599
|
*
|
600
|
* @param tableName the database table name
|
601
|
* @param fieldName the field name of the integer field in the table
|
602
|
* @return the maximum integer stored in the fieldName field of tableName
|
603
|
*/
|
604
|
private int getMaxValue(Connection conn, String tableName, String fieldName) {
|
605
|
int maxValue = 0;
|
606
|
int fieldValue;
|
607
|
String query = "SELECT " + fieldName + " FROM " + tableName;
|
608
|
Statement stmt;
|
609
|
|
610
|
try {
|
611
|
stmt = conn.createStatement();
|
612
|
ResultSet rs = stmt.executeQuery(query);
|
613
|
|
614
|
while (rs.next()) {
|
615
|
fieldValue = rs.getInt(fieldName);
|
616
|
maxValue = Math.max(maxValue, fieldValue);
|
617
|
}
|
618
|
|
619
|
stmt.close();
|
620
|
}
|
621
|
catch (SQLException ex) {
|
622
|
System.out.println("SQLException: " + ex.getMessage());
|
623
|
}
|
624
|
|
625
|
return maxValue;
|
626
|
}
|
627
|
|
628
|
|
629
|
/**
|
630
|
* Gets the siteScheduleID value from the HARVEST_SITE_SCHEDULE table, given
|
631
|
* the value of the ldapDN field.
|
632
|
*
|
633
|
* @param conn the database connection
|
634
|
* @param ldapDN the ldap DN string
|
635
|
* @return siteScheduleID, an integer, the primary key
|
636
|
*/
|
637
|
private int getSiteScheduleID(Connection conn, String ldapDN) {
|
638
|
String ldapDNValue; // value of LDAPDN field
|
639
|
String query = "SELECT * FROM HARVEST_SITE_SCHEDULE";
|
640
|
int siteScheduleID = 0;
|
641
|
Statement stmt;
|
642
|
|
643
|
try {
|
644
|
stmt = conn.createStatement();
|
645
|
ResultSet rs = stmt.executeQuery(query);
|
646
|
|
647
|
while (rs.next()) {
|
648
|
ldapDNValue = rs.getString("LDAPDN");
|
649
|
|
650
|
if (ldapDNValue.equalsIgnoreCase(ldapDN)) {
|
651
|
siteScheduleID = rs.getInt("SITE_SCHEDULE_ID");
|
652
|
}
|
653
|
}
|
654
|
|
655
|
stmt.close();
|
656
|
}
|
657
|
catch (SQLException ex) {
|
658
|
System.out.println("SQLException: " + ex.getMessage());
|
659
|
}
|
660
|
|
661
|
return siteScheduleID;
|
662
|
}
|
663
|
|
664
|
|
665
|
/**
|
666
|
* Initializes the servlet. Reads properties and initializes object fields.
|
667
|
*
|
668
|
* @throws ServletException
|
669
|
*/
|
670
|
public void init() throws ServletException {
|
671
|
String dbDriver;
|
672
|
Properties properties = loadProperties();
|
673
|
|
674
|
dbDriver = properties.getProperty("dbDriver");
|
675
|
password = properties.getProperty("password");
|
676
|
url = properties.getProperty("url");
|
677
|
user = properties.getProperty("user");
|
678
|
|
679
|
// Load the jdbc driver
|
680
|
try {
|
681
|
Class.forName(dbDriver);
|
682
|
}
|
683
|
catch (ClassNotFoundException e) {
|
684
|
System.out.println("Can't load driver " + e);
|
685
|
}
|
686
|
}
|
687
|
|
688
|
|
689
|
/**
|
690
|
* Loads Harvester properties from a configuration file.
|
691
|
*/
|
692
|
private Properties loadProperties() {
|
693
|
InputStream inputStream;
|
694
|
String path = "/harvester/harvester.properties";
|
695
|
Properties properties = new Properties();
|
696
|
ServletContext servletContext = getServletContext();
|
697
|
|
698
|
try {
|
699
|
inputStream = servletContext.getResourceAsStream(path);
|
700
|
properties.load(inputStream);
|
701
|
properties.list(System.out);
|
702
|
}
|
703
|
catch (IOException e) {
|
704
|
System.out.println("IOException: " + e.getMessage());
|
705
|
}
|
706
|
|
707
|
return properties;
|
708
|
}
|
709
|
|
710
|
|
711
|
/**
|
712
|
* Surrounds a string with single quotes.
|
713
|
* @param str the original string
|
714
|
* @return the quoted string
|
715
|
*/
|
716
|
private String quoteString(String str) {
|
717
|
return "'" + str + "'";
|
718
|
}
|
719
|
|
720
|
|
721
|
/**
|
722
|
* Reports the results of an insert or update to the client.
|
723
|
*
|
724
|
* @param out the PrintWriter
|
725
|
* @param ldapDN the LDAP DN string
|
726
|
* @param contactEmail the email address of the site contact
|
727
|
* @param documentListURL the URL of the harvester document list at the site
|
728
|
* @param updateFrequency the harvest update frequency
|
729
|
* @param unit the unit (e.g. "days", "weeks", "months"
|
730
|
* @param dateNextHarvest the date of the next scheduled harvest
|
731
|
*/
|
732
|
private void reportResults(PrintWriter out,
|
733
|
String ldapDN,
|
734
|
String contactEmail,
|
735
|
String documentListURL,
|
736
|
int updateFrequency,
|
737
|
String unit,
|
738
|
String dateNextHarvest
|
739
|
) {
|
740
|
out.println("Harvester registration updated for " + ldapDN);
|
741
|
out.println(" Email Address: " + contactEmail);
|
742
|
out.println(" Harvest Document List URL: " + documentListURL);
|
743
|
out.println(" Harvest Frequency: " + updateFrequency);
|
744
|
out.println(" Unit: " + unit);
|
745
|
out.println("");
|
746
|
out.println("Next scheduled harvest date: " + dateNextHarvest);
|
747
|
}
|
748
|
|
749
|
}
|