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 5108 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
43 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
44
import edu.ucsb.nceas.metacat.shared.AccessException;
45
import edu.ucsb.nceas.metacat.shared.BaseAccess;
46 4959 daigle
import edu.ucsb.nceas.utilities.StatusUtil;
47 4951 daigle
48 4959 daigle
public class ScheduledJobParamAccess extends BaseAccess {
49 4951 daigle
50 4959 daigle
	private Logger logMetacat = Logger.getLogger(ScheduledJobParamAccess.class);
51 4951 daigle
52 4971 daigle
	// Constructor
53 5108 daigle
	public ScheduledJobParamAccess() throws AccessException {}
54 4951 daigle
55 4971 daigle
	/**
56
	 * Insert job parameters into the database.
57
	 *
58
	 * @param jobId
59
	 *            the id of the job that these parameters belong to
60
	 * @param jobParams
61
	 *            a map of the job parameters
62
	 */
63 4959 daigle
	protected void createJobParams(Long jobId, HashMap<String, String> jobParams) throws AccessException {
64 4951 daigle
65 4971 daigle
		// iterate through and insert each job parameter
66 4959 daigle
		for(String paramKey : jobParams.keySet()) {
67
			ScheduledJobParamDAO jobParamsDAO = new ScheduledJobParamDAO();
68
			jobParamsDAO.setStatus(StatusUtil.ACTIVE);
69 4951 daigle
			jobParamsDAO.setJobId(jobId);
70
			jobParamsDAO.setKey(paramKey);
71
			jobParamsDAO.setValue(jobParams.get(paramKey));
72
73 4959 daigle
			PreparedStatement pstmt = null;
74 5108 daigle
			DBConnection conn = null;
75
			int serialNumber = -1;
76 4959 daigle
77 4951 daigle
			try {
78 5108 daigle
				conn = DBConnectionPool.getDBConnection("ScheduledJobParamAccess.createJobParams");
79
	    		serialNumber = conn.getCheckOutSerialNumber();
80
81 4951 daigle
				String sql =
82
					"INSERT INTO scheduled_job_params (date_created, date_updated, status, job_id, key, value) "
83
					+ "VALUES(now(), now(), ?, ?, ?, ?)";
84 4959 daigle
				pstmt = conn.prepareStatement(sql);
85 4951 daigle
86
				pstmt.setString(1, jobParamsDAO.getStatus());
87
				pstmt.setLong(2, jobParamsDAO.getJobId());
88
				pstmt.setString(3, jobParamsDAO.getKey());
89
				pstmt.setString(4, jobParamsDAO.getValue());
90
91
				logMetacat.info("SQL createJobParams - " + sql);
92
				logMetacat.info("SQL params:  [" + jobParamsDAO.getStatus() + ","
93
						+ jobParamsDAO.getJobId() + ","
94
						+ jobParamsDAO.getKey() + ","
95
						+ jobParamsDAO.getValue().toString() + "]");
96
				pstmt.execute();
97
98
			} catch (SQLException sqle) {
99
				// Just throw the exception.  The ScheduledJobAccess class should handle cleanup.
100
				throw new AccessException("ScheduledJobParamsAccess.createJobParams - SQL error when creating scheduled job parameter : "
101
					 + sqle.getMessage());
102 4959 daigle
			} finally {
103 5108 daigle
				closeDBObjects(pstmt, conn, serialNumber, logMetacat);
104 4959 daigle
			}
105 4951 daigle
		}
106
	}
107
108 4971 daigle
	/**
109
	 * Remove change a job status to deleted in the database.
110
	 *
111
	 * @param jobId
112
	 *            the id of the job to update
113
	 */
114 4951 daigle
	protected void deleteJobParams(Long jobId) throws AccessException {
115 4959 daigle
		PreparedStatement pstmt = null;
116 5108 daigle
		DBConnection conn = null;
117
		int serialNumber = -1;
118 4959 daigle
119 4971 daigle
		// change the status to deleted
120 4951 daigle
		try {
121 5108 daigle
			conn = DBConnectionPool.getDBConnection("ScheduledJobParamAccess.deleteJobParams");
122
    		serialNumber = conn.getCheckOutSerialNumber();
123
124 4951 daigle
			String sql = "UPDATE scheduled_job_params SET status = ? WHERE jobId = ?";
125 5030 daigle
			pstmt = conn.prepareStatement(sql);
126
127 4959 daigle
			pstmt.setString(1, StatusUtil.DELETED);
128 4951 daigle
			pstmt.setLong(1, jobId);
129
130 5030 daigle
			logMetacat.info("ScheduledJobParamsAccess.deleteJobParams - SQL: " + sql);
131 4951 daigle
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 4959 daigle
		} finally {
137 5108 daigle
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
138 4951 daigle
		}
139
140
	}
141 4959 daigle
142 4971 daigle
	/**
143
	 * Get all the job parameters for a given job id
144
	 *
145
	 * @param jobId
146
	 *            the job id whose parameters we want to return
147
	 * @return a list of the job parameter data objects
148
	 */
149
	protected Vector<ScheduledJobParamDAO> getJobParamsForJobId(Long jobId) throws AccessException {
150 4959 daigle
		Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
151
152 4971 daigle
		// Get the job parameters for the job id
153 4959 daigle
		PreparedStatement pstmt = null;
154 5108 daigle
		DBConnection conn = null;
155
		int serialNumber = -1;
156
157 4959 daigle
		ScheduledJobParamDAO jobParamDAO = null;
158
		try {
159 5108 daigle
			conn = DBConnectionPool.getDBConnection("ScheduledJobParamAccess.getJobParamsForJobId");
160
    		serialNumber = conn.getCheckOutSerialNumber();
161
162 4967 daigle
			String sql = "SELECT * FROM scheduled_job_params WHERE job_id = ? AND status != 'deleted'";
163 4959 daigle
			pstmt = conn.prepareStatement(sql);
164
165
			pstmt.setLong(1, jobId);
166
167
			logMetacat.info("SQL getJobParamsForJobId - " + sql);
168
			logMetacat.info("SQL params: [" + jobId + "]");
169
170
			pstmt.execute();
171
172
			ResultSet resultSet = pstmt.getResultSet();
173
			while (resultSet.next()) {
174
				jobParamDAO = populateDAO(resultSet);
175
176
				jobParamList.add(jobParamDAO);
177
			}
178
179
			return jobParamList;
180
181
		} catch (SQLException sqle) {
182 4967 daigle
			throw new AccessException("ScheduledJobAccess.getJobParamsForJobId - SQL error when getting "
183 4959 daigle
					+ "scheduled job parameter for job id: " + jobId  + " : "  + sqle.getMessage());
184
		} finally {
185 5108 daigle
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
186 4959 daigle
		}
187
188
	}
189
190 4971 daigle
	/**
191
	 * Get all job parameters
192
	 *
193
	 * @return a list of all job parameters in the database
194
	 */
195
	protected Vector<ScheduledJobParamDAO> getAllJobParams() throws AccessException {
196 4959 daigle
		Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
197
198 4971 daigle
		// get all job parameters
199 4959 daigle
		PreparedStatement pstmt = null;
200 5108 daigle
		DBConnection conn = null;
201
		int serialNumber = -1;
202
203 4959 daigle
		ScheduledJobParamDAO jobParamDAO = null;
204
		try {
205 5108 daigle
			conn = DBConnectionPool.getDBConnection("ScheduledJobParamAccess.getAllJobParams");
206
    		serialNumber = conn.getCheckOutSerialNumber();
207
208 4959 daigle
			String sql = "SELECT * FROM scheduled_job_params WHERE status != 'deleted'";
209
			pstmt = conn.prepareStatement(sql);
210
211
			logMetacat.info("SQL getAllJobParams - " + sql);
212
213
			pstmt.execute();
214
215
			ResultSet resultSet = pstmt.getResultSet();
216
			while (resultSet.next()) {
217
				jobParamDAO = populateDAO(resultSet);
218
219
				jobParamList.add(jobParamDAO);
220
			}
221
222
			return jobParamList;
223
224
		} catch (SQLException sqle) {
225 4967 daigle
			throw new AccessException("ScheduledJobParamAccess.getAllJobParams - SQL error when getting "
226 4959 daigle
					+ "scheduled job parameters : "  + sqle.getMessage());
227
		} finally {
228 5108 daigle
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
229 4959 daigle
		}
230
231
	}
232
233 4971 daigle
	/**
234
	 * Populate a job parameter data object with the current row in a resultset
235
	 *
236
	 * @param resultSet
237
	 *            the result set which is already pointing to the desired row.
238
	 * @return a scheduled job data parameter object
239
	 */
240 4959 daigle
	protected ScheduledJobParamDAO populateDAO(ResultSet resultSet) throws SQLException {
241
242
		ScheduledJobParamDAO jobParamDAO = new ScheduledJobParamDAO();
243
		jobParamDAO.setId(resultSet.getLong("id"));
244
		jobParamDAO.setCreateTime(resultSet.getTimestamp("date_created"));
245
		jobParamDAO.setModTime(resultSet.getTimestamp("date_updated"));
246
		jobParamDAO.setStatus(resultSet.getString("status"));
247
		jobParamDAO.setJobId(resultSet.getLong("job_id"));
248
		jobParamDAO.setKey(resultSet.getString("key"));
249
		jobParamDAO.setValue(resultSet.getString("value"));
250
251
		return jobParamDAO;
252
	}
253
 }