Пример #1
0
        /// <summary>
        /// 克隆一个实例
        /// </summary>
        /// <param name="WfRuntimeClone"></param>
        /// <param name="instanceClone"></param>
        /// <param name="WfRuntime"></param>
        /// <returns></returns>
        public static WorkflowInstance CloneWorkflowInstance(WorkflowRuntime WfRuntimeClone, WorkflowInstance instanceClone, WorkflowRuntime WfRuntime)
        {
            try
            {
                if (!WfRuntimeClone.IsStarted)
                {
                    WfRuntimeClone.StartRuntime();
                }
                StateMachineWorkflowInstance workflowinstance = new StateMachineWorkflowInstance(WfRuntimeClone, instanceClone.InstanceId);

                System.Workflow.Activities.StateMachineWorkflowActivity smworkflow = new StateMachineWorkflowActivity();
                smworkflow = workflowinstance.StateMachineWorkflow;
                RuleDefinitions          ruleDefinitions  = smworkflow.GetValue(RuleDefinitions.RuleDefinitionsProperty) as RuleDefinitions;
                WorkflowMarkupSerializer markupSerializer = new WorkflowMarkupSerializer();

                StringBuilder xoml       = new StringBuilder();
                StringBuilder rule       = new StringBuilder();
                XmlWriter     xmlWriter  = XmlWriter.Create(xoml);
                XmlWriter     ruleWriter = XmlWriter.Create(rule);
                markupSerializer.Serialize(xmlWriter, smworkflow);

                if (ruleDefinitions != null)
                {
                    markupSerializer.Serialize(ruleWriter, ruleDefinitions);
                }

                xmlWriter.Close();
                ruleWriter.Close();

                StringReader     readxoml   = new StringReader(xoml.ToString());
                XmlReader        readerxoml = XmlReader.Create(readxoml);
                WorkflowInstance instance;
                if (ruleDefinitions == null)
                {
                    instance = WfRuntime.CreateWorkflow(readerxoml);
                }
                else
                {
                    StringReader readrule   = new StringReader(rule.ToString());
                    XmlReader    readerrule = XmlReader.Create(readrule);
                    instance = WfRuntime.CreateWorkflow(readerxoml, readerrule, null);
                }

                instance.Start();
                return(instance);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("CloneWorkflowInstance异常信息 :" + ex.ToString());
                throw new Exception(ex.Message);
            }
        }
Пример #2
0
        public void EditCommon(object sender, EventArgs e)
        {
            RuleSet ruleSet = null;

            XmlTextReader            rulesReader = new XmlTextReader("Common.rules");
            WorkflowMarkupSerializer serializer  = new WorkflowMarkupSerializer();

            ruleSet = (RuleSet)serializer.Deserialize(rulesReader);

            rulesReader.Close();

            RuleSetDialog ruleSetDialog = new RuleSetDialog(typeof(keyHandlerState), null, ruleSet);
            DialogResult  result        = ruleSetDialog.ShowDialog();

            ruleSet = ruleSetDialog.RuleSet;

            if (result == DialogResult.OK)
            {
                // Serialize to a .rules file
                serializer = new WorkflowMarkupSerializer();

                XmlWriter rulesWriter = XmlWriter.Create("Common.rules");
                serializer.Serialize(rulesWriter, ruleSet);
                rulesWriter.Close();
            }
        }
Пример #3
0
        private void OpenXamlInNotepad(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            WorkflowNode      node = (WorkflowNode)item.Tag;

            object workflowInstance = null;

            try
            {
                workflowInstance = node.WorkflowType.InvokeMember(string.Empty, BindingFlags.CreateInstance, null, null, null, CultureInfo.InvariantCulture);
            }
            catch (Exception exc)
            {
                MessageBox.Show("The workflow type could not be created: " + exc.ToString());
                return;
            }

            WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

            string tempFileName = Path.GetTempFileName();


            using (XmlWriter xmlWriter = XmlWriter.Create(tempFileName))
            {
                serializer.Serialize(xmlWriter, workflowInstance);
            }

            ProcessStartInfo psi = new ProcessStartInfo();

            psi.Arguments = tempFileName;
            psi.FileName  = Path.Combine(Environment.GetEnvironmentVariable("windir"), "notepad.exe");

            Process.Start(psi);
        }
Пример #4
0
        protected override void PerformFlush(IDesignerSerializationManager manager)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));

            if (host != null && host.RootComponent != null)
            {
                Activity rootActivity = host.RootComponent as Activity;
                if (rootActivity != null)
                {
                    StringWriter writer = new StringWriter();
                    try
                    {
                        using (XmlWriter xmlWriter = XmlWriter.Create(writer))
                        {
                            WorkflowMarkupSerializer xamlSerializer = new WorkflowMarkupSerializer();
                            xamlSerializer.Serialize(xmlWriter, rootActivity);
                        }
                    }
                    finally
                    {
                        writer.Close();
                    }
                    Xaml = writer.ToString();
                }
            }
        }
Пример #5
0
 public static void Serialize(RuleSet ruleset, StringBuilder sb)
 {
     using (XmlWriter rulesWriter = XmlWriter.Create(sb))
     {
         WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
         serializer.Serialize(rulesWriter, ruleset);
     }
 }
Пример #6
0
 public void WriteXml(System.Xml.XmlWriter writer)
 {
     if (RuleSet != null)
     {
         WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
         serializer.Serialize(writer, RuleSet);
     }
 }
        /// <summary>
        /// Saves the expression.
        /// </summary>
        /// <param name="set">The set.</param>
        /// <param name="expressionId">The expression id.</param>
        protected virtual void SaveExpression(RuleSet set, int expressionId)
        {
            if (set == null)
            {
                throw new ArgumentNullException("RuleSet");
            }

            if (DataSource == null)
            {
                throw new ArgumentNullException("DataSource");
            }

            ExpressionDto dto = GetDataSourceDto(DataSource);

            ExpressionDto.ExpressionRow row = dto.Expression.FindByExpressionId(expressionId);

            if (row == null /*&& (expressionId == 0 || dto.Expression.Count == 0)*/)             // new one
            {
                row               = dto.Expression.NewExpressionRow();
                row.Name          = set.Name;
                row.Description   = String.Empty;
                row.ApplicationId = MarketingConfiguration.Instance.ApplicationId;
                row.Category      = ExpressionCategory.CategoryKey.Segment.ToString();
                row.Created       = DateTime.UtcNow;
            }
            else
            {
                row.Modified = DateTime.UtcNow;
            }

            // Serialize ruleset
            StringBuilder ruleDefinition = new StringBuilder();

            #region Serialize

            StringWriter             stringWriter = new StringWriter(ruleDefinition, CultureInfo.InvariantCulture);
            XmlTextWriter            writer       = new XmlTextWriter(stringWriter);
            WorkflowMarkupSerializer serializer   = new WorkflowMarkupSerializer();
            serializer.Serialize(writer, set);
            #endregion

            #region Cleanup

            writer.Flush();
            writer.Close();
            stringWriter.Flush();
            stringWriter.Close();
            #endregion

            row.ExpressionXml = ruleDefinition.ToString();

            if (row.RowState == System.Data.DataRowState.Detached)
            {
                dto.Expression.Rows.Add(row);
            }
        }
Пример #8
0
 private static string Serialize(RuleSet ruleSet)
 {
     using (var stringWriter = new StringWriter())
         using (var xmlTextWriter = new XmlTextWriter(stringWriter))
         {
             WorkflowMarkupSerializer.Serialize(xmlTextWriter, ruleSet);
             var retval = stringWriter.ToString();
             return(retval);
         }
 }
Пример #9
0
        public void SaveRuleSet()
        {
            RuleDefinitions ruleDefinitions = new RuleDefinitions();

            ruleDefinitions.RuleSets.Add(createRuleSet);
            WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
            XmlTextWriter            writer     = new XmlTextWriter("RackRateRules.rules", System.Text.Encoding.Unicode);

            serializer.Serialize(writer, ruleDefinitions);
            writer.Close();
        }
Пример #10
0
        static void SaveRules(RuleSet ruleSet, string ruleFilePath)
        {
            // Serialize to a .rules file

            WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

            XmlWriter rulesWriter = XmlWriter.Create(ruleFilePath);

            serializer.Serialize(rulesWriter, ruleSet);

            rulesWriter.Close();
        }
Пример #11
0
        /// <summary>
        /// Gets workflow markup string from the object.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static string GetString <T>(T obj)
        {
            StringBuilder sbOutput = new StringBuilder(1024);

            using (XmlWriter writer = XmlWriter.Create(sbOutput))
            {
                WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                serializer.Serialize(writer, obj);
            }

            return(sbOutput.ToString());
        }
Пример #12
0
        internal static string SerializeRuleSet(object rules, DotNetPlatForm platForm)
        {
            StringBuilder            ruleSet      = new StringBuilder();
            WorkflowMarkupSerializer serializer   = new WorkflowMarkupSerializer();
            StringWriter             stringWriter = new StringWriter(ruleSet, CultureInfo.InvariantCulture);
            XmlTextWriter            writer       = new XmlTextWriter(stringWriter);

            serializer.Serialize(writer, rules);
            writer.Flush();
            writer.Close();
            stringWriter.Flush();
            stringWriter.Close();
            return(ruleSet.ToString().UpdateStrongNameByPlatForm(platForm));
        }
Пример #13
0
        /// <summary>
        /// Serializes the given ruleset into Xml Format
        /// </summary>
        /// <param name="ruleSet">RuleSet to Serialize</param>
        /// <returns>Xml Formated representation of RuleSet</returns>
        private static string SerializeRuleSet(RuleSet ruleSet)
        {
            StringBuilder ruleDefinition = new StringBuilder();

            if (ruleSet != null)
            {
                try
                {
                    #region Serialize

                    StringWriter             stringWriter = new StringWriter(ruleDefinition, CultureInfo.InvariantCulture);
                    XmlTextWriter            writer       = new XmlTextWriter(stringWriter);
                    WorkflowMarkupSerializer serializer   = new WorkflowMarkupSerializer();
                    serializer.Serialize(writer, ruleSet);
                    #endregion

                    #region Cleanup

                    writer.Flush();
                    writer.Close();
                    stringWriter.Flush();
                    stringWriter.Close();
                    #endregion
                }
                catch (Exception ex)
                {
                    if (ruleSet != null)
                    {
                        MessageBox.Show(string.Format(CultureInfo.InvariantCulture, "Error serializing RuleSet: '{0}'. \r\n\n{1}", ruleSet.Name, ex.Message), "Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show(string.Format(CultureInfo.InvariantCulture, "Error serializing RuleSet. \r\n\n{0}", ex.Message), "Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                if (ruleSet != null)
                {
                    MessageBox.Show(String.Format(CultureInfo.InvariantCulture, "Error serializing RuleSet: '{0}'.", ruleSet.Name), "Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Error serializing RuleSet.", "Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            return(ruleDefinition.ToString());
        }
Пример #14
0
        public void ShowDefaultWorkflow()
        {
            SequentialWorkflowActivity workflow = new SequentialWorkflowActivity();

            workflow.Name = "Workflow1";

            using (StringWriter stringWriter = new StringWriter())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
                {
                    WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                    serializer.Serialize(xmlWriter, workflow);
                    this.Xaml = stringWriter.ToString();
                }
            }
        }
Пример #15
0
        public void PerformFlush(IDesignerHost host)
        {
            if (host != null && host.RootComponent != null)
            {
                Activity service = host.RootComponent as Activity;

                if (service != null)
                {
                    using (XmlWriter writer = XmlWriter.Create(this.xamlFile))
                    {
                        WorkflowMarkupSerializer xamlSerializer = new WorkflowMarkupSerializer();
                        xamlSerializer.Serialize(writer, service);
                    }
                }
            }
        }
Пример #16
0
        private static ArrayList CloneWorkflowChangeActions(ArrayList workflowChanges, Activity rootActivity)
        {
            if (workflowChanges == null)
            {
                throw new ArgumentNullException("workflowChanges");
            }

            if (rootActivity == null)
            {
                throw new ArgumentNullException("rootActivity");
            }

            string           dynamicUpdateHistory = null;
            TypeProvider     typeProvider         = CreateTypeProvider(rootActivity);
            ServiceContainer serviceContainer     = new ServiceContainer();

            serviceContainer.AddService(typeof(ITypeProvider), typeProvider);
            DesignerSerializationManager manager        = new DesignerSerializationManager(serviceContainer);
            WorkflowMarkupSerializer     xomlSerializer = new WorkflowMarkupSerializer();

            ArrayList clonedWorkflowChanges = null;

            // 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, workflowChanges);
                        dynamicUpdateHistory = sw.ToString();
                    }
                }

                // deserialize those
                using (StringReader sr = new StringReader(dynamicUpdateHistory))
                {
                    using (XmlReader xmlReader = XmlReader.Create(sr))
                    {
                        WorkflowMarkupSerializationManager xomlSerializationManager = new WorkflowMarkupSerializationManager(manager);
                        clonedWorkflowChanges = xomlSerializer.Deserialize(xomlSerializationManager, xmlReader) as ArrayList;
                    }
                }
            }
            return(clonedWorkflowChanges);
        }
Пример #17
0
        private static string SerializeRules(object drs)
        {
            System.Text.StringBuilder ruleDefinition = new System.Text.StringBuilder();
            WorkflowMarkupSerializer  serializer     = new WorkflowMarkupSerializer();

            using (System.IO.StringWriter stringWriter = new System.IO.StringWriter(ruleDefinition, System.Globalization.CultureInfo.InvariantCulture))
            {
                using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stringWriter))
                {
                    serializer.Serialize(writer, drs);
                    writer.Flush();
                }
                stringWriter.Flush();
            }

            return(ruleDefinition.ToString());
        }
Пример #18
0
        public void CreateNewWorkflow()
        {
            SequentialWorkflowActivity rootActivity = new SequentialWorkflowActivity("root");

            rootActivity.SetValue(WorkflowMarkupSerializer.XClassProperty, WorkflowTypeFactory.GetWorkflowClassFullName(Workflow));

            StringBuilder sb = new StringBuilder();

            XmlWriter xmlWriter = XmlWriter.Create(sb);
            WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

            serializer.Serialize(xmlWriter, rootActivity);
            xmlWriter.Close();
            Workflow.WorkflowXoml = sb.ToString();
            //Check out the created workflow
            CheckOutInObject(Workflow, true);
        }
Пример #19
0
        public string WorkflowClassToXomlString(Activity workflow)
        {
            WorkflowMarkupSerializer     wfSerializer = new WorkflowMarkupSerializer();
            DesignerSerializationManager sm           = new DesignerSerializationManager();

            sm.CreateSession();

            System.Text.StringBuilder s = new StringBuilder();
            XmlWriter xmlwriter         = XmlWriter.Create(s);

            wfSerializer.Serialize(sm, xmlwriter, workflow);
            if (sm.Errors.Count > 0)
            {
                throw new Exception("出现错误:" + sm.Errors.Count.ToString());
            }
            return(s.ToString());
        }
Пример #20
0
 /// <summary>
 /// Serialize a workflow to markup (xaml)
 /// </summary>
 /// <param name="workflow"></param>
 /// <param name="fileName"></param>
 private static void SerializeToMarkup(
     Activity workflow, String fileName)
 {
     try
     {
         using (XmlWriter xmlWriter = XmlWriter.Create(fileName))
         {
             WorkflowMarkupSerializer markupSerializer
                 = new WorkflowMarkupSerializer();
             markupSerializer.Serialize(xmlWriter, workflow);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception during serialization: {0}",
                           e.Message);
     }
 }
Пример #21
0
        public WorkflowTheme Clone()
        {
            WorkflowTheme theme = null;
            DesignerSerializationManager serializationManager = new DesignerSerializationManager();

            using (serializationManager.CreateSession())
            {
                ThemeSerializationProvider provider = new ThemeSerializationProvider();
                StringWriter output = new StringWriter(new StringBuilder(), CultureInfo.InvariantCulture);
                StringReader input  = null;
                try
                {
                    ((IDesignerSerializationManager)serializationManager).AddSerializationProvider(provider);
                    WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                    using (XmlWriter writer2 = Helpers.CreateXmlWriter(output))
                    {
                        serializer.Serialize(serializationManager, writer2, this);
                    }
                    input = new StringReader(output.ToString());
                    using (XmlReader reader2 = XmlReader.Create(input))
                    {
                        theme = serializer.Deserialize(serializationManager, reader2) as WorkflowTheme;
                    }
                }
                finally
                {
                    ((IDesignerSerializationManager)serializationManager).RemoveSerializationProvider(provider);
                    input.Close();
                    output.Close();
                }
            }
            if (theme != null)
            {
                theme.filePath = this.filePath;
                foreach (DesignerTheme theme2 in theme.DesignerThemes)
                {
                    theme2.Initialize();
                }
            }
            return(theme);
        }
Пример #22
0
        public void CreateNewWorkflow(Module module)
        {
            workflow             = new Cdc.MetaManager.DataAccess.Domain.Workflow();
            workflow.RequestMap  = new PropertyMap();
            workflow.Name        = tbName.Text;
            workflow.Module      = module;
            workflow.Description = tbDescription.Text;

            SequentialWorkflowActivity rootActivity = new SequentialWorkflowActivity("root");

            rootActivity.SetValue(WorkflowMarkupSerializer.XClassProperty, WorkflowTypeFactory.GetWorkflowClassFullName(workflow));

            StringBuilder sb = new StringBuilder();

            XmlWriter xmlWriter = XmlWriter.Create(sb);
            WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

            serializer.Serialize(xmlWriter, rootActivity);
            xmlWriter.Close();
            workflow.WorkflowXoml = sb.ToString();
        }
Пример #23
0
        /// <summary>
        /// Exports the DefaultRuleSet into xml
        /// </summary>
        /// <returns>Xml Representation of DefaultRuleSet</returns>
        private static string ExportRuleSets()
        {
            StringBuilder ruleDefinition = new StringBuilder();

            #region Serialize

            StringWriter             stringWriter = new StringWriter(ruleDefinition, CultureInfo.InvariantCulture);
            XmlTextWriter            writer       = new XmlTextWriter(stringWriter);
            WorkflowMarkupSerializer serializer   = new WorkflowMarkupSerializer();
            serializer.Serialize(writer, DefaultRuleSet.ActivityRuleSet);
            #endregion

            #region Cleanup

            writer.Flush();
            writer.Close();
            stringWriter.Flush();
            stringWriter.Close();
            #endregion

            return(ruleDefinition.ToString());
        }
Пример #24
0
        private static ArrayList CloneWorkflowChangeActions(ArrayList workflowChanges, Activity rootActivity)
        {
            if (workflowChanges == null)
            {
                throw new ArgumentNullException("workflowChanges");
            }
            if (rootActivity == null)
            {
                throw new ArgumentNullException("rootActivity");
            }
            string           s = null;
            TypeProvider     serviceInstance = CreateTypeProvider(rootActivity);
            ServiceContainer provider        = new ServiceContainer();

            provider.AddService(typeof(ITypeProvider), serviceInstance);
            DesignerSerializationManager manager    = new DesignerSerializationManager(provider);
            WorkflowMarkupSerializer     serializer = new WorkflowMarkupSerializer();

            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, workflowChanges);
                        s = writer.ToString();
                    }
                }
                using (StringReader reader = new StringReader(s))
                {
                    using (XmlReader reader2 = XmlReader.Create(reader))
                    {
                        WorkflowMarkupSerializationManager manager3 = new WorkflowMarkupSerializationManager(manager);
                        return(serializer.Deserialize(manager3, reader2) as ArrayList);
                    }
                }
            }
        }
Пример #25
0
        /// <summary>
        /// Serialize the workflow to a xoml file
        /// </summary>
        /// <param name="workflow"></param>
        /// <param name="fileName"></param>
        private void SerializeToMarkup(
            Activity workflow, String fileName)
        {
            //clear the class name property since we are
            //never creating a new class type.
            workflow.SetValue(
                WorkflowMarkupSerializer.XClassProperty, null);

            using (XmlWriter xmlWriter = XmlWriter.Create(fileName))
            {
                WorkflowMarkupSerializer markupSerializer
                    = new WorkflowMarkupSerializer();
                markupSerializer.Serialize(xmlWriter, workflow);
            }

            //Serialize rules if they exist
            RuleDefinitions ruleDefinitions = workflow.GetValue(
                RuleDefinitions.RuleDefinitionsProperty)
                                              as RuleDefinitions;

            if (ruleDefinitions != null)
            {
                if (ruleDefinitions.Conditions.Count > 0 ||
                    ruleDefinitions.RuleSets.Count > 0)
                {
                    String rulesFileName = GetRulesFileName(fileName);
                    using (XmlWriter xmlWriter
                               = XmlWriter.Create(rulesFileName))
                    {
                        WorkflowMarkupSerializer markupSerializer
                            = new WorkflowMarkupSerializer();
                        markupSerializer.Serialize(
                            xmlWriter, ruleDefinitions);
                    }
                }
            }
        }
Пример #26
0
        public void ManageProfile(object sender, EventArgs e)
        {
            var role = _game.GetProfileName(_process, _items.CurrentId());

            RuleSet ruleSet = null;

            if (File.Exists(role + ".rules"))
            {
                XmlTextReader            rulesReader = new XmlTextReader(role + ".rules");
                WorkflowMarkupSerializer serializer  = new WorkflowMarkupSerializer();
                ruleSet = (RuleSet)serializer.Deserialize(rulesReader);
                rulesReader.Close();
            }
            else
            {
                XmlTextReader            rulesReader = new XmlTextReader("Common.rules");
                WorkflowMarkupSerializer serializer  = new WorkflowMarkupSerializer();
                ruleSet = (RuleSet)serializer.Deserialize(rulesReader);

                rulesReader.Close();
            }

            RuleSetDialog ruleSetDialog = new RuleSetDialog(typeof(keyHandlerState), null, ruleSet);
            DialogResult  result        = ruleSetDialog.ShowDialog();

            ruleSet = ruleSetDialog.RuleSet;

            if (result == DialogResult.OK)
            {
                // Serialize to a .rules file
                WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();

                XmlWriter rulesWriter = XmlWriter.Create(role + ".rules");
                serializer.Serialize(rulesWriter, ruleSet);
                rulesWriter.Close();
            }
        }
Пример #27
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);
        }
Пример #28
0
        private void viewRuleSetButton_Click(object sender, RoutedEventArgs e)
        {
            // get the rulesFilePath property
            object rulesFilePath = ModelItem.Properties["RulesFilePath"].Value;

            rulesFilePath = ((ModelItem)rulesFilePath).GetCurrentValue();

            // correct the rules file path (in case is a relative path)
            string correctedRulesFilePath = GetRulesFilePath((string)rulesFilePath);

            // verify that RulesFilePath property has been configured
            if (rulesFilePath == null || !(rulesFilePath is string))
            {
                System.Windows.MessageBox.Show("Rules File Path needs to be configured before viewing or editing the rules");
                return;
            }
            else if (!File.Exists(correctedRulesFilePath))
            {
                System.Windows.MessageBox.Show(string.Format("Rules File Path provided not found ({0})", correctedRulesFilePath));
                return;
            }

            // verify that RuleSetName property has been configured
            object ruleSetName = ModelItem.Properties["RuleSetName"].Value;

            ruleSetName = ((ModelItem)ruleSetName).GetCurrentValue();
            if (ruleSetName == null)
            {
                System.Windows.MessageBox.Show("RuleSet Name needs to be configured before viewing or editing the rules");
                return;
            }

            // verify that TargetObject property has been configured
            object targetObject = ModelItem.Properties["TargetObject"].Value;

            targetObject = ((ModelItem)targetObject).GetCurrentValue();
            if (targetObject == null)
            {
                System.Windows.MessageBox.Show("TargetObject needs to be configured before viewing or editing the rules");
                return;
            }

            // verify that target object is correctly configured
            InArgument targetObjArg = targetObject as InArgument;

            if (targetObjArg == null)
            {
                System.Windows.MessageBox.Show("Invalid target object");
                return;
            }

            // open the ruleset editor
            Type targetObjectType             = targetObjArg.ArgumentType;
            WorkflowMarkupSerializer ser      = new WorkflowMarkupSerializer();
            RuleDefinitions          ruleDefs = ser.Deserialize(new System.Xml.XmlTextReader((string)correctedRulesFilePath)) as RuleDefinitions;
            RuleSet ruleSet = ruleDefs.RuleSets[(string)ruleSetName];

            // popup the dialog for viewing the rules
            RuleSetDialog ruleSetDialog = new RuleSetDialog(targetObjectType, null, ruleSet);
            DialogResult  result        = ruleSetDialog.ShowDialog();

            // update if they changed the Rules
            if (result == DialogResult.OK) //If OK was pressed
            {
                for (int index = 0; index < ruleDefs.RuleSets.Count; index++)
                {
                    if (ruleDefs.RuleSets[index].Name == (string)ruleSetName)
                    {
                        ruleDefs.RuleSets[index] = ruleSetDialog.RuleSet;
                        break;
                    }
                }
                try
                {
                    ser.Serialize(new System.Xml.XmlTextWriter(correctedRulesFilePath, null), ruleDefs);
                }
                catch (UnauthorizedAccessException)
                {
                    // File does not have write access. Make a local copy so user changes are not lost
                    FileInfo fileInfo = new FileInfo(correctedRulesFilePath);
                    // create local file by adding a random suffix to original filename
                    string localFileCopy = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.')) + new Random().Next() + fileInfo.Extension;
                    ser.Serialize(new System.Xml.XmlTextWriter((string)localFileCopy, null), ruleDefs);
                    System.Windows.MessageBox.Show("Rules file is not writeable. Created copy of your changes in " + localFileCopy);
                }
            }
        }
Пример #29
0
        /// <summary>
        /// Save the Xaml file and flush the code compile unit into a .CS file
        /// </summary>
        /// <param name="manager"></param>
        protected override void PerformFlush(IDesignerSerializationManager manager)
        {
            IDesignerHost host         = (IDesignerHost)GetService(typeof(IDesignerHost));
            Activity      rootActivity = host.RootComponent as Activity;

            if (host != null && host.RootComponent != null)
            {
                if (rootActivity != null)
                {
                    StringBuilder sb = new StringBuilder();

                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.NewLineOnAttributes = true;
                    settings.Indent = true;

                    XmlWriter xmlWriter = XmlWriter.Create(sb, settings);

                    try
                    {
                        WorkflowMarkupSerializer xomlSerializer = new WorkflowMarkupSerializer();
                        xomlSerializer.Serialize(xmlWriter, rootActivity);
                    }
                    finally
                    {
                        xmlWriter.Close();
                    }

                    Xoml = sb.ToString();
                }
            }

            // Flush CS code
            //CSharpCodeProvider provider = new CSharpCodeProvider();
            //ICodeGenerator generator = provider.CreateGenerator();

            //// Just chooses some formatting options, like four space indenting
            //CodeGeneratorOptions options = new CodeGeneratorOptions();
            //options.BlankLinesBetweenMembers = true;
            //options.BracingStyle = "C";
            //options.ElseOnClosing = false;
            //options.IndentString = "    ";

            //StringBuilder builder = new StringBuilder();
            //StringWriter writer = new StringWriter(builder);
            //generator.GenerateCodeFromCompileUnit(this.codeBesideccu, writer, options);
            //writer.Close();
            //CodeBesideSource = builder.ToString();


            //Flush the rules file
            //if (this.tempRulesStream != null)
            //{
            //    string rulesFile = Path.Combine(Path.GetDirectoryName(this.xoml), Path.GetFileNameWithoutExtension(this.xoml) + ".rules");
            //    using (StreamWriter rulesWriter = new StreamWriter(rulesFile))
            //    {
            //        rulesWriter.Write(this.tempRulesStream.ToString());
            //    }
            //}

            #region State Machine Workflow specific code
            // Need to save the layout in case of State Machine Workflow

            //string layoutFile = Path.Combine(Path.GetDirectoryName(this.xoml), Path.GetFileNameWithoutExtension(this.xoml) + ".layout");
            //ActivityDesigner rootdesigner = host.GetDesigner(rootActivity) as ActivityDesigner;
            //XmlWriter layoutwriter = XmlWriter.Create(layoutFile);
            //IList errors = null;
            //SaveDesignerLayout(layoutwriter, rootdesigner, out errors);
            //layoutwriter.Close();

            #endregion
        }
        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);
        }