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