protected override IWfActivity PrepareInstanceActivity()
        {
            ExceptionHelper.TrueThrow(this.TargetActivity.Descriptor.ActivityType == WfActivityType.CompletedActivity, "不能在结束活动后添加活动");

            IWfProcess            process    = this.TargetActivity.Process;
            IWfActivityDescriptor newActDesp = PrepareActivityDescriptor(this.TargetActivity.Descriptor);

            IWfActivity newActivity = WfActivityBase.CreateActivityInstance(newActDesp, process);

            if (WfRuntime.ProcessContext != null)
            {
                WfRuntime.ProcessContext.AffectedProcesses.AddOrReplace(process);
            }

            this.AddedActivity = newActivity;

            return(newActivity);
        }
        private IWfActivityDescriptor CreateClonedActivityDescriptor(IWfProcess processInstance, WfCopyMainStreamContext context, WfControlOperationType operationType)
        {
            WfActivityDescriptor result = this.TemplateActivityDescriptor.Clone() as WfActivityDescriptor;

            result.Key = processInstance.Descriptor.FindNotUsedActivityKey();

            //2012/12/03 修改克隆时向开始环节转换时需要设置的属性
            if (this.TemplateActivityDescriptor.ActivityType == WfActivityType.InitialActivity && operationType == WfControlOperationType.Return)
            {
                ResetPropertiesByDefinedName(result, "DefaultReturnToInitialActivityTemplate");
            }

            //如果模板活动不是主线活动,则使用该活动的关联Key,否则去实例上查找主线活动对应活动的关联Key。
            if (this.TemplateActivityDescriptor.IsMainStreamActivity == false)
            {
                result.AssociatedActivityKey = this.TemplateActivityDescriptor.GetAssociatedActivity().Key;

                result.ClonedKey = this.TemplateActivityDescriptor.Key;
            }
            else
            {
                IWfActivityDescriptor matchActDesp = FindActivityDescriptorByMainStreamKey(processInstance, this.TemplateActivityDescriptor.Key);

                if (matchActDesp != null)
                {
                    result.AssociatedActivityKey = matchActDesp.Key;

                    result.ClonedKey = matchActDesp.Key;
                }
            }

            //如果原来活动上没有配置资源,则使用原来活动实例的指派人作为资源
            if (result.Resources.Count == 0)
            {
                result.Resources.CopyFrom(this.TemplateActivityDescriptor.Instance.Assignees.ToResources());
            }

            WfActivityBase newActivity = WfActivityBase.CreateActivityInstance(result, processInstance);

            if (context.IsMainStream)
            {
                newActivity.MainStreamActivityKey = this.TemplateActivityDescriptor.Key;
            }

            //沈峥调整,生成资源
            if (result.Resources.Count == 0)
            {
                newActivity.Candidates.CopyFrom(this.TemplateActivityDescriptor.Instance.Candidates);

                if (newActivity.Candidates.Count == 0)
                {
                    IWfActivityDescriptor matchActDesp = FindActivityDescriptorByMainStreamKey(processInstance, this.TemplateActivityDescriptor.Key);

                    if (matchActDesp != null && OguBase.IsNotNullOrEmpty(matchActDesp.Instance.Operator))
                    {
                        newActivity.Candidates.Add(matchActDesp.Instance.Operator);
                    }
                }

                newActivity.Assignees.CopyFrom(newActivity.Candidates);
            }
            else
            {
                if (newActivity.Descriptor.Properties.GetValue("AutoGenerateCadidates", true))
                {
                    newActivity.GenerateCandidatesFromResources();
                    newActivity.Assignees.Clear();
                    newActivity.Assignees.CopyFrom(newActivity.Candidates);
                }
                else
                {
                    newActivity.Candidates.Clear();
                    newActivity.Assignees.Clear();

                    //仅复制能够和直接转换为用户的角色
                    foreach (IUser user in result.Resources.ToUsers())
                    {
                        newActivity.Candidates.Add(user);
                        newActivity.Assignees.Add(user);
                    }
                }
            }

            processInstance.Descriptor.Activities.Add(result);

            return(result);
        }
        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);
        }