1 64 65 package com.jcorporate.expresso.services.controller; 66 67 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 ; 99 import java.util.Hashtable ; 100 import java.util.Iterator ; 101 import java.util.Vector ; 102 103 104 111 public class QueueJob 112 extends DBController { 113 private static final String thisClass = QueueJob.class.getName() + "."; 114 private static Logger log = Logger.getLogger(QueueJob.class); 115 116 117 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 } 140 141 147 private Job getJob(String jobName) 148 throws ControllerException { 149 String myName = (thisClass + "getJob(String)"); 150 Job myJob = null; 151 152 try { 153 myJob = (Job) Class.forName(jobName).newInstance(); 154 } catch (IllegalAccessException ie) { 155 throw new ControllerException(myName + ":Illegal Access " + 156 "Exception loading Job class " + 157 jobName + ":" + ie.getMessage()); 158 } catch (InstantiationException ie) { 159 throw new ControllerException(myName + ":Can't instantiate " + 160 "Job class " + jobName + ":" + 161 ie.getMessage()); 162 } catch (ClassNotFoundException se) { 163 throw new ControllerException(myName + ":Can't find a Job " + 164 "class called " + jobName + ":" + 165 se.getMessage()); 166 } catch (Exception 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 } 176 177 178 183 public String getTitle() { 184 return ("Queue Job"); 185 } 186 187 193 private void jobParamsState(ControllerResponse myResponse, 194 ControllerRequest cparams) 195 throws ControllerException { 196 String 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 } 202 203 204 Job j = getJob(jobName); 205 Hashtable params = j.getParameterNamesAndDescriptions(); 206 Input oneParam; 207 String paramName; 208 Vector vv; 209 210 if (params.size() > 0) { 211 for (Enumeration pe = params.keys(); pe.hasMoreElements();) { 212 paramName = (String ) pe.nextElement(); 213 oneParam = new Input(); 214 oneParam.setLabel((String ) 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 } 224 225 } else { 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 } 235 236 237 246 public ControllerResponse newState(String 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 } 271 272 273 279 private void promptState(ControllerResponse myResponse, 280 ControllerRequest params) 281 throws ControllerException { 282 String myName = (thisClass + "promptState()"); 283 284 285 Input chooseSchema = new Input(); 286 chooseSchema.setLabel("Choose Schema"); 287 chooseSchema.setName("SchemaClass"); 288 289 Vector v = new Vector (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 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 } 319 320 321 327 private void queueJobState(ControllerResponse myResponse, 328 ControllerRequest params) 329 throws ControllerException { 330 String myName = (thisClass + "queueJobState()"); 331 String 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 } 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 oneParamName; 350 int paramNumber = 0; 351 352 for (Enumeration par = params.getParameters().keys(); 353 par.hasMoreElements();) { 354 oneParamName = (String ) par.nextElement(); 355 356 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 } 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 } 392 393 394 400 private void selJobState(ControllerResponse myResponse, 401 ControllerRequest params) 402 throws ControllerException { 403 String myName = (thisClass + "selJobState()"); 404 405 406 String 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 v = new Vector (); 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 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 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 } 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 } 465 466 467 477 public boolean jobAllowedForUser(String 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 myGroups = myUser.getGroups(); 487 JobSecurity js = new JobSecurity(); 488 js.setDataContext(params.getDataContext()); 489 490 for (Enumeration eg = myGroups.elements(); eg.hasMoreElements();) { 491 js.setField("GroupName", (String ) 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 } 504 505 506 } 507 | Popular Tags |