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

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

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

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

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