1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that manages database access of scheduled task
|
4
|
* information.
|
5
|
* Copyright: 2009 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Michael Daigle
|
8
|
*
|
9
|
* '$Author: daigle $'
|
10
|
* '$Date: 2009-03-23 13:56:56 -0800 (Mon, 23 Mar 2009) $'
|
11
|
* '$Revision: 4854 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacat.scheduler;
|
29
|
|
30
|
import java.sql.PreparedStatement;
|
31
|
import java.sql.ResultSet;
|
32
|
import java.sql.SQLException;
|
33
|
import java.sql.Timestamp;
|
34
|
import java.util.Calendar;
|
35
|
import java.util.HashMap;
|
36
|
import java.util.Vector;
|
37
|
|
38
|
import org.apache.log4j.Logger;
|
39
|
import org.quartz.Job;
|
40
|
|
41
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
42
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
43
|
import edu.ucsb.nceas.metacat.shared.AccessException;
|
44
|
import edu.ucsb.nceas.metacat.shared.BaseAccess;
|
45
|
import edu.ucsb.nceas.utilities.StatusUtil;
|
46
|
|
47
|
public class ScheduledJobAccess extends BaseAccess {
|
48
|
|
49
|
private Logger logMetacat = Logger.getLogger(ScheduledJobAccess.class);
|
50
|
|
51
|
// Constructor
|
52
|
public ScheduledJobAccess() throws AccessException {}
|
53
|
|
54
|
/**
|
55
|
* Get a job based on it's id
|
56
|
*
|
57
|
* @param jobId
|
58
|
* the id of the job in the database
|
59
|
* @return the scheduled job data object that represents the desired job
|
60
|
*/
|
61
|
public ScheduledJobDAO getJob(Long jobId) throws AccessException {
|
62
|
ScheduledJobDAO jobDAO = null;
|
63
|
|
64
|
// first get the job from the db and put it into a DAO
|
65
|
PreparedStatement pstmt = null;
|
66
|
DBConnection conn = null;
|
67
|
int serialNumber = -1;
|
68
|
try {
|
69
|
conn = DBConnectionPool.getDBConnection("ScheduledJobAccess.getJob");
|
70
|
serialNumber = conn.getCheckOutSerialNumber();
|
71
|
|
72
|
String sql = "SELECT * FROM scheduled_job WHERE id = ? AND status != 'deleted'";
|
73
|
pstmt = conn.prepareStatement(sql);
|
74
|
|
75
|
pstmt.setLong(1, jobId);
|
76
|
|
77
|
String sqlReport = "ScheduledJobAccess.getJob - SQL: " + sql;
|
78
|
sqlReport += " [" + jobId + "]";
|
79
|
|
80
|
logMetacat.info(sqlReport);
|
81
|
|
82
|
pstmt.execute();
|
83
|
|
84
|
ResultSet resultSet = pstmt.getResultSet();
|
85
|
if (resultSet.next()) {
|
86
|
jobDAO = populateDAO(resultSet);
|
87
|
}
|
88
|
|
89
|
} catch (SQLException sqle) {
|
90
|
throw new AccessException("ScheduledJobAccess.getJob - SQL error when getting scheduled job: "
|
91
|
+ jobId + " : " + sqle.getMessage());
|
92
|
} finally {
|
93
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
94
|
}
|
95
|
|
96
|
// Now get all the job parameters and put those into a list of job parameter
|
97
|
// DAOs and add that list to the job DAO
|
98
|
ScheduledJobParamAccess jobParamAccess = new ScheduledJobParamAccess();
|
99
|
Vector<ScheduledJobParamDAO> jobParamList =
|
100
|
jobParamAccess.getJobParamsForJobId(jobDAO.getId());
|
101
|
|
102
|
for(ScheduledJobParamDAO jobParamDAO : jobParamList) {
|
103
|
jobDAO.addJobParam(jobParamDAO);
|
104
|
}
|
105
|
|
106
|
return jobDAO;
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Get a job by it's name
|
111
|
* @param jobName the name of the job to get
|
112
|
* @return the scheduled job data object that represents the desired job
|
113
|
*/
|
114
|
public ScheduledJobDAO getJobByName(String jobName) throws AccessException {
|
115
|
ScheduledJobDAO jobDAO = null;
|
116
|
|
117
|
// first get the job from the db and put it into a DAO
|
118
|
PreparedStatement pstmt = null;
|
119
|
DBConnection conn = null;
|
120
|
int serialNumber = -1;
|
121
|
try {
|
122
|
conn = DBConnectionPool.getDBConnection("ScheduledJobAccess.getJobByName");
|
123
|
serialNumber = conn.getCheckOutSerialNumber();
|
124
|
|
125
|
String sql = "SELECT * FROM scheduled_job WHERE name = ? AND status != 'deleted'";
|
126
|
pstmt = conn.prepareStatement(sql);
|
127
|
|
128
|
pstmt.setString(1, jobName);
|
129
|
|
130
|
String sqlReport = "ScheduledJobAccess.getJobByName - SQL: " + sql;
|
131
|
sqlReport += " [" + jobName + "]";
|
132
|
|
133
|
logMetacat.info(sqlReport);
|
134
|
|
135
|
pstmt.execute();
|
136
|
|
137
|
ResultSet resultSet = pstmt.getResultSet();
|
138
|
if (resultSet.next()) {
|
139
|
jobDAO = populateDAO(resultSet);
|
140
|
} else {
|
141
|
throw new AccessException("ScheduledJobAccess.getJobByName - could not find scheduled job with name: "
|
142
|
+ jobName);
|
143
|
}
|
144
|
|
145
|
} catch (SQLException sqle) {
|
146
|
throw new AccessException("ScheduledJobAccess.getJobByName - SQL error when getting scheduled job by name: "
|
147
|
+ jobName + " : " + sqle.getMessage());
|
148
|
} finally {
|
149
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
150
|
}
|
151
|
|
152
|
// Now get all the job parameters and put those into a list of job parameter
|
153
|
// DAOs and add that list to the job DAO
|
154
|
ScheduledJobParamAccess jobParamAccess = new ScheduledJobParamAccess();
|
155
|
Vector<ScheduledJobParamDAO> jobParamList =
|
156
|
jobParamAccess.getJobParamsForJobId(jobDAO.getId());
|
157
|
|
158
|
for(ScheduledJobParamDAO jobParamDAO : jobParamList) {
|
159
|
jobDAO.addJobParam(jobParamDAO);
|
160
|
}
|
161
|
|
162
|
return jobDAO;
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* Get all jobs that have a given parameter with a given value
|
167
|
*
|
168
|
* @param groupName
|
169
|
* the group to which the job belongs. This keeps us from
|
170
|
* returning unrelated jobs that just happen to have a similar
|
171
|
* parameter
|
172
|
* @param paramName
|
173
|
* the name of the parameter we are looking for
|
174
|
* @param paramValue
|
175
|
* the value of the parameter we are looking for
|
176
|
* @return a HashMap of job data objects with all jobs in a given group that
|
177
|
* have parameters that match our parameter.
|
178
|
*/
|
179
|
public HashMap<Long, ScheduledJobDAO> getJobsWithParameter(String groupName, String paramName, String paramValue) throws AccessException {
|
180
|
|
181
|
// first get all jobs
|
182
|
HashMap<Long, ScheduledJobDAO> allJobsMap = getAllJobs(groupName);
|
183
|
HashMap<Long, ScheduledJobDAO> jobsWithParamMap = new HashMap<Long, ScheduledJobDAO>();
|
184
|
|
185
|
// then iterate through and grab the ones that have the desired parameter
|
186
|
for (Long jobDAOId : allJobsMap.keySet()) {
|
187
|
ScheduledJobDAO jobDAO = allJobsMap.get(jobDAOId);
|
188
|
if (paramValue != null && paramName != null) {
|
189
|
ScheduledJobParamDAO jobParamDAO = jobDAO.getJobParam(paramName);
|
190
|
if(jobParamDAO != null && jobParamDAO.getValue().equals(paramValue)) {
|
191
|
jobsWithParamMap.put(jobDAOId, jobDAO);
|
192
|
}
|
193
|
}
|
194
|
}
|
195
|
|
196
|
return jobsWithParamMap;
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* Get all jobs for a given group. A group is typically a category that coincides with
|
201
|
* the job type (has it's own job implementation).
|
202
|
* @param groupName the name of the group we want to search
|
203
|
* @return a HashMap of job data objects for the desired group.
|
204
|
*/
|
205
|
public HashMap<Long, ScheduledJobDAO> getAllJobs(String groupName) throws AccessException {
|
206
|
ScheduledJobDAO jobDAO = null;
|
207
|
|
208
|
HashMap<Long, ScheduledJobDAO> allJobDAOs = new HashMap<Long, ScheduledJobDAO>();
|
209
|
|
210
|
// Get all jobs where the status is not deleted
|
211
|
PreparedStatement pstmt = null;
|
212
|
DBConnection conn = null;
|
213
|
int serialNumber = -1;
|
214
|
try {
|
215
|
conn = DBConnectionPool.getDBConnection("ScheduledJobAccess.getAllJobs");
|
216
|
serialNumber = conn.getCheckOutSerialNumber();
|
217
|
|
218
|
String sql = "SELECT * FROM scheduled_job WHERE status != 'deleted'";
|
219
|
if (groupName != null) {
|
220
|
sql += " AND group_name = ?" ;
|
221
|
}
|
222
|
pstmt = conn.prepareStatement(sql);
|
223
|
|
224
|
String sqlReport = "ScheduledJobAccess.getAllJobs - SQL: " + sql;
|
225
|
if (groupName != null) {
|
226
|
pstmt.setString(1, groupName);
|
227
|
|
228
|
sqlReport += " [" + groupName + "]";
|
229
|
}
|
230
|
|
231
|
logMetacat.info(sqlReport);
|
232
|
|
233
|
pstmt.execute();
|
234
|
|
235
|
ResultSet resultSet = pstmt.getResultSet();
|
236
|
while (resultSet.next()) {
|
237
|
jobDAO = populateDAO(resultSet);
|
238
|
|
239
|
allJobDAOs.put(jobDAO.getId(), jobDAO);
|
240
|
}
|
241
|
|
242
|
// Here we grab all job params and put them into the associated jobs. THis
|
243
|
// takes a little stress off the database by avoiding a join.
|
244
|
ScheduledJobParamAccess jobParamAccess = new ScheduledJobParamAccess();
|
245
|
Vector<ScheduledJobParamDAO> jobParamList =
|
246
|
jobParamAccess.getAllJobParams();
|
247
|
|
248
|
for(ScheduledJobParamDAO jobParamDAO : jobParamList) {
|
249
|
Long jobId = jobParamDAO.getJobId();
|
250
|
if( allJobDAOs.containsKey(jobId)) {
|
251
|
allJobDAOs.get(jobId).addJobParam(jobParamDAO);
|
252
|
}
|
253
|
}
|
254
|
|
255
|
return allJobDAOs;
|
256
|
|
257
|
} catch (SQLException sqle) {
|
258
|
throw new AccessException("ScheduledJobAccess.getJobByName - SQL error when getting all jobs : "
|
259
|
+ sqle.getMessage());
|
260
|
} finally {
|
261
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
262
|
}
|
263
|
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* Create a job in the database.
|
268
|
*
|
269
|
* @param name
|
270
|
* the name of the job. This should be unique
|
271
|
* @param triggerName
|
272
|
* the name of the trigger that is registered in the scheduler
|
273
|
* service
|
274
|
* @param groupName
|
275
|
* the group of the job
|
276
|
* @param jobClass
|
277
|
* the class that implements the job functionality. The name of
|
278
|
* this class will be extracted and put into the database.
|
279
|
* @param startTime
|
280
|
* the time when the job should first run
|
281
|
* @param intervalValue
|
282
|
* the amount of time between job runs.
|
283
|
* @param intervalUnit
|
284
|
* the unit of time that the intervalValue represents. Valid
|
285
|
* values are s,m,h, d and w
|
286
|
* @param jobParams
|
287
|
* a map of parameters that are associated with this job.
|
288
|
*/
|
289
|
public void createJob(String name, String triggerName, String groupName,
|
290
|
Class<Job> jobClass, Calendar startTime, Calendar endTime, int intervalValue,
|
291
|
String intervalUnit, HashMap<String, String> jobParams)
|
292
|
throws AccessException {
|
293
|
|
294
|
// Create and populate a job data object
|
295
|
ScheduledJobDAO jobDAO = new ScheduledJobDAO();
|
296
|
jobDAO.setStatus(StatusUtil.SCHEDULED);
|
297
|
jobDAO.setName(name);
|
298
|
jobDAO.setTriggerName(name);
|
299
|
jobDAO.setGroupName(groupName);
|
300
|
jobDAO.setClassName(jobClass.getName());
|
301
|
jobDAO.setStartTime(new Timestamp(startTime.getTimeInMillis()));
|
302
|
jobDAO.setEndTime(new Timestamp(endTime.getTimeInMillis()));
|
303
|
jobDAO.setIntervalValue(intervalValue);
|
304
|
jobDAO.setIntervalUnit(intervalUnit);
|
305
|
|
306
|
createJob(jobDAO, jobParams);
|
307
|
}
|
308
|
|
309
|
/**
|
310
|
* Create a job in the database
|
311
|
* @param jobDAO the job data object that holds necessary information
|
312
|
* @param jobParams a map of parameters that are associated with this job.
|
313
|
*/
|
314
|
public void createJob(ScheduledJobDAO jobDAO, HashMap<String, String> jobParams) throws AccessException {
|
315
|
PreparedStatement pstmt = null;
|
316
|
DBConnection conn = null;
|
317
|
int serialNumber = -1;
|
318
|
|
319
|
// First insert the job
|
320
|
try {
|
321
|
conn = DBConnectionPool.getDBConnection("ScheduledJobAccess.createJob");
|
322
|
serialNumber = conn.getCheckOutSerialNumber();
|
323
|
|
324
|
String sql =
|
325
|
"INSERT INTO scheduled_job (date_created, date_updated, status, name, trigger_name, group_name, class_name, start_time, end_time, interval_value, interval_unit) "
|
326
|
+ "VALUES(now(), now(), ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
327
|
pstmt = conn.prepareStatement(sql);
|
328
|
|
329
|
pstmt.setString(1, jobDAO.getStatus());
|
330
|
pstmt.setString(2, jobDAO.getName());
|
331
|
pstmt.setString(3, jobDAO.getTriggerName());
|
332
|
pstmt.setString(4, jobDAO.getGroupName());
|
333
|
pstmt.setString(5, jobDAO.getClassName());
|
334
|
pstmt.setTimestamp(6, jobDAO.getStartTime());
|
335
|
pstmt.setTimestamp(7, jobDAO.getEndTime());
|
336
|
pstmt.setInt(8, jobDAO.getIntervalValue());
|
337
|
pstmt.setString(9, jobDAO.getIntervalUnit());
|
338
|
|
339
|
String sqlReport = "ScheduledJobAccess.createJob - SQL: " + sql
|
340
|
+ " [" + jobDAO.getStatus() + ","
|
341
|
+ jobDAO.getName() + ","
|
342
|
+ jobDAO.getTriggerName() + ","
|
343
|
+ jobDAO.getGroupName() + ","
|
344
|
+ jobDAO.getClassName() + ",(Timestamp)"
|
345
|
+ jobDAO.getStartTime().toString() + ",";
|
346
|
if(jobDAO.getEndTime() == null) {
|
347
|
sqlReport += "null,";
|
348
|
} else {
|
349
|
sqlReport += jobDAO.getEndTime().toString() + ",";
|
350
|
}
|
351
|
sqlReport += jobDAO.getIntervalValue() + ","
|
352
|
+ jobDAO.getIntervalUnit() + "]";
|
353
|
logMetacat.info(sqlReport);
|
354
|
|
355
|
pstmt.execute();
|
356
|
|
357
|
} catch (SQLException sqle) {
|
358
|
throw new AccessException("ScheduledJobAccess.createJob - SQL error when creating scheduled job "
|
359
|
+ jobDAO.getName() + " : " + sqle.getMessage());
|
360
|
} finally {
|
361
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
362
|
}
|
363
|
|
364
|
// Then iterate through the job params and insert them into the db
|
365
|
if (jobParams.size() > 0) {
|
366
|
ScheduledJobParamAccess scheduledJobParamsAccess = null;
|
367
|
ScheduledJobDAO updatedJobDAO = null;
|
368
|
try {
|
369
|
updatedJobDAO = getJobByName(jobDAO.getName());
|
370
|
scheduledJobParamsAccess = new ScheduledJobParamAccess();
|
371
|
scheduledJobParamsAccess.createJobParams(updatedJobDAO.getId(), jobParams);
|
372
|
} catch (AccessException ae) {
|
373
|
if (updatedJobDAO != null) {
|
374
|
updatedJobDAO.setStatus(StatusUtil.DELETED);
|
375
|
updateJobStatus(updatedJobDAO);
|
376
|
scheduledJobParamsAccess.deleteJobParams(updatedJobDAO.getId());
|
377
|
} else {
|
378
|
logMetacat.warn("ScheduledJobAccess.createJob - Tried to delete non-existant scheduled job: "
|
379
|
+ jobDAO.getName());
|
380
|
}
|
381
|
}
|
382
|
}
|
383
|
}
|
384
|
|
385
|
/**
|
386
|
* Update the status of a job in the database
|
387
|
*
|
388
|
* @param jobDAO
|
389
|
* the job data object that we want to change. The status should
|
390
|
* already have been updated in this object
|
391
|
*/
|
392
|
public void updateJobStatus(ScheduledJobDAO jobDAO) throws AccessException {
|
393
|
|
394
|
if (jobDAO == null) {
|
395
|
throw new AccessException("ScheduledJobAccess.updateJobStatus - job DAO cannot be null.");
|
396
|
}
|
397
|
|
398
|
PreparedStatement pstmt = null;
|
399
|
DBConnection conn = null;
|
400
|
int serialNumber = -1;
|
401
|
|
402
|
try {
|
403
|
conn = DBConnectionPool.getDBConnection("ScheduledJobAccess.createJob");
|
404
|
serialNumber = conn.getCheckOutSerialNumber();
|
405
|
|
406
|
String sql = "UPDATE scheduled_job SET status = ? WHERE id = ?";
|
407
|
|
408
|
pstmt = conn.prepareStatement(sql);
|
409
|
pstmt.setString(1, jobDAO.getStatus());
|
410
|
pstmt.setLong(2, jobDAO.getId());
|
411
|
|
412
|
String sqlReport = "ScheduledJobAccess.deleteJob - SQL: " + sql
|
413
|
+ " [" + jobDAO.getStatus() + "," + jobDAO.getId() + "]";
|
414
|
logMetacat.info(sqlReport);
|
415
|
|
416
|
pstmt.execute();
|
417
|
} catch (SQLException sqle) {
|
418
|
throw new AccessException("ScheduledJobAccess.deleteJob - SQL error when "
|
419
|
+ "deleting scheduled job " + jobDAO.getName() + " : " + sqle.getMessage());
|
420
|
} finally {
|
421
|
closeDBObjects(pstmt, conn, serialNumber, logMetacat);
|
422
|
}
|
423
|
|
424
|
}
|
425
|
|
426
|
/**
|
427
|
* Populate a job data object with the current row in a resultset
|
428
|
*
|
429
|
* @param resultSet
|
430
|
* the result set which is already pointing to the desired row.
|
431
|
* @return a scheduled job data object
|
432
|
*/
|
433
|
protected ScheduledJobDAO populateDAO(ResultSet resultSet) throws SQLException {
|
434
|
|
435
|
ScheduledJobDAO jobDAO = new ScheduledJobDAO();
|
436
|
jobDAO.setId(resultSet.getLong("id"));
|
437
|
jobDAO.setCreateTime(resultSet.getTimestamp("date_created"));
|
438
|
jobDAO.setModTime(resultSet.getTimestamp("date_updated"));
|
439
|
jobDAO.setStatus(resultSet.getString("status"));
|
440
|
jobDAO.setName(resultSet.getString("name"));
|
441
|
jobDAO.setTriggerName(resultSet.getString("trigger_name"));
|
442
|
jobDAO.setGroupName(resultSet.getString("group_name"));
|
443
|
jobDAO.setClassName(resultSet.getString("class_name"));
|
444
|
jobDAO.setStartTime(resultSet.getTimestamp("start_time"));
|
445
|
jobDAO.setEndTime(resultSet.getTimestamp("end_time"));
|
446
|
jobDAO.setIntervalValue(resultSet.getInt("interval_value"));
|
447
|
jobDAO.setIntervalUnit(resultSet.getString("interval_unit"));
|
448
|
|
449
|
return jobDAO;
|
450
|
}
|
451
|
|
452
|
}
|