Revision 4971
Added by daigle about 15 years ago
ScheduledJobAccess.java | ||
---|---|---|
47 | 47 |
|
48 | 48 |
private Logger logMetacat = Logger.getLogger(ScheduledJobAccess.class); |
49 | 49 |
|
50 |
// Constructor |
|
50 | 51 |
public ScheduledJobAccess() throws AccessException { |
51 | 52 |
super("ScheduledJobAccess"); |
52 | 53 |
} |
53 | 54 |
|
55 |
/** |
|
56 |
* Get a job based on it's id |
|
57 |
* |
|
58 |
* @param jobId |
|
59 |
* the id of the job in the database |
|
60 |
* @return the scheduled job data object that represents the desired job |
|
61 |
*/ |
|
54 | 62 |
public ScheduledJobDAO getJob(Long jobId) throws AccessException { |
55 | 63 |
ScheduledJobDAO jobDAO = null; |
56 | 64 |
|
65 |
// first get the job from the db and put it into a DAO |
|
57 | 66 |
PreparedStatement pstmt = null; |
58 | 67 |
try { |
59 | 68 |
String sql = "SELECT * FROM scheduled_job WHERE id = ? AND status != 'deleted'"; |
... | ... | |
85 | 94 |
} |
86 | 95 |
} |
87 | 96 |
|
97 |
// Now get all the job parameters and put those into a list of job parameter |
|
98 |
// DAOs and add that list to the job DAO |
|
88 | 99 |
ScheduledJobParamAccess jobParamAccess = new ScheduledJobParamAccess(); |
89 | 100 |
Vector<ScheduledJobParamDAO> jobParamList = |
90 | 101 |
jobParamAccess.getJobParamsForJobId(jobDAO.getId()); |
... | ... | |
96 | 107 |
return jobDAO; |
97 | 108 |
} |
98 | 109 |
|
110 |
/** |
|
111 |
* Get a job by it's name |
|
112 |
* @param jobName the name of the job to get |
|
113 |
* @return the scheduled job data object that represents the desired job |
|
114 |
*/ |
|
99 | 115 |
public ScheduledJobDAO getJobByName(String jobName) throws AccessException { |
100 | 116 |
ScheduledJobDAO jobDAO = null; |
101 | 117 |
|
118 |
// first get the job from the db and put it into a DAO |
|
102 | 119 |
PreparedStatement pstmt = null; |
103 | 120 |
try { |
104 | 121 |
String sql = "SELECT * FROM scheduled_job WHERE name = ? AND status != 'deleted'"; |
... | ... | |
130 | 147 |
} |
131 | 148 |
} |
132 | 149 |
|
150 |
// Now get all the job parameters and put those into a list of job parameter |
|
151 |
// DAOs and add that list to the job DAO |
|
133 | 152 |
ScheduledJobParamAccess jobParamAccess = new ScheduledJobParamAccess(); |
134 | 153 |
Vector<ScheduledJobParamDAO> jobParamList = |
135 | 154 |
jobParamAccess.getJobParamsForJobId(jobDAO.getId()); |
... | ... | |
141 | 160 |
return jobDAO; |
142 | 161 |
} |
143 | 162 |
|
163 |
/** |
|
164 |
* Get all jobs that have a given parameter with a given value |
|
165 |
* |
|
166 |
* @param groupName |
|
167 |
* the group to which the job belongs. This keeps us from |
|
168 |
* returning unrelated jobs that just happen to have a similar |
|
169 |
* parameter |
|
170 |
* @param paramName |
|
171 |
* the name of the parameter we are looking for |
|
172 |
* @param paramValue |
|
173 |
* the value of the parameter we are looking for |
|
174 |
* @return a HashMap of job data objects with all jobs in a given group that |
|
175 |
* have parameters that match our parameter. |
|
176 |
*/ |
|
144 | 177 |
public HashMap<Long, ScheduledJobDAO> getJobsWithParameter(String groupName, String paramName, String paramValue) throws AccessException { |
145 | 178 |
|
179 |
// first get all jobs |
|
146 | 180 |
HashMap<Long, ScheduledJobDAO> allJobsMap = getAllJobs(groupName); |
147 | 181 |
HashMap<Long, ScheduledJobDAO> jobsWithParamMap = new HashMap<Long, ScheduledJobDAO>(); |
148 | 182 |
|
183 |
// then iterate through and grab the ones that have the desired parameter |
|
149 | 184 |
for (Long jobDAOId : allJobsMap.keySet()) { |
150 | 185 |
ScheduledJobDAO jobDAO = allJobsMap.get(jobDAOId); |
151 | 186 |
if (paramValue != null && paramName != null) { |
... | ... | |
159 | 194 |
return jobsWithParamMap; |
160 | 195 |
} |
161 | 196 |
|
197 |
/** |
|
198 |
* Get all jobs for a given group. A group is typically a category that coincides with |
|
199 |
* the job type (has it's own job implementation). |
|
200 |
* @param groupName the name of the group we want to search |
|
201 |
* @return a HashMap of job data objects for the desired group. |
|
202 |
*/ |
|
162 | 203 |
public HashMap<Long, ScheduledJobDAO> getAllJobs(String groupName) throws AccessException { |
163 | 204 |
ScheduledJobDAO jobDAO = null; |
164 | 205 |
|
165 | 206 |
HashMap<Long, ScheduledJobDAO> allJobDAOs = new HashMap<Long, ScheduledJobDAO>(); |
166 | 207 |
|
208 |
// Get all jobs where the status is not deleted |
|
167 | 209 |
PreparedStatement pstmt = null; |
168 | 210 |
try { |
169 | 211 |
String sql = "SELECT * FROM scheduled_job WHERE status != 'deleted'"; |
... | ... | |
188 | 230 |
allJobDAOs.put(jobDAO.getId(), jobDAO); |
189 | 231 |
} |
190 | 232 |
|
233 |
// Here we grab all job params and put them into the associated jobs. THis |
|
234 |
// takes a little stress off the database by avoiding a join. |
|
191 | 235 |
ScheduledJobParamAccess jobParamAccess = new ScheduledJobParamAccess(); |
192 | 236 |
Vector<ScheduledJobParamDAO> jobParamList = |
193 | 237 |
jobParamAccess.getAllJobParams(); |
... | ... | |
221 | 265 |
|
222 | 266 |
} |
223 | 267 |
|
268 |
/** |
|
269 |
* Create a job in the database. |
|
270 |
* |
|
271 |
* @param name |
|
272 |
* the name of the job. This should be unique |
|
273 |
* @param triggerName |
|
274 |
* the name of the trigger that is registered in the scheduler |
|
275 |
* service |
|
276 |
* @param groupName |
|
277 |
* the group of the job |
|
278 |
* @param jobClass |
|
279 |
* the class that implements the job functionality. The name of |
|
280 |
* this class will be extracted and put into the database. |
|
281 |
* @param startTime |
|
282 |
* the time when the job should first run |
|
283 |
* @param intervalValue |
|
284 |
* the amount of time between job runs. |
|
285 |
* @param intervalUnit |
|
286 |
* the unit of time that the intervalValue represents. Valid |
|
287 |
* values are s,m,h, d and w |
|
288 |
* @param jobParams |
|
289 |
* a map of parameters that are associated with this job. |
|
290 |
*/ |
|
224 | 291 |
public void createJob(String name, String triggerName, String groupName, |
225 | 292 |
Class<Job> jobClass, Calendar startTime, int intervalValue, |
226 | 293 |
String intervalUnit, HashMap<String, String> jobParams) |
227 | 294 |
throws AccessException { |
228 | 295 |
|
296 |
// Create and populate a job data object |
|
229 | 297 |
ScheduledJobDAO jobDAO = new ScheduledJobDAO(); |
230 | 298 |
jobDAO.setStatus(StatusUtil.SCHEDULED); |
231 | 299 |
jobDAO.setName(name); |
... | ... | |
238 | 306 |
|
239 | 307 |
createJob(jobDAO, jobParams); |
240 | 308 |
} |
241 |
|
|
309 |
|
|
310 |
/** |
|
311 |
* Create a job in the database |
|
312 |
* @param jobDAO the job data object that holds necessary information |
|
313 |
* @param jobParams a map of parameters that are associated with this job. |
|
314 |
*/ |
|
242 | 315 |
public void createJob(ScheduledJobDAO jobDAO, HashMap<String, String> jobParams) throws AccessException { |
243 | 316 |
PreparedStatement pstmt = null; |
244 | 317 |
|
318 |
// First insert the job |
|
245 | 319 |
try { |
246 | 320 |
String sql = |
247 | 321 |
"INSERT INTO scheduled_job (date_created, date_updated, status, name, trigger_name, group_name, class_name, start_time, interval_value, interval_unit) " |
... | ... | |
284 | 358 |
} |
285 | 359 |
} |
286 | 360 |
|
361 |
// Then iterate through the job params and insert them into the db |
|
287 | 362 |
if (jobParams.size() > 0) { |
288 | 363 |
ScheduledJobParamAccess scheduledJobParamsAccess = null; |
289 | 364 |
ScheduledJobDAO updatedJobDAO = null; |
... | ... | |
304 | 379 |
} |
305 | 380 |
} |
306 | 381 |
|
382 |
/** |
|
383 |
* Update the status of a job in the database |
|
384 |
* |
|
385 |
* @param jobDAO |
|
386 |
* the job data object that we want to change. The status should |
|
387 |
* already have been updated in this object |
|
388 |
*/ |
|
307 | 389 |
public void updateJobStatus(ScheduledJobDAO jobDAO) throws AccessException { |
308 | 390 |
|
309 | 391 |
if (jobDAO == null) { |
... | ... | |
343 | 425 |
|
344 | 426 |
} |
345 | 427 |
|
428 |
/** |
|
429 |
* Populate a job data object with the current row in a resultset |
|
430 |
* |
|
431 |
* @param resultSet |
|
432 |
* the result set which is already pointing to the desired row. |
|
433 |
* @return a scheduled job data object |
|
434 |
*/ |
|
346 | 435 |
protected ScheduledJobDAO populateDAO(ResultSet resultSet) throws SQLException { |
347 | 436 |
|
348 | 437 |
ScheduledJobDAO jobDAO = new ScheduledJobDAO(); |
Also available in: Unified diff
Beef up comments