Project

General

Profile

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("ScheduledJobParamsAccess.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.setString(1, StatusUtil.DELETED);
126
			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
		} 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
		}	
146
		
147
	}	
148
	
149
	/**
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
		Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
158
		
159
		// Get the job parameters for the job id
160
		PreparedStatement pstmt = null;
161
		ScheduledJobParamDAO jobParamDAO = null;
162
		try {
163
			String sql = "SELECT * FROM scheduled_job_params WHERE job_id = ? AND status != 'deleted'"; 
164
			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
			throw new AccessException("ScheduledJobAccess.getJobParamsForJobId - SQL error when getting " 
184
					+ "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
				logMetacat.error("ScheduledJobParamAccess.getJobParamsForJobId - An error occurred " 
192
						+ "closing prepared statement: " + sqle.getMessage());
193
			} finally {
194
				DBConnectionPool.returnDBConnection(conn, serialNumber);
195
			}
196
		}
197
		
198
	}
199
	
200
	/**
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
		Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
207
		
208
		// get all job parameters
209
		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
			throw new AccessException("ScheduledJobParamAccess.getAllJobParams - SQL error when getting " 
230
					+ "scheduled job parameters : "  + sqle.getMessage());
231
		} finally {
232
			try {
233
				if (pstmt != null) {
234
					pstmt.close();
235
				}
236
			} catch (SQLException sqle) {
237
				logMetacat.error("ScheduledJobParamsAccess.getAllJobParams - An error occurred " 
238
						+ "closing prepared statement: " + sqle.getMessage());
239
			} finally {
240
				DBConnectionPool.returnDBConnection(conn, serialNumber);
241
			}
242
		}
243
		
244
	}
245
	
246
	/**
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
	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
 }
(5-5/7)