Project

General

Profile

1 5027 daigle
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that handles scheduling workflow jobs
4
 *  Copyright: 2009 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-03-25 13:41:15 -0800 (Wed, 25 Mar 2009) $'
10
 * '$Revision: 4861 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27
package edu.ucsb.nceas.metacat.workflow;
28
29
import java.io.IOException;
30
import java.io.PrintWriter;
31
import java.sql.SQLException;
32
import java.util.Hashtable;
33
34
import javax.servlet.http.HttpServletRequest;
35
import javax.servlet.http.HttpServletResponse;
36
37
import org.apache.log4j.Logger;
38
39
import edu.ucsb.nceas.metacat.DBTransform;
40 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
41 5027 daigle
import edu.ucsb.nceas.metacat.scheduler.MetacatSchedulerException;
42
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
43
import edu.ucsb.nceas.metacat.util.ErrorSendingErrorException;
44
import edu.ucsb.nceas.metacat.util.MetacatUtil;
45
import edu.ucsb.nceas.metacat.util.ResponseUtil;
46
import edu.ucsb.nceas.metacat.util.RequestUtil;
47
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
48
49
/**
50
 * @author daigle
51
 *
52
 */
53
public class WorkflowSchedulerClient {
54
55
	private static WorkflowSchedulerClient WorkflowSchedulerClient = null;
56
57
	private static Logger logMetacat = Logger.getLogger(WorkflowSchedulerClient.class);
58
59
	/**
60
	 * private constructor since this is a singleton
61
	 */
62
	private WorkflowSchedulerClient()  {}
63
64
	/**
65
	 * Get the single instance of SchedulerService.
66
	 *
67
	 * @return the single instance of SchedulerService
68
	 */
69
	public static WorkflowSchedulerClient getInstance() {
70
		if (WorkflowSchedulerClient == null) {
71
			WorkflowSchedulerClient = new WorkflowSchedulerClient();
72
		}
73
		return WorkflowSchedulerClient;
74
	}
75
76
	/**
77
	 * Scheduling a workflow
78
	 *
79
	 * @param request
80
	 *            the servlet request object
81
	 * @param response
82
	 *            the servlet response object
83
	 * @param params
84
	 *            the request parameters
85
	 * @param username
86
	 *            the user
87
	 * @param groups
88
	 *            the user's group
89
	 */
90
	public void scheduleJob(HttpServletRequest request, HttpServletResponse response,
91
			Hashtable<String, String[]> params, String username,
92
			String[] groups) throws MetacatSchedulerException {
93
94
		try {
95 5078 daigle
			logMetacat.debug("WorkflowSchedulerClient.scheduleJob");
96 5027 daigle
			params.put("action", new String[] { "scheduleWorkflow" });
97 5057 daigle
			params.put("sessionid", new String[] { RequestUtil.getSessionData(request).getId() });
98 5027 daigle
99
			String schedulerUrl = PropertyService.getProperty("workflowScheduler.url");
100
			String result = RequestUtil.get(schedulerUrl, params);
101
102
			String forwardTos[] = params.get("forwardto");
103
104
			if (forwardTos == null || forwardTos.length == 0) {
105
				ResponseUtil.sendSuccessXML(response, result);
106
			} else {
107
				String forwardTo = forwardTos[0];
108
				String qformat = null;
109
110
				String qformats[] = params.get("qformat");
111
				if (qformats == null || qformats.length == 0) {
112
					qformat = MetacatUtil.XMLFORMAT;
113
				} else {
114
					qformat = qformats[0];
115
				}
116
117
				String destination = "/style/skins/" + qformat + "/" + forwardTo;
118
119
				RequestUtil.forwardRequest(request, response, destination, params);
120
			}
121
122
		} catch (PropertyNotFoundException pnfe) {
123
			throw new MetacatSchedulerException("WorkflowSchedulerClient.scheduleJob - "
124
					+ "property error when scheduling job: " + pnfe.getMessage());
125
		} catch (MetacatUtilException mue) {
126
			throw new MetacatSchedulerException("WorkflowSchedulerClient.scheduleJob - "
127 5078 daigle
					+ "utility issue when scheduling job: " + mue.getMessage());
128 5027 daigle
		} catch (ErrorSendingErrorException esee) {
129
			throw new MetacatSchedulerException("WorkflowSchedulerClient.scheduleJob - "
130
					+ "Issue sending error when scheduling job: " + esee.getMessage());
131
		}
132
	}
133
134
	/**
135
	 * Unschedule a job
136
	 *
137
	 * @param request
138
	 *            the servlet request object
139
	 * @param response
140
	 *            the servlet response object
141
	 * @param params
142
	 *            the request parameters
143
	 * @param username
144
	 *            the user
145
	 * @param groups
146
	 *            the user's group
147
	 */
148
	public void unScheduleJob(HttpServletRequest request, HttpServletResponse response,
149
			Hashtable<String, String[]> params, String username,
150
			String[] groups) throws MetacatSchedulerException {
151
		try {
152 5078 daigle
			logMetacat.debug("WorkflowSchedulerClient.unScheduleJob");
153
154 5027 daigle
			params.put("action", new String[] { "unscheduleWorkflow" });
155 5057 daigle
			params.put("sessionid", new String[] { RequestUtil.getSessionData(request).getId() });
156 5027 daigle
157
			String schedulerUrl = PropertyService.getProperty("workflowScheduler.url");
158
			String result = RequestUtil.get(schedulerUrl, params);
159
160
			String forwardTos[] = params.get("forwardto");
161
162
			if (forwardTos == null || forwardTos.length == 0) {
163
				ResponseUtil.sendSuccessXML(response, result);
164
			} else {
165
				String forwardTo = forwardTos[0];
166
				String qformat = null;
167
168
				String qformats[] = params.get("qformat");
169
				if (qformats == null || qformats.length == 0) {
170
					qformat = MetacatUtil.XMLFORMAT;
171
				} else {
172
					qformat = qformats[0];
173
				}
174
175
				String destination = "/style/skins/" + qformat + "/" + forwardTo;
176
177
				RequestUtil.forwardRequest(request, response, destination, params);
178
			}
179
180
		} catch (PropertyNotFoundException pnfe) {
181
			throw new MetacatSchedulerException("WorkflowSchedulerClient.unScheduleJob - "
182
					+ "property error when unscheduling job: " + pnfe.getMessage());
183
		} catch (MetacatUtilException mue) {
184
			throw new MetacatSchedulerException("WorkflowSchedulerClient.unScheduleJob - "
185 5078 daigle
					+ "utility issue when unscheduling job: " + mue.getMessage());
186 5027 daigle
		} catch (ErrorSendingErrorException esee) {
187
			throw new MetacatSchedulerException("WorkflowSchedulerClient.unScheduleJob - "
188
					+ "Issue sending error when unscheduling job: " + esee.getMessage());
189
		}
190
	}
191
192
	/**
193
	 * reschedule job
194
	 *
195
	 * @param request
196
	 *            the servlet request object
197
	 * @param response
198
	 *            the servlet response object
199
	 * @param params
200
	 *            the request parameters
201
	 * @param username
202
	 *            the user
203
	 * @param groups
204
	 *            the user's group
205
	 */
206 5078 daigle
	@SuppressWarnings("unchecked")
207 5027 daigle
	public void reScheduleJob(HttpServletRequest request, HttpServletResponse response,
208
			Hashtable<String, String[]> params, String username,
209
			String[] groups) throws MetacatSchedulerException {
210
211
		try {
212 5078 daigle
			logMetacat.debug("WorkflowSchedulerClient.reScheduleJob");
213
214 5027 daigle
			params.put("action", new String[] { "rescheduleWorkflow" });
215 5057 daigle
			params.put("sessionid", new String[] { RequestUtil.getSessionData(request).getId() });
216 5027 daigle
217
			String schedulerUrl = PropertyService.getProperty("workflowScheduler.url");
218
			String result = RequestUtil.get(schedulerUrl, params);
219
220
			String forwardTos[] = params.get("forwardto");
221
222
			if (forwardTos == null || forwardTos.length == 0) {
223
				ResponseUtil.sendSuccessXML(response, result);
224 5078 daigle
			} else {
225
				RequestUtil.forwardRequest(request, response, params);
226 5027 daigle
			}
227
228
		} catch (PropertyNotFoundException pnfe) {
229
			throw new MetacatSchedulerException("WorkflowSchedulerClient.reScheduleJob - "
230
					+ "property error when rescheduling job: " + pnfe.getMessage());
231
		} catch (MetacatUtilException mue) {
232
			throw new MetacatSchedulerException("WorkflowSchedulerClient.reScheduleJob - "
233 5078 daigle
					+ "utility issue  when rescheduling job: " + mue.getMessage());
234 5027 daigle
		} catch (ErrorSendingErrorException esee) {
235
			throw new MetacatSchedulerException("WorkflowSchedulerClient.reScheduleJob - "
236
					+ "Issue sending error when rescheduling job: " + esee.getMessage());
237
		}
238
	}
239
240
	/**
241
	 * delete job - to be implemented
242
	 */
243
	public void deleteJob(HttpServletRequest request, HttpServletResponse response,
244
			Hashtable<String, String[]> params, String username,
245
			String[] groups) throws MetacatSchedulerException {
246
		try {
247 5078 daigle
			logMetacat.debug("WorkflowSchedulerClient.deleteJob");
248
249 5027 daigle
			params.put("action", new String[] { "deleteScheduledWorkflow" });
250 5057 daigle
			params.put("sessionid", new String[] { RequestUtil.getSessionData(request).getId() });
251 5027 daigle
252
			String schedulerUrl = PropertyService.getProperty("workflowScheduler.url");
253
			String result = RequestUtil.get(schedulerUrl, params);
254
255
			String forwardTos[] = params.get("forwardto");
256
257
			if (forwardTos == null || forwardTos.length == 0) {
258
				ResponseUtil.sendSuccessXML(response, result);
259
			} else {
260
				String forwardTo = forwardTos[0];
261
				String qformat = null;
262
263
				String qformats[] = params.get("qformat");
264
				if (qformats == null || qformats.length == 0) {
265
					qformat = MetacatUtil.XMLFORMAT;
266
				} else {
267
					qformat = qformats[0];
268
				}
269
270
				String destination = "/style/skins/" + qformat + "/" + forwardTo;
271
272
				RequestUtil.forwardRequest(request, response, destination, params);
273
			}
274
275
		} catch (PropertyNotFoundException pnfe) {
276
			throw new MetacatSchedulerException("WorkflowSchedulerClient.deleteJob - "
277
					+ "property error when deleting job: " + pnfe.getMessage());
278
		} catch (MetacatUtilException mue) {
279
			throw new MetacatSchedulerException("WorkflowSchedulerClient.deleteJob - "
280 5078 daigle
					+ "utility issue  when deleting job: " + mue.getMessage());
281 5027 daigle
		} catch (ErrorSendingErrorException esee) {
282
			throw new MetacatSchedulerException("WorkflowSchedulerClient.deleteJob - "
283
					+ "Issue sending error when deleting job: " + esee.getMessage());
284
		}
285
	}
286
287
	/**
288
	 * get job information for a given workflow in xml format
289
	 *
290
	 * @param request
291
	 *            the servlet request object
292
	 * @param response
293
	 *            the servlet response object
294
	 * @param params
295
	 *            the request parameters
296
	 * @param username
297
	 *            the user
298
	 * @param groups
299
	 *            the user's group
300
	 */
301
	public void getJobs(HttpServletRequest request, HttpServletResponse response,
302
			Hashtable<String, String[]> params, String username,
303
			String[] groups) throws MetacatSchedulerException {
304
305 5078 daigle
		try {
306
			logMetacat.debug("WorkflowSchedulerClient.getJobs");
307 5027 daigle
			params.put("action", new String[] { "getScheduledWorkflow" });
308
309
			String schedulerUrl = PropertyService.getProperty("workflowScheduler.url");
310
			String result = RequestUtil.get(schedulerUrl, params);
311
312
			String qformats[] = params.get("qformat");
313
			String qformat = null;
314
			if (qformats == null || qformats.length == 0) {
315
				qformat = MetacatUtil.XMLFORMAT;
316
			} else {
317
				qformat = qformats[0];
318
			}
319
320
			PrintWriter out = response.getWriter();
321
322
			DBTransform dbt = new DBTransform();
323
            dbt.transformXMLDocument(result,"-//NCEAS//scheduledWorkflowResultset//EN", "-//W3C//HTML//EN",
324
                    qformat, out, params, null);
325
326
		} catch (PropertyNotFoundException pnfe) {
327
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
328
					+ "Property error when getting jobs for workflow: " + pnfe.getMessage());
329
		} catch (IOException ioe) {
330
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
331
					+ "I/O error when getting jobs for workflow: " + ioe.getMessage());
332
		} catch (MetacatUtilException mue) {
333
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
334
					+ "Metacat utility error  when getting jobs for workflow: " + mue.getMessage());
335
		} catch (ClassNotFoundException cnfe) {
336
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
337
					+ "Error finding class when getting jobs for workflow: " + cnfe.getMessage());
338
		} catch (SQLException sqle) {
339
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
340
					+ "SQL error when getting jobs for workflow: " + sqle.getMessage());
341
		}
342
	}
343
}