KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > controller > QueueJob


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.controller;
66
67 /**
68  * QueueJob.java
69  *
70  * Copyright 2000, 2001 Jcorporate Ltd.
71  */

72
73 import com.jcorporate.expresso.core.controller.Controller;
74 import com.jcorporate.expresso.core.controller.ControllerException;
75 import com.jcorporate.expresso.core.controller.ControllerRequest;
76 import com.jcorporate.expresso.core.controller.ControllerResponse;
77 import com.jcorporate.expresso.core.controller.DBController;
78 import com.jcorporate.expresso.core.controller.ErrorCollection;
79 import com.jcorporate.expresso.core.controller.Input;
80 import com.jcorporate.expresso.core.controller.NonHandleableException;
81 import com.jcorporate.expresso.core.controller.Output;
82 import com.jcorporate.expresso.core.controller.State;
83 import com.jcorporate.expresso.core.controller.Transition;
84 import com.jcorporate.expresso.core.db.DBException;
85 import com.jcorporate.expresso.core.dbobj.Schema;
86 import com.jcorporate.expresso.core.dbobj.SchemaFactory;
87 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
88 import com.jcorporate.expresso.core.dbobj.ValidValue;
89 import com.jcorporate.expresso.core.job.Job;
90 import com.jcorporate.expresso.core.misc.StringUtil;
91 import com.jcorporate.expresso.core.security.User;
92 import com.jcorporate.expresso.services.dbobj.JobQueue;
93 import com.jcorporate.expresso.services.dbobj.JobQueueParam;
94 import com.jcorporate.expresso.services.dbobj.JobSecurity;
95 import com.jcorporate.expresso.services.dbobj.SchemaList;
96 import org.apache.log4j.Logger;
97
98 import java.util.Enumeration JavaDoc;
99 import java.util.Hashtable JavaDoc;
100 import java.util.Iterator JavaDoc;
101 import java.util.Vector JavaDoc;
102
103
104 /**
105  * Controller to queue any named job that the user has permission
106  * to queue.
107  *
108  * @author Michael Nash
109  * @version $Revision: 1.17 $ $Date: 2004/11/17 20:48:17 $
110  */

111 public class QueueJob
112         extends DBController {
113     private static final String JavaDoc thisClass = QueueJob.class.getName() + ".";
114     private static Logger log = Logger.getLogger(QueueJob.class);
115
116
117     /**
118      * Our constructor "declares" what states we handle
119      */

120     public QueueJob() {
121         super();
122
123         State prompt = new State("prompt", "Prompt for Schema");
124         addState(prompt);
125         setInitialState("prompt");
126
127         State seljob = new State("seljob", "Select Job");
128         seljob.addRequiredParameter("SchemaClass");
129         addState(seljob);
130
131         State jobparams = new State("jobparams", "Prompt for Job Parameters");
132         jobparams.addRequiredParameter("JobClass");
133         addState(jobparams);
134
135         State queuejob = new State("queuejob", "Queue Job");
136         queuejob.addRequiredParameter("JobClass");
137         addState(queuejob);
138         this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class);
139     } /* QueueJob() */
140
141     /**
142      * Return the named job object
143      *
144      * @param jobName The job classname
145      * @return an instantiated Job Object
146      */

147     private Job getJob(String JavaDoc jobName)
148             throws ControllerException {
149         String JavaDoc myName = (thisClass + "getJob(String)");
150         Job myJob = null;
151
152         try {
153             myJob = (Job) Class.forName(jobName).newInstance();
154         } catch (IllegalAccessException JavaDoc ie) {
155             throw new ControllerException(myName + ":Illegal Access " +
156                     "Exception loading Job class " +
157                     jobName + ":" + ie.getMessage());
158         } catch (InstantiationException JavaDoc ie) {
159             throw new ControllerException(myName + ":Can't instantiate " +
160                     "Job class " + jobName + ":" +
161                     ie.getMessage());
162         } catch (ClassNotFoundException JavaDoc se) {
163             throw new ControllerException(myName + ":Can't find a Job " +
164                     "class called " + jobName + ":" +
165                     se.getMessage());
166         } catch (Exception JavaDoc eo) {
167             log.error(eo);
168             throw new ControllerException(myName + ":Exception loading " +
169                     "Job " + jobName +
170                     "- see detailed message in server log:" +
171                     eo.getMessage());
172         }
173
174         return myJob;
175     } /* getJob(String) */
176
177
178     /**
179      * Return the title of this Controller
180      *
181      * @return The title of the controller
182      */

183     public String JavaDoc getTitle() {
184         return ("Queue Job");
185     } /* getTitle() */
186
187     /**
188      * Specify the parameters to the job as needed
189      *
190      * @param myResponse The ControllerResponse Object
191      * @param cparams The ControllerRequest Object
192      */

193     private void jobParamsState(ControllerResponse myResponse,
194                                 ControllerRequest cparams)
195             throws ControllerException {
196         String JavaDoc jobName = StringUtil.notNull(cparams.getParameter("JobClass"));
197
198         if (!jobAllowedForUser(jobName, cparams.getUid(), cparams)) {
199             throw new ControllerException("You are not allowed to " +
200                     "request Job '" + jobName + "'");
201         } /* if job not allowed */
202
203
204         Job j = getJob(jobName);
205         Hashtable JavaDoc params = j.getParameterNamesAndDescriptions();
206         Input oneParam;
207         String JavaDoc paramName;
208         Vector JavaDoc vv;
209
210         if (params.size() > 0) {
211             for (Enumeration JavaDoc pe = params.keys(); pe.hasMoreElements();) {
212                 paramName = (String JavaDoc) pe.nextElement();
213                 oneParam = new Input();
214                 oneParam.setLabel((String JavaDoc) params.get(paramName));
215                 oneParam.setName(paramName);
216                 vv = j.getParamValidValues(paramName);
217
218                 if (vv != null) {
219                     oneParam.setValidValues(vv);
220                 }
221
222                 myResponse.addInput(oneParam);
223             } /* for each parameter */
224
225         } else { /* if any params */
226             myResponse.addOutput(new Output("There are no parameters for this job"));
227         }
228
229         Transition queuejob = new Transition("Queue Job", getClass().getName());
230         queuejob.setName("queuejob");
231         queuejob.addParam(STATE_PARAM_KEY, "queuejob");
232         queuejob.addParam("JobClass", cparams.getParameter("JobClass"));
233         myResponse.addTransition(queuejob);
234     } /* jobParamsState() */
235
236
237     /**
238      * Transition to a new state
239      *
240      * @param newState The new state to transition into
241      * @param params The ControllerRequest Object
242      * @return the changed ControllerResponse object
243      * @throws ControllerException upon error
244      * @throws NonHandleableException upon fatal error
245      */

246     public ControllerResponse newState(String JavaDoc newState,
247                                        ControllerRequest params)
248             throws ControllerException,
249             NonHandleableException {
250         ControllerResponse myResponse = super.newState(newState, params);
251
252         if (newState.equals("prompt")) {
253             promptState(myResponse, params);
254         } else if (newState.equals("seljob")) {
255             selJobState(myResponse, params);
256         } else if (newState.equals("jobparams")) {
257             jobParamsState(myResponse, params);
258         } else if (newState.equals("queuejob")) {
259             queueJobState(myResponse, params);
260         }
261         if (!newState.equals("prompt")) {
262             Transition again = new Transition("Start Again",
263                     getClass().getName());
264             again.setName("again");
265             again.addParam(STATE_PARAM_KEY, "prompt");
266             myResponse.addTransition(again);
267         }
268
269         return myResponse;
270     } /* newState(String) */
271
272
273     /**
274      * Prompt for the job to queue
275      *
276      * @param myResponse The ControllerResponse Object
277      * @param params The ControllerRequest Object
278      */

279     private void promptState(ControllerResponse myResponse,
280                              ControllerRequest params)
281             throws ControllerException {
282         String JavaDoc myName = (thisClass + "promptState()");
283
284         /* ask the user to select a schema */
285         Input chooseSchema = new Input();
286         chooseSchema.setLabel("Choose Schema");
287         chooseSchema.setName("SchemaClass");
288
289         Vector JavaDoc v = new Vector JavaDoc(2);
290         v.addElement(new ValidValue("com.jcorporate.expresso.core.ExpressoSchema",
291                 "General"));
292
293         try {
294             SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT);
295             sl.setDataContext(params.getDataContext());
296
297             SchemaList oneSchema = null;
298
299             for (Iterator JavaDoc e = sl.searchAndRetrieveList().iterator();
300                  e.hasNext();) {
301                 oneSchema = (SchemaList) e.next();
302                 v.addElement(new ValidValue(oneSchema.getField("SchemaClass"),
303                         oneSchema.getField("Descrip")));
304             }
305         } catch (DBException de) {
306             throw new ControllerException(myName + ":Unable to retrieve " +
307                     "schema information:" +
308                     de.getMessage());
309         }
310
311         chooseSchema.setValidValues(v);
312         myResponse.addInput(chooseSchema);
313
314         Transition seljob = new Transition("Select Job", getClass().getName());
315         seljob.setName("seljob");
316         seljob.addParam(STATE_PARAM_KEY, "seljob");
317         myResponse.addTransition(seljob);
318     } /* promptState() */
319
320
321     /**
322      * Queue the job as requested
323      *
324      * @param myResponse The ControllerResponse Object
325      * @param params The ControllerRequest Object *
326      */

327     private void queueJobState(ControllerResponse myResponse,
328                                ControllerRequest params)
329             throws ControllerException {
330         String JavaDoc myName = (thisClass + "queueJobState()");
331         String JavaDoc jobName = StringUtil.notNull(params.getParameter("JobClass"));
332
333         if (!jobAllowedForUser(jobName, params.getUid(), params)) {
334             throw new ControllerException(myName + ":You are not allowed to " +
335                     "request Job '" + jobName + "'");
336         } /* if job not allowed */
337
338
339         Output paramOut = new Output("Parameters");
340
341         try {
342             JobQueue jq = new JobQueue(SecuredDBObject.SYSTEM_ACCOUNT);
343             jq.setDataContext(params.getDataContext());
344             jq.setField("ExpUid", params.getUid());
345             jq.setField("JobCode", jobName);
346             jq.setField("StatusCode", "N");
347             jq.add();
348
349             String JavaDoc oneParamName;
350             int paramNumber = 0;
351
352             for (Enumeration JavaDoc par = params.getParameters().keys();
353                  par.hasMoreElements();) {
354                 oneParamName = (String JavaDoc) par.nextElement();
355
356                 /* skip params used for management of this controller */
357                 if ((!oneParamName.equals(STATE_PARAM_KEY)) &&
358                         (!oneParamName.equals("cmd")) &&
359                         (!oneParamName.equals(Controller.CONTROLLER_PARAM_KEY)) &&
360                         (!oneParamName.equals("next")) &&
361                         (!oneParamName.equals("JobClass")) &&
362                         (oneParamName.indexOf("_") == -1)) {
363                     paramNumber++;
364
365                     JobQueueParam jqp = new JobQueueParam(SecuredDBObject.SYSTEM_ACCOUNT);
366                     jqp.setDataContext(params.getDataContext());
367                     jqp.setField("JobNumber", jq.getField("JobNumber"));
368                     jqp.setField("ParamNumber", "" + paramNumber);
369                     jqp.setField("ParamCode", oneParamName);
370                     jqp.setField("ParamValue",
371                             params.getParameter(oneParamName));
372                     jqp.add();
373                     paramOut.addNested(new Output("Parameter " + oneParamName +
374                             ", value " +
375                             params.getParameter(oneParamName)));
376                 } /* if not one of the names we use */
377
378             }
379
380             jq.setField("StatusCode", "A");
381             jq.update();
382
383             if (paramNumber > 0) {
384                 myResponse.addOutput(paramOut);
385             }
386         } catch (DBException de) {
387             throw new ControllerException("Unable to queue job", de);
388         }
389
390         myResponse.addOutput(new Output("Job Queued"));
391     } /* queueJobState() */
392
393
394     /**
395      * Select the job
396      *
397      * @param myResponse The ControllerResponse Object
398      * @param params The ControllerRequest Object
399      */

400     private void selJobState(ControllerResponse myResponse,
401                              ControllerRequest params)
402             throws ControllerException {
403         String JavaDoc myName = (thisClass + "selJobState()");
404
405         /* ask the user to select a job */
406         String JavaDoc schemaClass = StringUtil.notNull(params.getParameter("SchemaClass"));
407
408         if (schemaClass.equals("")) {
409             throw new ControllerException(myName +
410                     ":Must have a SchemaClass " +
411                     "parameter for 'seljob' state.");
412         }
413
414         Input chooseJob = new Input();
415         chooseJob.setLabel("Choose Job");
416         chooseJob.setName("JobClass");
417
418         Vector JavaDoc v = new Vector JavaDoc();
419         Schema mySchema = SchemaFactory.getInstance().getSchema(schemaClass);
420         if (mySchema == null) {
421             throw new ControllerException(myName +
422                     ":Exception loading Schema " +
423                     schemaClass +
424                     "- see detailed message in server log");
425         }
426
427         Enumeration JavaDoc e = mySchema.getJobs();
428
429         if (e == null || !e.hasMoreElements()) {
430             ErrorCollection ec = new ErrorCollection();
431             ec.addError("There are no jobs defined for this schema");
432             myResponse.saveErrors(ec);
433             return;
434         }
435
436         Job j;
437
438         for (Enumeration JavaDoc je = mySchema.getJobs(); je.hasMoreElements();) {
439             j = (Job) je.nextElement();
440
441             if (jobAllowedForUser(j.getClass().getName(), params.getUid(),
442                     params)) {
443                 v.addElement(new ValidValue(j.getClass().getName(),
444                         j.getTitle()));
445             }
446         } /* for each job */
447
448         if (v.size() == 0) {
449             ErrorCollection ec = new ErrorCollection();
450             ec.addError("You are not allowed to " +
451                     "queue any jobs in the selected schema");
452             myResponse.saveErrors(ec);
453             return;
454         }
455
456         chooseJob.setValidValues(v);
457         myResponse.addInput(chooseJob);
458
459         Transition jobparams = new Transition("Enter Job Parameters",
460                 getClass().getName());
461         jobparams.setName("queuejob");
462         jobparams.addParam(STATE_PARAM_KEY, "jobparams");
463         myResponse.addTransition(jobparams);
464     } /* selJobState() */
465
466
467     /**
468      * Is the given user allowed to access ANY functions of this
469      * job?
470      *
471      * @param jobClass The className for the job
472      * @param uid The uid of the logged in user
473      * @param params The ControllerRequest Object
474      * @return true if the job is allowed for the user?
475      * @throws ControllerException upon error
476      */

477     public boolean jobAllowedForUser(String JavaDoc jobClass, int uid,
478                                      ControllerRequest params)
479             throws ControllerException {
480         try {
481             User myUser = new User();
482             myUser.setDataContext(params.getDataContext());
483             myUser.setUid(uid);
484             myUser.retrieve();
485
486             Vector JavaDoc myGroups = myUser.getGroups();
487             JobSecurity js = new JobSecurity();
488             js.setDataContext(params.getDataContext());
489
490             for (Enumeration JavaDoc eg = myGroups.elements(); eg.hasMoreElements();) {
491                 js.setField("GroupName", (String JavaDoc) eg.nextElement());
492                 js.setField("JobClass", jobClass);
493
494                 if (js.find()) {
495                     return true;
496                 }
497             }
498         } catch (DBException de) {
499             throw new ControllerException(de);
500         }
501
502         return false;
503     } /* jobAllowedForUser(String, String) */
504
505
506 } /* QueueJob */
507
Popular Tags