예제 #1
0
        /// <summary>
        /// 创建跳转实例信息
        /// </summary>
        /// <param name="runner">执行者</param>
        /// <param name="result">结果对象</param>
        /// <returns>运行时实例对象</returns>
        public static WfRuntimeManager CreateRuntimeInstanceJump(WfAppRunner runner,
                                                                 ref WfExecutedResult result)
        {
            var rmins = new WfRuntimeManagerJump();

            rmins.WfExecutedResult = result = new WfExecutedResult();

            if (string.IsNullOrEmpty(runner.AppName) ||
                String.IsNullOrEmpty(runner.AppInstanceID) ||
                runner.ProcessGUID == null ||
                runner.NextActivityPerformers == null)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Jump_ErrorArguments;
                result.Message       = "方法参数错误,无法运行流程!";
                return(rmins);
            }

            //流程跳转时,只能跳转到一个节点
            if (runner.NextActivityPerformers.Count() > 1)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Jump_OverOneStep;
                result.Message       = string.Format("不能跳转到多个节点!节点数:{0}",
                                                     runner.NextActivityPerformers.Count());
                return(rmins);
            }

            //获取当前运行节点信息
            var            aim         = new ActivityInstanceManager();
            TaskViewEntity taskView    = null;
            var            runningNode = aim.GetRunningNode(runner, out taskView);

            //传递runner变量
            rmins.TaskView                = taskView;
            rmins.AppRunner               = runner;
            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;

            var processModel = ProcessModelFactory.Create(taskView.ProcessGUID, taskView.Version);

            rmins.ProcessModel = processModel;

            #region  考虑回跳方式
            ////获取跳转节点信息
            //var jumpActivityGUID = runner.NextActivityPerformers.First().Key;
            //var jumpActivityInstanceList = aim.GetActivityInstance(runner.AppInstanceID, runner.ProcessGUID, jumpActivityGUID);

            //if (jumpActivityInstanceList != null
            //    && jumpActivityInstanceList.Count > 0)
            //{
            //    //跳转到曾经执行过的节点上,可以作为跳回方式处理
            //    rmins.IsBackward = true;
            //    rmins.BackwardContext.ProcessInstance = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
            //    rmins.BackwardContext.BackwardToTaskActivity = processModel.GetActivity(jumpActivityGUID);

            //    //获取当前运行节点的上一步节点
            //    bool hasGatewayNode = false;
            //    var tim = new TransitionInstanceManager();
            //    var lastTaskTransitionInstance = tim.GetLastTaskTransition(runner.AppName,
            //        runner.AppInstanceID, runner.ProcessGUID);
            //    var previousActivityInstance = tim.GetPreviousActivityInstance(runningNode, true,
            //        out hasGatewayNode).ToList()[0];

            //    //仅仅是回跳到上一步节点,即按SendBack方式处理
            //    if (previousActivityInstance.ActivityGUID == jumpActivityGUID)
            //    {
            //        rmins.BackwardContext.BackwardToTaskActivityInstance = previousActivityInstance;
            //        rmins.BackwardContext.BackwardToTargetTransitionGUID =
            //            hasGatewayNode == false ? lastTaskTransitionInstance.TransitionGUID : System.Guid.Empty;        //如果中间有Gateway节点,则没有直接相连的TransitonGUID

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

            //        rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
            //            previousActivityInstance.ActivityGUID,
            //            previousActivityInstance.EndedByUserID.Value,
            //            previousActivityInstance.EndedByUserName);
            //    }
            //    else
            //    {
            //        //回跳到早前节点
            //        var jumptoActivityInstance = jumpActivityInstanceList[0];
            //        if (jumptoActivityInstance.ActivityState != (short)ActivityStateEnum.Completed)
            //        {
            //            result.Status = WfExecutedStatus.Exception;
            //            result.Exception = WfJumpException.NotActivityBackCompleted;
            //            result.Message = string.Format("回跳到的节点不在完成状态,无法重新回跳!");

            //            return rmins;
            //        }

            //        rmins.BackwardContext.BackwardToTaskActivityInstance = jumptoActivityInstance;

            //        //判断两个节点是否有Transition的定义存在
            //        var transition = processModel.GetForwardTransition(runningNode.ActivityGUID, runner.JumpbackActivityGUID.Value);
            //        rmins.BackwardContext.BackwardToTargetTransitionGUID = transition != null ? transition.TransitionGUID : System.Guid.Empty;

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

            //        rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
            //            jumptoActivityInstance.ActivityGUID,
            //            jumptoActivityInstance.EndedByUserID.Value,
            //            jumptoActivityInstance.EndedByUserName);
            //    }
            //    //获取资源数据
            //    var activityResourceBack = new ActivityResource(rmins.AppRunner,
            //        rmins.AppRunner.NextActivityPerformers,
            //        runner.Conditions);
            //    rmins.ActivityResource = activityResourceBack;
            //}
            //else
            //{
            //    //跳转到从未执行过的节点上
            //    var activityResource = new ActivityResource(runner, runner.NextActivityPerformers, runner.Conditions);
            //    rmins.ActivityResource = activityResource;
            //    rmins.RunningActivityInstance = runningNode;
            //}
            #endregion

            //跳转到从未执行过的节点上
            var activityResource = new ActivityResource(runner, runner.NextActivityPerformers, runner.Conditions);
            rmins.ActivityResource        = activityResource;
            rmins.RunningActivityInstance = runningNode;

            return(rmins);
        }
        /// <summary>
        /// 创建跳转实例信息
        /// </summary>
        /// <param name="runner">执行者</param>
        /// <param name="result">结果对象</param>
        /// <returns>运行时实例对象</returns>
        public static WfRuntimeManager CreateRuntimeInstanceJump(WfAppRunner runner,
                                                                 ref WfExecutedResult result)
        {
            var rmins = new WfRuntimeManagerJump();

            rmins.WfExecutedResult = result = new WfExecutedResult();

            if (string.IsNullOrEmpty(runner.AppName) ||
                String.IsNullOrEmpty(runner.AppInstanceID) ||
                runner.ProcessGUID == null ||
                runner.NextActivityPerformers == null)
            {
                //缺失方法参数
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Jump_ErrorArguments;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceJump.missing.error");
                return(rmins);
            }

            //流程跳转时,只能跳转到一个节点
            if (runner.NextActivityPerformers.Count() > 1)
            {
                result.Status        = WfExecutedStatus.Exception;
                result.ExceptionType = WfExceptionType.Jump_OverOneStep;
                result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceJump.jump.error",
                                                                       runner.NextActivityPerformers.Count().ToString());
                return(rmins);
            }

            //获取当前运行节点信息
            var            aim         = new ActivityInstanceManager();
            TaskViewEntity taskView    = null;
            var            runningNode = aim.GetRunningNode(runner, out taskView);

            //传递runner变量
            rmins.TaskView                = taskView;
            rmins.AppRunner               = runner;
            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;

            //用于流程注册时间调用时候的流程实例ID提供
            rmins.ProcessInstanceID = runningNode.ProcessInstanceID;

            var processModel = ProcessModelFactory.Create(taskView.ProcessGUID, taskView.Version);

            rmins.ProcessModel = processModel;

            #region 考虑回跳方式
            //获取跳转节点信息
            var jumpBackActivityGUID     = runner.NextActivityPerformers.First().Key;
            var jumpBackActivityInstance = aim.GetActivityInstanceLatest(runningNode.ProcessInstanceID, jumpBackActivityGUID);

            if (jumpBackActivityInstance != null)
            {
                //跳转到曾经执行过的节点上,可以作为跳回方式处理
                rmins.IsBackward = true;
                rmins.BackwardContext.ProcessInstance        = (new ProcessInstanceManager()).GetById(runningNode.ProcessInstanceID);
                rmins.BackwardContext.BackwardToTaskActivity = processModel.GetActivity(jumpBackActivityGUID);

                //获取当前运行节点的上一步节点
                bool hasGatewayNode             = false;
                var  tim                        = new TransitionInstanceManager();
                var  lastTaskTransitionInstance = tim.GetLastTaskTransition(runner.AppName,
                                                                            runner.AppInstanceID, runner.ProcessGUID);

                var npc = new PreviousStepChecker();
                var previousActivityInstance = npc.GetPreviousActivityInstanceList(runningNode, true,
                                                                                   out hasGatewayNode).ToList()[0];

                //仅仅是回跳到上一步节点,即按SendBack方式处理
                if (previousActivityInstance.ActivityGUID == jumpBackActivityGUID)
                {
                    rmins.BackwardContext.BackwardToTaskActivityInstance = previousActivityInstance;
                    rmins.BackwardContext.BackwardToTargetTransitionGUID =
                        hasGatewayNode == false ? lastTaskTransitionInstance.TransitionGUID : WfDefine.WF_XPDL_GATEWAY_BYPASS_GUID;        //如果中间有Gateway节点,则没有直接相连的TransitonGUID

                    rmins.BackwardContext.BackwardFromActivity         = processModel.GetActivity(runningNode.ActivityGUID);
                    rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
                    rmins.BackwardContext.BackwardTaskReceiver         = WfBackwardTaskReceiver.Instance(
                        previousActivityInstance.ActivityName,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);

                    rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
                        previousActivityInstance.ActivityGUID,
                        previousActivityInstance.EndedByUserID,
                        previousActivityInstance.EndedByUserName);
                }
                else
                {
                    //回跳到早前节点
                    if (jumpBackActivityInstance.ActivityState != (short)ActivityStateEnum.Completed)
                    {
                        //回跳节点不在完成状态
                        result.Status        = WfExecutedStatus.Exception;
                        result.ExceptionType = WfExceptionType.Jump_NotActivityBackCompleted;
                        result.Message       = LocalizeHelper.GetEngineMessage("wfruntimemanagerfactory.CreateRuntimeInstanceJump.back.error");

                        return(rmins);
                    }

                    rmins.BackwardContext.BackwardToTaskActivityInstance = jumpBackActivityInstance;

                    //判断两个节点是否有Transition的定义存在
                    var transition = processModel.GetForwardTransition(runningNode.ActivityGUID, jumpBackActivityGUID);
                    rmins.BackwardContext.BackwardToTargetTransitionGUID = transition != null ? transition.TransitionGUID : WfDefine.WF_XPDL_GATEWAY_BYPASS_GUID;

                    rmins.BackwardContext.BackwardFromActivity         = processModel.GetActivity(runningNode.ActivityGUID);
                    rmins.BackwardContext.BackwardFromActivityInstance = runningNode;
                    rmins.BackwardContext.BackwardTaskReceiver         = WfBackwardTaskReceiver.Instance(
                        jumpBackActivityInstance.ActivityName,
                        jumpBackActivityInstance.EndedByUserID,
                        jumpBackActivityInstance.EndedByUserName);

                    rmins.AppRunner.NextActivityPerformers = ActivityResource.CreateNextActivityPerformers(
                        jumpBackActivityInstance.ActivityGUID,
                        jumpBackActivityInstance.EndedByUserID,
                        jumpBackActivityInstance.EndedByUserName);
                }
                //获取资源数据
                var activityResourceBack = new ActivityResource(rmins.AppRunner,
                                                                rmins.AppRunner.NextActivityPerformers,
                                                                runner.Conditions);
                rmins.ActivityResource = activityResourceBack;
            }
            else
            {
                //跳转到从未执行过的节点上
                var activityResource = new ActivityResource(runner, runner.NextActivityPerformers, runner.Conditions);
                rmins.ActivityResource        = activityResource;
                rmins.RunningActivityInstance = runningNode;
            }
            #endregion

            return(rmins);
        }