1 25 package org.ofbiz.shark.instance; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Date ; 32 import java.util.Map ; 33 34 import org.ofbiz.base.util.Debug; 35 import org.ofbiz.base.util.UtilMisc; 36 import org.ofbiz.base.util.UtilValidate; 37 import org.ofbiz.entity.GenericDelegator; 38 import org.ofbiz.entity.GenericEntityException; 39 import org.ofbiz.entity.GenericValue; 40 import org.ofbiz.entity.util.EntityListIterator; 41 import org.ofbiz.entity.model.DynamicViewEntity; 42 import org.ofbiz.entity.model.ModelKeyMap; 43 import org.ofbiz.entity.condition.EntityCondition; 44 import org.ofbiz.entity.condition.EntityConditionList; 45 import org.ofbiz.entity.condition.EntityExpr; 46 import org.ofbiz.entity.condition.EntityJoinOperator; 47 import org.ofbiz.entity.condition.EntityOperator; 48 import org.ofbiz.entity.condition.EntityComparisonOperator; 49 import org.ofbiz.shark.container.SharkContainer; 50 51 import org.enhydra.shark.api.RootException; 52 import org.enhydra.shark.api.SharkTransaction; 53 import org.enhydra.shark.api.internal.instancepersistence.*; 54 import org.enhydra.shark.api.internal.working.CallbackUtilities; 55 56 63 public class EntityPersistentMgr implements PersistentManagerInterface { 64 65 public static final String module = EntityPersistentMgr.class.getName(); 66 67 protected CallbackUtilities callBackUtil = null; 68 69 public void configure(CallbackUtilities callBackUtil) throws RootException { 70 this.callBackUtil = callBackUtil; 71 } 72 73 public void shutdownDatabase() throws PersistenceException { 74 } 75 76 public void persist(ProcessMgrPersistenceInterface processMgr, SharkTransaction trans) throws PersistenceException { 78 try { 79 ((ProcessMgr) processMgr).store(); 80 } catch (GenericEntityException e) { 81 throw new PersistenceException(e); 82 } 83 } 84 85 public void persist(ProcessPersistenceInterface process, SharkTransaction trans) throws PersistenceException { 86 try { 87 ((Process ) process).store(); 88 } catch (GenericEntityException e) { 89 throw new PersistenceException(e); 90 } 91 } 92 93 public void persistExternalRequester(String processId, Object req, SharkTransaction trans) throws PersistenceException { 94 ProcessPersistenceInterface proc = this.restoreProcess(processId, trans); 95 proc.setExternalRequester(req); 96 this.persist(proc, trans); 97 } 98 99 public void persist(ActivityPersistenceInterface activity, SharkTransaction trans) throws PersistenceException { 100 try { 101 ((Activity) activity).store(); 102 } catch (GenericEntityException e) { 103 throw new PersistenceException(e); 104 } 105 } 106 107 public void persist(ResourcePersistenceInterface resource, SharkTransaction trans) throws PersistenceException { 108 try { 109 ((Resource) resource).store(); 110 } catch (GenericEntityException e) { 111 throw new PersistenceException(e); 112 } 113 } 114 115 public void persist(AssignmentPersistenceInterface assignment, SharkTransaction trans) throws PersistenceException { 116 try { 117 ((Assignment) assignment).store(); 118 } catch (GenericEntityException e) { 119 throw new PersistenceException(e); 120 } 121 } 122 123 public void persist(AssignmentPersistenceInterface assignment, String oldResUname, SharkTransaction trans) throws PersistenceException { 124 persist(assignment, trans); 126 } 127 128 public void persist(ProcessVariablePersistenceInterface processVariable, SharkTransaction trans) throws PersistenceException { 129 try { 130 ((ProcessVariable) processVariable).store(); 131 } catch (GenericEntityException e) { 132 throw new PersistenceException(e); 133 } 134 } 135 136 public void persist(ActivityVariablePersistenceInterface activityVariable, SharkTransaction trans) throws PersistenceException { 137 try { 138 ((ActivityVariable) activityVariable).store(); 139 } catch (GenericEntityException e) { 140 throw new PersistenceException(e); 141 } 142 } 143 144 public void persist(AndJoinEntryInterface andJoin, SharkTransaction trans) throws PersistenceException { 145 try { 146 ((AndJoinEntry) andJoin).store(); 147 } catch (GenericEntityException e) { 148 throw new PersistenceException(e); 149 } 150 } 151 152 public void persist(DeadlinePersistenceInterface dpe, SharkTransaction ti) throws PersistenceException { 153 try { 154 ((Deadline) dpe).store(); 155 } catch (GenericEntityException e) { 156 throw new PersistenceException(e); 157 } 158 } 159 160 public ProcessMgrPersistenceInterface restoreProcessMgr(String mgrName, SharkTransaction trans) throws PersistenceException { 162 return ProcessMgr.getInstance(this, mgrName); 163 } 164 165 public ProcessPersistenceInterface restoreProcess(String processId, SharkTransaction trans) throws PersistenceException { 166 return Process.getInstance(this, processId); 167 } 168 169 public ActivityPersistenceInterface restoreActivity(String activityId, SharkTransaction trans) throws PersistenceException { 170 return Activity.getInstance(this, activityId); 171 } 172 173 public ResourcePersistenceInterface restoreResource(String resourceId, SharkTransaction trans) throws PersistenceException { 174 return Resource.getInstance(this, resourceId); 175 } 176 177 public AssignmentPersistenceInterface restoreAssignment(String activityId, String userName, SharkTransaction trans) throws PersistenceException { 178 return Assignment.getInstance(this, activityId, userName); 179 } 180 181 public boolean restore(ProcessVariablePersistenceInterface processVariablePersistenceInterface, SharkTransaction trans) throws PersistenceException { 182 if (Debug.verboseOn()) Debug.log(":: ProcessVariablePersistenceInterface ::", module); 183 if (processVariablePersistenceInterface == null) { 184 return false; 185 } 186 try { 187 ((ProcessVariable) processVariablePersistenceInterface).reload(); 188 } catch (GenericEntityException e) { 189 Debug.logError(e, module); 190 throw new PersistenceException(e); 191 } 192 return true; 193 } 194 195 public boolean restore(ActivityVariablePersistenceInterface activityVariablePersistenceInterface, SharkTransaction trans) throws PersistenceException { 196 if (Debug.verboseOn()) Debug.log(":: ActivityVariablePersistenceInterface ::", module); 197 if (activityVariablePersistenceInterface == null) { 198 return false; 199 } 200 try { 201 ((ActivityVariable) activityVariablePersistenceInterface).reload(); 202 } catch (GenericEntityException e) { 203 Debug.logError(e, module); 204 throw new PersistenceException(e); 205 } 206 return true; 207 } 208 209 public void deleteProcessMgr(String mgrName, SharkTransaction trans) throws PersistenceException { 211 if (Debug.infoOn()) Debug.log(":: deleteProcessMgr ::", module); 212 try { 213 ((ProcessMgr) restoreProcessMgr(mgrName, trans)).remove(); 214 } catch (GenericEntityException e) { 215 throw new PersistenceException(e); 216 } 217 } 218 219 public void deleteProcess(String processId, boolean admin, SharkTransaction trans) throws PersistenceException { 220 if (admin) { 221 if (Debug.infoOn()) Debug.log(":: deleteProcess ::", module); 222 Process process = (Process ) this.restoreProcess(processId, trans); 223 List activities = this.getAllActivitiesForProcess(processId, trans); 224 if (activities != null) { 225 Iterator i = activities.iterator(); 226 while (i.hasNext()) { 227 Activity activity = (Activity) i.next(); 228 try { 229 activity.remove(); 230 } catch (GenericEntityException e) { 231 throw new PersistenceException(e); 232 } 233 } 234 } 235 236 try { 237 process.remove(); 238 } catch (GenericEntityException e) { 239 throw new PersistenceException(e); 240 } 241 } 242 } 243 244 public void deleteActivity(String activityId, SharkTransaction trans) throws PersistenceException { 245 if (Debug.infoOn()) Debug.log(":: deleteActivity ::", module); 246 Activity activity = (Activity) this.restoreActivity(activityId, trans); 247 List assignments = this.getAllAssignmentsForActivity(activityId, trans); 248 if (assignments != null) { 249 Iterator i = assignments.iterator(); 250 while (i.hasNext()) { 251 Assignment assignment = (Assignment) i.next(); 252 try { 253 assignment.remove(); 254 } catch (GenericEntityException e) { 255 throw new PersistenceException(e); 256 } 257 } 258 259 try { 260 activity.remove(); 261 } catch (GenericEntityException e) { 262 throw new PersistenceException(e); 263 } 264 } 265 } 266 267 public void deleteResource(String userName, SharkTransaction trans) throws PersistenceException { 268 if (Debug.infoOn()) Debug.log(":: deleteResource ::", module); 269 try { 270 ((Resource) restoreResource(userName, trans)).remove(); 271 } catch (GenericEntityException e) { 272 throw new PersistenceException(e); 273 } 274 } 275 276 public void deleteAssignment(String activityId, String userName, SharkTransaction trans) throws PersistenceException { 277 if (Debug.infoOn()) Debug.log(":: deleteAssignment ::", module); 278 try { 279 ((Assignment) restoreAssignment(activityId, userName, trans)).remove(); 280 } catch (GenericEntityException e) { 281 throw new PersistenceException(e); 282 } 283 } 284 285 public void deleteAndJoinEntries(String procId, String asDefId, String aDefId, SharkTransaction trans) throws PersistenceException { 286 if (Debug.infoOn()) Debug.log(":: deleteAndJoinEntries ::", module); 287 GenericDelegator delegator = SharkContainer.getDelegator(); 288 try { 289 delegator.removeByAnd("WfAndJoin", UtilMisc.toMap("processId", procId, 290 "activitySetDefId", asDefId, "activityDefId", aDefId)); 291 } catch (GenericEntityException e) { 292 throw new PersistenceException(e); 293 } 294 } 295 296 public void deleteDeadlines(String procId, SharkTransaction trans) throws PersistenceException { 297 GenericDelegator delegator = SharkContainer.getDelegator(); 298 try { 299 delegator.removeByAnd("WfDeadline", UtilMisc.toMap("processId", procId)); 300 } catch (GenericEntityException e) { 301 throw new PersistenceException(e); 302 } 303 } 304 305 public void deleteDeadlines(String procId, String actId, SharkTransaction trans) throws PersistenceException { 306 GenericDelegator delegator = SharkContainer.getDelegator(); 307 try { 308 delegator.removeByAnd("WfDeadline", UtilMisc.toMap("processId", procId, "activityId", actId)); 309 } catch (GenericEntityException e) { 310 throw new PersistenceException(e); 311 } 312 } 313 314 public void delete(ProcessVariablePersistenceInterface processVariable, SharkTransaction trans) throws PersistenceException { 315 } 317 318 public void delete(ActivityVariablePersistenceInterface activityVariable, SharkTransaction trans) throws PersistenceException { 319 } 321 322 public List getProcessMgrsWhere(SharkTransaction trans, String sqlWhere) throws PersistenceException { 323 if (sqlWhere != null) { 324 Debug.log("Attempt to call getProcessMgrsWhere() - " + sqlWhere, module); 325 throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")"); 326 } 327 return this.getAllProcessMgrs(trans); 328 } 329 330 public List getAllProcessMgrs(SharkTransaction trans) throws PersistenceException { 331 if (Debug.verboseOn()) Debug.log(":: getAllProcessMgrs ::", module); 332 GenericDelegator delegator = SharkContainer.getDelegator(); 333 List createdList = new ArrayList (); 334 List lookupList = null; 335 try { 336 lookupList = delegator.findAll("WfProcessMgr"); 337 } catch (GenericEntityException e) { 338 Debug.logError(e, module); 339 throw new PersistenceException(e); 340 } 341 if (lookupList != null && lookupList.size() > 0) { 342 Iterator i = lookupList.iterator(); 343 while (i.hasNext()) { 344 GenericValue v = (GenericValue) i.next(); 345 createdList.add(ProcessMgr.getInstance(this, v)); 346 } 347 } 348 if (Debug.verboseOn()) Debug.log("ProcessMgr : " + createdList.size(), module); 349 return createdList; 350 } 351 352 public List getResourcesWhere(SharkTransaction trans, String sqlWhere) throws PersistenceException { 353 if (sqlWhere != null) { 354 Debug.log("Attempt to call getResourcesWhere() - " + sqlWhere, module); 355 throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")"); 356 } 357 return this.getAllResources(trans); 358 } 359 360 public List getAllResources(SharkTransaction trans) throws PersistenceException { 361 if (Debug.verboseOn()) Debug.log(":: getAllResources ::", module); 362 GenericDelegator delegator = SharkContainer.getDelegator(); 363 List createdList = new ArrayList (); 364 List lookupList = null; 365 try { 366 lookupList = delegator.findAll("WfResource"); 367 } catch (GenericEntityException e) { 368 throw new PersistenceException(e); 369 } 370 if (lookupList != null && lookupList.size() > 0) { 371 Iterator i = lookupList.iterator(); 372 while (i.hasNext()) { 373 GenericValue v = (GenericValue) i.next(); 374 createdList.add(Resource.getInstance(this, v)); 375 } 376 } 377 return createdList; 378 } 379 380 public List getAllAssignments(SharkTransaction trans) throws PersistenceException { 381 if (Debug.verboseOn()) Debug.log(":: getAllAssignments ::", module); 382 GenericDelegator delegator = SharkContainer.getDelegator(); 383 List createdList = new ArrayList (); 384 List lookupList = null; 385 try { 386 lookupList = delegator.findAll("WfAssignment"); 387 } catch (GenericEntityException e) { 388 throw new PersistenceException(e); 389 } 390 if (lookupList != null && lookupList.size() > 0) { 391 Iterator i = lookupList.iterator(); 392 while (i.hasNext()) { 393 GenericValue v = (GenericValue) i.next(); 394 createdList.add(Assignment.getInstance(this, v)); 395 } 396 } 397 return createdList; 398 } 399 400 public List getAllProcesses(SharkTransaction trans) throws PersistenceException { 401 if (Debug.verboseOn()) Debug.log(":: getAllProcesses ::", module); 402 GenericDelegator delegator = SharkContainer.getDelegator(); 403 List createdList = new ArrayList (); 404 List lookupList = null; 405 try { 406 lookupList = delegator.findAll("WfProcess"); 407 } catch (GenericEntityException e) { 408 throw new PersistenceException(e); 409 } 410 if (lookupList != null && lookupList.size() > 0) { 411 Iterator i = lookupList.iterator(); 412 while (i.hasNext()) { 413 GenericValue v = (GenericValue) i.next(); 414 createdList.add(Process.getInstance(this, v)); 415 } 416 } 417 return createdList; 418 } 419 420 public List getAllActivities(SharkTransaction trans) throws PersistenceException { 421 if (Debug.verboseOn()) Debug.log(":: getAllActivities ::", module); 422 GenericDelegator delegator = SharkContainer.getDelegator(); 423 List createdList = new ArrayList (); 424 List lookupList = null; 425 try { 426 lookupList = delegator.findAll("WfActivity"); 427 } catch (GenericEntityException e) { 428 throw new PersistenceException(e); 429 } 430 if (lookupList != null && lookupList.size() > 0) { 431 Iterator i = lookupList.iterator(); 432 while (i.hasNext()) { 433 GenericValue v = (GenericValue) i.next(); 434 createdList.add(Activity.getInstance(this, v)); 435 } 436 } 437 return createdList; 438 } 439 440 public List getAllProcessesForMgr(String mgrName, SharkTransaction trans) throws PersistenceException { 441 if (Debug.verboseOn()) Debug.log(":: getAllProcessesForMgr ::", module); 442 return this.getProcessesForMgr(mgrName, null, trans); 443 } 444 445 public List getProcessesForMgr(String mgrName, String state, SharkTransaction trans) throws PersistenceException { 446 if (Debug.verboseOn()) Debug.log(":: getProcessesForMgr ::", module); 447 GenericDelegator delegator = SharkContainer.getDelegator(); 448 List createdList = new ArrayList (); 449 List lookupList = null; 450 451 Map findBy = UtilMisc.toMap("mgrName", mgrName); 452 if (state != null) { 453 findBy.put("currentState", state); 454 } 455 456 try { 457 lookupList = delegator.findByAnd("WfProcess", findBy); 458 } catch (GenericEntityException e) { 459 throw new PersistenceException(e); 460 } 461 if (lookupList != null && lookupList.size() > 0) { 462 Iterator i = lookupList.iterator(); 463 while (i.hasNext()) { 464 GenericValue v = (GenericValue) i.next(); 465 createdList.add(Process.getInstance(this, v)); 466 } 467 } 468 return createdList; 469 } 470 471 public List getAllRunningProcesses(SharkTransaction trans) throws PersistenceException { 472 GenericDelegator delegator = SharkContainer.getDelegator(); 473 List runningStates = UtilMisc.toList("open.running"); 474 List order = UtilMisc.toList("startedTime"); 475 List createdList = new ArrayList (); 476 List lookupList = null; 477 try { 478 lookupList = delegator.findByCondition("WfProcess", 479 makeStateListCondition("currentState", runningStates, EntityOperator.EQUALS, EntityOperator.OR), null, order); 480 } catch (GenericEntityException e) { 481 throw new PersistenceException(e); 482 } 483 if (!UtilValidate.isEmpty(lookupList)) { 484 Iterator i = lookupList.iterator(); 485 while (i.hasNext()) { 486 GenericValue v = (GenericValue) i.next(); 487 createdList.add(Process.getInstance(this, v)); 488 } 489 } 490 return createdList; 491 } 492 493 protected List findFinishedProcesses(SharkTransaction trans, String packageId, String processDefId, String packageVer, Date finishedBefore) throws PersistenceException { 494 GenericDelegator delegator = SharkContainer.getDelegator(); 495 List finsihedStates = UtilMisc.toList("closed.completed", "closed.terminated", "closed.aborted"); 496 List order = UtilMisc.toList("lastStateTime"); 497 List createdList = new ArrayList (); 498 List lookupList = null; 499 500 try { 501 EntityCondition stateCond = this.makeStateListCondition("currentState", finsihedStates, EntityOperator.EQUALS, EntityOperator.OR); 502 EntityCondition cond = this.makeProcessFilterCondition(stateCond, packageId, processDefId, packageVer, finishedBefore); 503 lookupList = delegator.findByCondition("WfProcess", cond, null, order); 504 } catch (GenericEntityException e) { 505 throw new PersistenceException(e); 506 } 507 if (!UtilValidate.isEmpty(lookupList)) { 508 Iterator i = lookupList.iterator(); 509 while (i.hasNext()) { 510 GenericValue v = (GenericValue) i.next(); 511 createdList.add(Process.getInstance(this, v)); 512 } 513 } 514 return createdList; 515 } 516 517 public List getAllFinishedProcesses(SharkTransaction trans) throws PersistenceException { 518 return this.findFinishedProcesses(trans, null, null, null, null); 519 } 520 521 public List getAllFinishedProcesses(SharkTransaction trans, Date finishedBefore) throws PersistenceException { 522 return this.findFinishedProcesses(trans, null, null, null, finishedBefore); 523 } 524 525 public List getAllFinishedProcesses(SharkTransaction trans, String packageId) throws PersistenceException { 526 return this.findFinishedProcesses(trans, packageId, null, null, null); 527 } 528 529 public List getAllFinishedProcesses(SharkTransaction trans, String packageId, String processDefId) throws PersistenceException { 530 return this.findFinishedProcesses(trans, packageId, processDefId, null, null); 531 } 532 533 public List getAllFinishedProcesses(SharkTransaction trans, String packageId, String processDefId, String packageVer) throws PersistenceException { 534 return this.findFinishedProcesses(trans, packageId, processDefId, packageVer, null); 535 } 536 537 private EntityCondition makeStateListCondition(String field, List states, EntityComparisonOperator operator, EntityJoinOperator jop) throws GenericEntityException { 538 if (states != null) { 539 if (states.size() > 1) { 540 List exprs = new LinkedList (); 541 Iterator i = states.iterator(); 542 while (i.hasNext()) { 543 exprs.add(new EntityExpr(field, operator, i.next())); 544 } 545 return new EntityConditionList(exprs, jop); 546 } else { 547 return new EntityExpr(field, operator, states.get(0)); 548 } 549 } else { 550 throw new GenericEntityException("Cannot create entity condition from list :" + states); 551 } 552 } 553 554 private EntityCondition makeProcessFilterCondition(EntityCondition cond, String packageId, String processDefId, String packageVer, Date finishBefore) { 555 EntityCondition newCond = null; 556 List exprs = new LinkedList (); 557 if (packageId != null) { 558 exprs.add(new EntityExpr("packageId", EntityOperator.EQUALS, packageId)); 559 } 560 if (processDefId != null) { 561 exprs.add(new EntityExpr("definitionId", EntityOperator.EQUALS, processDefId)); 562 } 563 if (packageVer != null) { 564 exprs.add(new EntityExpr("packageVer", EntityOperator.EQUALS, packageVer)); 565 } 566 if (finishBefore != null) { 567 exprs.add(new EntityExpr("lastStateTime", EntityOperator.LESS_THAN, finishBefore)); 568 } 569 570 if (exprs.size() > 0) { 571 newCond = new EntityConditionList(exprs, EntityJoinOperator.AND); 572 } 573 574 if (newCond != null) { 575 if (cond != null) { 576 return new EntityConditionList(UtilMisc.toList(cond, newCond), EntityJoinOperator.AND); 577 } else { 578 return newCond; 579 } 580 } else { 581 return cond; 582 } 583 } 584 585 protected List findProcessActivities(String processId, List states, EntityComparisonOperator operator, SharkTransaction trans) throws PersistenceException { 586 GenericDelegator delegator = SharkContainer.getDelegator(); 587 List createdList = new ArrayList (); 588 List order = UtilMisc.toList("lastStateTime"); 589 List lookupList = null; 590 try { 591 EntityCondition cond = null; 592 EntityCondition proc = new EntityExpr("processId", EntityOperator.EQUALS, processId); 593 594 if (states != null) { 595 EntityCondition stateCond = this.makeStateListCondition("currentState", states, operator, EntityOperator.OR); 596 cond = new EntityConditionList(UtilMisc.toList(proc, stateCond), EntityOperator.AND); 597 } else { 598 cond = proc; 599 } 600 lookupList = delegator.findByCondition("WfActivity", cond, null, order); 601 } catch (GenericEntityException e) { 602 throw new PersistenceException(e); 603 } 604 if (lookupList != null && lookupList.size() > 0) { 605 Iterator i = lookupList.iterator(); 606 while (i.hasNext()) { 607 GenericValue v = (GenericValue) i.next(); 608 createdList.add(Activity.getInstance(this, v)); 609 } 610 } 611 return createdList; 612 } 613 614 public List getAllActivitiesForProcess(String processId, SharkTransaction trans) throws PersistenceException { 615 return this.findProcessActivities(processId, null, null, trans); 616 } 617 618 public List getActivitiesForProcess(String processId, String actState, SharkTransaction trans) throws PersistenceException { 619 return this.findProcessActivities(processId, UtilMisc.toList(actState), null, trans); 620 } 621 622 public List getAllFinishedActivitiesForProcess(String processId, SharkTransaction trans) throws PersistenceException { 623 List finishedStates = UtilMisc.toList("closed.completed", "closed.terminated", "closed.aborted"); 624 return this.findProcessActivities(processId, finishedStates, EntityOperator.EQUALS, trans); 625 } 626 627 public List getAllActiveActivitiesForProcess(String processId, SharkTransaction trans) throws PersistenceException { 628 List finishedStates = UtilMisc.toList("closed.completed", "closed.terminated", "closed.aborted"); 629 return this.findProcessActivities(processId, finishedStates, EntityOperator.NOT_EQUAL, trans); 630 } 631 632 636 public List getAllAssignmentsForResource(String user, SharkTransaction trans) throws PersistenceException { 637 GenericDelegator delegator = SharkContainer.getDelegator(); 638 List createdList = new ArrayList (); 639 List lookupList = null; 640 try { 641 lookupList = delegator.findByAnd("WfAssignment", UtilMisc.toMap("userName", user)); 642 } catch (GenericEntityException e) { 643 throw new PersistenceException(e); 644 } 645 if (lookupList != null && lookupList.size() > 0) { 646 Iterator i = lookupList.iterator(); 647 while (i.hasNext()) { 648 GenericValue v = (GenericValue) i.next(); 649 createdList.add(Assignment.getInstance(this, v)); 650 } 651 } 652 return createdList; 653 } 654 655 659 public List getAllAssignmentsForNotClosedActivitiesForResource(String user, SharkTransaction trans) throws PersistenceException { 660 List allAssignments = getAllAssignmentsForResource(user, trans); 661 List notClosed = new ArrayList (); 662 Iterator i = allAssignments.iterator(); 663 while (i.hasNext()) { 664 Assignment as = (Assignment) i.next(); 665 Activity at = Activity.getInstance(this, as.getActivityId()); 666 if (!at.getState().startsWith("closed")) { 667 notClosed.add(as); 668 } 669 } 670 return notClosed; 671 } 672 673 680 public List getAllValidAssignmentsForResource(String user, SharkTransaction trans) throws PersistenceException { 681 List allAssignments = getAllAssignmentsForResource(user, trans); 682 List valid = new ArrayList (); 683 Iterator i = allAssignments.iterator(); 684 while (i.hasNext()) { 685 Assignment as = (Assignment) i.next(); 686 Activity at = Activity.getInstance(this, as.getActivityId()); 687 if (!at.getState().startsWith("closed")) { 688 if (at.getResourceUsername() == null || user.equals(at.getResourceUsername())) { 689 valid.add(as); 690 } 691 } 692 } 693 return valid; 694 } 695 696 700 public List getAllAssignmentsForActivity(String activityId, SharkTransaction trans) throws PersistenceException { 701 GenericDelegator delegator = SharkContainer.getDelegator(); 702 List createdList = new ArrayList (); 703 List lookupList = null; 704 try { 705 lookupList = delegator.findByAnd("WfAssignment", UtilMisc.toMap("activityId", activityId)); 706 } catch (GenericEntityException e) { 707 throw new PersistenceException(e); 708 } 709 if (lookupList != null && lookupList.size() > 0) { 710 Iterator i = lookupList.iterator(); 711 while (i.hasNext()) { 712 GenericValue v = (GenericValue) i.next(); 713 createdList.add(Assignment.getInstance(this, v)); 714 } 715 } 716 return createdList; 717 } 718 719 724 public List getAllAssignmentsForNotClosedActivity(String activityId, SharkTransaction trans) throws PersistenceException { 725 Activity at = Activity.getInstance(this, activityId); 726 if (at.getState().startsWith("closed")) { 727 return new ArrayList (); 728 } else { 729 return getAllAssignmentsForActivity(activityId, trans); 730 } 731 } 732 733 739 public List getAllValidAssignmentsForActivity(String activityId, SharkTransaction trans) throws PersistenceException { 740 Activity at = Activity.getInstance(this, activityId); 741 if (at.getState().startsWith("closed")) { 742 return new ArrayList (); 743 } 744 745 List assignments = getAllAssignmentsForActivity(activityId, trans); 746 if (at.getResourceUsername() == null) { 747 return assignments; 748 } 749 750 List valid = new ArrayList (); 751 Iterator i = assignments.iterator(); 752 while (i.hasNext()) { 753 Assignment as = (Assignment) i.next(); 754 if (at.getResourceUsername().equals(as.getResourceUsername())) { 755 valid.add(as); 756 } 757 } 758 return valid; 759 } 760 761 public List getAllVariablesForProcess(String processId, SharkTransaction trans) throws PersistenceException { 762 if (Debug.verboseOn()) Debug.log(":: getAllVariablesForProcess ::", module); 763 GenericDelegator delegator = SharkContainer.getDelegator(); 764 List createdList = new ArrayList (); 765 List lookupList = null; 766 try { 767 lookupList = delegator.findByAnd("WfProcessVariable", UtilMisc.toMap("processId", processId)); 768 } catch (GenericEntityException e) { 769 Debug.logError(e, module); 770 throw new PersistenceException(e); 771 } 772 if (lookupList != null && lookupList.size() > 0) { 773 Debug.log("Lookup list contains : " + lookupList.size(), module); 774 Iterator i = lookupList.iterator(); 775 while (i.hasNext()) { 776 GenericValue v = (GenericValue) i.next(); 777 createdList.add(ProcessVariable.getInstance(this, v)); 778 } 779 } else { 780 Debug.log("Lookup list empty", module); 781 } 782 783 if (Debug.verboseOn()) Debug.log("Returning list : " + createdList.size(), module); 784 return createdList; 786 } 787 788 public List getAllVariablesForActivity(String activityId, SharkTransaction trans) throws PersistenceException { 789 if (Debug.verboseOn()) Debug.log(":: getAllVariablesForActivity ::", module); 790 GenericDelegator delegator = SharkContainer.getDelegator(); 791 List createdList = new ArrayList (); 792 List lookupList = null; 793 try { 794 lookupList = delegator.findByAnd("WfActivityVariable", UtilMisc.toMap("activityId", activityId)); 795 } catch (GenericEntityException e) { 796 throw new PersistenceException(e); 797 } 798 if (lookupList != null && lookupList.size() > 0) { 799 Iterator i = lookupList.iterator(); 800 while (i.hasNext()) { 801 GenericValue v = (GenericValue) i.next(); 802 createdList.add(ActivityVariable.getInstance(this, v)); 803 } 804 } 805 return createdList; 806 } 807 808 public List getResourceRequestersProcessIds(String userName, SharkTransaction trans) throws PersistenceException { 809 GenericDelegator delegator = SharkContainer.getDelegator(); 810 List idList = new ArrayList (); 811 List lookupList = null; 812 try { 813 lookupList = delegator.findByAnd("WfProcess", UtilMisc.toMap("resourceReqId", userName)); 814 } catch (GenericEntityException e) { 815 throw new PersistenceException(e); 816 } 817 if (!UtilValidate.isEmpty(lookupList)) { 818 Iterator i = lookupList.iterator(); 819 while (i.hasNext()) { 820 GenericValue v = (GenericValue) i.next(); 821 idList.add(v.getString("processId")); 822 } 823 } 824 return idList; 825 } 826 827 public List getAndJoinEntries(String procId, String asDefId, String aDefId, SharkTransaction trans) throws PersistenceException { 828 List createdList = new ArrayList (); 829 List lookupList = getAndJoinValues(procId, asDefId, aDefId); 830 831 if (lookupList != null && lookupList.size() > 0) { 832 Iterator i = lookupList.iterator(); 833 while (i.hasNext()) { 834 GenericValue v = (GenericValue) i.next(); 835 createdList.add(AndJoinEntry.getInstance(this, v)); 836 } 837 } 838 return createdList; 839 } 840 841 public int howManyAndJoinEntries(String procId, String asDefId, String aDefId, SharkTransaction trans) throws PersistenceException { 842 List lookupList = getAndJoinValues(procId, asDefId, aDefId); 843 return lookupList.size(); 844 } 845 846 public List getAllDeadlinesForProcess(String procId, SharkTransaction trans) throws PersistenceException { 847 List lookupList = getDeadlineValues(UtilMisc.toList(new EntityExpr("processId", EntityOperator.EQUALS, procId))); 848 return getDealineObjects(lookupList); 849 } 850 851 public List getAllDeadlinesForProcess(String procId, long timeLimit, SharkTransaction trans) throws PersistenceException { 852 List lookupList = getDeadlineValues(UtilMisc.toList(new EntityExpr("processId", EntityOperator.EQUALS, procId), 853 new EntityExpr("timeLimit", EntityOperator.LESS_THAN, new Long (timeLimit)))); 854 return getDealineObjects(lookupList); 855 } 856 857 public List getAllIdsForProcessesWithExpiriedDeadlines(long l, SharkTransaction trans) throws PersistenceException { 858 if (Debug.infoOn()) Debug.log(":: getAllIdsForProcessesWithExpiriedDeadlines ::", module); 859 GenericDelegator delegator = SharkContainer.getDelegator(); 860 List processIds = new ArrayList (); 861 862 DynamicViewEntity view = new DynamicViewEntity(); 863 view.addMemberEntity("WFDL", "WfDeadline"); 864 view.addMemberEntity("WFPR", "WfProcess"); 865 view.addMemberEntity("WFAC", "WfActivity"); 866 view.addAlias("WFPR", "currentState", "processState", null, null, null, null); 867 view.addAlias("WFAC", "currentState", "activityState", null, null, null, null); 868 view.addViewLink("WFDL", "WFPR", Boolean.FALSE, ModelKeyMap.makeKeyMapList("processId")); 869 view.addViewLink("WFDL", "WFAC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("activityId")); 870 871 EntityListIterator eli = null; 872 try { 873 EntityCondition procState = new EntityExpr("processState", EntityOperator.EQUALS, "open.running"); 874 EntityCondition actState1 = new EntityExpr("activityState", EntityOperator.EQUALS, "open.not_running.not_started"); 875 EntityCondition actState2 = new EntityExpr("activityState", EntityOperator.EQUALS, "open.running"); 876 877 EntityCondition actState = new EntityConditionList(UtilMisc.toList(actState1, actState2), EntityOperator.OR); 878 EntityCondition timeCond = new EntityExpr("timeLimit", EntityOperator.LESS_THAN, new Long (l)); 879 880 EntityCondition cond = new EntityConditionList(UtilMisc.toList(timeCond, procState, actState), EntityOperator.AND); 881 eli = delegator.findListIteratorByCondition(view, cond, null, null, null, null); 882 GenericValue v = null; 883 while ((v = (GenericValue) eli.next()) != null) { 884 processIds.add(v.getString("processId")); 885 } 886 } catch (GenericEntityException e) { 887 throw new PersistenceException(e); 888 } finally { 889 if (eli != null) { 890 try { 891 eli.close(); 892 } catch (GenericEntityException e) { 893 throw new PersistenceException(e); 894 } 895 } 896 } 897 return processIds; 898 } 899 900 public List getAllDeadlinesForActivity(String procId, String actId, SharkTransaction trans) throws PersistenceException { 901 List lookupList = getDeadlineValues(UtilMisc.toList(new EntityExpr("processId", EntityOperator.EQUALS, procId), 902 new EntityExpr("activityId", EntityOperator.EQUALS, actId))); 903 return getDealineObjects(lookupList); 904 } 905 906 public List getAllDeadlinesForActivity(String procId, String actId, long timeLimit, SharkTransaction trans) throws PersistenceException { 907 List lookupList = getDeadlineValues(UtilMisc.toList(new EntityExpr("processId", EntityOperator.EQUALS, procId), 908 new EntityExpr("activityId", EntityOperator.EQUALS, actId), 909 new EntityExpr("timeLimit", EntityOperator.LESS_THAN, new Long (timeLimit)))); 910 return getDealineObjects(lookupList); 911 } 912 913 public int getExecuteCount(String procId, String asDefId, String aDefId, SharkTransaction trans) throws PersistenceException { 914 GenericDelegator delegator = SharkContainer.getDelegator(); 915 long count = 0; 916 try { 917 count = delegator.findCountByAnd("WfActivity", UtilMisc.toMap("processId", procId, "setDefinitionId", asDefId, "definitionId", aDefId)); 918 } catch (GenericEntityException e) { 919 throw new PersistenceException(e); 920 } 921 922 return (int) count; 923 } 924 925 private List getAndJoinValues(String processId, String activitySetDefId, String activityDefId) throws PersistenceException { 926 GenericDelegator delegator = SharkContainer.getDelegator(); 927 List lookupList = null; 928 try { 929 lookupList = delegator.findByAnd("WfAndJoin", UtilMisc.toMap("processId", processId, 930 "activitySetDefId", activitySetDefId, "activityDefId", activityDefId)); 931 } catch (GenericEntityException e) { 932 throw new PersistenceException(e); 933 } 934 if (lookupList == null) { 935 lookupList = new ArrayList (); 936 } 937 return lookupList; 938 } 939 940 private List getDeadlineValues(List exprList) throws PersistenceException { 941 GenericDelegator delegator = SharkContainer.getDelegator(); 942 List lookupList = null; 943 if (exprList == null) { 944 lookupList = new ArrayList (); 945 } else { 946 try { 947 lookupList = delegator.findByAnd("WfDeadline", exprList); 948 } catch (GenericEntityException e) { 949 throw new PersistenceException(e); 950 } 951 if (lookupList == null) { 952 lookupList = new ArrayList (); 953 } 954 } 955 return lookupList; 956 } 957 958 private List getDealineObjects(List deadlineValues) { 959 List deadlines = new ArrayList (); 960 if (deadlineValues != null) { 961 Iterator i = deadlineValues.iterator(); 962 while (i.hasNext()) { 963 GenericValue v = (GenericValue) i.next(); 964 deadlines.add(Deadline.getInstance(this, v)); 965 } 966 } 967 return deadlines; 968 } 969 970 public ActivityPersistenceInterface createActivity() { 972 return new Activity(this, SharkContainer.getDelegator()); 973 } 974 975 public ProcessPersistenceInterface createProcess() { 976 return new Process (this, SharkContainer.getDelegator()); 977 } 978 979 public ProcessMgrPersistenceInterface createProcessMgr() { 980 return new ProcessMgr(this, SharkContainer.getDelegator()); 981 } 982 983 public AssignmentPersistenceInterface createAssignment() { 984 return new Assignment(this, SharkContainer.getDelegator()); 985 } 986 987 public ResourcePersistenceInterface createResource() { 988 return new Resource(this, SharkContainer.getDelegator()); 989 } 990 991 public ProcessVariablePersistenceInterface createProcessVariable() { 992 return new ProcessVariable(this, SharkContainer.getDelegator()); 993 } 994 995 public ActivityVariablePersistenceInterface createActivityVariable() { 996 return new ActivityVariable(this, SharkContainer.getDelegator()); 997 } 998 999 public AndJoinEntryInterface createAndJoinEntry() { 1000 return new AndJoinEntry(this, SharkContainer.getDelegator()); 1001 } 1002 1003 public DeadlinePersistenceInterface createDeadline() { 1004 return new Deadline(this, SharkContainer.getDelegator()); 1005 } 1006 1007 public synchronized String getNextId(String string) throws PersistenceException { 1008 GenericDelegator delegator = SharkContainer.getDelegator(); 1009 return delegator.getNextSeqId("SharkWorkflowSeq").toString(); 1010 } 1011 1012 public List getAssignmentsWhere(SharkTransaction trans, String sqlWhere) throws PersistenceException { 1013 if (sqlWhere != null) { 1014 Debug.log("Attempt to call getAssignmentsWhere() - " + sqlWhere, module); 1015 throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")"); 1016 } 1017 return this.getAllAssignments(trans); 1018 } 1019 1020 public List getProcessesWhere(SharkTransaction trans, String sqlWhere) throws PersistenceException { 1021 if (sqlWhere != null) { 1022 Debug.log("Attempt to call getProcessesWhere() - " + sqlWhere, module); 1023 throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")"); 1024 } 1025 return this.getAllProcesses(trans); 1026 } 1027 1028 public List getActivitiesWhere(SharkTransaction trans, String sqlWhere) throws PersistenceException { 1029 if (sqlWhere != null) { 1030 Debug.log("Attempt to call getActivitiesWhere() - " + sqlWhere, module); 1031 throw new PersistenceException("Method not available to this implementation! (" + sqlWhere + ")"); 1032 } 1033 return this.getAllActivities(trans); 1034 } 1035} 1036 | Popular Tags |