public void run(IWorkflowSession currentSession, RuntimeContext runtimeContext, IProcessInstance processInstance, ITaskInstance taskInstance) // throws EngineException, KernelException { if (taskInstance.TaskType != TaskTypeEnum.FORM) //!Task.FORM.Equals(taskInstance.TaskType)) { throw new EngineException(processInstance, taskInstance.Activity, "DefaultFormTaskInstanceRunner:TaskInstance的任务类型错误,只能为FORM类型"); } DynamicAssignmentHandler dynamicAssignmentHandler = ((WorkflowSession)currentSession).consumeCurrentDynamicAssignmentHandler(); FormTask task = (FormTask)taskInstance.Task; // performer(id,name,type,handler) Participant performer = task.Performer; if (performer == null || performer.AssignmentHandler.Trim().Equals("")) { throw new EngineException(processInstance, taskInstance.Activity, "流程定义错误,Form类型的 task必须指定performer及其AssignmentHandler"); } assign(currentSession, processInstance, runtimeContext, taskInstance, task, performer, dynamicAssignmentHandler); }
/// <summary>分配, 按照当前任务的参与者插入工单</summary> /// <param name="currentSession"></param> /// <param name="processInstance"></param> /// <param name="runtimeContext"></param> /// <param name="taskInstance"></param> /// <param name="formTask"></param> /// <param name="part"></param> /// <param name="dynamicAssignmentHandler"></param> protected void assign(IWorkflowSession currentSession, IProcessInstance processInstance, RuntimeContext runtimeContext, ITaskInstance taskInstance, FormTask formTask, Participant part, DynamicAssignmentHandler dynamicAssignmentHandler)// throws EngineException, KernelException { //如果有指定的Actor,则按照指定的Actor分配任务 if (dynamicAssignmentHandler != null) { dynamicAssignmentHandler.assign((IAssignable)taskInstance, part.Name); } else { IPersistenceService persistenceService = runtimeContext.PersistenceService; List <ITaskInstance> taskInstanceList = persistenceService.FindTaskInstancesForProcessInstance(taskInstance.ProcessInstanceId, taskInstance.ActivityId); ITaskInstance theLastCompletedTaskInstance = null; for (int i = 0; taskInstanceList != null && i < taskInstanceList.Count; i++) { ITaskInstance tmp = (ITaskInstance)taskInstanceList[i]; if (tmp.Id.Equals(taskInstance.Id)) { continue; } if (!tmp.TaskId.Equals(taskInstance.TaskId)) { continue; } if (tmp.State != TaskInstanceStateEnum.COMPLETED) { continue; } if (theLastCompletedTaskInstance == null) { theLastCompletedTaskInstance = tmp; } else { if (theLastCompletedTaskInstance.StepNumber < tmp.StepNumber) { theLastCompletedTaskInstance = tmp; } } } //如果是循环且LoopStrategy==REDO,则分配个上次完成该工作的操作员 if (theLastCompletedTaskInstance != null && (LoopStrategyEnum.REDO == formTask.LoopStrategy || currentSession.isInWithdrawOrRejectOperation())) { List <IWorkItem> workItemList = persistenceService.FindCompletedWorkItemsForTaskInstance(theLastCompletedTaskInstance.Id); ITaskInstanceManager taskInstanceMgr = runtimeContext.TaskInstanceManager; for (int k = 0; k < workItemList.Count; k++) { IWorkItem completedWorkItem = (IWorkItem)workItemList[k]; IWorkItem newFromWorkItem = taskInstanceMgr.createWorkItem(currentSession, processInstance, taskInstance, completedWorkItem.ActorId); newFromWorkItem.claim();//并自动签收 } } else { IBeanFactory beanFactory = runtimeContext.BeanFactory; //从spring中获取到对应任务的Performer,创建工单 //201004 add lwz 参与者通过业务接口实现默认获取用户 switch (part.AssignmentType) { case AssignmentTypeEnum.Current: runtimeContext.AssignmentBusinessHandler.assignCurrent( currentSession, processInstance, (IAssignable)taskInstance); break; case AssignmentTypeEnum.Role: runtimeContext.AssignmentBusinessHandler.assignRole( currentSession, processInstance, (IAssignable)taskInstance, part.PerformerValue); break; case AssignmentTypeEnum.Agency: runtimeContext.AssignmentBusinessHandler.assignAgency( currentSession, processInstance, (IAssignable)taskInstance, part.PerformerValue); break; case AssignmentTypeEnum.Fixed: runtimeContext.AssignmentBusinessHandler.assignFixed( currentSession, processInstance, (IAssignable)taskInstance, part.PerformerValue); break; case AssignmentTypeEnum.Superiors: runtimeContext.AssignmentBusinessHandler.assignSuperiors( currentSession, processInstance, (IAssignable)taskInstance); break; default: IAssignmentHandler assignmentHandler = (IAssignmentHandler)beanFactory.GetBean(part.AssignmentHandler); //modified by wangmj 20090904 ((IAssignmentHandler)assignmentHandler).assign((IAssignable)taskInstance, part.PerformerValue); break; } } } }
public void jumpToEx(String targetActivityId, DynamicAssignmentHandler dynamicAssignmentHandler, String comments) { if (this._workflowSession == null) { new EngineException(this.TaskInstance.ProcessInstanceId, this.TaskInstance.WorkflowProcess, this.TaskInstance.TaskId, "The current workflow session is null."); } if (this.RuntimeContext == null) { new EngineException(this.TaskInstance.ProcessInstanceId, this.TaskInstance.WorkflowProcess, this.TaskInstance.TaskId, "The current runtime context is null."); } if (dynamicAssignmentHandler != null) { this._workflowSession.setDynamicAssignmentHandler(dynamicAssignmentHandler); } ITaskInstanceManager taskInstanceManager = this.RuntimeContext.TaskInstanceManager; taskInstanceManager.completeWorkItemAndJumpToEx(this, targetActivityId, comments); }
/* (non-Javadoc) * @see org.fireflow.engine.ITaskInstance#abortEx(java.lang.String, org.fireflow.engine.taskinstance.DynamicAssignmentHandler) */ public void abortEx(String targetActivityId, DynamicAssignmentHandler dynamicAssignmentHandler) { if (this.CurrentWorkflowSession == null) { new EngineException(this.ProcessInstanceId, this.WorkflowProcess, this.TaskId, "The current workflow session is null."); } if (this.RuntimeContext == null) { new EngineException(this.ProcessInstanceId, this.WorkflowProcess, this.TaskId, "The current runtime context is null."); } if ((this.State == TaskInstanceStateEnum.COMPLETED) || (this.State == TaskInstanceStateEnum.CANCELED)) { throw new EngineException(this.ProcessInstanceId, this.WorkflowProcess, this.TaskId, "Abort task instance failed . The state of the task instance [id=" + this.Id + "] is " + this.State); } if (dynamicAssignmentHandler != null) { this.CurrentWorkflowSession.setDynamicAssignmentHandler(dynamicAssignmentHandler); } ITaskInstanceManager taskInstanceMgr = this.RuntimeContext.TaskInstanceManager; taskInstanceMgr.abortTaskInstanceEx(this.CurrentWorkflowSession, this.AliveProcessInstance, this, targetActivityId); }
/// <summary> /// <para>结束当前WorkItem;并由工作流引擎根据流程定义决定下一步操作。引擎的执行规则如下</para> /// <para>1、工作流引擎首先判断该WorkItem对应的TaskInstance是否可以结束。</para> /// <para> 如果TaskInstance的assignment策略为ANY,或者,assignment策略为ALL且它所有的WorkItem都已经完成</para> /// <para> 则结束当前TaskInstance</para> /// <para>2、判断TaskInstance对应的ActivityInstance是否可以结束。如果ActivityInstance的complete strategy为ANY,</para> /// <para> 或者,complete strategy为ALL且他的所有的TaskInstance都已经结束,则结束当前ActivityInstance</para> /// <para>3、根据流程定义,启动下一个Activity,并创建相关的TaskInstance和WorkItem</para> /// </summary> /// <param name="dynamicAssignmentHandler">通过动态分配句柄指定下一个环节的操作者。</param> /// <param name="comments">备注信息</param> public void complete(DynamicAssignmentHandler dynamicAssignmentHandler, String comments) { if (this._workflowSession == null) { new EngineException(this.TaskInstance.ProcessInstanceId, this.TaskInstance.WorkflowProcess, this.TaskInstance.TaskId, "The current workflow session is null."); } if (this.RuntimeContext == null) { new EngineException(this.TaskInstance.ProcessInstanceId, this.TaskInstance.WorkflowProcess, this.TaskInstance.TaskId, "The current runtime context is null."); } if (this.State != WorkItemEnum.RUNNING) { TaskInstance thisTaskInst = (TaskInstance)this.TaskInstance; // System.out.println("WorkItem的当前状态为"+this.State+",不可以执行complete操作。"); throw new EngineException(thisTaskInst.ProcessInstanceId, thisTaskInst.WorkflowProcess, thisTaskInst.TaskId, "Complete work item failed . The state of the work item [id=" + this.Id + "] is " + this.State); } if (dynamicAssignmentHandler != null) { this._workflowSession.setDynamicAssignmentHandler(dynamicAssignmentHandler); } ITaskInstanceManager taskInstanceManager = this.RuntimeContext.TaskInstanceManager; taskInstanceManager.completeWorkItem(this, null, comments); }
public void completeWorkItem(String workItemId, DynamicAssignmentHandler dynamicAssignmentHandler, String comments) { IWorkItem wi = this.findWorkItemById(workItemId); wi.complete(dynamicAssignmentHandler, comments); }
public IWorkItem withdrawWorkItem(IWorkItem workItem) { Activity thisActivity = workItem.TaskInstance.Activity; TaskInstance thisTaskInstance = (TaskInstance)workItem.TaskInstance; if ((int)workItem.State < 5) {//小于5的状态为活动状态 throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId, "Withdraw operation is refused! Current workitem is in running state!!"); } //当前Activity只允许一个Form类型的Task if (thisActivity.getTasks().Count > 1) { throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId, "Withdraw operation is refused! The activity[id=" + thisActivity.Id + "] has more than 1 tasks"); } //汇签Task不允许撤销 if (FormTaskEnum.ALL == thisTaskInstance.AssignmentStrategy) { throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId, "Withdraw operation is refused! The assignment strategy for activity[id=" + thisActivity.Id + "] is 'ALL'"); } // Activity targetActivity = null; // List targetActivityList = new ArrayList(); IPersistenceService persistenceService = this.RuntimeContext.PersistenceService; List<ITaskInstance> targetTaskInstancesList = null; targetTaskInstancesList = persistenceService.FindTaskInstancesForProcessInstanceByStepNumber( thisTaskInstance.ProcessInstanceId, thisTaskInstance.StepNumber + 1); // String targetActivityId = workItem.getTaskInstance().getTargetActivityId(); //如果targetTaskInstancesList为空或size 等于0,则表示流程实例执行了汇聚操作。 if (targetTaskInstancesList == null || targetTaskInstancesList.Count == 0) { throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId, "Withdraw operation is refused!Because the process instance has taken a join operation after this activity[id=" + thisActivity.Id + "]."); } else { TaskInstance taskInstance = (TaskInstance)targetTaskInstancesList[0]; if (!taskInstance.FromActivityId.Equals(thisTaskInstance.ActivityId)) { throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId, "Withdraw operation is refused!Because the process instance has taken a join operation after this activity[id=" + thisActivity.Id + "]."); } } for (int i = 0; targetTaskInstancesList != null && i < targetTaskInstancesList.Count; i++) { TaskInstance targetTaskInstanceTmp = (TaskInstance)targetTaskInstancesList[i]; if (!targetTaskInstanceTmp.CanBeWithdrawn) { //说明已经有某些WorkItem处于已签收状态,或者已经处于完毕状态,此时不允许退回 throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId, "Withdraw operation is refused! Some task instances of the next activity[id=" + targetTaskInstanceTmp.ActivityId + "] are not in 'Initialized' state"); } } INetInstance netInstance = this.RuntimeContext.KernelManager.getNetInstance(thisTaskInstance.ProcessId, workItem.TaskInstance.Version); if (netInstance == null) { throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId, "Withdraw operation failed.Not find the net instance for workflow process [id=" + thisTaskInstance.ProcessId + ", version=" + workItem.TaskInstance.Version + "]"); } Object obj = netInstance.getWFElementInstance(thisTaskInstance.ActivityId); IActivityInstance thisActivityInstance = (IActivityInstance)obj; //一切检查通过之后进行“收回”处理 IWorkflowSession session = ((IWorkflowSessionAware)workItem).CurrentWorkflowSession; session.setWithdrawOrRejectOperationFlag(true); int newStepNumber = (int)thisTaskInstance.StepNumber + 2; try { DynamicAssignmentHandler dynamicAssignmentHandler = new DynamicAssignmentHandler(); List<String> actorIds = new List<String>(); actorIds.Add(workItem.ActorId); dynamicAssignmentHandler.ActorIdsList=actorIds; ((WorkflowSession)session).setCurrentDynamicAssignmentHandler(dynamicAssignmentHandler); //1、首先将后续环节的TaskInstance极其workItem变成Canceled状态 List<String> targetActivityIdList = new List<String>(); StringBuilder theFromActivityIds = new StringBuilder(); for (int i = 0; i < targetTaskInstancesList.Count; i++) { TaskInstance taskInstTemp = (TaskInstance)targetTaskInstancesList[i]; persistenceService.AbortTaskInstance(taskInstTemp); if (!(targetActivityIdList.IndexOf(taskInstTemp.ActivityId) >= 0)) { targetActivityIdList.Add(taskInstTemp.ActivityId); if (theFromActivityIds.Length == 0) { theFromActivityIds.Append(taskInstTemp.ActivityId); } else { theFromActivityIds.Append(TokenFrom.FROM_ACTIVITY_ID_SEPARATOR).Append(taskInstTemp.ActivityId); } } } persistenceService.DeleteTokensForNodes(thisTaskInstance.ProcessInstanceId, targetActivityIdList); if (this.RuntimeContext.IsEnableTrace) { for (int i = 0; targetActivityIdList != null && i < targetActivityIdList.Count; i++) { String tmpActId = (String)targetActivityIdList[i]; ProcessInstanceTrace trace = new ProcessInstanceTrace(); trace.ProcessInstanceId=thisTaskInstance.ProcessInstanceId; trace.StepNumber=newStepNumber; trace.Type=ProcessInstanceTraceEnum.WITHDRAW_TYPE; trace.FromNodeId=tmpActId; trace.ToNodeId=thisActivity.Id; trace.EdgeId=""; this.RuntimeContext.PersistenceService.SaveOrUpdateProcessInstanceTrace(trace); } } ITransitionInstance thisLeavingTransitionInstance = (ITransitionInstance)thisActivityInstance.LeavingTransitionInstances[0]; ISynchronizerInstance synchronizerInstance = (ISynchronizerInstance)thisLeavingTransitionInstance.LeavingNodeInstance; if (synchronizerInstance.EnteringTransitionInstances.Count > 1) { Token supplementToken = new Token(); ((Token)supplementToken).IsAlive = false; ((Token)supplementToken).NodeId = synchronizerInstance.Synchronizer.Id; supplementToken.ProcessInstanceId = thisTaskInstance.ProcessInstanceId; supplementToken.ProcessInstance = ((TaskInstance)thisTaskInstance).AliveProcessInstance; supplementToken.FromActivityId = "Empty(created by withdraw)"; supplementToken.StepNumber = newStepNumber; supplementToken.Value = synchronizerInstance.Volume - thisLeavingTransitionInstance.Weight; persistenceService.SaveOrUpdateToken(supplementToken); } Token newToken = new Token(); ((Token)newToken).IsAlive = true; ((Token)newToken).NodeId = workItem.TaskInstance.ActivityId; newToken.ProcessInstanceId = thisTaskInstance.ProcessInstanceId; newToken.ProcessInstance = ((TaskInstance)thisTaskInstance).AliveProcessInstance; newToken.FromActivityId = theFromActivityIds.ToString(); newToken.StepNumber = newStepNumber; newToken.Value = 0; persistenceService.SaveOrUpdateToken(newToken); this.createTaskInstances(newToken, thisActivityInstance); List<IWorkItem> workItems = persistenceService.FindTodoWorkItems(workItem.ActorId, workItem.TaskInstance.ProcessId, workItem.TaskInstance.TaskId); if (workItems == null || workItems.Count == 0) { throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId, "Withdraw operation failed.No work item has been created for Task[id=" + thisTaskInstance.TaskId + "]"); } if (workItems.Count > 1) { throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId, "Withdraw operation failed.More than one work item have been created for Task[id=" + thisTaskInstance.TaskId + "]"); } return (IWorkItem)workItems[0]; } finally { session.setWithdrawOrRejectOperationFlag(false); } }
public void setDynamicAssignmentHandler(DynamicAssignmentHandler dynamicAssignmentHandler) { this.dynamicAssignmentHandler = dynamicAssignmentHandler; }
public void setCurrentDynamicAssignmentHandler(DynamicAssignmentHandler handler) { this.dynamicAssignmentHandler = handler; }
public DynamicAssignmentHandler consumeCurrentDynamicAssignmentHandler() { DynamicAssignmentHandler handler = this.dynamicAssignmentHandler; this.dynamicAssignmentHandler = null; return handler; }
public void completeWorkItemAndJumpToEx(String workItemId, String targetActivityId, DynamicAssignmentHandler dynamicAssignmentHandler, String comments) { IWorkItem wi = this.findWorkItemById(workItemId); wi.jumpToEx(targetActivityId, dynamicAssignmentHandler, comments); }
// throws EngineException, KernelException /// <summary>分配, 按照当前任务的参与者插入工单</summary> /// <param name="currentSession"></param> /// <param name="processInstance"></param> /// <param name="runtimeContext"></param> /// <param name="taskInstance"></param> /// <param name="formTask"></param> /// <param name="part"></param> /// <param name="dynamicAssignmentHandler"></param> protected void assign(IWorkflowSession currentSession, IProcessInstance processInstance, RuntimeContext runtimeContext, ITaskInstance taskInstance, FormTask formTask, Participant part, DynamicAssignmentHandler dynamicAssignmentHandler) { //如果有指定的Actor,则按照指定的Actor分配任务 if (dynamicAssignmentHandler != null) { dynamicAssignmentHandler.assign((IAssignable)taskInstance, part.Name); } else { IPersistenceService persistenceService = runtimeContext.PersistenceService; List<ITaskInstance> taskInstanceList = persistenceService.FindTaskInstancesForProcessInstance(taskInstance.ProcessInstanceId, taskInstance.ActivityId); ITaskInstance theLastCompletedTaskInstance = null; for (int i = 0; taskInstanceList != null && i < taskInstanceList.Count; i++) { ITaskInstance tmp = (ITaskInstance)taskInstanceList[i]; if (tmp.Id.Equals(taskInstance.Id)) continue; if (!tmp.TaskId.Equals(taskInstance.TaskId)) continue; if (tmp.State != TaskInstanceStateEnum.COMPLETED) continue; if (theLastCompletedTaskInstance == null) { theLastCompletedTaskInstance = tmp; } else { if (theLastCompletedTaskInstance.StepNumber < tmp.StepNumber) { theLastCompletedTaskInstance = tmp; } } } //如果是循环且LoopStrategy==REDO,则分配个上次完成该工作的操作员 if (theLastCompletedTaskInstance != null && (LoopStrategyEnum.REDO==formTask.LoopStrategy || currentSession.isInWithdrawOrRejectOperation())) { List<IWorkItem> workItemList = persistenceService.FindCompletedWorkItemsForTaskInstance(theLastCompletedTaskInstance.Id); ITaskInstanceManager taskInstanceMgr = runtimeContext.TaskInstanceManager; for (int k = 0; k < workItemList.Count; k++) { IWorkItem completedWorkItem = (IWorkItem)workItemList[k]; IWorkItem newFromWorkItem = taskInstanceMgr.createWorkItem(currentSession, processInstance, taskInstance, completedWorkItem.ActorId); newFromWorkItem.claim();//并自动签收 } } else { IBeanFactory beanFactory = runtimeContext.BeanFactory; //从spring中获取到对应任务的Performer,创建工单 //201004 add lwz 参与者通过业务接口实现默认获取用户 switch (part.AssignmentType) { case AssignmentTypeEnum.Current: runtimeContext.AssignmentBusinessHandler.assignCurrent( currentSession, processInstance, (IAssignable)taskInstance); break; case AssignmentTypeEnum.Role: runtimeContext.AssignmentBusinessHandler.assignRole( currentSession, processInstance, (IAssignable)taskInstance, part.PerformerValue); break; case AssignmentTypeEnum.Agency: runtimeContext.AssignmentBusinessHandler.assignAgency( currentSession, processInstance, (IAssignable)taskInstance, part.PerformerValue); break; case AssignmentTypeEnum.Fixed: runtimeContext.AssignmentBusinessHandler.assignFixed( currentSession, processInstance, (IAssignable)taskInstance, part.PerformerValue); break; case AssignmentTypeEnum.Superiors: runtimeContext.AssignmentBusinessHandler.assignSuperiors( currentSession, processInstance, (IAssignable)taskInstance); break; default: IAssignmentHandler assignmentHandler = (IAssignmentHandler)beanFactory.GetBean(part.AssignmentHandler); //modified by wangmj 20090904 ((IAssignmentHandler)assignmentHandler).assign((IAssignable)taskInstance, part.PerformerValue); break; } } } }