예제 #1
0
        public static ProcessInstanceTrace GetProcessInstanceTrace(IDataReader dr)
        {
            ProcessInstanceTrace processInstanceTrace = new ProcessInstanceTrace();

                 processInstanceTrace.Id=Convert.ToString(dr["id"]);
                 processInstanceTrace.ProcessInstanceId=Convert.ToString(dr["processinstance_id"]);
                 processInstanceTrace.StepNumber=Convert.ToInt32(dr["step_number"]);
                 processInstanceTrace.MinorNumber=Convert.ToInt32(dr["minor_number"]);
                 processInstanceTrace.Type = (ProcessInstanceTraceEnum)Enum.Parse( typeof(ProcessInstanceTraceEnum), Convert.ToString(dr["type"]));

                 processInstanceTrace.EdgeId=Convert.ToString(dr["edge_id"]);
                 processInstanceTrace.FromNodeId=Convert.ToString(dr["from_node_id"]);
                 processInstanceTrace.ToNodeId=Convert.ToString(dr["to_node_id"]);

                 return processInstanceTrace;
        }
        /********************************process instance trace info **********************/
        public bool SaveOrUpdateProcessInstanceTrace(ProcessInstanceTrace processInstanceTrace)
        {
            if (String.IsNullOrEmpty(processInstanceTrace.Id))
            {
                processInstanceTrace.Id = Guid.NewGuid().ToString().Replace("-", "");
                string insert = "INSERT INTO T_FF_HIST_TRACE (" +
                    "ID, PROCESSINSTANCE_ID, STEP_NUMBER, MINOR_NUMBER, TYPE, " +
                    "EDGE_ID, FROM_NODE_ID, TO_NODE_ID )VALUES(@1, @2, @3, @4, @5, @6, @7, @8)";
                SqlParameter[] insertParms = { 
				SqlServerHelper.NewSqlParameter("@1", SqlDbType.VarChar, 50, processInstanceTrace.Id), 
				SqlServerHelper.NewSqlParameter("@2", SqlDbType.VarChar, 50, processInstanceTrace.ProcessInstanceId), 
				SqlServerHelper.NewSqlParameter("@3", SqlDbType.Int, processInstanceTrace.StepNumber), 
				SqlServerHelper.NewSqlParameter("@4", SqlDbType.Int, processInstanceTrace.MinorNumber), 
				SqlServerHelper.NewSqlParameter("@5", SqlDbType.VarChar, 15, processInstanceTrace.Type.ToString()), 
				SqlServerHelper.NewSqlParameter("@6", SqlDbType.VarChar, 100, processInstanceTrace.EdgeId), 
				SqlServerHelper.NewSqlParameter("@7", SqlDbType.VarChar, 100, processInstanceTrace.FromNodeId), 
				SqlServerHelper.NewSqlParameter("@8", SqlDbType.VarChar, 100, processInstanceTrace.ToNodeId)
			};
                if (SqlServerHelper.ExecuteNonQuery(connectionString, CommandType.Text, insert, insertParms) != 1)
                    return false;
                else return true;
            }
            else
            {
                string update = "UPDATE T_FF_HIST_TRACE SET " +
                    "PROCESSINSTANCE_ID=@2, STEP_NUMBER=@3, MINOR_NUMBER=@4, TYPE=@5, EDGE_ID=@6, " +
                    "FROM_NODE_ID=@7, TO_NODE_ID=@8" +
                    " WHERE ID=@1";
                SqlParameter[] updateParms = { 
				SqlServerHelper.NewSqlParameter("@2", SqlDbType.VarChar, 50, processInstanceTrace.ProcessInstanceId), 
				SqlServerHelper.NewSqlParameter("@3", SqlDbType.Int, processInstanceTrace.StepNumber), 
				SqlServerHelper.NewSqlParameter("@4", SqlDbType.Int, processInstanceTrace.MinorNumber), 
				SqlServerHelper.NewSqlParameter("@5", SqlDbType.VarChar, 15, processInstanceTrace.Type), 
				SqlServerHelper.NewSqlParameter("@6", SqlDbType.VarChar, 100, processInstanceTrace.EdgeId), 
				SqlServerHelper.NewSqlParameter("@7", SqlDbType.VarChar, 100, processInstanceTrace.FromNodeId), 
				SqlServerHelper.NewSqlParameter("@8", SqlDbType.VarChar, 100, processInstanceTrace.ToNodeId),
				SqlServerHelper.NewSqlParameter("@1", SqlDbType.VarChar, 50, processInstanceTrace.Id)
			};
                if (SqlServerHelper.ExecuteNonQuery(connectionString, CommandType.Text, update, updateParms) != 1)
                    return false;
                else return true;
            }
        }
        /// <summary>
        /// 自由流方法扩展,去除“同一执行线”的限制
        /// </summary>
        /// <param name="workItem"></param>
        /// <param name="targetActivityId"></param>
        /// <param name="comments"></param>
        public void completeWorkItemAndJumpToEx(IWorkItem workItem, String targetActivityId, String comments)
        {
            // 首先检查是否可以正确跳转
            WorkflowProcess workflowProcess = workItem.TaskInstance.WorkflowProcess;
            String thisActivityId = workItem.TaskInstance.ActivityId;
            TaskInstance thisTaskInst = (TaskInstance)workItem.TaskInstance;
            //如果是在同一条执行线上,那么可以直接跳过去,只是重复判断了是否在同一条执行线上
            Boolean isInSameLine = workflowProcess.isInSameLine(thisActivityId, targetActivityId);
            if (isInSameLine)
            {
                this.completeWorkItemAndJumpTo(workItem, targetActivityId, comments);
                return;
            }

            // 1)检查是否在同一个“执行线”上(关闭该检查,20091002)
            //		if (!isInSameLine) {
            //			throw new EngineException(
            //					thisTaskInst.ProcessInstanceId,
            //					thisTaskInst.WorkflowProcess,
            //					thisTaskInst.TaskId,
            //					"Jumpto refused because of the current activitgy and the target activity are NOT in the same 'Execution Thread'.");
            //		}

            // 2)检查目标Activity Form Task的数量(暂时关闭该检查项目)
            // Activity targetActivity =
            // (Activity)workflowProcess.findWFElementById(activityId);
            // int count = getFormTaskCount(targetActivity);
            // if (count!=1){
            // if (!isInSameLine) throw new
            // EngineException("Jumpto refused because of the  FORM-type-task count of the target activitgy  is NOT 1; the count is "+count);
            // }

            // 3)检查当前的 taskinstance是否可以结束
            IPersistenceService persistenceService = this.RuntimeContext.PersistenceService;

            Int32 aliveWorkItemCount = persistenceService.GetAliveWorkItemCountForTaskInstance(thisTaskInst.Id);
            if ( aliveWorkItemCount > 1)
            {
                throw new EngineException(
                        thisTaskInst.ProcessInstanceId,
                        thisTaskInst.WorkflowProcess,
                        thisTaskInst.TaskId,
                        "Jumpto refused because of current taskinstance can NOT be completed. some workitem of this taskinstance is in runing state or initialized state");
            }

            // 4)检查当前的activity instance是否可以结束
            if (workItem.TaskInstance.Activity.CompletionStrategy == FormTaskEnum.ALL)
            {
                Int32 aliveTaskInstanceCount4ThisActivity = persistenceService.GetAliveTaskInstanceCountForActivity(
                    workItem.TaskInstance.ProcessInstanceId, workItem.TaskInstance.ActivityId);
                if (aliveTaskInstanceCount4ThisActivity > 1)
                {// 大于1表明当前Activity不可以complete
                    throw new EngineException(
                            thisTaskInst.ProcessInstanceId,
                            thisTaskInst.WorkflowProcess,
                            thisTaskInst.TaskId,
                            "Jumpto refused because of current activity instance can NOT be completed. some task instance of this activity instance is in runing state or initialized state");
                }
            }

            //4)首先检查目标状态M是否存在冲突,如果存在冲突则不允许跳转;如果不存在冲突,则需要调整token
            List<IToken> allTokens = persistenceService.FindTokensForProcessInstance(thisTaskInst.ProcessInstanceId, null);
            WorkflowProcess thisProcess = thisTaskInst.WorkflowProcess;//找到当前的工作里模型
            List<String> aliveActivityIdsAfterJump = new List<String>();//计算跳转后,哪些activity节点复活
            aliveActivityIdsAfterJump.Add(targetActivityId);

            for (int i = 0; allTokens != null && i < allTokens.Count; i++)
            {
                IToken tokenTmp = allTokens[i];
                IWFElement workflowElement = thisProcess.findWFElementById(tokenTmp.NodeId); //找到拥有此token的工作流元素
                if ((workflowElement is Activity) && !workflowElement.Id.Equals(thisActivityId))
                {
                    //注意:不能自己跳转到自己,同时此工作流元素是activity类型
                    aliveActivityIdsAfterJump.Add(workflowElement.Id);

                    if (thisProcess.isReachable(targetActivityId, workflowElement.Id)
                        || thisProcess.isReachable(workflowElement.Id, targetActivityId))
                    {
                        throw new EngineException(
                                thisTaskInst.ProcessInstanceId,
                                thisTaskInst.WorkflowProcess,
                                thisTaskInst.TaskId,
                                "Jumpto refused because of the business-logic conflict!");

                    }
                }
            }

            //所有检查结束,开始执行跳转操作

            INetInstance netInstance = this.RuntimeContext.KernelManager.getNetInstance(
                    workflowProcess.Id,
                    workItem.TaskInstance.Version);
            if (netInstance == null)
            {
                throw new EngineException(thisTaskInst.ProcessInstanceId,
                        thisTaskInst.WorkflowProcess,
                        thisTaskInst.TaskId,
                        "Not find the net instance for workflow process [id=" + workflowProcess.Id + ", version=" + workItem.TaskInstance.Version + "]");
            }
            Object obj = netInstance.getWFElementInstance(targetActivityId);
            IActivityInstance targetActivityInstance = (IActivityInstance)obj;
            if (targetActivityInstance == null)
            {
                throw new EngineException(thisTaskInst.ProcessInstanceId, thisTaskInst.WorkflowProcess, thisTaskInst.TaskId,
                        "Not find the activity instance  for activity[process id=" + workflowProcess.Id + ", version=" + workItem.TaskInstance.Version + ",activity id=" + targetActivityId + "]");
            }

            if (this.RuntimeContext.IsEnableTrace)
            {
                ProcessInstanceTrace trace = new ProcessInstanceTrace();
                trace.ProcessInstanceId=workItem.TaskInstance.ProcessInstanceId;
                trace.StepNumber=workItem.TaskInstance.StepNumber + 1;
                trace.Type = ProcessInstanceTraceEnum.JUMPTO_TYPE;
                trace.FromNodeId=workItem.TaskInstance.ActivityId;
                trace.ToNodeId=targetActivityId;
                trace.EdgeId="";
                this.RuntimeContext.PersistenceService.SaveOrUpdateProcessInstanceTrace(trace);
            }

            //调整token布局
            List<Synchronizer> allSynchronizersAndEnds = new List<Synchronizer>();
            allSynchronizersAndEnds.AddRange(thisProcess.Synchronizers);
            allSynchronizersAndEnds.AddRange((IEnumerable<Synchronizer>)thisProcess.EndNodes.ToArray());
            for (int i = 0; i < allSynchronizersAndEnds.Count; i++)
            {
                Synchronizer synchronizer = allSynchronizersAndEnds[i];

                int volumn = 0;
                if (synchronizer is EndNode)
                {
                    volumn = synchronizer.EnteringTransitions.Count;
                }
                else
                {
                    volumn = synchronizer.EnteringTransitions.Count * synchronizer.LeavingTransitions.Count;
                }
                IToken tokenTmp = new Token();
                tokenTmp.NodeId = synchronizer.Id;
                tokenTmp.IsAlive = false;
                tokenTmp.ProcessInstanceId = thisTaskInst.ProcessInstanceId;
                tokenTmp.StepNumber = -1;

                List<String> incomingTransitionIds = new List<String>();
                Boolean reachable = false;
                List<Transition> enteringTrans = synchronizer.EnteringTransitions;
                for (int m = 0; m < aliveActivityIdsAfterJump.Count; m++)
                {
                    String aliveActivityId = aliveActivityIdsAfterJump[m];
                    if (thisProcess.isReachable(aliveActivityId, synchronizer.Id))
                    {
                        Transition trans = null;
                        reachable = true;
                        for (int j = 0; j < enteringTrans.Count; j++)
                        {
                            trans = enteringTrans[j];
                            Node fromNode = trans.FromNode;
                            if (thisProcess.isReachable(aliveActivityId, fromNode.Id))
                            {
                                if (!incomingTransitionIds.Contains(trans.Id))
                                {
                                    incomingTransitionIds.Add(trans.Id);
                                }
                            }
                        }
                    }
                }
                if (reachable)
                {
                    tokenTmp.Value = volumn - (incomingTransitionIds.Count * volumn / enteringTrans.Count);

                    IToken virtualToken = getJoinInfo(allTokens, synchronizer.Id); //获取一个虚拟的综合性token

                    if (virtualToken != null)
                    {
                        persistenceService.DeleteTokensForNode(thisTaskInst.ProcessInstanceId, synchronizer.Id);
                    }

                    if (tokenTmp.Value != 0)
                    {
                        tokenTmp.ProcessInstance = thisTaskInst.AliveProcessInstance;
                        persistenceService.SaveOrUpdateToken(tokenTmp);
                    }
                }
            }
            this.completeWorkItem(workItem, targetActivityInstance, comments);
        }
        public void completeWorkItemAndJumpTo(IWorkItem workItem, String targetActivityId, String comments)
        {
            WorkflowSession workflowSession = (WorkflowSession)((IWorkflowSessionAware)workItem).CurrentWorkflowSession;
            //首先检查是否可以正确跳转
            //1)检查是否在同一个“执行线”上
            WorkflowProcess workflowProcess = workItem.TaskInstance.WorkflowProcess;
            String thisActivityId = workItem.TaskInstance.ActivityId;
            Boolean isInSameLine = workflowProcess.isInSameLine(thisActivityId, targetActivityId);
            TaskInstance thisTaskInst = (TaskInstance)workItem.TaskInstance;
            if (!isInSameLine)
            {
                throw new EngineException(thisTaskInst.ProcessInstanceId, thisTaskInst.WorkflowProcess,
                        thisTaskInst.TaskId, "Jumpto refused because of the current activitgy and the target activity are NOT in the same 'Execution Thread'.");
            }

            //2)检查目标Activity Form Task的数量(暂时关闭该检查项目)
            //        Activity targetActivity = (Activity)workflowProcess.findWFElementById(activityId);
            //        int count = getFormTaskCount(targetActivity);
            //        if (count!=1){
            //            if (!isInSameLine) throw new EngineException("Jumpto refused because of the  FORM-type-task count of the target activitgy  is NOT 1; the count is "+count);
            //        }

            //3)检查当前的 taskinstance是否可以结束
            IPersistenceService persistenceService = RuntimeContext.PersistenceService;

            Int32 aliveWorkItemCount = persistenceService.GetAliveWorkItemCountForTaskInstance(thisTaskInst.Id);
            if (aliveWorkItemCount > 1)
            {
                throw new EngineException(thisTaskInst.ProcessInstanceId, thisTaskInst.WorkflowProcess,
                        thisTaskInst.TaskId, "Jumpto refused because of current taskinstance can NOT be completed. some workitem of this taskinstance is in runing state or initialized state");
            }

            //4)检查当前的activity instance是否可以结束
            if (workItem.TaskInstance.Activity.CompletionStrategy == FormTaskEnum.ALL)
            {
                Int32 aliveTaskInstanceCount4ThisActivity = persistenceService.GetAliveTaskInstanceCountForActivity(workItem.TaskInstance.ProcessInstanceId, workItem.TaskInstance.ActivityId);
                if (aliveTaskInstanceCount4ThisActivity > 1)
                {//大于2表明当前Activity不可以complete
                    throw new EngineException(thisTaskInst.ProcessInstanceId, thisTaskInst.WorkflowProcess,
                            thisTaskInst.TaskId, "Jumpto refused because of current activity instance can NOT be completed. some task instance of this activity instance is in runing state or initialized state");
                }
            }

            INetInstance netInstance = RuntimeContext.KernelManager.getNetInstance(workflowProcess.Id, workItem.TaskInstance.Version);
            if (netInstance == null)
            {
                throw new EngineException(thisTaskInst.ProcessInstanceId, thisTaskInst.WorkflowProcess,
                        thisTaskInst.TaskId, "Not find the net instance for workflow process [id=" + workflowProcess.Id + ", version=" + workItem.TaskInstance.Version + "]");
            }
            Object obj = netInstance.getWFElementInstance(targetActivityId);
            IActivityInstance targetActivityInstance = (IActivityInstance)obj;
            if (targetActivityInstance == null)
            {
                throw new EngineException(thisTaskInst.ProcessInstanceId, thisTaskInst.WorkflowProcess, thisTaskInst.TaskId,
                    "Not find the activity instance  for activity[process id=" + workflowProcess.Id + ", version=" + workItem.TaskInstance.Version + ",activity id=" + targetActivityId + "]");
            }

            if (this.RuntimeContext.IsEnableTrace)
            {
                ProcessInstanceTrace trace = new ProcessInstanceTrace();
                trace.ProcessInstanceId=workItem.TaskInstance.ProcessInstanceId;
                trace.StepNumber=workItem.TaskInstance.StepNumber + 1;
                trace.Type = ProcessInstanceTraceEnum.JUMPTO_TYPE;
                trace.FromNodeId=workItem.TaskInstance.ActivityId;
                trace.ToNodeId=targetActivityId;
                trace.EdgeId="";
                RuntimeContext.PersistenceService.SaveOrUpdateProcessInstanceTrace(trace);
            }

            this.completeWorkItem(workItem, targetActivityInstance, 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 rejectWorkItem(IWorkItem workItem, String comments)
        {
            Activity thisActivity = workItem.TaskInstance.Activity;
            TaskInstance thisTaskInstance = (TaskInstance)workItem.TaskInstance;
            if ((int)workItem.State > 5 || workItem.TaskInstance.IsSuspended())
            {//处于非活动状态,或者被挂起,则不允许reject
                throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId,
                        "Reject operation refused!Current work item is completed or the correspond task instance is suspended!!");
            }
            //当前Activity只允许一个Form类型的Task
            if (thisActivity.getTasks().Count > 1)
            {
                throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId,
                        "Reject operation refused!The correspond activity has more than 1 tasks");
            }
            //汇签Task不允许Reject
            if (FormTaskEnum.ALL == thisTaskInstance.AssignmentStrategy)
            {
                throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId,
                        "Reject operation refused!The assignment strategy is 'ALL'");
            }
            //----added by wmj2003 20090915 ---start---
            //处理拒收的边界问题
            if (thisTaskInstance.FromActivityId == TokenFrom.FROM_START_NODE)
            {
                throw new EngineException(
                                thisTaskInstance.ProcessInstanceId,
                                thisTaskInstance.WorkflowProcess,
                                thisTaskInstance.TaskId,
                                "Reject operation refused!Because the from activityId equals " + TokenFrom.FROM_START_NODE);
            }
            //----added by wmj2003 20090915 ---end---

            IPersistenceService persistenceService = this.RuntimeContext.PersistenceService;
            List<ITaskInstance> siblingTaskInstancesList = null;

            siblingTaskInstancesList = persistenceService.FindTaskInstancesForProcessInstanceByStepNumber(
                workItem.TaskInstance.ProcessInstanceId, thisTaskInstance.StepNumber);

            //如果执行了split操作,则不允许reject
            if (siblingTaskInstancesList.Count > 1)
            {
                throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess, thisTaskInstance.TaskId,
                    "Reject operation refused!Because the process instance has taken a split operation.");
            }

            //检查From Activity中是否有ToolTask和SubflowTask
            List<String> fromActivityIdList = new List<String>();
            String[] tokenizer = thisTaskInstance.FromActivityId.Split(new String[] { TokenFrom.FROM_ACTIVITY_ID_SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);//)new string[]{});
            fromActivityIdList.AddRange(tokenizer);
            //StringTokenizer tokenizer = new StringTokenizer(thisTaskInstance.FromActivityId, IToken.FROM_ACTIVITY_ID_SEPARATOR);
            //while (tokenizer.hasMoreTokens()) {
            //    fromActivityIdList.add(tokenizer.nextToken());
            //}
            WorkflowProcess workflowProcess = workItem.TaskInstance.WorkflowProcess;
            for (int i = 0; i < fromActivityIdList.Count; i++)
            {
                String fromActivityId = (String)fromActivityIdList[i];
                Activity fromActivity = (Activity)workflowProcess.findWFElementById(fromActivityId);
                List<Task> fromTaskList = fromActivity.getTasks();
                for (int j = 0; j < fromTaskList.Count; j++)
                {
                    Task task = (Task)fromTaskList[j];
                    if (TaskTypeEnum.TOOL == task.TaskType || TaskTypeEnum.SUBFLOW == task.TaskType)
                    {
                        throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess,
                                thisTaskInstance.TaskId, "Reject operation refused!The previous activity contains tool-task or subflow-task");
                    }
                }
            }
            //恢复所有的FromTaskInstance
            INetInstance netInstance = RuntimeContext.KernelManager.getNetInstance(workflowProcess.Id, workItem.TaskInstance.Version);
            if (netInstance == null)
            {
                throw new EngineException(thisTaskInstance.ProcessInstanceId, thisTaskInstance.WorkflowProcess,
                        thisTaskInstance.TaskId, "Not find the net instance for workflow process [id=" + workflowProcess.Id + ", version=" + workItem.TaskInstance.Version + "]");
            }

            //执行reject操作。
            IWorkflowSession session = ((IWorkflowSessionAware)workItem).CurrentWorkflowSession;
            session.setWithdrawOrRejectOperationFlag(true);
            int newStepNumber = (int)thisTaskInstance.StepNumber + 1;
            try
            {
                //首先将本WorkItem和TaskInstance cancel掉。
                workItem.Comments=comments;
                ((WorkItem)workItem).State = WorkItemEnum.CANCELED;
                ((WorkItem)workItem).EndTime=this.RuntimeContext.CalendarService.getSysDate();
                this.RuntimeContext.PersistenceService.SaveOrUpdateWorkItem(workItem);

                persistenceService.AbortTaskInstance(thisTaskInstance);

                //删除本环节的token
                persistenceService.DeleteTokensForNode(thisTaskInstance.ProcessInstanceId, thisTaskInstance.ActivityId);

                IActivityInstance fromActivityInstance = null;
                for (int i = 0; i < fromActivityIdList.Count; i++)
                {
                    String fromActivityId = (String)fromActivityIdList[i];
                    Object obj = netInstance.getWFElementInstance(fromActivityId);
                    fromActivityInstance = (IActivityInstance)obj;
                    Token newToken = new Token();
                    ((Token)newToken).IsAlive = true;
                    ((Token)newToken).NodeId = fromActivityId;
                    newToken.ProcessInstanceId = thisTaskInstance.ProcessInstanceId;
                    newToken.ProcessInstance = ((TaskInstance)thisTaskInstance).AliveProcessInstance;
                    newToken.FromActivityId = thisTaskInstance.ActivityId;
                    newToken.StepNumber = newStepNumber;
                    newToken.Value = 0;
                    persistenceService.SaveOrUpdateToken(newToken);

                    this.createTaskInstances(newToken, fromActivityInstance);

                    if (this.RuntimeContext.IsEnableTrace)
                    {
                        ProcessInstanceTrace trace = new ProcessInstanceTrace();
                        trace.ProcessInstanceId=thisTaskInstance.ProcessInstanceId;
                        trace.StepNumber=newStepNumber;
                        trace.Type=ProcessInstanceTraceEnum.REJECT_TYPE;
                        trace.FromNodeId=thisActivity.Id;
                        trace.ToNodeId=fromActivityId;
                        trace.EdgeId="";
                        this.RuntimeContext.PersistenceService.SaveOrUpdateProcessInstanceTrace(trace);
                    }
                }

                ITransitionInstance theLeavingTransitionInstance = (ITransitionInstance)fromActivityInstance.LeavingTransitionInstances[0];
                ISynchronizerInstance synchronizerInstance = (ISynchronizerInstance)theLeavingTransitionInstance.LeavingNodeInstance;
                if (synchronizerInstance.EnteringTransitionInstances.Count > fromActivityIdList.Count)
                {
                    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 reject)";
                    supplementToken.StepNumber = (int)thisTaskInstance.StepNumber + 1;
                    supplementToken.Value = synchronizerInstance.Volume - theLeavingTransitionInstance.Weight * fromActivityIdList.Count;
                    persistenceService.SaveOrUpdateToken(supplementToken);
                }
            }
            finally
            {
                session.setWithdrawOrRejectOperationFlag(false);
            }
        }
        /********************************process instance trace info **********************/
        public bool SaveOrUpdateProcessInstanceTrace(ProcessInstanceTrace processInstanceTrace) {
            if (String.IsNullOrEmpty(processInstanceTrace.Id))
            {
                processInstanceTrace.Id = Guid.NewGuid().ToString().Replace("-", "");
                string insert = "INSERT INTO T_FF_HIST_TRACE (" +
                    "ID, PROCESSINSTANCE_ID, STEP_NUMBER, MINOR_NUMBER, TYPE, " +
                    "EDGE_ID, FROM_NODE_ID, TO_NODE_ID )VALUES(:1, :2, :3, :4, :5, :6, :7, :8)";
                OracleParameter[] insertParms = { 
				OracleHelper.NewOracleParameter(":1", OracleType.VarChar, 50, processInstanceTrace.Id), 
				OracleHelper.NewOracleParameter(":2", OracleType.VarChar, 50, processInstanceTrace.ProcessInstanceId), 
				OracleHelper.NewOracleParameter(":3", OracleType.Int32, processInstanceTrace.StepNumber), 
				OracleHelper.NewOracleParameter(":4", OracleType.Int32, processInstanceTrace.MinorNumber), 
				OracleHelper.NewOracleParameter(":5", OracleType.VarChar, 15, processInstanceTrace.Type.ToString()), 
				OracleHelper.NewOracleParameter(":6", OracleType.VarChar, 100, processInstanceTrace.EdgeId), 
				OracleHelper.NewOracleParameter(":7", OracleType.VarChar, 100, processInstanceTrace.FromNodeId), 
				OracleHelper.NewOracleParameter(":8", OracleType.VarChar, 100, processInstanceTrace.ToNodeId)
			};
                if (OracleHelper.ExecuteNonQuery(connectionString, CommandType.Text, insert, insertParms) != 1)
                    return false;
                else return true;
            }
            else
            {
                string update = "UPDATE T_FF_HIST_TRACE SET " +
                    "PROCESSINSTANCE_ID=:2, STEP_NUMBER=:3, MINOR_NUMBER=:4, TYPE=:5, EDGE_ID=:6, " +
                    "FROM_NODE_ID=:7, TO_NODE_ID=:8" +
                    " WHERE ID=:1";
                OracleParameter[] updateParms = { 
				OracleHelper.NewOracleParameter(":2", OracleType.VarChar, 50, processInstanceTrace.ProcessInstanceId), 
				OracleHelper.NewOracleParameter(":3", OracleType.Int32, processInstanceTrace.StepNumber), 
				OracleHelper.NewOracleParameter(":4", OracleType.Int32, processInstanceTrace.MinorNumber), 
				OracleHelper.NewOracleParameter(":5", OracleType.VarChar, 15, processInstanceTrace.Type), 
				OracleHelper.NewOracleParameter(":6", OracleType.VarChar, 100, processInstanceTrace.EdgeId), 
				OracleHelper.NewOracleParameter(":7", OracleType.VarChar, 100, processInstanceTrace.FromNodeId), 
				OracleHelper.NewOracleParameter(":8", OracleType.VarChar, 100, processInstanceTrace.ToNodeId),
				OracleHelper.NewOracleParameter(":1", OracleType.VarChar, 50, processInstanceTrace.Id)
			};
                if (OracleHelper.ExecuteNonQuery(connectionString, CommandType.Text, update, updateParms) != 1)
                    return false;
                else return true;
            }
        }
예제 #8
0
        /// <summary>节点实例监听器</summary>
        public void onEdgeInstanceEventFired(EdgeInstanceEvent e)
        {
            if (e.EventType == EdgeInstanceEventEnum.ON_TAKING_THE_TOKEN)
            {
                IToken token = e.Token;
                // 计算token的alive值
                ILoopInstance transInst = (ILoopInstance)e.getSource();
                String condition = transInst.Loop.Condition;

                calculateTheAliveValue(token, condition);

                if (this.RuntimeContext.IsEnableTrace && token.IsAlive)
                {
                    ProcessInstanceTrace trace = new ProcessInstanceTrace();
                    trace.ProcessInstanceId=e.Token.ProcessInstanceId;
                    trace.StepNumber=e.Token.StepNumber + 1;
                    trace.Type=ProcessInstanceTraceEnum.LOOP_TYPE;
                    trace.FromNodeId=transInst.Loop.FromNode.Id;
                    trace.ToNodeId=transInst.Loop.ToNode.Id;
                    trace.EdgeId=transInst.Loop.Id;
                    //TODO wmj2003 一旦token从当前边上经过,那么就保存流程运行轨迹,这里应该是insert
                    RuntimeContext.PersistenceService.SaveOrUpdateProcessInstanceTrace(trace);
                }
            }
        }
        /// <summary>节点实例监听器</summary>
        public void onEdgeInstanceEventFired(EdgeInstanceEvent e)
        {
            if (e.EventType == EdgeInstanceEventEnum.ON_TAKING_THE_TOKEN)
            {
                IToken token = e.Token;
                ITransitionInstance transInst = (ITransitionInstance)e.getSource();
                String condition = transInst.Transition.Condition;
                calculateTheAliveValue(token, condition);

                if (this.RuntimeContext.IsEnableTrace && token.IsAlive)
                {
                    Transition transition = transInst.Transition;
                    IWFElement fromNode = transition.FromNode;
                    int minorNumber = 1;
                    if (fromNode is Activity)
                    {
                        minorNumber = 2;
                    }
                    else
                    {
                        minorNumber = 1;
                    }

                    ProcessInstanceTrace trace = new ProcessInstanceTrace();
                    trace.ProcessInstanceId=e.Token.ProcessInstanceId;
                    trace.StepNumber=e.Token.StepNumber;
                    trace.Type = ProcessInstanceTraceEnum.TRANSITION_TYPE;
                    trace.FromNodeId=transInst.Transition.FromNode.Id;
                    trace.ToNodeId=transInst.Transition.ToNode.Id;
                    trace.EdgeId=transInst.Transition.Id;
                    trace.MinorNumber=minorNumber;
                    //TODO wmj2003 这里应该是insert。一旦token从当前边上经过,那么就保存流程运行轨迹.
                    RuntimeContext.PersistenceService.SaveOrUpdateProcessInstanceTrace(trace);
                }
            }
        }