1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that manages database access of scheduled task parameter
|
4
|
* information. These parameters allow us to maintain the concept of
|
5
|
* a generic job with specific parameters. Note that the methods in
|
6
|
* this class are all protected. They should only be accessed from the
|
7
|
* ScheduledJobAccess class in conjunction with some action being taken
|
8
|
* on a job.
|
9
|
* Copyright: 2009 Regents of the University of California and the
|
10
|
* National Center for Ecological Analysis and Synthesis
|
11
|
* Authors: Michael Daigle
|
12
|
*
|
13
|
* '$Author: daigle $'
|
14
|
* '$Date: 2009-03-23 13:56:56 -0800 (Mon, 23 Mar 2009) $'
|
15
|
* '$Revision: 4854 $'
|
16
|
*
|
17
|
* This program is free software; you can redistribute it and/or modify
|
18
|
* it under the terms of the GNU General Public License as published by
|
19
|
* the Free Software Foundation; either version 2 of the License, or
|
20
|
* (at your option) any later version.
|
21
|
*
|
22
|
* This program is distributed in the hope that it will be useful,
|
23
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
25
|
* GNU General Public License for more details.
|
26
|
*
|
27
|
* You should have received a copy of the GNU General Public License
|
28
|
* along with this program; if not, write to the Free Software
|
29
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
30
|
*/
|
31
|
|
32
|
package edu.ucsb.nceas.metacat.scheduler;
|
33
|
|
34
|
import java.sql.PreparedStatement;
|
35
|
import java.sql.ResultSet;
|
36
|
import java.sql.SQLException;
|
37
|
import java.util.HashMap;
|
38
|
import java.util.Vector;
|
39
|
|
40
|
import org.apache.log4j.Logger;
|
41
|
|
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 ScheduledJobParamAccess extends BaseAccess {
|
48
|
|
49
|
private Logger logMetacat = Logger.getLogger(ScheduledJobParamAccess.class);
|
50
|
|
51
|
// Constructor
|
52
|
public ScheduledJobParamAccess() throws AccessException {
|
53
|
super("ScheduledJobParamsAccess");
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Insert job parameters into the database.
|
58
|
*
|
59
|
* @param jobId
|
60
|
* the id of the job that these parameters belong to
|
61
|
* @param jobParams
|
62
|
* a map of the job parameters
|
63
|
*/
|
64
|
protected void createJobParams(Long jobId, HashMap<String, String> jobParams) throws AccessException {
|
65
|
|
66
|
// iterate through and insert each job parameter
|
67
|
for(String paramKey : jobParams.keySet()) {
|
68
|
ScheduledJobParamDAO jobParamsDAO = new ScheduledJobParamDAO();
|
69
|
jobParamsDAO.setStatus(StatusUtil.ACTIVE);
|
70
|
jobParamsDAO.setJobId(jobId);
|
71
|
jobParamsDAO.setKey(paramKey);
|
72
|
jobParamsDAO.setValue(jobParams.get(paramKey));
|
73
|
|
74
|
PreparedStatement pstmt = null;
|
75
|
|
76
|
try {
|
77
|
String sql =
|
78
|
"INSERT INTO scheduled_job_params (date_created, date_updated, status, job_id, key, value) "
|
79
|
+ "VALUES(now(), now(), ?, ?, ?, ?)";
|
80
|
pstmt = conn.prepareStatement(sql);
|
81
|
|
82
|
pstmt.setString(1, jobParamsDAO.getStatus());
|
83
|
pstmt.setLong(2, jobParamsDAO.getJobId());
|
84
|
pstmt.setString(3, jobParamsDAO.getKey());
|
85
|
pstmt.setString(4, jobParamsDAO.getValue());
|
86
|
|
87
|
logMetacat.info("SQL createJobParams - " + sql);
|
88
|
logMetacat.info("SQL params: [" + jobParamsDAO.getStatus() + ","
|
89
|
+ jobParamsDAO.getJobId() + ","
|
90
|
+ jobParamsDAO.getKey() + ","
|
91
|
+ jobParamsDAO.getValue().toString() + "]");
|
92
|
pstmt.execute();
|
93
|
|
94
|
} catch (SQLException sqle) {
|
95
|
// Just throw the exception. The ScheduledJobAccess class should handle cleanup.
|
96
|
throw new AccessException("ScheduledJobParamsAccess.createJobParams - SQL error when creating scheduled job parameter : "
|
97
|
+ sqle.getMessage());
|
98
|
} finally {
|
99
|
try {
|
100
|
if (pstmt != null) {
|
101
|
pstmt.close();
|
102
|
}
|
103
|
} catch (SQLException sqle) {
|
104
|
logMetacat.error("ScheduledJobParamAccess.createJobParams - An error occurred "
|
105
|
+ "closing prepared statement: " + sqle.getMessage());
|
106
|
} finally {
|
107
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Remove change a job status to deleted in the database.
|
115
|
*
|
116
|
* @param jobId
|
117
|
* the id of the job to update
|
118
|
*/
|
119
|
protected void deleteJobParams(Long jobId) throws AccessException {
|
120
|
PreparedStatement pstmt = null;
|
121
|
|
122
|
// change the status to deleted
|
123
|
try {
|
124
|
String sql = "UPDATE scheduled_job_params SET status = ? WHERE jobId = ?";
|
125
|
pstmt = conn.prepareStatement(sql);
|
126
|
|
127
|
pstmt.setString(1, StatusUtil.DELETED);
|
128
|
pstmt.setLong(1, jobId);
|
129
|
|
130
|
logMetacat.info("ScheduledJobParamsAccess.deleteJobParams - SQL: " + sql);
|
131
|
|
132
|
pstmt.execute();
|
133
|
} catch (SQLException sqle) {
|
134
|
throw new AccessException("ScheduledJobParamsAccess.deleteJobParams - SQL error "
|
135
|
+ "when deleting scheduled job params for job" + jobId + " : " + sqle.getMessage());
|
136
|
} finally {
|
137
|
try {
|
138
|
if (pstmt != null) {
|
139
|
pstmt.close();
|
140
|
}
|
141
|
} catch (SQLException sqle) {
|
142
|
logMetacat.error("ScheduledJobParamsAccess.deleteJobParams - An error occurred "
|
143
|
+ "closing prepared statement: " + sqle.getMessage());
|
144
|
} finally {
|
145
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
146
|
}
|
147
|
}
|
148
|
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Get all the job parameters for a given job id
|
153
|
*
|
154
|
* @param jobId
|
155
|
* the job id whose parameters we want to return
|
156
|
* @return a list of the job parameter data objects
|
157
|
*/
|
158
|
protected Vector<ScheduledJobParamDAO> getJobParamsForJobId(Long jobId) throws AccessException {
|
159
|
Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
|
160
|
|
161
|
// Get the job parameters for the job id
|
162
|
PreparedStatement pstmt = null;
|
163
|
ScheduledJobParamDAO jobParamDAO = null;
|
164
|
try {
|
165
|
String sql = "SELECT * FROM scheduled_job_params WHERE job_id = ? AND status != 'deleted'";
|
166
|
pstmt = conn.prepareStatement(sql);
|
167
|
|
168
|
pstmt.setLong(1, jobId);
|
169
|
|
170
|
logMetacat.info("SQL getJobParamsForJobId - " + sql);
|
171
|
logMetacat.info("SQL params: [" + jobId + "]");
|
172
|
|
173
|
pstmt.execute();
|
174
|
|
175
|
ResultSet resultSet = pstmt.getResultSet();
|
176
|
while (resultSet.next()) {
|
177
|
jobParamDAO = populateDAO(resultSet);
|
178
|
|
179
|
jobParamList.add(jobParamDAO);
|
180
|
}
|
181
|
|
182
|
return jobParamList;
|
183
|
|
184
|
} catch (SQLException sqle) {
|
185
|
throw new AccessException("ScheduledJobAccess.getJobParamsForJobId - SQL error when getting "
|
186
|
+ "scheduled job parameter for job id: " + jobId + " : " + sqle.getMessage());
|
187
|
} finally {
|
188
|
try {
|
189
|
if (pstmt != null) {
|
190
|
pstmt.close();
|
191
|
}
|
192
|
} catch (SQLException sqle) {
|
193
|
logMetacat.error("ScheduledJobParamAccess.getJobParamsForJobId - An error occurred "
|
194
|
+ "closing prepared statement: " + sqle.getMessage());
|
195
|
} finally {
|
196
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
197
|
}
|
198
|
}
|
199
|
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* Get all job parameters
|
204
|
*
|
205
|
* @return a list of all job parameters in the database
|
206
|
*/
|
207
|
protected Vector<ScheduledJobParamDAO> getAllJobParams() throws AccessException {
|
208
|
Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
|
209
|
|
210
|
// get all job parameters
|
211
|
PreparedStatement pstmt = null;
|
212
|
ScheduledJobParamDAO jobParamDAO = null;
|
213
|
try {
|
214
|
String sql = "SELECT * FROM scheduled_job_params WHERE status != 'deleted'";
|
215
|
pstmt = conn.prepareStatement(sql);
|
216
|
|
217
|
logMetacat.info("SQL getAllJobParams - " + sql);
|
218
|
|
219
|
pstmt.execute();
|
220
|
|
221
|
ResultSet resultSet = pstmt.getResultSet();
|
222
|
while (resultSet.next()) {
|
223
|
jobParamDAO = populateDAO(resultSet);
|
224
|
|
225
|
jobParamList.add(jobParamDAO);
|
226
|
}
|
227
|
|
228
|
return jobParamList;
|
229
|
|
230
|
} catch (SQLException sqle) {
|
231
|
throw new AccessException("ScheduledJobParamAccess.getAllJobParams - SQL error when getting "
|
232
|
+ "scheduled job parameters : " + sqle.getMessage());
|
233
|
} finally {
|
234
|
try {
|
235
|
if (pstmt != null) {
|
236
|
pstmt.close();
|
237
|
}
|
238
|
} catch (SQLException sqle) {
|
239
|
logMetacat.error("ScheduledJobParamsAccess.getAllJobParams - An error occurred "
|
240
|
+ "closing prepared statement: " + sqle.getMessage());
|
241
|
} finally {
|
242
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
243
|
}
|
244
|
}
|
245
|
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* Populate a job parameter data object with the current row in a resultset
|
250
|
*
|
251
|
* @param resultSet
|
252
|
* the result set which is already pointing to the desired row.
|
253
|
* @return a scheduled job data parameter object
|
254
|
*/
|
255
|
protected ScheduledJobParamDAO populateDAO(ResultSet resultSet) throws SQLException {
|
256
|
|
257
|
ScheduledJobParamDAO jobParamDAO = new ScheduledJobParamDAO();
|
258
|
jobParamDAO.setId(resultSet.getLong("id"));
|
259
|
jobParamDAO.setCreateTime(resultSet.getTimestamp("date_created"));
|
260
|
jobParamDAO.setModTime(resultSet.getTimestamp("date_updated"));
|
261
|
jobParamDAO.setStatus(resultSet.getString("status"));
|
262
|
jobParamDAO.setJobId(resultSet.getLong("job_id"));
|
263
|
jobParamDAO.setKey(resultSet.getString("key"));
|
264
|
jobParamDAO.setValue(resultSet.getString("value"));
|
265
|
|
266
|
return jobParamDAO;
|
267
|
}
|
268
|
}
|