📄 abstractworkflow.java
字号:
// (get only current steps that have a result to this join) Collection joinSteps = new ArrayList(); joinSteps.add(step); //currentSteps = store.findCurrentSteps(id); // shouldn't need to refresh the list for (Iterator iterator = currentSteps.iterator(); iterator.hasNext();) { Step currentStep = (Step) iterator.next(); if (currentStep.getId() != step.getId()) { StepDescriptor stepDesc = wf.getStep(currentStep.getStepId()); if (stepDesc.resultsInJoin(theResults[0].getJoin())) { joinSteps.add(currentStep); } } } //we also need to check history steps that were finished before this one //that might be part of the join List historySteps = store.findHistorySteps(entry.getId()); for (Iterator i = historySteps.iterator(); i.hasNext();) { Step historyStep = (Step) i.next(); if (historyStep.getId() != step.getId()) { StepDescriptor stepDesc = wf.getStep(historyStep.getStepId()); if (stepDesc.resultsInJoin(theResults[0].getJoin())) { joinSteps.add(historyStep); } } } JoinNodes jn = new JoinNodes(joinSteps); transientVars.put("jn", jn); //todo verify that 0 is the right value for currentstep here if (passesConditions(null, joinDesc.getConditions(), Collections.unmodifiableMap(transientVars), ps, 0)) { // move the rest without creating a new step ... ResultDescriptor joinresult = joinDesc.getResult(); if (joinresult.getValidators().size() > 0) { verifyInputs(entry, joinresult.getValidators(), Collections.unmodifiableMap(transientVars), ps); } // now execute the pre-functions for (Iterator iterator = joinresult.getPreFunctions().iterator(); iterator.hasNext();) { FunctionDescriptor function = (FunctionDescriptor) iterator.next(); executeFunction(function, transientVars, ps); } long[] previousIds = new long[joinSteps.size()]; int i = 1; for (Iterator iterator = joinSteps.iterator(); iterator.hasNext();) { Step currentStep = (Step) iterator.next(); if (currentStep.getId() != step.getId()) { //if this is already a history step (eg, for all join steps completed prior to this one), //we don't move it, since it's already history. if (!historySteps.contains(currentStep)) { store.moveToHistory(currentStep); } previousIds[i] = currentStep.getId(); i++; } } if (!action.isFinish()) { // ... now finish this step normally previousIds[0] = step.getId(); theResults[0] = joinDesc.getResult(); //we pass in null for the current step since we've already moved it to history above createNewCurrentStep(joinDesc.getResult(), entry, store, action.getId(), null, previousIds, transientVars, ps); } // now execute the post-functions for (Iterator iterator = joinresult.getPostFunctions().iterator(); iterator.hasNext();) { FunctionDescriptor function = (FunctionDescriptor) iterator.next(); executeFunction(function, transientVars, ps); } } } else { // normal finish, no splits or joins long[] previousIds = null; if (step != null) { previousIds = new long[] {step.getId()}; } if (!action.isFinish()) { createNewCurrentStep(theResults[0], entry, store, action.getId(), step, previousIds, transientVars, ps); } } // postFunctions (BOTH) if (extraPostFunctions != null) { for (Iterator iterator = extraPostFunctions.iterator(); iterator.hasNext();) { FunctionDescriptor function = (FunctionDescriptor) iterator.next(); executeFunction(function, transientVars, ps); } } List postFunctions = action.getPostFunctions(); for (Iterator iterator = postFunctions.iterator(); iterator.hasNext();) { FunctionDescriptor function = (FunctionDescriptor) iterator.next(); executeFunction(function, transientVars, ps); } //if executed action was an initial action then workflow is activated if ((wf.getInitialAction(action.getId()) != null) && (entry.getState() != WorkflowEntry.ACTIVATED)) { changeEntryState(entry.getId(), WorkflowEntry.ACTIVATED); } //if it's a finish action, then we halt if (action.isFinish()) { completeEntry(action, entry.getId(), getCurrentSteps(entry.getId()), WorkflowEntry.COMPLETED); return true; } //get available autoexec actions int[] availableAutoActions = getAvailableAutoActions(entry.getId(), inputs); //we perform the first autoaction that applies, not all of them. if (availableAutoActions.length > 0) { doAction(entry.getId(), availableAutoActions[0], inputs); } return false; } /** * Validates input against a list of ValidatorDescriptor objects. * * @param entry the workflow instance * @param validators the list of ValidatorDescriptors * @param transientVars the transientVars * @param ps the persistence variables * @throws InvalidInputException if the input is deemed invalid by any validator */ protected void verifyInputs(WorkflowEntry entry, List validators, Map transientVars, PropertySet ps) throws WorkflowException { for (Iterator iterator = validators.iterator(); iterator.hasNext();) { ValidatorDescriptor input = (ValidatorDescriptor) iterator.next(); if (input != null) { String type = input.getType(); HashMap args = new HashMap(input.getArgs()); for (Iterator iterator2 = args.entrySet().iterator(); iterator2.hasNext();) { Map.Entry mapEntry = (Map.Entry) iterator2.next(); mapEntry.setValue(getConfiguration().getVariableResolver().translateVariables((String) mapEntry.getValue(), transientVars, ps)); } Validator validator = getResolver().getValidator(type, args); if (validator == null) { String message = "Could not load validator class"; context.setRollbackOnly(); throw new WorkflowException(message); } try { validator.validate(transientVars, args, ps); } catch (InvalidInputException e) { throw e; } catch (Exception e) { context.setRollbackOnly(); if (e instanceof WorkflowException) { throw (WorkflowException) e; } String message = "An unknown exception occured executing Validator " + validator; throw new WorkflowException(message, e); } } } } /** * check if an action is available or not * @param action The action descriptor * @return true if the action is available */ private boolean isActionAvailable(ActionDescriptor action, Map transientVars, PropertySet ps, int stepId) throws WorkflowException { if (action == null) { return false; } WorkflowDescriptor wf = getWorkflowDescriptorForAction(action); Map cache = (Map) stateCache.get(); Boolean result = null; if (cache != null) { result = (Boolean) cache.get(action); } else { cache = new HashMap(); stateCache.set(cache); } if (result == null) { RestrictionDescriptor restriction = action.getRestriction(); ConditionsDescriptor conditions = null; if (restriction != null) { conditions = restriction.getConditionsDescriptor(); } result = new Boolean(passesConditions(wf.getGlobalConditions(), new HashMap(transientVars), ps, stepId) && passesConditions(conditions, new HashMap(transientVars), ps, stepId)); cache.put(action, result); } return result.booleanValue(); } private Step getCurrentStep(WorkflowDescriptor wfDesc, int actionId, List currentSteps, Map transientVars, PropertySet ps) throws WorkflowException { if (currentSteps.size() == 1) { return (Step) currentSteps.get(0); } for (Iterator iterator = currentSteps.iterator(); iterator.hasNext();) { Step step = (Step) iterator.next(); ActionDescriptor action = wfDesc.getStep(step.getStepId()).getAction(actionId); //$AR init if (isActionAvailable(action, transientVars, ps, step.getStepId())) { return step; } //$AR end } return null; } private WorkflowDescriptor getWorkflowDescriptorForAction(ActionDescriptor action) { AbstractDescriptor objWfd = action; while (!(objWfd instanceof WorkflowDescriptor)) { objWfd = objWfd.getParent(); } WorkflowDescriptor wf = (WorkflowDescriptor) objWfd; return wf; } private boolean canInitialize(String workflowName, int initialAction, Map transientVars, PropertySet ps) throws WorkflowException { WorkflowDescriptor wf = getConfiguration().getWorkflow(workflowName); ActionDescriptor actionDescriptor = wf.getInitialAction(initialAction); if (actionDescriptor == null) { throw new InvalidActionException("Invalid Initial Action #" + initialAction); } RestrictionDescriptor restriction = actionDescriptor.getRestriction(); ConditionsDescriptor conditions = null; if (restriction != null) { conditions = restriction.getConditionsDescriptor(); } return passesConditions(conditions, new HashMap(transientVars), ps, 0); } private Step createNewCurrentStep(ResultDescriptor theResult, WorkflowEntry entry, WorkflowStore store, int actionId, Step currentStep, long[] previousIds, Map transientVars, PropertySet ps) throws WorkflowException { try { int nextStep = theResult.getStep(); if (nextStep == -1) { if (currentStep != null) { nextStep = currentStep.getStepId(); } else { throw new StoreException("Illegal argument: requested new current step same as current step, but current step not specified"); } } if (log.isDebugEnabled()) { log.debug("Outcome: stepId=" + nextStep + ", status=" + theResult.getStatus() + ", owner=" + theResult.getOwner() + ", actionId=" + actionId + ", currentStep=" + ((currentStep != null) ? currentStep.getStepId() : 0)); } if (previousIds == null) { previousIds = new long[0]; } String owner = theResult.getOwner(); VariableResolver variableResolver = getConfiguration().getVariableResolver(); if (owner != null) { Object o = variableResolver.translateVariables(owner, transientVars, ps); owner = (o != null) ? o.toString() : null; } String oldStatus = theResult.getOldStatus(); oldStatus = variableResolver.translateVariables(oldStatus, transientVars, ps).toString(); String status = theResult.getStatus(); status = variableResolver.translateVariables(status, transientVars, ps).toString(); if (currentStep != null) { store.markFinished(currentStep, actionId, new Date(), oldStatus, context.getCaller()); store.moveToHistory(currentStep); //store.moveToHistory(actionId, new Date(), currentStep, oldStatus, context.getCaller()); } // construct the start date and optional due date Date startDate = new Date(); Date dueDate = null; if ((theResult.getDueDate() != null) && (theResult.getDueDate().length() > 0)) { Object dueDateObject = variableResolver.translateVariables(theResult.getDueDate(), transientVars, ps); if (dueDateObject instanceof Date) { dueDate = (Date) dueDateObject; } else if (dueDateObject instanceof String) { long offset = 0; try { offset = Long.parseLong((String) dueDateObject); } catch (NumberFormatException e) { } if (offset > 0) { dueDate = new Date(startDate.getTime() + offset); } } else if (dueDateObject instanceof Number) { Number num = (Number) dueDateObject; long offset = num.longValue(); if (offset > 0) { dueDate = new Date(startDate.getTime() + offset); } } } Step newStep = store.createCurrentStep(entry.getId(), nextStep, owner, startDate, dueDate, status, previousIds); transientVars.put("createdStep", newStep); if ((previousIds != null) && (previousIds.length == 0) && (currentStep == null)) { // At this point, it must be a brand new workflow, so we'll overwrite the empty currentSteps // with an array of just this current step List currentSteps = new ArrayList(); currentSteps.add(newStep); transientVars.put("currentSteps", new ArrayList(currentSteps)); } WorkflowDescriptor descriptor = (WorkflowDescriptor) transientVars.get("descriptor"); StepDescriptor step = descriptor.getStep(nextStep); if (step == null) { throw new WorkflowException("step #" + nextStep + " does not exist"); } List preFunctions = step.getPreFunctions(); for (Iterator iterator = preFunctions.iterator(); iterator.hasNext();) { FunctionDescriptor function = (FunctionDescriptor) iterator.next(); executeFunction(function, transientVars, ps); } return newStep; } catch (WorkflowException e) { context.setRollbackOnly(); throw e; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -