Beispiel #1
0
 public void complete(IActivityInstance targetActivityInstance)
 {
     ITaskInstanceManager taskInstanceMgr = this.RuntimeContext.TaskInstanceManager;
     taskInstanceMgr.completeTaskInstance(this.CurrentWorkflowSession, this.AliveProcessInstance, this, targetActivityInstance);
     //        taskInstanceMgr.completeTaskInstance(this, targetActivityInstance);
 }
        //throws EngineException, KernelException
        //fireflow.engine.taskinstance.ITaskInstanceManager#createTaskInstances(org.fireflow.kenel.IActivityInstance)
        public void createTaskInstances(IToken token, IActivityInstance activityInstance)
        {
            // TODO Auto-generated method stub
            Activity activity = activityInstance.Activity;
            IPersistenceService persistenceService = this.RuntimeContext.PersistenceService;
            ICalendarService calService = this.RuntimeContext.CalendarService;

            IProcessInstance processInstance = token.ProcessInstance;
            WorkflowSession workflowSession = (WorkflowSession)((IWorkflowSessionAware)processInstance).CurrentWorkflowSession;

            if (workflowSession == null)
            {
                throw new EngineException(token.ProcessInstance,
                        activityInstance.Activity,
                        "The workflow session in process instance can NOT be null");
            }

            int createdTaskInstanceCount = 0;
            for (int i = 0; i < activity.getTasks().Count; i++)
            {
                Task task = activity.getTasks()[i];
                // 1、创建Task实例,并设置工作流系统定义的属性
                ITaskInstance taskInstance = this.createTaskInstance(workflowSession, processInstance, task,
                        activity);

                if (taskInstance == null)
                {
                    continue;
                }
                createdTaskInstanceCount = createdTaskInstanceCount + 1;

                TaskTypeEnum taskType = task.TaskType;
                ((TaskInstance)taskInstance).TaskType=taskType;
                ((TaskInstance)taskInstance).StepNumber=token.StepNumber;

                ((TaskInstance)taskInstance).ProcessInstanceId=processInstance.Id;
                ((TaskInstance)taskInstance).ProcessId=processInstance.ProcessId;
                ((TaskInstance)taskInstance).Version=processInstance.Version;
                ((TaskInstance)taskInstance).ActivityId=activity.Id;
                if (TaskTypeEnum.FORM==taskType)
                {
                    ((TaskInstance)taskInstance).AssignmentStrategy=((FormTask)task).AssignmentStrategy;
                    ((TaskInstance)taskInstance).CanBeWithdrawn=true;
                }
                else
                {
                    ((TaskInstance)taskInstance).CanBeWithdrawn=false;
                }
                ((TaskInstance)taskInstance).CreatedTime=calService.getSysDate();
                ((TaskInstance)taskInstance).DisplayName=task.DisplayName;
                ((TaskInstance)taskInstance).Name=task.Name;

                ((TaskInstance)taskInstance).State=TaskInstanceStateEnum.INITIALIZED;

                ((TaskInstance)taskInstance).TaskId=task.Id;

                ((TaskInstance)taskInstance).FromActivityId=token.FromActivityId;

                ((IRuntimeContextAware)taskInstance).RuntimeContext = this.RuntimeContext;
                ((IWorkflowSessionAware)taskInstance).CurrentWorkflowSession=workflowSession;
                //计算超时
                Duration duration = task.Duration;

                if (duration != null && calService != null)
                {
                    ((TaskInstance)taskInstance).ExpiredTime=calService.dateAfter(calService.getSysDate(), duration);
                }

                // 2、保存实例taskInstance
                persistenceService.SaveOrUpdateTaskInstance(taskInstance);

                //3、启动实例
                this.startTaskInstance(workflowSession, processInstance, taskInstance);
            }
            if (createdTaskInstanceCount == 0)
            {
                //如果是空activity,哪么直接结束
                activityInstance.complete(token, null);
            }
        }
        /// <summary>
        /// 终止当前TaskInstance,检查是否可以中止当前ActivityInstance,如果可以,
        /// 则结束当前ActivityInstance,并触发targetActivityInstance或后继ActivityInstance
        /// </summary>
        /// <param name="currentSession"></param>
        /// <param name="processInstance"></param>
        /// <param name="taskInstance"></param>
        /// <param name="targetActivityInstance"></param>
        public void completeTaskInstance(IWorkflowSession currentSession, IProcessInstance processInstance,
            ITaskInstance taskInstance, IActivityInstance targetActivityInstance)
        {
            //如果TaskInstance处于结束状态,则直接返回
            if (taskInstance.State == TaskInstanceStateEnum.COMPLETED || taskInstance.State == TaskInstanceStateEnum.CANCELED)
            {
                return;
            }
            if (taskInstance.State == TaskInstanceStateEnum.INITIALIZED)
            {
                WorkflowProcess process = taskInstance.WorkflowProcess;
                throw new EngineException(taskInstance.ProcessInstanceId, process,
                        taskInstance.TaskId,
                        "Complete task insatance failed.The state of the task insatnce[id=" + taskInstance.Id + "] is " + taskInstance.State);
            }
            if (taskInstance.IsSuspended())
            {
                WorkflowProcess process = taskInstance.WorkflowProcess;
                throw new EngineException(taskInstance.ProcessInstanceId, process,
                        taskInstance.TaskId,
                        "Complete task insatance failed. The task instance [id=" + taskInstance.Id + "] is suspended");
            }

            if (targetActivityInstance != null)
            {
                ((TaskInstance)taskInstance).TargetActivityId=targetActivityInstance.Activity.Id;
            }

            IPersistenceService persistenceService = this.RuntimeContext.PersistenceService;

            //第一步,首先结束当前taskInstance
            if (!this.taskInstanceCanBeCompleted(currentSession, this.RuntimeContext, processInstance, taskInstance))
            {
                return;
            }
            ((TaskInstance)taskInstance).State=TaskInstanceStateEnum.COMPLETED;
            ((TaskInstance)taskInstance).CanBeWithdrawn=false;
            ((TaskInstance)taskInstance).EndTime=RuntimeContext.CalendarService.getSysDate();
            persistenceService.SaveOrUpdateTaskInstance(taskInstance);
            //触发相应的事件
            TaskInstanceEvent e = new TaskInstanceEvent();
            e.Source=taskInstance;
            e.WorkflowSession=currentSession;
            e.ProcessInstance=processInstance;
            e.EventType = TaskInstanceEventEnum.AFTER_TASK_INSTANCE_COMPLETE;
            if (this.DefaultTaskInstanceEventListener != null)
            {
                this.DefaultTaskInstanceEventListener.onTaskInstanceEventFired(e);
            }

            this.fireTaskInstanceEvent(taskInstance, e);

            //第二步,检查ActivityInstance是否可以结束
            if (!activityInstanceCanBeCompleted(taskInstance))
            {
                return;
            }

            //第三步,尝试结束对应的activityInstance
            List<IToken> tokens = persistenceService.FindTokensForProcessInstance(taskInstance.ProcessInstanceId, taskInstance.ActivityId);
            //        System.out.println("Inside TaskInstance.complete(targetActivityInstance):: tokens.size is "+tokens.Count);
            if (tokens == null || tokens.Count == 0)
            {
                return;//表明activityInstance已经结束了。
            }
            if (tokens.Count > 1)
            {
                WorkflowProcess process = taskInstance.WorkflowProcess;
                throw new EngineException(taskInstance.ProcessInstanceId, process, taskInstance.TaskId,
                        "与activityId=" + taskInstance.ActivityId + "对应的token数量(=" + tokens.Count + ")不正确,正确只能为1,因此无法完成complete操作");
            }
            IToken token = tokens[0];
            //stepNumber不相等,不允许执行结束操作。
            if (token.StepNumber != taskInstance.StepNumber)
            {
                return;
            }
            if (token.IsAlive == false)
            {
                WorkflowProcess process = taskInstance.WorkflowProcess;
                throw new EngineException(taskInstance.ProcessInstanceId, process,
                        taskInstance.TaskId,
                        "与activityId=" + taskInstance.ActivityId + "对应的token.alive=false,因此无法完成complete操作");
            }

            INetInstance netInstance = this.RuntimeContext.KernelManager.getNetInstance(taskInstance.ProcessId, taskInstance.Version);
            Object obj = netInstance.getWFElementInstance(taskInstance.ActivityId);
            if (obj == null)
            {
                WorkflowProcess process = taskInstance.WorkflowProcess;
                throw new EngineException(taskInstance.ProcessInstanceId, process, taskInstance.TaskId,
                        "系统没有找到与activityId=" + taskInstance.ActivityId + "对应activityInstance,无法执行complete操作。");
            }

            token.ProcessInstance = processInstance;

            ((IActivityInstance)obj).complete(token, targetActivityInstance);
        }
        public void completeWorkItem(IWorkItem workItem, IActivityInstance targetActivityInstance, String comments)
        {
            if (workItem.State != WorkItemEnum.RUNNING)
            {
                TaskInstance thisTaskInst = (TaskInstance)workItem.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=" + workItem.Id + "] is " + workItem.State);
            }

            if (workItem.TaskInstance.IsSuspended())
            {
                TaskInstance thisTaskInst = (TaskInstance)workItem.TaskInstance;
                WorkflowProcess process = thisTaskInst.WorkflowProcess;
                throw new EngineException(thisTaskInst.ProcessInstanceId, process, thisTaskInst.TaskId,
                        "Complete work item failed. The correspond task instance [id=" + thisTaskInst.Id + "] is suspended");
            }

            IPersistenceService persistenceService = RuntimeContext.PersistenceService;

            ((WorkItem)workItem).Comments=comments;
            ((WorkItem)workItem).State = WorkItemEnum.COMPLETED;
            ((WorkItem)workItem).EndTime=RuntimeContext.CalendarService.getSysDate();
            persistenceService.SaveOrUpdateWorkItem(workItem);

            // 触发AFTER_WORKITEM_COMPLETE事件
            TaskInstanceEvent e = new TaskInstanceEvent();
            e.Source=workItem.TaskInstance;
            e.WorkflowSession=((IWorkflowSessionAware)workItem).CurrentWorkflowSession;
            e.ProcessInstance=((TaskInstance)workItem.TaskInstance).AliveProcessInstance;
            e.EventType = TaskInstanceEventEnum.AFTER_WORKITEM_COMPLETE;
            if (this.DefaultTaskInstanceEventListener != null)
            {
                this.DefaultTaskInstanceEventListener.onTaskInstanceEventFired(e);
            }

            this.fireTaskInstanceEvent(workItem.TaskInstance, e);

            ((TaskInstance)workItem.TaskInstance).complete(targetActivityInstance);
        }
 //fireflow.engine.taskinstance.ITaskInstanceManager#archiveTaskInstances(org.fireflow.kenel.IActivityInstance)
 public void archiveTaskInstances(IActivityInstance activityInstance)
 {
     // TODO Auto-generated method stub
 }