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.DBConnection;
43
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
44
import edu.ucsb.nceas.metacat.shared.AccessException;
45
import edu.ucsb.nceas.metacat.shared.BaseAccess;
46
import edu.ucsb.nceas.utilities.StatusUtil;
47

    
48
public class ScheduledJobParamAccess extends BaseAccess {
49
	
50
	private Logger logMetacat = Logger.getLogger(ScheduledJobParamAccess.class);
51
	
52
	// Constructor
53
	public ScheduledJobParamAccess() throws AccessException {}
54
	
55
	/**
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
	protected void createJobParams(Long jobId, HashMap<String, String> jobParams) throws AccessException {
64
		
65
		// iterate through and insert each job parameter
66
		for(String paramKey : jobParams.keySet()) {			
67
			ScheduledJobParamDAO jobParamsDAO = new ScheduledJobParamDAO();
68
			jobParamsDAO.setStatus(StatusUtil.ACTIVE);
69
			jobParamsDAO.setJobId(jobId);
70
			jobParamsDAO.setKey(paramKey);
71
			jobParamsDAO.setValue(jobParams.get(paramKey));
72
			
73
			PreparedStatement pstmt = null;
74
			DBConnection conn = null;
75
			int serialNumber = -1;
76
			
77
			try {
78
				conn = DBConnectionPool.getDBConnection("ScheduledJobParamAccess.createJobParams");
79
	    		serialNumber = conn.getCheckOutSerialNumber();
80

    
81
				String sql = 
82
					"INSERT INTO scheduled_job_params (date_created, date_updated, status, job_id, key, value) " 
83
					+ "VALUES(now(), now(), ?, ?, ?, ?)";		
84
				pstmt = conn.prepareStatement(sql);
85
			
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
			} finally {
103
				closeDBObjects(pstmt, conn, serialNumber, logMetacat);
104
			}	
105
		}
106
	}
107
	
108
	/**
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
	protected void deleteJobParams(Long jobId) throws AccessException {
115
		PreparedStatement pstmt = null;
116
		DBConnection conn = null;
117
		int serialNumber = -1;
118
		
119
		// change the status to deleted
120
		try {
121
			conn = DBConnectionPool.getDBConnection("ScheduledJobParamAccess.deleteJobParams");
122
    		serialNumber = conn.getCheckOutSerialNumber();
123

    
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
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
138
		}	
139
		
140
	}	
141
	
142
	/**
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
		Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
151
		
152
		// Get the job parameters for the job id
153
		PreparedStatement pstmt = null;
154
		DBConnection conn = null;
155
		int serialNumber = -1;
156
		
157
		ScheduledJobParamDAO jobParamDAO = null;
158
		try {
159
			conn = DBConnectionPool.getDBConnection("ScheduledJobParamAccess.getJobParamsForJobId");
160
    		serialNumber = conn.getCheckOutSerialNumber();
161

    
162
			String sql = "SELECT * FROM scheduled_job_params WHERE job_id = ? AND status != 'deleted'"; 
163
			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
			throw new AccessException("ScheduledJobAccess.getJobParamsForJobId - SQL error when getting " 
183
					+ "scheduled job parameter for job id: " + jobId  + " : "  + sqle.getMessage());
184
		} finally {
185
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
186
		}
187
		
188
	}
189
	
190
	/**
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
		Vector<ScheduledJobParamDAO> jobParamList = new Vector<ScheduledJobParamDAO>();
197
		
198
		// get all job parameters
199
		PreparedStatement pstmt = null;
200
		DBConnection conn = null;
201
		int serialNumber = -1;
202
		
203
		ScheduledJobParamDAO jobParamDAO = null;
204
		try {
205
			conn = DBConnectionPool.getDBConnection("ScheduledJobParamAccess.getAllJobParams");
206
    		serialNumber = conn.getCheckOutSerialNumber();
207

    
208
			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
			throw new AccessException("ScheduledJobParamAccess.getAllJobParams - SQL error when getting " 
226
					+ "scheduled job parameters : "  + sqle.getMessage());
227
		} finally {
228
			closeDBObjects(pstmt, conn, serialNumber, logMetacat);
229
		}
230
		
231
	}
232
	
233
	/**
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
	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
 }
(5-5/7)