Project

General

Profile

1
/**
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
import javax.servlet.ServletException;
37

    
38
import org.apache.log4j.Logger;
39

    
40
import edu.ucsb.nceas.metacat.DBTransform;
41
import edu.ucsb.nceas.metacat.properties.PropertyService;
42
import edu.ucsb.nceas.metacat.scheduler.MetacatSchedulerException;
43
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
44
import edu.ucsb.nceas.metacat.util.ErrorSendingErrorException;
45
import edu.ucsb.nceas.metacat.util.MetacatUtil;
46
import edu.ucsb.nceas.metacat.util.ResponseUtil;
47
import edu.ucsb.nceas.metacat.util.RequestUtil;
48
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
49

    
50
/**
51
 * @author daigle
52
 *
53
 */
54
public class WorkflowSchedulerClient {
55
	
56
	private static WorkflowSchedulerClient WorkflowSchedulerClient = null;
57
	
58
	private static Logger logMetacat = Logger.getLogger(WorkflowSchedulerClient.class);
59

    
60
	/**
61
	 * private constructor since this is a singleton
62
	 */
63
	private WorkflowSchedulerClient()  {}
64
	
65
	/**
66
	 * Get the single instance of SchedulerService.
67
	 * 
68
	 * @return the single instance of SchedulerService
69
	 */
70
	public static WorkflowSchedulerClient getInstance() {
71
		if (WorkflowSchedulerClient == null) {
72
			WorkflowSchedulerClient = new WorkflowSchedulerClient();
73
		}
74
		return WorkflowSchedulerClient;
75
	}
76
	
77
	/**
78
	 * Scheduling a workflow
79
	 * 
80
	 * @param request
81
	 *            the servlet request object
82
	 * @param response
83
	 *            the servlet response object
84
	 * @param params
85
	 *            the request parameters
86
	 * @param username
87
	 *            the user
88
	 * @param groups
89
	 *            the user's group
90
	 */
91
	public void scheduleJob(HttpServletRequest request, HttpServletResponse response, 
92
			Hashtable<String, String[]> params, String username,
93
			String[] groups) throws MetacatSchedulerException {
94

    
95
		try {
96
			params.put("action", new String[] { "scheduleWorkflow" });
97
			params.put("sessionid", new String[] { RequestUtil.getSessionData(request).getId() });
98
			
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
					+ "Service issue when scheduling job: " + mue.getMessage());
128
		} catch (IOException ioe) {
129
			throw new MetacatSchedulerException("WorkflowSchedulerClient.scheduleJob - " 
130
					+ "I/O issue when scheduling job: " + ioe.getMessage());
131
		} catch (ServletException se) {
132
			throw new MetacatSchedulerException("WorkflowSchedulerClient.scheduleJob - " 
133
					+ "Servlet issue when scheduling job: " + se.getMessage());
134
		} catch (ErrorSendingErrorException esee) {
135
			throw new MetacatSchedulerException("WorkflowSchedulerClient.scheduleJob - " 
136
					+ "Issue sending error when scheduling job: " + esee.getMessage());			
137
		}
138
	}
139
	
140
	/**
141
	 * Unschedule a job
142
	 * 
143
	 * @param request
144
	 *            the servlet request object
145
	 * @param response
146
	 *            the servlet response object
147
	 * @param params
148
	 *            the request parameters
149
	 * @param username
150
	 *            the user
151
	 * @param groups
152
	 *            the user's group
153
	 */
154
	public void unScheduleJob(HttpServletRequest request, HttpServletResponse response,
155
			Hashtable<String, String[]> params, String username,
156
			String[] groups) throws MetacatSchedulerException {
157
		try {
158
			params.put("action", new String[] { "unscheduleWorkflow" });
159
			params.put("sessionid", new String[] { RequestUtil.getSessionData(request).getId() });
160
			
161
			String schedulerUrl = PropertyService.getProperty("workflowScheduler.url");
162
			String result = RequestUtil.get(schedulerUrl, params);
163
			
164
			String forwardTos[] = params.get("forwardto");
165
			
166
			if (forwardTos == null || forwardTos.length == 0) {
167
				ResponseUtil.sendSuccessXML(response, result);		
168
			} else {		
169
				String forwardTo = forwardTos[0];
170
				String qformat = null;
171
				
172
				String qformats[] = params.get("qformat");
173
				if (qformats == null || qformats.length == 0) {
174
					qformat = MetacatUtil.XMLFORMAT;
175
				} else {
176
					qformat = qformats[0];
177
				}
178
				
179
				String destination = "/style/skins/" + qformat + "/" + forwardTo;
180
				
181
				RequestUtil.forwardRequest(request, response, destination, params);
182
			} 
183
				
184
		} catch (PropertyNotFoundException pnfe) {
185
			throw new MetacatSchedulerException("WorkflowSchedulerClient.unScheduleJob - "
186
					+ "property error when unscheduling job: " + pnfe.getMessage());
187
		} catch (MetacatUtilException mue) {
188
			throw new MetacatSchedulerException("WorkflowSchedulerClient.unScheduleJob - "
189
					+ "Service issue when unscheduling job: " + mue.getMessage());
190
		} catch (IOException ioe) {
191
			throw new MetacatSchedulerException("WorkflowSchedulerClient.unScheduleJob - " 
192
					+ "I/O issue  when unscheduling job: " + ioe.getMessage());
193
		} catch (ServletException se) {
194
			throw new MetacatSchedulerException("WorkflowSchedulerClient.unScheduleJob - " 
195
					+ "Servlet issue  when unscheduling job: " + se.getMessage());
196
		} catch (ErrorSendingErrorException esee) {
197
			throw new MetacatSchedulerException("WorkflowSchedulerClient.unScheduleJob - " 
198
					+ "Issue sending error when unscheduling job: " + esee.getMessage());			
199
		}
200
	}
201
	
202
	/**
203
	 * reschedule job
204
	 * 
205
	 * @param request
206
	 *            the servlet request object
207
	 * @param response
208
	 *            the servlet response object
209
	 * @param params
210
	 *            the request parameters
211
	 * @param username
212
	 *            the user
213
	 * @param groups
214
	 *            the user's group
215
	 */
216
	public void reScheduleJob(HttpServletRequest request, HttpServletResponse response, 
217
			Hashtable<String, String[]> params, String username,
218
			String[] groups) throws MetacatSchedulerException {
219
		 		
220
		try {
221
			params.put("action", new String[] { "rescheduleWorkflow" });
222
			params.put("sessionid", new String[] { RequestUtil.getSessionData(request).getId() });
223
			
224
			String schedulerUrl = PropertyService.getProperty("workflowScheduler.url");
225
			String result = RequestUtil.get(schedulerUrl, params);
226

    
227
			String forwardTos[] = params.get("forwardto");
228
			
229
			if (forwardTos == null || forwardTos.length == 0) {
230
				ResponseUtil.sendSuccessXML(response, result);		
231
			} else {		
232
				String forwardTo = forwardTos[0];
233
				String qformat = null;
234
				
235
				String qformats[] = params.get("qformat");
236
				if (qformats == null || qformats.length == 0) {
237
					qformat = MetacatUtil.XMLFORMAT;
238
				} else {
239
					qformat = qformats[0];
240
				}
241
				
242
				String destination = "/style/skins/" + qformat + "/" + forwardTo;
243
				
244
				RequestUtil.forwardRequest(request, response, destination, params);
245
			} 
246
			
247
		} catch (PropertyNotFoundException pnfe) {
248
			throw new MetacatSchedulerException("WorkflowSchedulerClient.reScheduleJob - "
249
					+ "property error when rescheduling job: " + pnfe.getMessage());
250
		} catch (MetacatUtilException mue) {
251
			throw new MetacatSchedulerException("WorkflowSchedulerClient.reScheduleJob - "
252
					+ "Service issue  when rescheduling job: " + mue.getMessage());
253
		} catch (IOException ioe) {
254
			throw new MetacatSchedulerException("WorkflowSchedulerClient.reScheduleJob - " 
255
					+ "I/O issue  when rescheduling job: " + ioe.getMessage());
256
		} catch (ServletException se) {
257
			throw new MetacatSchedulerException("WorkflowSchedulerClient.reScheduleJob - " 
258
					+ "Servlet issue  when rescheduling job: " + se.getMessage());
259
		} catch (ErrorSendingErrorException esee) {
260
			throw new MetacatSchedulerException("WorkflowSchedulerClient.reScheduleJob - " 
261
					+ "Issue sending error when rescheduling job: " + esee.getMessage());			
262
		}
263
	}
264
	
265
	/**
266
	 * delete job - to be implemented
267
	 */
268
	public void deleteJob(HttpServletRequest request, HttpServletResponse response,
269
			Hashtable<String, String[]> params, String username,
270
			String[] groups) throws MetacatSchedulerException {
271
		try {
272
			params.put("action", new String[] { "deleteScheduledWorkflow" });
273
			params.put("sessionid", new String[] { RequestUtil.getSessionData(request).getId() });
274
			
275
			String schedulerUrl = PropertyService.getProperty("workflowScheduler.url");
276
			String result = RequestUtil.get(schedulerUrl, params);
277

    
278
			String forwardTos[] = params.get("forwardto");
279
			
280
			if (forwardTos == null || forwardTos.length == 0) {
281
				ResponseUtil.sendSuccessXML(response, result);		
282
			} else {		
283
				String forwardTo = forwardTos[0];
284
				String qformat = null;
285
				
286
				String qformats[] = params.get("qformat");
287
				if (qformats == null || qformats.length == 0) {
288
					qformat = MetacatUtil.XMLFORMAT;
289
				} else {
290
					qformat = qformats[0];
291
				}
292
				
293
				String destination = "/style/skins/" + qformat + "/" + forwardTo;
294
				
295
				RequestUtil.forwardRequest(request, response, destination, params);
296
			} 			
297

    
298
		} catch (PropertyNotFoundException pnfe) {
299
			throw new MetacatSchedulerException("WorkflowSchedulerClient.deleteJob - "
300
					+ "property error when deleting job: " + pnfe.getMessage());
301
		} catch (MetacatUtilException mue) {
302
			throw new MetacatSchedulerException("WorkflowSchedulerClient.deleteJob - "
303
					+ "Service issue  when deleting job: " + mue.getMessage());
304
		} catch (IOException ioe) {
305
			throw new MetacatSchedulerException("WorkflowSchedulerClient.deleteJob - "
306
					+ "I/O issue  when deleting job: " + ioe.getMessage());
307
		} catch (ServletException se) {
308
			throw new MetacatSchedulerException("WorkflowSchedulerClient.deleteJob - "
309
					+ "Servlet issue  when deleting job: " + se.getMessage());
310
		} catch (ErrorSendingErrorException esee) {
311
			throw new MetacatSchedulerException("WorkflowSchedulerClient.deleteJob - "
312
					+ "Issue sending error when deleting job: " + esee.getMessage());
313
		}
314
	}
315
	
316
	/**
317
	 * get job information for a given workflow in xml format
318
	 * 
319
	 * @param request
320
	 *            the servlet request object
321
	 * @param response
322
	 *            the servlet response object
323
	 * @param params
324
	 *            the request parameters
325
	 * @param username
326
	 *            the user
327
	 * @param groups
328
	 *            the user's group
329
	 */
330
	public void getJobs(HttpServletRequest request, HttpServletResponse response,
331
			Hashtable<String, String[]> params, String username,
332
			String[] groups) throws MetacatSchedulerException {
333
		
334
		try {	
335
			params.put("action", new String[] { "getScheduledWorkflow" });
336
			
337
			String schedulerUrl = PropertyService.getProperty("workflowScheduler.url");
338
			String result = RequestUtil.get(schedulerUrl, params);
339
			
340
			String qformats[] = params.get("qformat");			
341
			String qformat = null;
342
			if (qformats == null || qformats.length == 0) {
343
				qformat = MetacatUtil.XMLFORMAT;
344
			} else {
345
				qformat = qformats[0];
346
			}
347
            
348
			PrintWriter out = response.getWriter();
349
			
350
			DBTransform dbt = new DBTransform();
351
            dbt.transformXMLDocument(result,"-//NCEAS//scheduledWorkflowResultset//EN", "-//W3C//HTML//EN",
352
                    qformat, out, params, null);
353

    
354
		} catch (PropertyNotFoundException pnfe) {
355
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
356
					+ "Property error when getting jobs for workflow: " + pnfe.getMessage());
357
		} catch (IOException ioe) {
358
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
359
					+ "I/O error when getting jobs for workflow: " + ioe.getMessage());
360
		} catch (MetacatUtilException mue) {
361
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
362
					+ "Metacat utility error  when getting jobs for workflow: " + mue.getMessage());
363
		} catch (ClassNotFoundException cnfe) {
364
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
365
					+ "Error finding class when getting jobs for workflow: " + cnfe.getMessage());
366
		} catch (SQLException sqle) {
367
			throw new MetacatSchedulerException("WorkflowSchedulerClient.getJobs - "
368
					+ "SQL error when getting jobs for workflow: " + sqle.getMessage());
369
		} 
370
	}
371
}
    (1-1/1)