예제 #1
0
        protected internal override ValidationErrorCollection ValidateChanges(Activity contextActivity)
        {
            if (contextActivity == null)
            {
                throw new ArgumentNullException("contextActivity");
            }

            ValidationErrorCollection errors = new ValidationErrorCollection();

            CompositeActivity ownerActivity = contextActivity.TraverseDottedPathFromRoot(this.OwnerActivityDottedPath) as CompositeActivity;

            if (ownerActivity != null && WorkflowChanges.IsActivityExecutable(ownerActivity))
            {
                foreach (Validator validator in ComponentDispenser.CreateComponents(ownerActivity.GetType(), typeof(ActivityValidatorAttribute)))
                {
                    ValidationError error = validator.ValidateActivityChange(ownerActivity, this);
                    if (error != null)
                    {
                        errors.Add(error);
                    }
                }
            }

            return(errors);
        }
 protected internal override bool ApplyTo(Activity rootActivity)
 {
     if (rootActivity == null)
     {
         throw new ArgumentNullException("rootActivity");
     }
     if (!(rootActivity is CompositeActivity))
     {
         throw new ArgumentException(SR.GetString("Error_RootActivityTypeInvalid"), "rootActivity");
     }
     CompositeActivity activity = rootActivity.TraverseDottedPathFromRoot(base.OwnerActivityDottedPath) as CompositeActivity;
     if (activity == null)
     {
         return false;
     }
     if (this.removedActivityIndex >= activity.Activities.Count)
     {
         return false;
     }
     activity.DynamicUpdateMode = true;
     try
     {
         this.originalRemovedActivity = activity.Activities[this.removedActivityIndex];
         activity.Activities.RemoveAt(this.removedActivityIndex);
     }
     finally
     {
         activity.DynamicUpdateMode = false;
     }
     return true;
 }
예제 #3
0
        protected internal override bool ApplyTo(Activity rootActivity)
        {
            if (rootActivity == null)
            {
                throw new ArgumentNullException("rootActivity");
            }
            if (!(rootActivity is CompositeActivity))
            {
                throw new ArgumentException(SR.GetString("Error_RootActivityTypeInvalid"), "rootActivity");
            }
            CompositeActivity activity = rootActivity.TraverseDottedPathFromRoot(base.OwnerActivityDottedPath) as CompositeActivity;

            if (activity == null)
            {
                return(false);
            }
            if (this.removedActivityIndex >= activity.Activities.Count)
            {
                return(false);
            }
            activity.DynamicUpdateMode = true;
            try
            {
                this.originalRemovedActivity = activity.Activities[this.removedActivityIndex];
                activity.Activities.RemoveAt(this.removedActivityIndex);
            }
            finally
            {
                activity.DynamicUpdateMode = false;
            }
            return(true);
        }
 protected internal override ValidationErrorCollection ValidateChanges(Activity contextActivity)
 {
     ValidationErrorCollection errors = base.ValidateChanges(contextActivity);
     Activity activity = contextActivity.TraverseDottedPathFromRoot(this.originalRemovedActivity.DottedPath);
     if (WorkflowChanges.IsActivityExecutable(activity) && (activity.ExecutionStatus == ActivityExecutionStatus.Executing))
     {
         errors.Add(new ValidationError(SR.GetString("Error_RemoveExecutingActivity", new object[] { this.originalRemovedActivity.QualifiedName }), 0x11d));
     }
     return errors;
 }
예제 #5
0
        protected internal override ValidationErrorCollection ValidateChanges(Activity contextActivity)
        {
            ValidationErrorCollection errors = base.ValidateChanges(contextActivity);
            Activity activity = contextActivity.TraverseDottedPathFromRoot(this.originalRemovedActivity.DottedPath);

            if (WorkflowChanges.IsActivityExecutable(activity) && (activity.ExecutionStatus == ActivityExecutionStatus.Executing))
            {
                errors.Add(new ValidationError(SR.GetString("Error_RemoveExecutingActivity", new object[] { this.originalRemovedActivity.QualifiedName }), 0x11d));
            }
            return(errors);
        }
예제 #6
0
        private void NotifyChangesToChildExecutors(IWorkflowCoreRuntime workflowCoreRuntime, Activity contextActivity, IList <WorkflowChangeAction> changeActions)
        {
            foreach (WorkflowChangeAction action in changeActions)
            {
                if (!(action is ActivityChangeAction))
                {
                    continue;
                }

                CompositeActivity ownerActivity = contextActivity.TraverseDottedPathFromRoot(((ActivityChangeAction)action).OwnerActivityDottedPath) as CompositeActivity;
                if (ownerActivity == null || !WorkflowChanges.IsActivityExecutable(ownerActivity))
                {
                    continue;
                }

                ISupportWorkflowChanges compositeActivityExecutor = ActivityExecutors.GetActivityExecutor(ownerActivity) as ISupportWorkflowChanges;
                if (compositeActivityExecutor == null)
                {
                    throw new ApplicationException(SR.GetString(SR.Error_WorkflowChangesNotSupported, ownerActivity.GetType().FullName));
                }

                using (workflowCoreRuntime.SetCurrentActivity(ownerActivity))
                {
                    using (ActivityExecutionContext executionContext = new ActivityExecutionContext(ownerActivity))
                    {
                        if (action is AddedActivityAction)
                        {
                            Activity addedActivity = ownerActivity.Activities[((AddedActivityAction)action).Index];
                            if (WorkflowChanges.IsActivityExecutable(addedActivity))
                            {
                                addedActivity.OnActivityExecutionContextLoad(executionContext.Activity.RootActivity.WorkflowCoreRuntime);
                                executionContext.InitializeActivity(addedActivity);
                                compositeActivityExecutor.OnActivityAdded(executionContext, addedActivity);
                            }
                        }
                        else if (action is RemovedActivityAction)
                        {
                            RemovedActivityAction removedActivityAction = (RemovedActivityAction)action;
                            if (WorkflowChanges.IsActivityExecutable(removedActivityAction.OriginalRemovedActivity))
                            {
                                compositeActivityExecutor.OnActivityRemoved(executionContext, removedActivityAction.OriginalRemovedActivity);
                                if (removedActivityAction.OriginalRemovedActivity.ExecutionResult != ActivityExecutionResult.Uninitialized)
                                {
                                    removedActivityAction.OriginalRemovedActivity.Uninitialize(executionContext.Activity.RootActivity.WorkflowCoreRuntime);
                                    removedActivityAction.OriginalRemovedActivity.SetValue(Activity.ExecutionResultProperty, ActivityExecutionResult.Uninitialized);
                                }
                                removedActivityAction.OriginalRemovedActivity.OnActivityExecutionContextUnload(executionContext.Activity.RootActivity.WorkflowCoreRuntime);
                                removedActivityAction.OriginalRemovedActivity.Dispose();
                            }
                        }
                    }
                }
            }
        }
예제 #7
0
        protected internal override ValidationErrorCollection ValidateChanges(Activity contextActivity)
        {
            ValidationErrorCollection errors  = base.ValidateChanges(contextActivity);
            Activity removedActivityInContext = contextActivity.TraverseDottedPathFromRoot(this.originalRemovedActivity.DottedPath);

            if (WorkflowChanges.IsActivityExecutable(removedActivityInContext) && removedActivityInContext.ExecutionStatus == ActivityExecutionStatus.Executing)
            {
                errors.Add(new ValidationError(SR.GetString(SR.Error_RemoveExecutingActivity, this.originalRemovedActivity.QualifiedName), ErrorNumbers.Error_RemoveExecutingActivity));
            }
            return(errors);
        }
예제 #8
0
 private void NotifyChangesToChildExecutors(IWorkflowCoreRuntime workflowCoreRuntime, Activity contextActivity, IList <WorkflowChangeAction> changeActions)
 {
     foreach (WorkflowChangeAction action in changeActions)
     {
         if (action is ActivityChangeAction)
         {
             CompositeActivity activity = contextActivity.TraverseDottedPathFromRoot(((ActivityChangeAction)action).OwnerActivityDottedPath) as CompositeActivity;
             if ((activity != null) && IsActivityExecutable(activity))
             {
                 ISupportWorkflowChanges activityExecutor = ActivityExecutors.GetActivityExecutor(activity) as ISupportWorkflowChanges;
                 if (activityExecutor == null)
                 {
                     throw new ApplicationException(SR.GetString("Error_WorkflowChangesNotSupported", new object[] { activity.GetType().FullName }));
                 }
                 using (workflowCoreRuntime.SetCurrentActivity(activity))
                 {
                     using (ActivityExecutionContext context = new ActivityExecutionContext(activity))
                     {
                         if (action is AddedActivityAction)
                         {
                             Activity activity2 = activity.Activities[((AddedActivityAction)action).Index];
                             if (IsActivityExecutable(activity2))
                             {
                                 activity2.OnActivityExecutionContextLoad(context.Activity.RootActivity.WorkflowCoreRuntime);
                                 context.InitializeActivity(activity2);
                                 activityExecutor.OnActivityAdded(context, activity2);
                             }
                         }
                         else if (action is RemovedActivityAction)
                         {
                             RemovedActivityAction action2 = (RemovedActivityAction)action;
                             if (IsActivityExecutable(action2.OriginalRemovedActivity))
                             {
                                 activityExecutor.OnActivityRemoved(context, action2.OriginalRemovedActivity);
                                 if (action2.OriginalRemovedActivity.ExecutionResult != ActivityExecutionResult.Uninitialized)
                                 {
                                     action2.OriginalRemovedActivity.Uninitialize(context.Activity.RootActivity.WorkflowCoreRuntime);
                                     action2.OriginalRemovedActivity.SetValue(Activity.ExecutionResultProperty, ActivityExecutionResult.Uninitialized);
                                 }
                                 action2.OriginalRemovedActivity.OnActivityExecutionContextUnload(context.Activity.RootActivity.WorkflowCoreRuntime);
                                 action2.OriginalRemovedActivity.Dispose();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 protected internal override ValidationErrorCollection ValidateChanges(Activity contextActivity)
 {
     if (contextActivity == null)
     {
         throw new ArgumentNullException("contextActivity");
     }
     ValidationErrorCollection errors = new ValidationErrorCollection();
     CompositeActivity activity = contextActivity.TraverseDottedPathFromRoot(this.OwnerActivityDottedPath) as CompositeActivity;
     if ((activity != null) && WorkflowChanges.IsActivityExecutable(activity))
     {
         foreach (Validator validator in ComponentDispenser.CreateComponents(activity.GetType(), typeof(ActivityValidatorAttribute)))
         {
             ValidationError item = validator.ValidateActivityChange(activity, this);
             if (item != null)
             {
                 errors.Add(item);
             }
         }
     }
     return errors;
 }
 private void NotifyChangesToChildExecutors(IWorkflowCoreRuntime workflowCoreRuntime, Activity contextActivity, IList<WorkflowChangeAction> changeActions)
 {
     foreach (WorkflowChangeAction action in changeActions)
     {
         if (action is ActivityChangeAction)
         {
             CompositeActivity activity = contextActivity.TraverseDottedPathFromRoot(((ActivityChangeAction) action).OwnerActivityDottedPath) as CompositeActivity;
             if ((activity != null) && IsActivityExecutable(activity))
             {
                 ISupportWorkflowChanges activityExecutor = ActivityExecutors.GetActivityExecutor(activity) as ISupportWorkflowChanges;
                 if (activityExecutor == null)
                 {
                     throw new ApplicationException(SR.GetString("Error_WorkflowChangesNotSupported", new object[] { activity.GetType().FullName }));
                 }
                 using (workflowCoreRuntime.SetCurrentActivity(activity))
                 {
                     using (ActivityExecutionContext context = new ActivityExecutionContext(activity))
                     {
                         if (action is AddedActivityAction)
                         {
                             Activity activity2 = activity.Activities[((AddedActivityAction) action).Index];
                             if (IsActivityExecutable(activity2))
                             {
                                 activity2.OnActivityExecutionContextLoad(context.Activity.RootActivity.WorkflowCoreRuntime);
                                 context.InitializeActivity(activity2);
                                 activityExecutor.OnActivityAdded(context, activity2);
                             }
                         }
                         else if (action is RemovedActivityAction)
                         {
                             RemovedActivityAction action2 = (RemovedActivityAction) action;
                             if (IsActivityExecutable(action2.OriginalRemovedActivity))
                             {
                                 activityExecutor.OnActivityRemoved(context, action2.OriginalRemovedActivity);
                                 if (action2.OriginalRemovedActivity.ExecutionResult != ActivityExecutionResult.Uninitialized)
                                 {
                                     action2.OriginalRemovedActivity.Uninitialize(context.Activity.RootActivity.WorkflowCoreRuntime);
                                     action2.OriginalRemovedActivity.SetValue(Activity.ExecutionResultProperty, ActivityExecutionResult.Uninitialized);
                                 }
                                 action2.OriginalRemovedActivity.OnActivityExecutionContextUnload(context.Activity.RootActivity.WorkflowCoreRuntime);
                                 action2.OriginalRemovedActivity.Dispose();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
예제 #11
0
        protected internal override bool ApplyTo(Activity rootActivity)
        {
            if (rootActivity == null)
            {
                throw new ArgumentNullException("rootActivity");
            }
            if (!(rootActivity is CompositeActivity))
            {
                throw new ArgumentException(SR.GetString(SR.Error_RootActivityTypeInvalid), "rootActivity");
            }

            CompositeActivity ownerActivity = rootActivity.TraverseDottedPathFromRoot(this.OwnerActivityDottedPath) as CompositeActivity;

            if (ownerActivity == null)
            {
                return(false);
            }

            // !!!work around:
            ownerActivity.DynamicUpdateMode = true;
            CompositeActivity addedActivityOwner = this.addedActivity.Parent;

            try
            {
                this.addedActivity.SetParent(ownerActivity);
                Activity clonedAddedActivity = this.addedActivity;
                if (!this.addedActivity.DesignMode)
                {
                    clonedAddedActivity = this.addedActivity.Clone();
                }
                // We need to serialize and deserialize in order to clone during design mode
                else
                {
                    TypeProvider     typeProvider     = WorkflowChanges.CreateTypeProvider(rootActivity);
                    ServiceContainer serviceContainer = new ServiceContainer();
                    serviceContainer.AddService(typeof(ITypeProvider), typeProvider);
                    DesignerSerializationManager manager        = new DesignerSerializationManager(serviceContainer);
                    WorkflowMarkupSerializer     xomlSerializer = new WorkflowMarkupSerializer();
                    string addedActivityText = string.Empty;
                    // serialize dynamic updates
                    using (manager.CreateSession())
                    {
                        using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
                        {
                            using (XmlWriter xmlWriter = Helpers.CreateXmlWriter(sw))
                            {
                                WorkflowMarkupSerializationManager xomlSerializationManager = new WorkflowMarkupSerializationManager(manager);
                                xomlSerializer.Serialize(xomlSerializationManager, xmlWriter, this.addedActivity);
                                addedActivityText = sw.ToString();
                            }
                        }

                        // deserialize those
                        using (StringReader sr = new StringReader(addedActivityText))
                        {
                            using (XmlReader xmlReader = XmlReader.Create(sr))
                            {
                                WorkflowMarkupSerializationManager xomlSerializationManager = new WorkflowMarkupSerializationManager(manager);
                                clonedAddedActivity = xomlSerializer.Deserialize(xomlSerializationManager, xmlReader) as Activity;
                            }
                        }
                    }
                    if (clonedAddedActivity == null)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.Error_ApplyDynamicChangeFailed));
                    }
                }
                if (ownerActivity.WorkflowCoreRuntime != null)
                {
                    ((IDependencyObjectAccessor)clonedAddedActivity).InitializeInstanceForRuntime(ownerActivity.WorkflowCoreRuntime);
                }

                clonedAddedActivity.SetParent(null);
                ownerActivity.Activities.Insert(this.index, clonedAddedActivity);
            }
            finally
            {
                this.addedActivity.SetParent(addedActivityOwner);
                ownerActivity.DynamicUpdateMode = false;
            }
            return(true);
        }
예제 #12
0
        internal void ApplyTo(Activity activity)
        {
            if (activity == null)
            {
                throw new ArgumentNullException("activity");
            }

            if (activity.Parent != null)
            {
                throw new ArgumentException(SR.GetString(SR.Error_RootActivityTypeInvalid), "activity");
            }

            if (activity.RootActivity == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.Error_MissingRootActivity));
            }

            if (activity.WorkflowCoreRuntime == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.Error_NoRuntimeAvailable));
            }

            if (this.saved)
            {
                throw new InvalidOperationException(SR.GetString(SR.Error_TransactionAlreadyApplied));
            }

            if (!CompareWorkflowDefinition((Activity)this.originalRootActivity, (Activity)activity.RootActivity.GetValue(Activity.WorkflowDefinitionProperty)))
            {
                throw new ArgumentException(SR.GetString(SR.Error_WorkflowDefinitionModified), "activity");
            }

            this.Save();

            // go up in the chain and then apply changes
            IWorkflowCoreRuntime workflowCoreRuntime = activity.WorkflowCoreRuntime;

            if (workflowCoreRuntime.CurrentAtomicActivity != null)
            {
                throw new InvalidOperationException(SR.GetString(SR.Error_InsideAtomicScope));
            }
            bool suspended = workflowCoreRuntime.SuspendInstance(SR.GetString(SR.SuspendReason_WorkflowChange));

            try
            {
                // collect all context Activities
                List <Activity>  contextActivities      = new List <Activity>();
                Queue <Activity> contextActivitiesQueue = new Queue <Activity>();
                contextActivitiesQueue.Enqueue(workflowCoreRuntime.RootActivity);
                while (contextActivitiesQueue.Count > 0)
                {
                    Activity contextActivity = contextActivitiesQueue.Dequeue();
                    contextActivities.Add(contextActivity);

                    // enqueue child context Activities
                    IList <Activity> nestedContextActivities = (IList <Activity>)contextActivity.GetValue(Activity.ActiveExecutionContextsProperty);
                    if (nestedContextActivities != null)
                    {
                        foreach (Activity nestedContextActivity in nestedContextActivities)
                        {
                            contextActivitiesQueue.Enqueue(nestedContextActivity);
                        }
                    }
                }

                // run instance level validations
                ValidationErrorCollection validationErrors = new ValidationErrorCollection();
                foreach (WorkflowChangeAction changeAction in this.modelChangeActions)
                {
                    if (changeAction is ActivityChangeAction)
                    {
                        foreach (Activity contextActivity in contextActivities)
                        {
                            // WinOE

                            if (changeAction is RemovedActivityAction &&
                                contextActivity.DottedPath == ((RemovedActivityAction)changeAction).OriginalRemovedActivity.DottedPath)
                            {
                                validationErrors.AddRange(changeAction.ValidateChanges(contextActivity));
                            }

                            // Ask the parent context activity whether or not this child activity can be added or removed.
                            // The call to TraverseDottedPathFromRoot here should return the parent context activity for this change action.
                            if (contextActivity.TraverseDottedPathFromRoot(((ActivityChangeAction)changeAction).OwnerActivityDottedPath) != null)
                            {
                                validationErrors.AddRange(changeAction.ValidateChanges(contextActivity));
                            }
                        }
                    }
                }

                // if errors then return
                if (validationErrors.HasErrors)
                {
                    throw new WorkflowValidationFailedException(SR.GetString(SR.Error_RuntimeValidationFailed), validationErrors);
                }

                // verify if workflow can be changed
                VerifyWorkflowCanBeChanged(workflowCoreRuntime);

                // inform workflow runtime
                workflowCoreRuntime.OnBeforeDynamicChange(this.modelChangeActions);

                // set the new Workflow Definition
                workflowCoreRuntime.RootActivity.SetValue(Activity.WorkflowDefinitionProperty, this.clonedRootActivity);

                // apply changes to all context Activities
                foreach (Activity contextActivity in contextActivities)
                {
                    // apply change to state reader
                    foreach (WorkflowChangeAction changeAction in this.modelChangeActions)
                    {
                        if (changeAction is ActivityChangeAction)
                        {
                            if (contextActivity.TraverseDottedPathFromRoot(((ActivityChangeAction)changeAction).OwnerActivityDottedPath) != null)
                            {
                                bool result = changeAction.ApplyTo(contextActivity);
                                Debug.Assert(result, "ApplyTo failed");
                            }
                        }
                    }
                    // fixup meta properties and notify changes
                    // if the context activity is the one that's being removed, we do not fixup the meta properties.
                    Activity clonedActivity = ((Activity)this.clonedRootActivity).GetActivityByName(contextActivity.QualifiedName);
                    if (clonedActivity != null)
                    {
                        contextActivity.FixUpMetaProperties(clonedActivity);
                    }
                    NotifyChangesToChildExecutors(workflowCoreRuntime, contextActivity, this.modelChangeActions);
                    NotifyChangesCompletedToChildExecutors(workflowCoreRuntime, contextActivity);
                }

                // inform workflow runtime
                workflowCoreRuntime.OnAfterDynamicChange(true, this.modelChangeActions);
            }
            catch
            {
                workflowCoreRuntime.OnAfterDynamicChange(false, this.modelChangeActions);
                throw;
            }
            finally
            {
                if (suspended)
                {
                    workflowCoreRuntime.Resume();
                }
            }
        }
        private void NotifyChangesToChildExecutors(IWorkflowCoreRuntime workflowCoreRuntime, Activity contextActivity, IList<WorkflowChangeAction> changeActions)
        {
            foreach (WorkflowChangeAction action in changeActions)
            {
                if (!(action is ActivityChangeAction))
                    continue;

                CompositeActivity ownerActivity = contextActivity.TraverseDottedPathFromRoot(((ActivityChangeAction)action).OwnerActivityDottedPath) as CompositeActivity;
                if (ownerActivity == null || !WorkflowChanges.IsActivityExecutable(ownerActivity))
                    continue;

                ISupportWorkflowChanges compositeActivityExecutor = ActivityExecutors.GetActivityExecutor(ownerActivity) as ISupportWorkflowChanges;
                if (compositeActivityExecutor == null)
                    throw new ApplicationException(SR.GetString(SR.Error_WorkflowChangesNotSupported, ownerActivity.GetType().FullName));

                using (workflowCoreRuntime.SetCurrentActivity(ownerActivity))
                {
                    using (ActivityExecutionContext executionContext = new ActivityExecutionContext(ownerActivity))
                    {
                        if (action is AddedActivityAction)
                        {
                            Activity addedActivity = ownerActivity.Activities[((AddedActivityAction)action).Index];
                            if (WorkflowChanges.IsActivityExecutable(addedActivity))
                            {
                                addedActivity.OnActivityExecutionContextLoad(executionContext.Activity.RootActivity.WorkflowCoreRuntime);
                                executionContext.InitializeActivity(addedActivity);
                                compositeActivityExecutor.OnActivityAdded(executionContext, addedActivity);
                            }
                        }
                        else if (action is RemovedActivityAction)
                        {
                            RemovedActivityAction removedActivityAction = (RemovedActivityAction)action;
                            if (WorkflowChanges.IsActivityExecutable(removedActivityAction.OriginalRemovedActivity))
                            {
                                compositeActivityExecutor.OnActivityRemoved(executionContext, removedActivityAction.OriginalRemovedActivity);
                                if (removedActivityAction.OriginalRemovedActivity.ExecutionResult != ActivityExecutionResult.Uninitialized)
                                {
                                    removedActivityAction.OriginalRemovedActivity.Uninitialize(executionContext.Activity.RootActivity.WorkflowCoreRuntime);
                                    removedActivityAction.OriginalRemovedActivity.SetValue(Activity.ExecutionResultProperty, ActivityExecutionResult.Uninitialized);
                                }
                                removedActivityAction.OriginalRemovedActivity.OnActivityExecutionContextUnload(executionContext.Activity.RootActivity.WorkflowCoreRuntime);
                                removedActivityAction.OriginalRemovedActivity.Dispose();
                            }
                        }
                    }
                }
            }
        }
        protected internal override bool ApplyTo(Activity rootActivity)
        {
            if (rootActivity == null)
            {
                throw new ArgumentNullException("rootActivity");
            }
            if (!(rootActivity is CompositeActivity))
            {
                throw new ArgumentException(SR.GetString("Error_RootActivityTypeInvalid"), "rootActivity");
            }
            CompositeActivity compositeActivity = rootActivity.TraverseDottedPathFromRoot(base.OwnerActivityDottedPath) as CompositeActivity;

            if (compositeActivity == null)
            {
                return(false);
            }
            compositeActivity.DynamicUpdateMode = true;
            CompositeActivity parent = this.addedActivity.Parent;

            try
            {
                this.addedActivity.SetParent(compositeActivity);
                Activity addedActivity = this.addedActivity;
                if (!this.addedActivity.DesignMode)
                {
                    addedActivity = this.addedActivity.Clone();
                }
                else
                {
                    TypeProvider     serviceInstance = WorkflowChanges.CreateTypeProvider(rootActivity);
                    ServiceContainer provider        = new ServiceContainer();
                    provider.AddService(typeof(ITypeProvider), serviceInstance);
                    DesignerSerializationManager manager    = new DesignerSerializationManager(provider);
                    WorkflowMarkupSerializer     serializer = new WorkflowMarkupSerializer();
                    string s = string.Empty;
                    using (manager.CreateSession())
                    {
                        using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
                        {
                            using (XmlWriter writer2 = Helpers.CreateXmlWriter(writer))
                            {
                                WorkflowMarkupSerializationManager serializationManager = new WorkflowMarkupSerializationManager(manager);
                                serializer.Serialize(serializationManager, writer2, this.addedActivity);
                                s = writer.ToString();
                            }
                        }
                        using (StringReader reader = new StringReader(s))
                        {
                            using (XmlReader reader2 = XmlReader.Create(reader))
                            {
                                WorkflowMarkupSerializationManager manager3 = new WorkflowMarkupSerializationManager(manager);
                                addedActivity = serializer.Deserialize(manager3, reader2) as Activity;
                            }
                        }
                    }
                    if (addedActivity == null)
                    {
                        throw new InvalidOperationException(SR.GetString("Error_ApplyDynamicChangeFailed"));
                    }
                }
                if (compositeActivity.WorkflowCoreRuntime != null)
                {
                    ((IDependencyObjectAccessor)addedActivity).InitializeInstanceForRuntime(compositeActivity.WorkflowCoreRuntime);
                }
                addedActivity.SetParent(null);
                compositeActivity.Activities.Insert(this.index, addedActivity);
            }
            finally
            {
                this.addedActivity.SetParent(parent);
                compositeActivity.DynamicUpdateMode = false;
            }
            return(true);
        }
        protected internal override bool ApplyTo(Activity rootActivity)
        {
            if (rootActivity == null)
                throw new ArgumentNullException("rootActivity");
            if (!(rootActivity is CompositeActivity))
                throw new ArgumentException(SR.GetString(SR.Error_RootActivityTypeInvalid), "rootActivity");

            CompositeActivity ownerActivity = rootActivity.TraverseDottedPathFromRoot(this.OwnerActivityDottedPath) as CompositeActivity;
            if (ownerActivity == null)
                return false;

            // !!!work around: 
            ownerActivity.DynamicUpdateMode = true;
            CompositeActivity addedActivityOwner = this.addedActivity.Parent;
            try
            {
                this.addedActivity.SetParent(ownerActivity);
                Activity clonedAddedActivity = this.addedActivity;
                if (!this.addedActivity.DesignMode)
                    clonedAddedActivity = this.addedActivity.Clone();
                // We need to serialize and deserialize in order to clone during design mode
                else
                {
                    TypeProvider typeProvider = WorkflowChanges.CreateTypeProvider(rootActivity);
                    ServiceContainer serviceContainer = new ServiceContainer();
                    serviceContainer.AddService(typeof(ITypeProvider), typeProvider);
                    DesignerSerializationManager manager = new DesignerSerializationManager(serviceContainer);
                    WorkflowMarkupSerializer xomlSerializer = new WorkflowMarkupSerializer();
                    string addedActivityText = string.Empty;
                    // serialize dynamic updates
                    using (manager.CreateSession())
                    {
                        using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
                        {
                            using (XmlWriter xmlWriter = Helpers.CreateXmlWriter(sw))
                            {
                                WorkflowMarkupSerializationManager xomlSerializationManager = new WorkflowMarkupSerializationManager(manager);
                                xomlSerializer.Serialize(xomlSerializationManager, xmlWriter, this.addedActivity);
                                addedActivityText = sw.ToString();
                            }
                        }

                        // deserialize those
                        using (StringReader sr = new StringReader(addedActivityText))
                        {
                            using (XmlReader xmlReader = XmlReader.Create(sr))
                            {
                                WorkflowMarkupSerializationManager xomlSerializationManager = new WorkflowMarkupSerializationManager(manager);
                                clonedAddedActivity = xomlSerializer.Deserialize(xomlSerializationManager, xmlReader) as Activity;
                            }
                        }
                    }
                    if (clonedAddedActivity == null)
                        throw new InvalidOperationException(SR.GetString(SR.Error_ApplyDynamicChangeFailed));
                }
                if (ownerActivity.WorkflowCoreRuntime != null)
                    ((IDependencyObjectAccessor)clonedAddedActivity).InitializeInstanceForRuntime(ownerActivity.WorkflowCoreRuntime);

                clonedAddedActivity.SetParent(null);
                ownerActivity.Activities.Insert(this.index, clonedAddedActivity);
            }
            finally
            {
                this.addedActivity.SetParent(addedActivityOwner);
                ownerActivity.DynamicUpdateMode = false;
            }
            return true;
        }
 protected internal override ValidationErrorCollection ValidateChanges(Activity contextActivity)
 {
     ValidationErrorCollection errors = base.ValidateChanges(contextActivity);
     Activity removedActivityInContext = contextActivity.TraverseDottedPathFromRoot(this.originalRemovedActivity.DottedPath);
     if (WorkflowChanges.IsActivityExecutable(removedActivityInContext) && removedActivityInContext.ExecutionStatus == ActivityExecutionStatus.Executing)
         errors.Add(new ValidationError(SR.GetString(SR.Error_RemoveExecutingActivity, this.originalRemovedActivity.QualifiedName), ErrorNumbers.Error_RemoveExecutingActivity));
     return errors;
 }
 protected internal override bool ApplyTo(Activity rootActivity)
 {
     if (rootActivity == null)
     {
         throw new ArgumentNullException("rootActivity");
     }
     if (!(rootActivity is CompositeActivity))
     {
         throw new ArgumentException(SR.GetString("Error_RootActivityTypeInvalid"), "rootActivity");
     }
     CompositeActivity compositeActivity = rootActivity.TraverseDottedPathFromRoot(base.OwnerActivityDottedPath) as CompositeActivity;
     if (compositeActivity == null)
     {
         return false;
     }
     compositeActivity.DynamicUpdateMode = true;
     CompositeActivity parent = this.addedActivity.Parent;
     try
     {
         this.addedActivity.SetParent(compositeActivity);
         Activity addedActivity = this.addedActivity;
         if (!this.addedActivity.DesignMode)
         {
             addedActivity = this.addedActivity.Clone();
         }
         else
         {
             TypeProvider serviceInstance = WorkflowChanges.CreateTypeProvider(rootActivity);
             ServiceContainer provider = new ServiceContainer();
             provider.AddService(typeof(ITypeProvider), serviceInstance);
             DesignerSerializationManager manager = new DesignerSerializationManager(provider);
             WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
             string s = string.Empty;
             using (manager.CreateSession())
             {
                 using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
                 {
                     using (XmlWriter writer2 = Helpers.CreateXmlWriter(writer))
                     {
                         WorkflowMarkupSerializationManager serializationManager = new WorkflowMarkupSerializationManager(manager);
                         serializer.Serialize(serializationManager, writer2, this.addedActivity);
                         s = writer.ToString();
                     }
                 }
                 using (StringReader reader = new StringReader(s))
                 {
                     using (XmlReader reader2 = XmlReader.Create(reader))
                     {
                         WorkflowMarkupSerializationManager manager3 = new WorkflowMarkupSerializationManager(manager);
                         addedActivity = serializer.Deserialize(manager3, reader2) as Activity;
                     }
                 }
             }
             if (addedActivity == null)
             {
                 throw new InvalidOperationException(SR.GetString("Error_ApplyDynamicChangeFailed"));
             }
         }
         if (compositeActivity.WorkflowCoreRuntime != null)
         {
             ((IDependencyObjectAccessor) addedActivity).InitializeInstanceForRuntime(compositeActivity.WorkflowCoreRuntime);
         }
         addedActivity.SetParent(null);
         compositeActivity.Activities.Insert(this.index, addedActivity);
     }
     finally
     {
         this.addedActivity.SetParent(parent);
         compositeActivity.DynamicUpdateMode = false;
     }
     return true;
 }
        protected internal override bool ApplyTo(Activity rootActivity)
        {
            if (rootActivity == null)
                throw new ArgumentNullException("rootActivity");
            if (!(rootActivity is CompositeActivity))
                throw new ArgumentException(SR.GetString(SR.Error_RootActivityTypeInvalid), "rootActivity");


            CompositeActivity ownerActivity = rootActivity.TraverseDottedPathFromRoot(this.OwnerActivityDottedPath) as CompositeActivity;
            if (ownerActivity == null)
                return false;

            if (this.removedActivityIndex >= ownerActivity.Activities.Count)
                return false;

            // !!!work around: 
            ownerActivity.DynamicUpdateMode = true;
            try
            {
                this.originalRemovedActivity = ownerActivity.Activities[this.removedActivityIndex];
                ownerActivity.Activities.RemoveAt(this.removedActivityIndex);
            }
            finally
            {
                ownerActivity.DynamicUpdateMode = false;
            }
            return true;
        }