/// <summary>
        /// 添加主线活动的秘书环节
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="msOperationActDesp"></param>
        private static void AddMainStreamSecretaryActivities(WfSecretaryOperation operation, IWfActivityDescriptor msOperationActDesp, AddSecretaryResult instanceResult)
        {
            AddSecretaryResult result = new AddSecretaryResult();

            //保留住原来领导活动的出线
            List <IWfTransitionDescriptor> templateToTransitions = new List <IWfTransitionDescriptor>(msOperationActDesp.ToTransitions);

            IWfActivityDescriptor prevSecretaryActDesp = CreateSecretaryActivityDescriptor(msOperationActDesp, operation.Secretaries);

            SetActivityPropertiesBooleanValue(prevSecretaryActDesp, false, WfSecretaryDecorator._PrevActForbidProperties);
            SetActivityPropertiesBooleanValue(prevSecretaryActDesp, true, WfSecretaryDecorator._PrevActAllowProperties);
            msOperationActDesp.InsertBefore(prevSecretaryActDesp);

            DecroateTemplateActivity(msOperationActDesp, operation.Secretaries);

            IWfActivityDescriptor succeedSecretaryActDesp = CreateSecretaryActivityDescriptor(msOperationActDesp, operation.Secretaries);

            SetActivityPropertiesBooleanValue(succeedSecretaryActDesp, false, WfSecretaryDecorator._SucceedActForbidProperties);
            msOperationActDesp.Append(succeedSecretaryActDesp, true);

            AdjustTemplateActivityToTransitions(msOperationActDesp, succeedSecretaryActDesp, templateToTransitions);

            ((WfActivityBase)instanceResult.PrevActivity).MainStreamActivityKey    = prevSecretaryActDesp.Key;
            ((WfActivityBase)instanceResult.SucceedActivity).MainStreamActivityKey = succeedSecretaryActDesp.Key;
        }
        /// <summary>
        /// 添加实例活动的秘书环节
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="generatedActivities"></param>
        private static AddSecretaryResult AddInstanceSecretaryActivities(WfSecretaryOperation operation, List <IWfActivity> generatedActivities)
        {
            AddSecretaryResult result = new AddSecretaryResult();

            //保留住原来领导活动的出线
            List <IWfTransitionDescriptor> templateToTransitions = new List <IWfTransitionDescriptor>(operation.ActivityDescriptor.ToTransitions);

            IWfActivityDescriptor prevSecretaryActDesp = CreateSecretaryActivityDescriptor(operation.ActivityDescriptor, operation.Secretaries);

            SetActivityPropertiesBooleanValue(prevSecretaryActDesp, false, WfSecretaryDecorator._PrevActForbidProperties);
            SetActivityPropertiesBooleanValue(prevSecretaryActDesp, true, WfSecretaryDecorator._PrevActAllowProperties);

            //在领导活动之前插入秘书活动
            result.PrevActivity = operation.ActivityDescriptor.Instance.InsertBefore(prevSecretaryActDesp);

            generatedActivities.Add(result.PrevActivity);

            DecroateTemplateActivity(operation.ActivityDescriptor, operation.Secretaries);

            IWfActivityDescriptor succeedSecretaryActDesp = CreateSecretaryActivityDescriptor(operation.ActivityDescriptor, operation.Secretaries);

            SetActivityPropertiesBooleanValue(succeedSecretaryActDesp, false, WfSecretaryDecorator._SucceedActForbidProperties);

            result.SucceedActivity = operation.ActivityDescriptor.Instance.Append(succeedSecretaryActDesp, true);

            generatedActivities.Add(result.SucceedActivity);

            AdjustTemplateActivityToTransitions(operation.ActivityDescriptor, succeedSecretaryActDesp, templateToTransitions);

            return(result);
        }
        /// <summary>
        /// 删除秘书活动
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="generatedActivities"></param>
        private static void ClearSecretaryActivities(WfSecretaryOperation operation, List <IWfActivity> generatedActivities)
        {
            ClearInstanceSecretaryActivities(operation, generatedActivities);

            if (operation.ActivityDescriptor.Instance.MainStreamActivityKey.IsNotEmpty())
            {
                ClearMainStreamSecretaryActivities(operation, operation.ActivityDescriptor.Instance.GetMainStreamActivityDescriptor());
            }
        }
        private static void AddSecretaryActivities(WfSecretaryOperation operation, List <IWfActivity> generatedActivities)
        {
            AddSecretaryResult instanceResult = AddInstanceSecretaryActivities(operation, generatedActivities);

            if (operation.ActivityDescriptor.Instance.MainStreamActivityKey.IsNotEmpty())
            {
                AddMainStreamSecretaryActivities(operation, operation.ActivityDescriptor.Instance.GetMainStreamActivityDescriptor(), instanceResult);
            }
        }
        private static void ClearInstanceSecretaryActivities(WfSecretaryOperation operation, List <IWfActivity> generatedActivities)
        {
            IWfTransitionDescriptor prevTransition    = operation.ActivityDescriptor.FromTransitions.Find(t => t.FromActivity.Variables.GetValue(WfHelper.SecretaryActivity, false));
            IWfTransitionDescriptor succeedTransition = operation.ActivityDescriptor.ToTransitions.Find(t => t.ToActivity.Variables.GetValue(WfHelper.SecretaryActivity, false));

            bool needRemoveInitial = false;

            if (prevTransition != null)
            {
                if (prevTransition.FromActivity.ActivityType == WfActivityType.InitialActivity)
                {
                    needRemoveInitial = true;
                }

                List <IWfTransitionDescriptor> fromTransitions = new List <IWfTransitionDescriptor>();
                prevTransition.FromActivity.FromTransitions.CopyTo(fromTransitions);
                ((WfActivityBase)prevTransition.FromActivity.Instance).InternalRemove();

                foreach (IWfTransitionDescriptor fromTransition in fromTransitions)
                {
                    fromTransition.ConnectActivities(fromTransition.FromActivity, operation.ActivityDescriptor);
                }
            }

            if (succeedTransition != null)
            {
                List <IWfTransitionDescriptor> toTransitions = new List <IWfTransitionDescriptor>();

                succeedTransition.ToActivity.ToTransitions.CopyTo(toTransitions);
                succeedTransition.ToActivity.Instance.Remove();

                foreach (IWfTransitionDescriptor toTransition in toTransitions)
                {
                    toTransition.ConnectActivities(operation.ActivityDescriptor, toTransition.ToActivity);
                }
            }

            if (needRemoveInitial)
            {
                ((WfActivityDescriptor)operation.ActivityDescriptor).ActivityType          = WfActivityType.InitialActivity;
                operation.ActivityDescriptor.Process.Activities.InitialActivity            = operation.ActivityDescriptor;
                ((WfProcess)operation.ActivityDescriptor.Instance.Process).InitialActivity = operation.ActivityDescriptor.Instance;
            }

            DedecroateTemplateActivity(operation.ActivityDescriptor, operation.Secretaries);
        }
        private static void ChangeMainStreamSecretaryActivities(WfSecretaryOperation operation, IWfActivityDescriptor msOperationActDesp)
        {
            IWfTransitionDescriptor prevTransition    = msOperationActDesp.FromTransitions.Find(t => t.FromActivity.Variables.GetValue(WfHelper.SecretaryActivity, false));
            IWfTransitionDescriptor succeedTransition = msOperationActDesp.ToTransitions.Find(t => t.ToActivity.Variables.GetValue(WfHelper.SecretaryActivity, false));

            if (prevTransition != null)
            {
                ChangeActivityResources(prevTransition.FromActivity, operation.Secretaries);
            }

            if (succeedTransition != null)
            {
                ChangeActivityResources(succeedTransition.ToActivity, operation.Secretaries);
            }

            DecroateTemplateActivity(operation.ActivityDescriptor, operation.Secretaries);
        }
        private static void ClearMainStreamSecretaryActivities(WfSecretaryOperation operation, IWfActivityDescriptor msOperationActDesp)
        {
            IWfTransitionDescriptor prevTransition    = msOperationActDesp.FromTransitions.Find(t => t.FromActivity.Variables.GetValue(WfHelper.SecretaryActivity, false));
            IWfTransitionDescriptor succeedTransition = msOperationActDesp.ToTransitions.Find(t => t.ToActivity.Variables.GetValue(WfHelper.SecretaryActivity, false));

            bool needRemoveInitial = false;

            if (prevTransition != null)
            {
                if (prevTransition.FromActivity.ActivityType == WfActivityType.InitialActivity)
                {
                    needRemoveInitial = true;
                }

                List <IWfTransitionDescriptor> fromTransitions = new List <IWfTransitionDescriptor>();
                prevTransition.FromActivity.FromTransitions.CopyTo(fromTransitions);
                prevTransition.FromActivity.Remove();

                foreach (IWfTransitionDescriptor fromTransition in fromTransitions)
                {
                    fromTransition.ConnectActivities(fromTransition.FromActivity, msOperationActDesp);
                }
            }

            if (succeedTransition != null)
            {
                List <IWfTransitionDescriptor> toTransitions = new List <IWfTransitionDescriptor>();

                succeedTransition.ToActivity.ToTransitions.CopyTo(toTransitions);
                succeedTransition.ToActivity.Remove();

                foreach (IWfTransitionDescriptor toTransition in toTransitions)
                {
                    toTransition.ConnectActivities(msOperationActDesp, toTransition.ToActivity);
                }
            }

            if (needRemoveInitial)
            {
                ((WfActivityDescriptor)msOperationActDesp).ActivityType = WfActivityType.InitialActivity;
                msOperationActDesp.Process.Activities.InitialActivity   = msOperationActDesp;
            }

            DedecroateTemplateActivity(msOperationActDesp, operation.Secretaries);
        }
        private static void ChangeInstanceSecretaryActivities(WfSecretaryOperation operation, List <IWfActivity> generatedActivities)
        {
            IWfTransitionDescriptor prevTransition    = operation.ActivityDescriptor.FromTransitions.Find(t => t.FromActivity.Variables.GetValue(WfHelper.SecretaryActivity, false));
            IWfTransitionDescriptor succeedTransition = operation.ActivityDescriptor.ToTransitions.Find(t => t.ToActivity.Variables.GetValue(WfHelper.SecretaryActivity, false));

            if (prevTransition != null)
            {
                ChangeActivityResources(prevTransition.FromActivity, operation.Secretaries);
                generatedActivities.Add(prevTransition.FromActivity.Instance);
            }

            if (succeedTransition != null)
            {
                ChangeActivityResources(succeedTransition.ToActivity, operation.Secretaries);
                generatedActivities.Add(succeedTransition.ToActivity.Instance);
            }

            DecroateTemplateActivity(operation.ActivityDescriptor, operation.Secretaries);
        }
        /// <summary>
        /// 得到秘书操作的类型,包括:增加,修改,清除秘书的操作类
        /// </summary>
        /// <param name="actDesp"></param>
        /// <returns></returns>
        private static WfSecretaryOperation GetSecretaryOperation(IWfActivityDescriptor actDesp)
        {
            WfSecretaryOperation result = new WfSecretaryOperation();

            if (actDesp.Variables.GetValue(WfHelper.SecretaryTemplateActivity, false))
            {
                List <IUser> secretaries = GetSecretariesFromCandidates(actDesp.Instance.Candidates);

                result.Secretaries = secretaries;

                if (secretaries.Count > 0)
                {
                    if (CompareOriginalSecretaries(actDesp, secretaries) == false)
                    {
                        result.OperationType = WfSecretaryOperationType.ChangeSecretaries;
                    }
                }
                else
                {
                    result.OperationType = WfSecretaryOperationType.ClearSecretaries;
                }
            }
            else if (CanGenerateSecretaries(actDesp))
            {
                List <IUser> secretaries = GetSecretariesFromCandidates(actDesp.Instance.Candidates);

                result.Secretaries = secretaries;

                if (secretaries.Count > 0)
                {
                    result.OperationType = WfSecretaryOperationType.AddSecretaries;
                }
            }
            else
            {
                result.Secretaries = new List <IUser>();
            }

            result.ActivityDescriptor = actDesp;

            return(result);
        }