退回流程运行时
Inheritance: WfRuntimeManager
Esempio n. 1
0
        /// <summary>
        /// 退回操作
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="appInstanceID"></param>
        /// <param name="processGUID"></param>
        /// <param name="userID"></param>
        /// <param name="userName"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static WfRuntimeManager CreateRuntimeInstanceSendBack(WfAppRunner runner,
                                                                     ref WfExecutedResult result)
        {
            WfRuntimeManager rmins = null;

            //检查当前运行节点信息
            var aim         = new ActivityInstanceManager();
            var runningNode = aim.GetRunningNode(runner);

            if (runningNode.ActivityType != (short)ActivityTypeEnum.TaskNode)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotTaskNode;
                result.Message       = "当前节点不是任务节点,无法退回上一步节点!";
                return(rmins);
            }

            if (!(runningNode.ActivityState == (short)ActivityStateEnum.Ready ||
                  runningNode.ActivityState == (short)ActivityStateEnum.Running))
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotInRunning;
                result.Message       = string.Format("当前节点的状态不在运行状态,无法退回上一步节点!当前节点状态:{0}",
                                                     runningNode.ActivityState);
                return(rmins);
            }

            if (aim.IsMineTask(runningNode, runner.UserID) == false)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotMineTask;
                result.Message       = "不是登录用户的任务,无法退回!";
                return(rmins);
            }

            var processInstance = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
            var processModel    = (new ProcessModel(processInstance.ProcessGUID, processInstance.Version));

            //判断是否是多实例节点
            if (runningNode.MIHostActivityInstanceID != null &&
                runningNode.MIHostActivityInstanceID.Value > 0)
            {
                var previousActivityInstance = aim.GetPreviousOfMultipleInstanceNode(runningNode.MIHostActivityInstanceID.Value,
                                                                                     runningNode.ID, runningNode.CompleteOrder.Value);

                if (previousActivityInstance != null &&
                    previousActivityInstance.EndedByUserID == runner.UserID)
                {
                    rmins = new WfRuntimeManagerSendBackMI();
                    rmins.WfExecutedResult = result = new WfExecutedResult();

                    rmins.BackwardContext.ProcessInstance                = processInstance;
                    rmins.BackwardContext.BackwardToTaskActivity         = processModel.GetActivity(previousActivityInstance.ActivityGUID);
                    rmins.BackwardContext.BackwardToTaskActivityInstance = previousActivityInstance;
                    rmins.BackwardContext.BackwardFromActivityInstance   = runningNode;
                    rmins.BackwardContext.BackwardTaskReciever           = WfBackwardTaskReciever.Instance(
                        previousActivityInstance.ActivityName,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);


                    //封装AppUser对象
                    rmins.AppRunner.AppName                = runner.AppName;
                    rmins.AppRunner.AppInstanceID          = runner.AppInstanceID;
                    rmins.AppRunner.UserID                 = runner.UserID;
                    rmins.AppRunner.UserName               = runner.UserName;
                    rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
                        previousActivityInstance.ActivityGUID,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);
                    rmins.ActivityResource = new ActivityResource(runner, rmins.AppRunner.NextActivityPerformers);
                    return(rmins);
                }
            }

            //以下部分是处理非多实例节点的情况
            //正常流转过程,无会签加签等特殊场景
            rmins = new WfRuntimeManagerSendBack();
            rmins.WfExecutedResult = result = new WfExecutedResult();

            //获取上一步流转节点信息,可能经过And, Or等路由节点
            var tim = new TransitionInstanceManager();
            var lastTaskTransitionInstance = tim.GetLastTaskTransition(runner.AppName,
                                                                       runner.AppInstanceID, runner.ProcessGUID);

            if (lastTaskTransitionInstance.TransitionType == (short)TransitionTypeEnum.Loop)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_IsLoopNode;
                result.Message       = "当前流转是自循环,无需退回!";
                return(rmins);
            }

            //设置退回节点的相关信息
            bool hasGatewayNode             = false;
            var  sendbackToActivityInstance = tim.GetPreviousActivityInstance(runningNode, true,
                                                                              out hasGatewayNode).ToList()[0];

            if (sendbackToActivityInstance.ActivityType == (short)ActivityTypeEnum.StartNode)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_PreviousIsStartNode;
                result.Message       = "上一步是开始节点,无需退回!";
                return(rmins);
            }

            rmins.ProcessModel = processModel;
            rmins.BackwardContext.ProcessInstance                = processInstance;
            rmins.BackwardContext.BackwardToTaskActivity         = processModel.GetActivity(sendbackToActivityInstance.ActivityGUID);
            rmins.BackwardContext.BackwardToTaskActivityInstance = sendbackToActivityInstance;
            rmins.BackwardContext.BackwardToTargetTransitionGUID =
                hasGatewayNode == false ? lastTaskTransitionInstance.TransitionGUID : String.Empty;        //如果中间有Gateway节点,则没有直接相连的TransitonGUID

            rmins.BackwardContext.BackwardFromActivity         = processModel.GetActivity(runningNode.ActivityGUID);
            rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
            rmins.BackwardContext.BackwardTaskReciever         = WfBackwardTaskReciever.Instance(sendbackToActivityInstance.ActivityName,
                                                                                                 sendbackToActivityInstance.EndedByUserID, sendbackToActivityInstance.EndedByUserName);

            //封装AppUser对象
            rmins.AppRunner.AppName                = runner.AppName;
            rmins.AppRunner.AppInstanceID          = runner.AppInstanceID;
            rmins.AppRunner.ProcessGUID            = runner.ProcessGUID;
            rmins.AppRunner.UserID                 = runner.UserID;
            rmins.AppRunner.UserName               = runner.UserName;
            rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(sendbackToActivityInstance.ActivityGUID,
                                                                                                   sendbackToActivityInstance.EndedByUserID,
                                                                                                   sendbackToActivityInstance.EndedByUserName);
            rmins.ActivityResource = new ActivityResource(runner, rmins.AppRunner.NextActivityPerformers);

            return(rmins);
        }
        /// <summary>
        /// 退回操作
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="appInstanceID"></param>
        /// <param name="processGUID"></param>
        /// <param name="userID"></param>
        /// <param name="userName"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static WfRuntimeManager CreateRuntimeInstanceSendBack(WfAppRunner runner,
            ref WfExecutedResult result)
        {
            WfRuntimeManager rmins = null;

            //检查当前运行节点信息
            var aim = new ActivityInstanceManager();
            var runningNode = aim.GetRunningNode(runner);

            if (runningNode.ActivityType != (short)ActivityTypeEnum.TaskNode)
            {
                result.Status = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotTaskNode;
                result.Message = "当前节点不是任务节点,无法退回上一步节点!";
                return rmins;
            }

            if (!(runningNode.ActivityState == (short)ActivityStateEnum.Ready
                || runningNode.ActivityState == (short)ActivityStateEnum.Running))
            {
                result.Status = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotInRunning;
                result.Message = string.Format("当前节点的状态不在运行状态,无法退回上一步节点!当前节点状态:{0}",
                    runningNode.ActivityState);
                return rmins;
            }

            if (aim.IsMineTask(runningNode, runner.UserID) == false)
            {
                result.Status = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_NotMineTask;
                result.Message = "不是登录用户的任务,无法退回!";
                return rmins;
            }

            var processInstance = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
            var processModel = (new ProcessModel(processInstance.ProcessGUID, processInstance.Version));

            //判断是否是多实例节点
            if (runningNode.MIHostActivityInstanceID != null
                && runningNode.MIHostActivityInstanceID.Value > 0)
            {
                var previousActivityInstance = aim.GetPreviousOfMultipleInstanceNode(runningNode.MIHostActivityInstanceID.Value,
                    runningNode.ID, runningNode.CompleteOrder.Value);

                if (previousActivityInstance != null
                        && previousActivityInstance.EndedByUserID == runner.UserID)
                {
                    rmins = new WfRuntimeManagerSendBackMI();
                    rmins.WfExecutedResult = result = new WfExecutedResult();

                    rmins.BackwardContext.ProcessInstance = processInstance;
                    rmins.BackwardContext.BackwardToTaskActivity = processModel.GetActivity(previousActivityInstance.ActivityGUID);
                    rmins.BackwardContext.BackwardToTaskActivityInstance = previousActivityInstance;
                    rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
                    rmins.BackwardContext.BackwardTaskReciever = WfBackwardTaskReciever.Instance(
                        previousActivityInstance.ActivityName,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);

                    //封装AppUser对象
                    rmins.AppRunner.AppName = runner.AppName;
                    rmins.AppRunner.AppInstanceID = runner.AppInstanceID;
                    rmins.AppRunner.UserID = runner.UserID;
                    rmins.AppRunner.UserName = runner.UserName;
                    rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
                        previousActivityInstance.ActivityGUID,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);
                    rmins.ActivityResource = new ActivityResource(runner, rmins.AppRunner.NextActivityPerformers);
                    return rmins;
                }
            }

            //以下部分是处理非多实例节点的情况
            //正常流转过程,无会签加签等特殊场景
            rmins = new WfRuntimeManagerSendBack();
            rmins.WfExecutedResult = result = new WfExecutedResult();

            //获取上一步流转节点信息,可能经过And, Or等路由节点
            var tim = new TransitionInstanceManager();
            var lastTaskTransitionInstance = tim.GetLastTaskTransition(runner.AppName,
                runner.AppInstanceID, runner.ProcessGUID);

            if (lastTaskTransitionInstance.TransitionType == (short)TransitionTypeEnum.Loop)
            {
                result.Status = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_IsLoopNode;
                result.Message = "当前流转是自循环,无需退回!";
                return rmins;
            }

            //设置退回节点的相关信息
            bool hasGatewayNode = false;
            var sendbackToActivityInstance = tim.GetPreviousActivityInstance(runningNode, true,
                out hasGatewayNode).ToList()[0];

            if (sendbackToActivityInstance.ActivityType == (short)ActivityTypeEnum.StartNode)
            {
                result.Status = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Sendback_PreviousIsStartNode;
                result.Message = "上一步是开始节点,无需退回!";
                return rmins;
            }

            rmins.ProcessModel = processModel;
            rmins.BackwardContext.ProcessInstance = processInstance;
            rmins.BackwardContext.BackwardToTaskActivity = processModel.GetActivity(sendbackToActivityInstance.ActivityGUID);
            rmins.BackwardContext.BackwardToTaskActivityInstance = sendbackToActivityInstance;
            rmins.BackwardContext.BackwardToTargetTransitionGUID =
                hasGatewayNode == false ? lastTaskTransitionInstance.TransitionGUID : String.Empty;        //如果中间有Gateway节点,则没有直接相连的TransitonGUID

            rmins.BackwardContext.BackwardFromActivity = processModel.GetActivity(runningNode.ActivityGUID);
            rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
            rmins.BackwardContext.BackwardTaskReciever = WfBackwardTaskReciever.Instance(sendbackToActivityInstance.ActivityName,
                sendbackToActivityInstance.EndedByUserID, sendbackToActivityInstance.EndedByUserName);

            //封装AppUser对象
            rmins.AppRunner.AppName = runner.AppName;
            rmins.AppRunner.AppInstanceID = runner.AppInstanceID;
            rmins.AppRunner.ProcessGUID = runner.ProcessGUID;
            rmins.AppRunner.UserID = runner.UserID;
            rmins.AppRunner.UserName = runner.UserName;
            rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(sendbackToActivityInstance.ActivityGUID,
                sendbackToActivityInstance.EndedByUserID,
                sendbackToActivityInstance.EndedByUserName);
            rmins.ActivityResource = new ActivityResource(runner, rmins.AppRunner.NextActivityPerformers);

            return rmins;
        }