/// <summary>
        /// 克隆后续的活动并且连线
        /// </summary>
        /// <param name="endActivity"></param>
        /// <param name="context"></param>
        /// <param name="operationType"></param>
        private void GenerateSubActivitiesDescriptors(IWfProcess processInstance, IWfActivityDescriptor endActivity, IWfActivityDescriptor lastActDesp, WfCopyMainStreamContext context, WfControlOperationType operationType)
        {
            foreach (WfCopyMainStreamSubActivityNode subNode in this.ToActivities)
            {
                IWfActivityDescriptor targetActDesp = null;

                if (subNode.ActivityNode.NeedCopy)
                {
                    if (context.ClonedActivities.TryGetValue(subNode.ActivityNode.TemplateActivityDescriptor.Key, out targetActDesp) == false)
                    {
                        targetActDesp = subNode.ActivityNode.CreateClonedActivityDescriptor(processInstance, context, operationType);
                        context.ClonedActivities.Add(subNode.ActivityNode.TemplateActivityDescriptor.Key, targetActDesp);
                    }
                }
                else
                {
                    targetActDesp = subNode.ActivityNode.OriginalActivityDescriptor;
                    //targetActDesp = subNode.ActivityNode.TemplateActivityDescriptor;
                }

                //本身是复制点,则必须复制线
                if (this.NeedCopy)
                {
                    WfTransitionDescriptor clonedTransition = (WfTransitionDescriptor)((WfTransitionDescriptor)subNode.FromTransition).Clone();

                    //沈峥注释,2015-6-25,FindNotUsedTransitionKey应该从实例流程查找
                    //if (clonedTransition.Key.IsNullOrEmpty())
                    //    clonedTransition.Key = subNode.ActivityNode.TemplateActivityDescriptor.Process.FindNotUsedTransitionKey();
                    if (clonedTransition.Key.IsNullOrEmpty())
                    {
                        clonedTransition.Key = endActivity.Process.FindNotUsedTransitionKey();
                    }

                    if (this.IsEndActivity)
                    {
                        string targetAssociatedKey = null;

                        //沈峥调整,2015-1-24,目标点非Clone点且是加签点
                        if (targetActDesp.ClonedKey.IsNullOrEmpty() && targetActDesp.GetAssociatedActivity() != null)
                        {
                            targetAssociatedKey = targetActDesp.GetAssociatedActivity().Key;
                        }

                        if (lastActDesp != null && targetAssociatedKey != null && lastActDesp.GetAssociatedActivity().Key == targetAssociatedKey)
                        {
                            targetActDesp = lastActDesp;
                        }
                    }

                    endActivity.ToTransitions.AddTransition(targetActDesp, clonedTransition);
                }

                subNode.ActivityNode.GenerateSubActivitiesDescriptors(processInstance, targetActDesp, lastActDesp, context, operationType);
            }
        }
        private static IWfTransitionDescriptor JoinOriginalActivityToClonedActivity(IWfActivityDescriptor originalActDesp, IWfActivityDescriptor clonedActDesp, IWfTransitionDescriptor transitionTemplate)
        {
            WfTransitionDescriptor newTransition = (WfTransitionDescriptor)originalActDesp.ToTransitions.AddForwardTransition(clonedActDesp);

            //将新添加的线的属性设置为原来线的属性
            //string newKey = newTransition.Key;
            newTransition.Properties.ReplaceExistedPropertyValues(transitionTemplate.Properties);
            //newTransition.Key = newKey;
            newTransition.IsBackward    = false;
            newTransition.DefaultSelect = true;

            return(newTransition);
        }
        private static void AdjustTemplateActivityToTransitions(IWfActivityDescriptor templateActDesp,
                                                                IWfActivityDescriptor secondSecretaryActDesp,
                                                                IEnumerable <IWfTransitionDescriptor> originalToTransitions)
        {
            templateActDesp.ToTransitions.Clear();

            foreach (IWfTransitionDescriptor transition in originalToTransitions)
            {
                WfTransitionDescriptor clonedTransition = (WfTransitionDescriptor)((WfTransitionDescriptor)transition).Clone();

                clonedTransition.Key = templateActDesp.Process.FindNotUsedTransitionKey();
                ((WfTransitionDescriptor)clonedTransition).ConnectActivities(templateActDesp, secondSecretaryActDesp);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="actKey"></param>
        /// <returns></returns>
        private IWfTransitionDescriptor GetTransitionByToActivity(string actKey)
        {
            ExceptionHelper.CheckStringIsNullOrEmpty(actKey, "actKey");

            WfTransitionDescriptor result = null;

            foreach (WfTransitionDescriptor transition in base.List)
            {
                if (transition.ToActivity.Key == actKey)
                {
                    result = transition;
                    break;
                }
            }

            return(result);
        }
        public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            WfTransitionDescriptor transitionDesp = (WfForwardTransitionDescriptor)obj;

            IDictionary <string, object> dictionary = base.Serialize(obj, serializer);

            DictionaryHelper.AddNonDefaultValue <string, object>(dictionary, "Priority", transitionDesp.Priority);
            DictionaryHelper.AddNonDefaultValue <string, object>(dictionary, "DefaultSelect", transitionDesp.DefaultSelect);
            DictionaryHelper.AddNonDefaultValue <string, object>(dictionary, "__type", transitionDesp.GetType().AssemblyQualifiedName);

            dictionary.Add("ToActivityKey", transitionDesp.ToActivityKey);
            dictionary.Add("FromActivityKey", transitionDesp.FromActivityKey);

            DictionaryHelper.AddNonDefaultValue <string, object>(dictionary, "Condition", ((WfForwardTransitionDescriptor)transitionDesp).Condition);

            dictionary.Add("Variables", transitionDesp.Variables);
            return(dictionary);
        }
        /// <summary>
        /// 添加同意和不同意的出线
        /// </summary>
        /// <param name="currentActDesp"></param>
        private static void AddAgreeAndDisagreeLine(IWfActivityDescriptor currentActDesp, IWfActivityDescriptor nextActDesp, IWfBranchProcessTemplateDescriptor template)
        {
            IWfActivityDescriptor completedActDesp = currentActDesp.Process.CompletedActivity;

            WfTransitionDescriptor agreeTransition = (WfTransitionDescriptor)currentActDesp.ToTransitions.AddForwardTransition(nextActDesp);

            agreeTransition.Name                       = template.Properties.GetValue("AgreeLineName", "同意");
            agreeTransition.DefaultSelect              = true;
            agreeTransition.AffectProcessReturnValue   = true;
            agreeTransition.AffectedProcessReturnValue = true;
            agreeTransition.Priority                   = 0;

            WfTransitionDescriptor disagreeTransition = (WfTransitionDescriptor)currentActDesp.ToTransitions.AddForwardTransition(completedActDesp);

            disagreeTransition.Name     = template.Properties.GetValue("DisagreeLineName", "不同意");
            disagreeTransition.Priority = 1;
            disagreeTransition.AffectProcessReturnValue   = true;
            disagreeTransition.AffectedProcessReturnValue = false;
        }
        public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            WfTransitionDescriptor transitionDesp = (WfTransitionDescriptor)base.Deserialize(dictionary, type, serializer);

            transitionDesp.ToActivityKey   = DictionaryHelper.GetValue(dictionary, "ToActivityKey", string.Empty);
            transitionDesp.FromActivityKey = DictionaryHelper.GetValue(dictionary, "FromActivityKey", string.Empty);

            if (dictionary.ContainsKey("Condition"))
            {
                transitionDesp.Condition       = JSONSerializerExecute.Deserialize <WfConditionDescriptor>(dictionary["Condition"]);
                transitionDesp.Condition.Owner = transitionDesp;
            }

            WfVariableDescriptorCollection variables = JSONSerializerExecute.Deserialize <WfVariableDescriptorCollection>(dictionary["Variables"]);

            transitionDesp.Variables.Clear();
            transitionDesp.Variables.CopyFrom(variables);

            return(transitionDesp);
        }
Esempio n. 8
0
        protected override void OnModifyWorkflow(WfExecutorDataContext dataContext)
        {
            this.TargetActivity.Process.Committed = true;

            IWfProcess process = TargetActivity.Process;

            WfRuntime.ProcessContext.BeginChangeActivityChangingContext();
            try
            {
                WfRuntime.ProcessContext.ActivityChangingContext.CreatorInstanceID     = TargetActivity.ID;
                WfRuntime.ProcessContext.ActivityChangingContext.AssociatedActivityKey =
                    TargetActivity.Descriptor.AssociatedActivityKey.IsNotEmpty() ?
                    TargetActivity.Descriptor.AssociatedActivityKey : TargetActivity.Descriptor.Key;

                //string activityKey = process.Descriptor.FindNotUsedActivityKey();

                //WfActivityDescriptor actDesp = new WfActivityDescriptor(activityKey);

                //actDesp.Name = "会签";
                var toReturnTrans = process.CurrentActivity.Descriptor.ToTransitions.FindAll(t => t.IsBackward == true);

                WfActivityDescriptor actDesp = process.CurrentActivity.Descriptor.Clone() as WfActivityDescriptor;
                actDesp.ActivityType = WfActivityType.NormalActivity;
                actDesp.Properties.SetValue("AutoMoveAfterPending", false);
                actDesp.ClonedKey = process.CurrentActivity.Descriptor.Key;
                actDesp.BranchProcessTemplates.Clear();
                process.CurrentActivity.Append(actDesp);

                foreach (WfTransitionDescriptor t in toReturnTrans)
                {
                    WfTransitionDescriptor trans = t.Clone() as WfTransitionDescriptor;
                    if (t.FromActivityKey == t.ToActivityKey)
                    {
                        trans.JoinActivity(actDesp, actDesp);
                    }
                    else
                    {
                        trans.JoinActivity(actDesp, t.ToActivity);
                    }

                    actDesp.ToTransitions.Add(trans);
                }

                //添加子流程
                WfTransferParams tp = new WfTransferParams(actDesp);
                tp.Assignees.CopyFrom(Assignees);

                tp.BranchTransferParams.Add(new WfBranchProcessTransferParams(
                                                WfTemplateBuilder.CreateDefaultConsignTemplate(
                                                    "WfConsignProcessTemplateDescriptorKey",
                                                    this.Sequence,
                                                    this.BlockingType,
                                                    this.ConsignUsers)));

                if (this.CirculateUsers.Count <IUser>() > 0)
                {
                    tp.BranchTransferParams.Add(new WfBranchProcessTransferParams(
                                                    WfTemplateBuilder.CreateDefaultCirculationTemplate(
                                                        "WfCirculationProcessTemplateDescriptorKey",
                                                        this.CirculateUsers)));
                }

                WfRuntime.ProcessContext.AfterStartupBranchProcess += new WfAfterStartupBranchProcessHandler(WfActivityRelativeExecutorBase.AfterStartupBranchProcess);
                process.MoveTo(tp);
            }
            finally
            {
                WfRuntime.ProcessContext.RestoreChangeActivityChangingContext();
            }
        }
        /// <summary>
        /// 将MainStreamContext中的活动,复制到某个活动后
        /// </summary>
        /// <param name="processInstance"></param>
        /// <param name="activityToAppend"></param>
        /// <param name="context"></param>
        /// <param name="operationType"></param>
        /// <returns>返回的第一个Clone活动的出线</returns>
        public IWfTransitionDescriptor GenerateCopiedActivitiesDescriptors(IWfProcess processInstance, IWfActivityDescriptor activityToAppend, WfCopyMainStreamContext context, WfControlOperationType operationType)
        {
            IWfTransitionDescriptor resultTransition = null;

            CloneRelativeActivities(activityToAppend.ProcessInstance, context, operationType);

            IWfActivityDescriptor clonedActDesp = null;

            if (context.ClonedActivities.TryGetValue(this._TemplateActivityDescriptor.Key, out clonedActDesp))
            {
                //如果本活动是模板生成的动态活动,且退件的目标不是模板,则删除后续的动态活动
                if (activityToAppend.GeneratedByTemplate && clonedActDesp.GeneratedByTemplate == false)
                {
                    IWfTransitionDescriptor templateTransition = activityToAppend.ToTransitions.Find(t => t.GeneratedByTemplate);

                    while (templateTransition != null)
                    {
                        IWfActivityDescriptor actDesp = templateTransition.ToActivity;

                        //如果线的目标活动是动态活动,则删除,否则忽略
                        if (actDesp.GeneratedByTemplate)
                        {
                            actDesp.Instance.Delete();
                        }

                        templateTransition = templateTransition.ToActivity.ToTransitions.Find(t => t.GeneratedByTemplate);
                    }
                }

                IWfTransitionDescriptor lastTransition = context.EntryTransition;

                if (lastTransition == null)
                {
                    lastTransition = activityToAppend.ToTransitions.GetAllCanTransitForwardTransitions().FindDefaultSelectTransition();
                }

                IWfActivityDescriptor lastActDesp = null;

                if (lastTransition != null)
                {
                    lastActDesp = lastTransition.ToActivity;
                }

                string entryTransitionKey = (lastTransition != null) ? lastTransition.Key : string.Empty;

                //将第一个克隆的活动与原流程的某一个活动关联。
                //保留退回线
                IList <IWfTransitionDescriptor> originalBackwardTransitions = activityToAppend.ToTransitions.FindAll(t => t.IsBackward);

                activityToAppend.ToTransitions.Clear();

                foreach (WfTransitionDescriptor t in originalBackwardTransitions)
                {
                    WfTransitionDescriptor newBackwardTransition = (WfTransitionDescriptor)t.Clone();

                    newBackwardTransition.Key = activityToAppend.Process.FindNotUsedTransitionKey();
                    activityToAppend.ToTransitions.AddTransition(t.ToActivity, newBackwardTransition);
                }

                //activityToAppend.ToTransitions.CopyFrom(originalBackwardTransitions);

                IWfTransitionDescriptor newTransition = JoinOriginalActivityToClonedActivity(activityToAppend, clonedActDesp, lastTransition);

                resultTransition = newTransition;

                //将自动流转设置为False
                if (clonedActDesp.Properties.GetValue("AutoMoveTo", false))
                {
                    clonedActDesp.Properties.TrySetValue("AutoMoveTo", false);
                }

                GenerateSubActivitiesDescriptors(processInstance, clonedActDesp, lastActDesp, context, operationType);
            }

            return(resultTransition);
        }
        protected override IWfActivityDescriptor PrepareActivityDescriptor(IWfActivityDescriptor targetActDesp)
        {
            IWfProcessDescriptor processDesp = targetActDesp.Process;

            string activityKey = processDesp.FindNotUsedActivityKey();

            WfActivityDescriptor newActDesp = new WfActivityDescriptor(activityKey);

            newActDesp.ActivityType = WfActivityType.NormalActivity;

            //主线活动不应该有关联Key
            newActDesp.AssociatedActivityKey = WfRuntime.ProcessContext.ActivityChangingContext.AssociatedActivityKey;
            newActDesp.FromTransitions.Clear();
            newActDesp.ToTransitions.Clear();

            processDesp.Activities.Add(newActDesp);
            newActDesp.Properties.SetValue("AutoMoveAfterPending", true);

            IWfTransitionDescriptor defaultSelectTran = targetActDesp.ToTransitions.FindDefaultSelectTransition();

            List <IWfTransitionDescriptor> movedTransitions = new List <IWfTransitionDescriptor>();

            foreach (WfTransitionDescriptor t in targetActDesp.ToTransitions)
            {
                WfTransitionDescriptor cloneTransition = (WfTransitionDescriptor)t.Clone();
                cloneTransition.Key = processDesp.FindNotUsedTransitionKey();

                if (t.IsBackward == false)
                {
                    cloneTransition.JoinActivity(newActDesp, t.ToActivity);
                    newActDesp.ToTransitions.Add(cloneTransition);

                    movedTransitions.Add(t);

                    t.ToActivity.FromTransitions.Remove(td => td.Key == t.Key);
                    t.ToActivity.FromTransitions.Add(cloneTransition);

                    if (t.Key == defaultSelectTran.Key)
                    {
                        t.Key = processDesp.FindNotUsedTransitionKey();
                        t.JoinActivity(targetActDesp, newActDesp);
                        newActDesp.FromTransitions.Add(t);
                    }
                }
                else
                {
                    cloneTransition.JoinActivity(newActDesp, t.ToActivity);
                    newActDesp.ToTransitions.Add(cloneTransition);

                    movedTransitions.Add(t);
                }
            }

            foreach (WfTransitionDescriptor t in movedTransitions)
            {
                targetActDesp.ToTransitions.Remove(t);
            }

            SetDynamicActivityProperties(targetActDesp, newActDesp);

            IWfTransitionDescriptor newTransition = targetActDesp.ToTransitions.AddForwardTransition(newActDesp);

            SetDynamicTransitionProperties(targetActDesp, newTransition);

            return(newActDesp);
        }
        private WfActivityDescriptorCollection CreateActivities(WfTransitionDescriptorCollection fromTransitions, WfCreateActivityParamCollection capc, WfTransitionDescriptorCollection toTransitions)
        {
            WfActivityDescriptorCollection result = new WfActivityDescriptorCollection(this.Process);

            IWfActivityDescriptor firstDynamicActivity     = null;
            IWfActivityDescriptor lastDynamicActivity      = null;
            WfCreateActivityParam lastCreatedActivityParam = null;

            foreach (WfCreateActivityParam cap in capc)
            {
                WfActivityDescriptor actDesp = (WfActivityDescriptor)cap.Template.Clone();

                actDesp.Process = this.Process;

                string newActKey = "ND" + cap.ActivitySN;

                if (this.Process.Activities.ContainsKey(newActKey))
                {
                    newActKey = this.Process.FindNotUsedActivityKey();
                }

                actDesp.Key           = newActKey;
                cap.CreatedDescriptor = actDesp;

                this.Process.Activities.Add(actDesp);

                if (lastCreatedActivityParam != null)
                {
                    lastCreatedActivityParam.DefaultNextDescriptor = actDesp;
                }

                lastCreatedActivityParam = cap;

                if (this.IsMainStreamActivity == false && this.ProcessInstance != null)
                {
                    WfActivityBase.CreateActivityInstance(actDesp, this.ProcessInstance);
                }

                if (firstDynamicActivity == null)
                {
                    firstDynamicActivity = actDesp;
                }

                if (lastDynamicActivity != null)
                {
                    WfTransitionDescriptor newTransition = (WfTransitionDescriptor)lastDynamicActivity.ToTransitions.AddForwardTransition(actDesp);

                    //如果不是第一个生成的活动
                    if (firstDynamicActivity != actDesp)
                    {
                        newTransition.GeneratedByTemplate = true;
                        newTransition.TemplateKey         = this.Key;
                    }
                }

                lastDynamicActivity = actDesp;

                result.Add(actDesp);
            }

            if (firstDynamicActivity != null && lastDynamicActivity != null)
            {
                SetEntryTransitionsProperties(firstDynamicActivity, fromTransitions);

                if (lastCreatedActivityParam != null)
                {
                    IWfTransitionDescriptor defaultTransition =
                        toTransitions.GetAllConditionMatchedTransitions().FindDefaultSelectTransition(true);

                    if (defaultTransition != null)
                    {
                        lastCreatedActivityParam.DefaultNextDescriptor = defaultTransition.ToActivity;
                    }
                }

                capc.AdjustTransitionsByTemplate(this.Key);

                //如果在活动模板参数的最后一个活动没有配置出线,则使用默认的出线;否则使用配置的出线
                if (lastDynamicActivity.ToTransitions.Count == 0)
                {
                    SetExitTransitionsProperties(lastDynamicActivity, toTransitions);
                }
                else
                {
                    lastDynamicActivity.ToTransitions.ForEach(t => SetDynamicTransitionProperties(null, t, this, false));
                }
            }
            else
            {
                //当没有动态活动生成时,前后的活动直接串联到一起
                JoinOriginalActivitiesAndSetProperties(fromTransitions, toTransitions);
            }

            //将模版点的进出线的Enabled都设置为False
            fromTransitions.ForEach(t => ((WfTransitionDescriptor)t).Enabled = false);
            toTransitions.ForEach(t => ((WfTransitionDescriptor)t).Enabled   = false);

            return(result);
        }