Пример #1
0
        public bool SaveWorkflow(Stream streamdata)
        {
            try
            {
                StreamReader reader = new StreamReader(streamdata);
                string       json   = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();

                JObject o  = JObject.Parse(json);
                var     wi = o.SelectToken("WorkflowInfo");

                var isNew = (bool)wi.SelectToken("IsNew");
                if (isNew)
                {
                    XNamespace xn   = "urn:wexflow-schema";
                    var        xdoc = new XDocument();

                    int        workflowId         = (int)wi.SelectToken("Id");
                    string     workflowName       = (string)wi.SelectToken("Name");
                    LaunchType workflowLaunchType = (LaunchType)((int)wi.SelectToken("LaunchType"));
                    string     p = (string)wi.SelectToken("Period");
                    TimeSpan   workflowPeriod = TimeSpan.Parse(string.IsNullOrEmpty(p) ? "00.00:00:00" : p);
                    string     cronExpression = (string)wi.SelectToken("CronExpression");

                    if (workflowLaunchType == LaunchType.Cron && !WexflowEngine.IsCronExpressionValid(cronExpression))
                    {
                        throw new Exception("The cron expression '" + cronExpression + "' is not valid.");
                    }

                    bool   isWorkflowEnabled = (bool)wi.SelectToken("IsEnabled");
                    string workflowDesc      = (string)wi.SelectToken("Description");

                    // tasks
                    var xtasks = new XElement(xn + "Tasks");
                    var tasks  = o.SelectToken("Tasks");
                    foreach (var task in tasks)
                    {
                        int    taskId        = (int)task.SelectToken("Id");
                        string taskName      = (string)task.SelectToken("Name");
                        string taskDesc      = (string)task.SelectToken("Description");
                        bool   isTaskEnabled = (bool)task.SelectToken("IsEnabled");

                        var xtask = new XElement(xn + "Task"
                                                 , new XAttribute("id", taskId)
                                                 , new XAttribute("name", taskName)
                                                 , new XAttribute("description", taskDesc)
                                                 , new XAttribute("enabled", isTaskEnabled.ToString().ToLower())
                                                 );

                        var settings = task.SelectToken("Settings");
                        foreach (var setting in settings)
                        {
                            string settingName  = (string)setting.SelectToken("Name");
                            string settingValue = (string)setting.SelectToken("Value");

                            var xsetting = new XElement(xn + "Setting"
                                                        , new XAttribute("name", settingName)
                                                        );

                            if (!string.IsNullOrEmpty(settingValue))
                            {
                                xsetting.SetAttributeValue("value", settingValue);
                            }

                            if (settingName == "selectFiles" || settingName == "selectAttachments")
                            {
                                if (!string.IsNullOrEmpty(settingValue))
                                {
                                    xsetting.SetAttributeValue("value", settingValue);
                                }
                            }
                            else
                            {
                                xsetting.SetAttributeValue("value", settingValue);
                            }

                            var attributes = setting.SelectToken("Attributes");
                            foreach (var attribute in attributes)
                            {
                                string attributeName  = (string)attribute.SelectToken("Name");
                                string attributeValue = (string)attribute.SelectToken("Value");
                                xsetting.SetAttributeValue(attributeName, attributeValue);
                            }

                            xtask.Add(xsetting);
                        }

                        xtasks.Add(xtask);
                    }

                    // root
                    var xwf = new XElement(xn + "Workflow"
                                           , new XAttribute("id", workflowId)
                                           , new XAttribute("name", workflowName)
                                           , new XAttribute("description", workflowDesc)
                                           , new XElement(xn + "Settings"
                                                          , new XElement(xn + "Setting"
                                                                         , new XAttribute("name", "launchType")
                                                                         , new XAttribute("value", workflowLaunchType.ToString().ToLower()))
                                                          , new XElement(xn + "Setting"
                                                                         , new XAttribute("name", "enabled")
                                                                         , new XAttribute("value", isWorkflowEnabled.ToString().ToLower()))
                                                          , new XElement(xn + "Setting"
                                                                         , new XAttribute("name", "period")
                                                                         , new XAttribute("value", workflowPeriod.ToString(@"dd\.hh\:mm\:ss")))
                                                          , new XElement(xn + "Setting"
                                                                         , new XAttribute("name", "cronExpression")
                                                                         , new XAttribute("value", cronExpression))
                                                          )
                                           , xtasks
                                           );

                    xdoc.Add(xwf);

                    var path = (string)wi.SelectToken("Path");
                    xdoc.Save(path);
                }
                else
                {
                    int id = int.Parse((string)o.SelectToken("Id"));
                    var wf = WexflowWindowsService.WexflowEngine.GetWorkflow(id);
                    if (wf != null)
                    {
                        var xdoc = wf.XDoc;

                        int        workflowId         = (int)wi.SelectToken("Id");
                        string     workflowName       = (string)wi.SelectToken("Name");
                        LaunchType workflowLaunchType = (LaunchType)((int)wi.SelectToken("LaunchType"));
                        string     p = (string)wi.SelectToken("Period");
                        TimeSpan   workflowPeriod = TimeSpan.Parse(string.IsNullOrEmpty(p) ? "00.00:00:00" : p);
                        string     cronExpression = (string)wi.SelectToken("CronExpression");

                        if (workflowLaunchType == LaunchType.Cron &&
                            !WexflowEngine.IsCronExpressionValid(cronExpression))
                        {
                            throw new Exception("The cron expression '" + cronExpression + "' is not valid.");
                        }

                        bool   isWorkflowEnabled = (bool)wi.SelectToken("IsEnabled");
                        string workflowDesc      = (string)wi.SelectToken("Description");

                        //if(xdoc.Root == null) throw new Exception("Root is null");
                        xdoc.Root.Attribute("id").Value          = workflowId.ToString();
                        xdoc.Root.Attribute("name").Value        = workflowName;
                        xdoc.Root.Attribute("description").Value = workflowDesc;

                        var xwfEnabled = xdoc.Root.XPathSelectElement("wf:Settings/wf:Setting[@name='enabled']",
                                                                      wf.XmlNamespaceManager);
                        xwfEnabled.Attribute("value").Value = isWorkflowEnabled.ToString().ToLower();
                        var xwfLaunchType = xdoc.Root.XPathSelectElement("wf:Settings/wf:Setting[@name='launchType']",
                                                                         wf.XmlNamespaceManager);
                        xwfLaunchType.Attribute("value").Value = workflowLaunchType.ToString().ToLower();

                        var xwfPeriod = xdoc.Root.XPathSelectElement("wf:Settings/wf:Setting[@name='period']",
                                                                     wf.XmlNamespaceManager);
                        //if (workflowLaunchType == LaunchType.Periodic)
                        //{
                        if (xwfPeriod != null)
                        {
                            xwfPeriod.Attribute("value").Value = workflowPeriod.ToString(@"dd\.hh\:mm\:ss");
                        }
                        else
                        {
                            xdoc.Root.XPathSelectElement("wf:Settings", wf.XmlNamespaceManager)
                            .Add(new XElement(wf.XNamespaceWf + "Setting", new XAttribute("name", "period"),
                                              new XAttribute("value", workflowPeriod.ToString())));
                        }
                        //}

                        var xwfCronExpression = xdoc.Root.XPathSelectElement(
                            "wf:Settings/wf:Setting[@name='cronExpression']",
                            wf.XmlNamespaceManager);

                        if (xwfCronExpression != null)
                        {
                            xwfCronExpression.Attribute("value").Value = cronExpression ?? string.Empty;
                        }
                        else if (!string.IsNullOrEmpty(cronExpression))
                        {
                            xdoc.Root.XPathSelectElement("wf:Settings", wf.XmlNamespaceManager)
                            .Add(new XElement(wf.XNamespaceWf + "Setting", new XAttribute("name", "cronExpression"),
                                              new XAttribute("value", cronExpression)));
                        }

                        var xtasks   = xdoc.Root.Element(wf.XNamespaceWf + "Tasks");
                        var alltasks = xtasks.Elements(wf.XNamespaceWf + "Task");
                        alltasks.Remove();

                        var tasks = o.SelectToken("Tasks");
                        foreach (var task in tasks)
                        {
                            int    taskId        = (int)task.SelectToken("Id");
                            string taskName      = (string)task.SelectToken("Name");
                            string taskDesc      = (string)task.SelectToken("Description");
                            bool   isTaskEnabled = (bool)task.SelectToken("IsEnabled");

                            var xtask = new XElement(wf.XNamespaceWf + "Task"
                                                     , new XAttribute("id", taskId)
                                                     , new XAttribute("name", taskName)
                                                     , new XAttribute("description", taskDesc)
                                                     , new XAttribute("enabled", isTaskEnabled.ToString().ToLower())
                                                     );

                            var settings = task.SelectToken("Settings");
                            foreach (var setting in settings)
                            {
                                string settingName  = (string)setting.SelectToken("Name");
                                string settingValue = (string)setting.SelectToken("Value");

                                var xsetting = new XElement(wf.XNamespaceWf + "Setting"
                                                            , new XAttribute("name", settingName)
                                                            );

                                if (settingName == "selectFiles" || settingName == "selectAttachments")
                                {
                                    if (!string.IsNullOrEmpty(settingValue))
                                    {
                                        xsetting.SetAttributeValue("value", settingValue);
                                    }
                                }
                                else
                                {
                                    xsetting.SetAttributeValue("value", settingValue);
                                }

                                var attributes = setting.SelectToken("Attributes");
                                foreach (var attribute in attributes)
                                {
                                    string attributeName  = (string)attribute.SelectToken("Name");
                                    string attributeValue = (string)attribute.SelectToken("Value");
                                    xsetting.SetAttributeValue(attributeName, attributeValue);
                                }

                                xtask.Add(xsetting);
                            }

                            xtasks.Add(xtask);
                        }

                        xdoc.Save(wf.WorkflowFilePath);
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }
Пример #2
0
        public bool IsCronExpressionValid(string expression)
        {
            var res = WexflowEngine.IsCronExpressionValid(expression);

            return(res);
        }