Project

General

Profile

1 4951 daigle
/**
2
 *  '$RCSfile$'
3 4971 daigle
 *    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 4951 daigle
 *  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 4959 daigle
import java.sql.ResultSet;
36 4951 daigle
import java.sql.SQLException;
37 4959 daigle
import java.util.HashMap;
38
import java.util.Vector;
39 4951 daigle
40
import org.apache.log4j.Logger;
41
42 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
43
import edu.ucsb.nceas.metacat.shared.AccessException;
44
import edu.ucsb.nceas.metacat.shared.BaseAccess;
45 4959 daigle
import edu.ucsb.nceas.utilities.StatusUtil;
46 4951 daigle
47 4959 daigle
public class ScheduledJobParamAccess extends BaseAccess {
48 4951 daigle
49 4959 daigle
	private Logger logMetacat = Logger.getLogger(ScheduledJobParamAccess.class);
50 4951 daigle
51 4971 daigle
	// Constructor
52 4959 daigle
	public ScheduledJobParamAccess() throws AccessException {
53 4951 daigle
		super("ScheduledJobParamsAccess");
54
	}
55
56 4971 daigle
	/**
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 4959 daigle
	protected void createJobParams(Long jobId, HashMap<String, String> jobParams) throws AccessException {
65 4951 daigle
66 4971 daigle
		// iterate through and insert each job parameter
67 4959 daigle
		for(String paramKey : jobParams.keySet()) {
68
			ScheduledJobParamDAO jobParamsDAO = new ScheduledJobParamDAO();
69
			jobParamsDAO.setStatus(StatusUtil.ACTIVE);
70 4951 daigle
			jobParamsDAO.setJobId(jobId);
71
			jobParamsDAO.setKey(paramKey);
72
			jobParamsDAO.setValue(jobParams.get(paramKey));
73
74 4959 daigle
			PreparedStatement pstmt = null;
75
76 4951 daigle
			try {
77
				String sql =
78
					"INSERT INTO scheduled_job_params (date_created, date_updated, status, job_id, key, value) "
79
					+ "VALUES(now(), now(), ?, ?, ?, ?)";
80 4959 daigle
				pstmt = conn.prepareStatement(sql);
81 4951 daigle
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 4959 daigle
			} finally {
99
				try {
100
					if (pstmt != null) {
101
						pstmt.close();
102
					}
103
				} catch (SQLException sqle) {
104
					logMetacat.error("ScheduledJobParamsAccess.createJobParams - An error occurred "
105
							+ "closing prepared statement: " + sqle.getMessage());
106
				} finally {
107
					DBConnectionPool.returnDBConnection(conn, serialNumber);
108
				}
109
			}
110 4951 daigle
		}
111
	}
112
113 4971 daigle
	/**
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 4951 daigle
	protected void deleteJobParams(Long jobId) throws AccessException {
120 4959 daigle
		PreparedStatement pstmt = null;
121
122 4971 daigle
		// change the status to deleted
123 4951 daigle
		try {
124
			String sql = "UPDATE scheduled_job_params SET status = ? WHERE jobId = ?";
125 4959 daigle
			pstmt.setString(1, StatusUtil.DELETED);
126 4951 daigle
			pstmt.setLong(1, jobId);
127
128
			logMetacat.info("SQL deleteJobParams - " + sql);
129
130
			pstmt.execute();
131
		} catch (SQLException sqle) {
132
			throw new AccessException("ScheduledJobParamsAccess.deleteJobParams - SQL error "
133
					+ "when deleting scheduled job params for job" + jobId  + " : "  + sqle.getMessage());
134 4959 daigle
		} finally {
135
			try {
136
				if (pstmt != null) {
137
					pstmt.close();
138
				}
139
			} catch (SQLException sqle) {
140
				logMetacat.error("ScheduledJobParamsAccess.createJobParams - An error occurred "
141
						+ "closing prepared statement: " + sqle.getMessage());
142
			} finally {
143
				DBConnectionPool.returnDBConnection(conn, serialNumber);
144
			}
145 4951 daigle
		}
146
147
	}
148 4959 daigle
149 4971 daigle
	/**
150
	 * Get all the job parameters for a given job id
151
	 *
152
	 * @param jobId
153
	 *            the job id whose parameters we want to return
154
	 * @return a list of the job parameter data objects
155
	 */
156
	protected Vector<ScheduledJobParamDAO> getJobParamsForJobId(Long jobId) throws AccessException {
157 4959 daigle
		Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
158
159 4971 daigle
		// Get the job parameters for the job id
160 4959 daigle
		PreparedStatement pstmt = null;
161
		ScheduledJobParamDAO jobParamDAO = null;
162
		try {
163 4967 daigle
			String sql = "SELECT * FROM scheduled_job_params WHERE job_id = ? AND status != 'deleted'";
164 4959 daigle
			pstmt = conn.prepareStatement(sql);
165
166
			pstmt.setLong(1, jobId);
167
168
			logMetacat.info("SQL getJobParamsForJobId - " + sql);
169
			logMetacat.info("SQL params: [" + jobId + "]");
170
171
			pstmt.execute();
172
173
			ResultSet resultSet = pstmt.getResultSet();
174
			while (resultSet.next()) {
175
				jobParamDAO = populateDAO(resultSet);
176
177
				jobParamList.add(jobParamDAO);
178
			}
179
180
			return jobParamList;
181
182
		} catch (SQLException sqle) {
183 4967 daigle
			throw new AccessException("ScheduledJobAccess.getJobParamsForJobId - SQL error when getting "
184 4959 daigle
					+ "scheduled job parameter for job id: " + jobId  + " : "  + sqle.getMessage());
185
		} finally {
186
			try {
187
				if (pstmt != null) {
188
					pstmt.close();
189
				}
190
			} catch (SQLException sqle) {
191 4967 daigle
				logMetacat.error("ScheduledJobParamAccess.getJobParamsForJobId - An error occurred "
192 4959 daigle
						+ "closing prepared statement: " + sqle.getMessage());
193
			} finally {
194
				DBConnectionPool.returnDBConnection(conn, serialNumber);
195
			}
196
		}
197
198
	}
199
200 4971 daigle
	/**
201
	 * Get all job parameters
202
	 *
203
	 * @return a list of all job parameters in the database
204
	 */
205
	protected Vector<ScheduledJobParamDAO> getAllJobParams() throws AccessException {
206 4959 daigle
		Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
207
208 4971 daigle
		// get all job parameters
209 4959 daigle
		PreparedStatement pstmt = null;
210
		ScheduledJobParamDAO jobParamDAO = null;
211
		try {
212
			String sql = "SELECT * FROM scheduled_job_params WHERE status != 'deleted'";
213
			pstmt = conn.prepareStatement(sql);
214
215
			logMetacat.info("SQL getAllJobParams - " + sql);
216
217
			pstmt.execute();
218
219
			ResultSet resultSet = pstmt.getResultSet();
220
			while (resultSet.next()) {
221
				jobParamDAO = populateDAO(resultSet);
222
223
				jobParamList.add(jobParamDAO);
224
			}
225
226
			return jobParamList;
227
228
		} catch (SQLException sqle) {
229 4967 daigle
			throw new AccessException("ScheduledJobParamAccess.getAllJobParams - SQL error when getting "
230 4959 daigle
					+ "scheduled job parameters : "  + sqle.getMessage());
231
		} finally {
232
			try {
233
				if (pstmt != null) {
234
					pstmt.close();
235
				}
236
			} catch (SQLException sqle) {
237 4967 daigle
				logMetacat.error("ScheduledJobParamsAccess.getAllJobParams - An error occurred "
238 4959 daigle
						+ "closing prepared statement: " + sqle.getMessage());
239
			} finally {
240
				DBConnectionPool.returnDBConnection(conn, serialNumber);
241
			}
242
		}
243
244
	}
245
246 4971 daigle
	/**
247
	 * Populate a job parameter data object with the current row in a resultset
248
	 *
249
	 * @param resultSet
250
	 *            the result set which is already pointing to the desired row.
251
	 * @return a scheduled job data parameter object
252
	 */
253 4959 daigle
	protected ScheduledJobParamDAO populateDAO(ResultSet resultSet) throws SQLException {
254
255
		ScheduledJobParamDAO jobParamDAO = new ScheduledJobParamDAO();
256
		jobParamDAO.setId(resultSet.getLong("id"));
257
		jobParamDAO.setCreateTime(resultSet.getTimestamp("date_created"));
258
		jobParamDAO.setModTime(resultSet.getTimestamp("date_updated"));
259
		jobParamDAO.setStatus(resultSet.getString("status"));
260
		jobParamDAO.setJobId(resultSet.getLong("job_id"));
261
		jobParamDAO.setKey(resultSet.getString("key"));
262
		jobParamDAO.setValue(resultSet.getString("value"));
263
264
		return jobParamDAO;
265
	}
266
 }