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