001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.services.controller;
066:
067: /**
068: * QueueJob.java
069: *
070: * Copyright 2000, 2001 Jcorporate Ltd.
071: */
072:
073: import com.jcorporate.expresso.core.controller.Controller;
074: import com.jcorporate.expresso.core.controller.ControllerException;
075: import com.jcorporate.expresso.core.controller.ControllerRequest;
076: import com.jcorporate.expresso.core.controller.ControllerResponse;
077: import com.jcorporate.expresso.core.controller.DBController;
078: import com.jcorporate.expresso.core.controller.ErrorCollection;
079: import com.jcorporate.expresso.core.controller.Input;
080: import com.jcorporate.expresso.core.controller.NonHandleableException;
081: import com.jcorporate.expresso.core.controller.Output;
082: import com.jcorporate.expresso.core.controller.State;
083: import com.jcorporate.expresso.core.controller.Transition;
084: import com.jcorporate.expresso.core.db.DBException;
085: import com.jcorporate.expresso.core.dbobj.Schema;
086: import com.jcorporate.expresso.core.dbobj.SchemaFactory;
087: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
088: import com.jcorporate.expresso.core.dbobj.ValidValue;
089: import com.jcorporate.expresso.core.job.Job;
090: import com.jcorporate.expresso.core.misc.StringUtil;
091: import com.jcorporate.expresso.core.security.User;
092: import com.jcorporate.expresso.services.dbobj.JobQueue;
093: import com.jcorporate.expresso.services.dbobj.JobQueueParam;
094: import com.jcorporate.expresso.services.dbobj.JobSecurity;
095: import com.jcorporate.expresso.services.dbobj.SchemaList;
096: import org.apache.log4j.Logger;
097:
098: import java.util.Enumeration;
099: import java.util.Hashtable;
100: import java.util.Iterator;
101: import java.util.Vector;
102:
103: /**
104: * Controller to queue any named job that the user has permission
105: * to queue.
106: *
107: * @author Michael Nash
108: * @version $Revision: 1.17 $ $Date: 2004/11/17 20:48:17 $
109: */
110: public class QueueJob extends DBController {
111: private static final String this Class = QueueJob.class.getName()
112: + ".";
113: private static Logger log = Logger.getLogger(QueueJob.class);
114:
115: /**
116: * Our constructor "declares" what states we handle
117: */
118: public QueueJob() {
119: super ();
120:
121: State prompt = new State("prompt", "Prompt for Schema");
122: addState(prompt);
123: setInitialState("prompt");
124:
125: State seljob = new State("seljob", "Select Job");
126: seljob.addRequiredParameter("SchemaClass");
127: addState(seljob);
128:
129: State jobparams = new State("jobparams",
130: "Prompt for Job Parameters");
131: jobparams.addRequiredParameter("JobClass");
132: addState(jobparams);
133:
134: State queuejob = new State("queuejob", "Queue Job");
135: queuejob.addRequiredParameter("JobClass");
136: addState(queuejob);
137: this
138: .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 jobName) throws ControllerException {
148: String myName = (this Class + "getJob(String)");
149: Job myJob = null;
150:
151: try {
152: myJob = (Job) Class.forName(jobName).newInstance();
153: } catch (IllegalAccessException ie) {
154: throw new ControllerException(myName + ":Illegal Access "
155: + "Exception loading Job class " + jobName + ":"
156: + ie.getMessage());
157: } catch (InstantiationException ie) {
158: throw new ControllerException(myName
159: + ":Can't instantiate " + "Job class " + jobName
160: + ":" + ie.getMessage());
161: } catch (ClassNotFoundException se) {
162: throw new ControllerException(myName + ":Can't find a Job "
163: + "class called " + jobName + ":" + se.getMessage());
164: } catch (Exception eo) {
165: log.error(eo);
166: throw new ControllerException(myName
167: + ":Exception loading " + "Job " + jobName
168: + "- see detailed message in server log:"
169: + eo.getMessage());
170: }
171:
172: return myJob;
173: } /* getJob(String) */
174:
175: /**
176: * Return the title of this Controller
177: *
178: * @return The title of the controller
179: */
180: public String getTitle() {
181: return ("Queue Job");
182: } /* getTitle() */
183:
184: /**
185: * Specify the parameters to the job as needed
186: *
187: * @param myResponse The ControllerResponse Object
188: * @param cparams The ControllerRequest Object
189: */
190: private void jobParamsState(ControllerResponse myResponse,
191: ControllerRequest cparams) throws ControllerException {
192: String jobName = StringUtil.notNull(cparams
193: .getParameter("JobClass"));
194:
195: if (!jobAllowedForUser(jobName, cparams.getUid(), cparams)) {
196: throw new ControllerException("You are not allowed to "
197: + "request Job '" + jobName + "'");
198: } /* if job not allowed */
199:
200: Job j = getJob(jobName);
201: Hashtable params = j.getParameterNamesAndDescriptions();
202: Input oneParam;
203: String paramName;
204: Vector vv;
205:
206: if (params.size() > 0) {
207: for (Enumeration pe = params.keys(); pe.hasMoreElements();) {
208: paramName = (String) pe.nextElement();
209: oneParam = new Input();
210: oneParam.setLabel((String) params.get(paramName));
211: oneParam.setName(paramName);
212: vv = j.getParamValidValues(paramName);
213:
214: if (vv != null) {
215: oneParam.setValidValues(vv);
216: }
217:
218: myResponse.addInput(oneParam);
219: } /* for each parameter */
220:
221: } else { /* if any params */
222: myResponse.addOutput(new Output(
223: "There are no parameters for this job"));
224: }
225:
226: Transition queuejob = new Transition("Queue Job", getClass()
227: .getName());
228: queuejob.setName("queuejob");
229: queuejob.addParam(STATE_PARAM_KEY, "queuejob");
230: queuejob.addParam("JobClass", cparams.getParameter("JobClass"));
231: myResponse.addTransition(queuejob);
232: } /* jobParamsState() */
233:
234: /**
235: * Transition to a new state
236: *
237: * @param newState The new state to transition into
238: * @param params The ControllerRequest Object
239: * @return the changed ControllerResponse object
240: * @throws ControllerException upon error
241: * @throws NonHandleableException upon fatal error
242: */
243: public ControllerResponse newState(String newState,
244: ControllerRequest params) throws ControllerException,
245: NonHandleableException {
246: ControllerResponse myResponse = super
247: .newState(newState, params);
248:
249: if (newState.equals("prompt")) {
250: promptState(myResponse, params);
251: } else if (newState.equals("seljob")) {
252: selJobState(myResponse, params);
253: } else if (newState.equals("jobparams")) {
254: jobParamsState(myResponse, params);
255: } else if (newState.equals("queuejob")) {
256: queueJobState(myResponse, params);
257: }
258: if (!newState.equals("prompt")) {
259: Transition again = new Transition("Start Again", getClass()
260: .getName());
261: again.setName("again");
262: again.addParam(STATE_PARAM_KEY, "prompt");
263: myResponse.addTransition(again);
264: }
265:
266: return myResponse;
267: } /* newState(String) */
268:
269: /**
270: * Prompt for the job to queue
271: *
272: * @param myResponse The ControllerResponse Object
273: * @param params The ControllerRequest Object
274: */
275: private void promptState(ControllerResponse myResponse,
276: ControllerRequest params) throws ControllerException {
277: String myName = (this Class + "promptState()");
278:
279: /* ask the user to select a schema */
280: Input chooseSchema = new Input();
281: chooseSchema.setLabel("Choose Schema");
282: chooseSchema.setName("SchemaClass");
283:
284: Vector v = new Vector(2);
285: v.addElement(new ValidValue(
286: "com.jcorporate.expresso.core.ExpressoSchema",
287: "General"));
288:
289: try {
290: SchemaList sl = new SchemaList(
291: SecuredDBObject.SYSTEM_ACCOUNT);
292: sl.setDataContext(params.getDataContext());
293:
294: SchemaList oneSchema = null;
295:
296: for (Iterator e = sl.searchAndRetrieveList().iterator(); e
297: .hasNext();) {
298: oneSchema = (SchemaList) e.next();
299: v.addElement(new ValidValue(oneSchema
300: .getField("SchemaClass"), oneSchema
301: .getField("Descrip")));
302: }
303: } catch (DBException de) {
304: throw new ControllerException(myName
305: + ":Unable to retrieve " + "schema information:"
306: + de.getMessage());
307: }
308:
309: chooseSchema.setValidValues(v);
310: myResponse.addInput(chooseSchema);
311:
312: Transition seljob = new Transition("Select Job", getClass()
313: .getName());
314: seljob.setName("seljob");
315: seljob.addParam(STATE_PARAM_KEY, "seljob");
316: myResponse.addTransition(seljob);
317: } /* promptState() */
318:
319: /**
320: * Queue the job as requested
321: *
322: * @param myResponse The ControllerResponse Object
323: * @param params The ControllerRequest Object *
324: */
325: private void queueJobState(ControllerResponse myResponse,
326: ControllerRequest params) throws ControllerException {
327: String myName = (this Class + "queueJobState()");
328: String jobName = StringUtil.notNull(params
329: .getParameter("JobClass"));
330:
331: if (!jobAllowedForUser(jobName, params.getUid(), params)) {
332: throw new ControllerException(myName
333: + ":You are not allowed to " + "request Job '"
334: + jobName + "'");
335: } /* if job not allowed */
336:
337: Output paramOut = new Output("Parameters");
338:
339: try {
340: JobQueue jq = new JobQueue(SecuredDBObject.SYSTEM_ACCOUNT);
341: jq.setDataContext(params.getDataContext());
342: jq.setField("ExpUid", params.getUid());
343: jq.setField("JobCode", jobName);
344: jq.setField("StatusCode", "N");
345: jq.add();
346:
347: String oneParamName;
348: int paramNumber = 0;
349:
350: for (Enumeration par = params.getParameters().keys(); par
351: .hasMoreElements();) {
352: oneParamName = (String) par.nextElement();
353:
354: /* skip params used for management of this controller */
355: if ((!oneParamName.equals(STATE_PARAM_KEY))
356: && (!oneParamName.equals("cmd"))
357: && (!oneParamName
358: .equals(Controller.CONTROLLER_PARAM_KEY))
359: && (!oneParamName.equals("next"))
360: && (!oneParamName.equals("JobClass"))
361: && (oneParamName.indexOf("_") == -1)) {
362: paramNumber++;
363:
364: JobQueueParam jqp = new JobQueueParam(
365: 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", params
371: .getParameter(oneParamName));
372: jqp.add();
373: paramOut.addNested(new Output("Parameter "
374: + oneParamName + ", 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: * Select the job
395: *
396: * @param myResponse The ControllerResponse Object
397: * @param params The ControllerRequest Object
398: */
399: private void selJobState(ControllerResponse myResponse,
400: ControllerRequest params) throws ControllerException {
401: String myName = (this Class + "selJobState()");
402:
403: /* ask the user to select a job */
404: String schemaClass = StringUtil.notNull(params
405: .getParameter("SchemaClass"));
406:
407: if (schemaClass.equals("")) {
408: throw new ControllerException(myName
409: + ":Must have a SchemaClass "
410: + "parameter for 'seljob' state.");
411: }
412:
413: Input chooseJob = new Input();
414: chooseJob.setLabel("Choose Job");
415: chooseJob.setName("JobClass");
416:
417: Vector v = new Vector();
418: Schema mySchema = SchemaFactory.getInstance().getSchema(
419: schemaClass);
420: if (mySchema == null) {
421: throw new ControllerException(myName
422: + ":Exception loading Schema " + schemaClass
423: + "- see detailed message in server log");
424: }
425:
426: Enumeration e = mySchema.getJobs();
427:
428: if (e == null || !e.hasMoreElements()) {
429: ErrorCollection ec = new ErrorCollection();
430: ec.addError("There are no jobs defined for this schema");
431: myResponse.saveErrors(ec);
432: return;
433: }
434:
435: Job j;
436:
437: for (Enumeration je = mySchema.getJobs(); je.hasMoreElements();) {
438: j = (Job) je.nextElement();
439:
440: if (jobAllowedForUser(j.getClass().getName(), params
441: .getUid(), params)) {
442: v.addElement(new ValidValue(j.getClass().getName(), j
443: .getTitle()));
444: }
445: } /* for each job */
446:
447: if (v.size() == 0) {
448: ErrorCollection ec = new ErrorCollection();
449: ec.addError("You are not allowed to "
450: + "queue any jobs in the selected schema");
451: myResponse.saveErrors(ec);
452: return;
453: }
454:
455: chooseJob.setValidValues(v);
456: myResponse.addInput(chooseJob);
457:
458: Transition jobparams = new Transition("Enter Job Parameters",
459: getClass().getName());
460: jobparams.setName("queuejob");
461: jobparams.addParam(STATE_PARAM_KEY, "jobparams");
462: myResponse.addTransition(jobparams);
463: } /* selJobState() */
464:
465: /**
466: * Is the given user allowed to access ANY functions of this
467: * job?
468: *
469: * @param jobClass The className for the job
470: * @param uid The uid of the logged in user
471: * @param params The ControllerRequest Object
472: * @return true if the job is allowed for the user?
473: * @throws ControllerException upon error
474: */
475: public boolean jobAllowedForUser(String jobClass, int uid,
476: ControllerRequest params) throws ControllerException {
477: try {
478: User myUser = new User();
479: myUser.setDataContext(params.getDataContext());
480: myUser.setUid(uid);
481: myUser.retrieve();
482:
483: Vector myGroups = myUser.getGroups();
484: JobSecurity js = new JobSecurity();
485: js.setDataContext(params.getDataContext());
486:
487: for (Enumeration eg = myGroups.elements(); eg
488: .hasMoreElements();) {
489: js.setField("GroupName", (String) eg.nextElement());
490: js.setField("JobClass", jobClass);
491:
492: if (js.find()) {
493: return true;
494: }
495: }
496: } catch (DBException de) {
497: throw new ControllerException(de);
498: }
499:
500: return false;
501: } /* jobAllowedForUser(String, String) */
502:
503: } /* QueueJob */
|