public static void ImportChartScripts(string folderName, Options options = null)
        {
            var files = Directory.GetFiles(folderName, "*.xml").ToDictionary(Path.GetFileNameWithoutExtension);

            var charts = Database.Query<ChartScriptEntity>().ToDictionary(a => a.Name);

            options = options ?? new Options();

            using (OperationLogic.AllowSave<ChartScriptEntity>())
                Synchronizer.SynchronizeReplacing(new Replacements(), "scripts",
                    files,
                    charts,
                    (name, file) =>
                    {
                        var cs = new ChartScriptEntity();
                        cs.ImportXml(XDocument.Load(file), name, force: false);
                        cs.Save();

                        Console.WriteLine("{0} entity created.".FormatWith(name));
                    },
                    (name, script) =>
                    {
                        if (AskYesNoAll("Remove {0} entity?".FormatWith(name), ref options.RemoveOld))
                        {
                            try
                            {
                                script.Delete();
                                Console.WriteLine("{0} entity removed.".FormatWith(name));
                            }
                            catch (Exception e)
                            {
                                SafeConsole.WriteLineColor(ConsoleColor.Red, "Error removing {0} entity: {1}".FormatWith(name, e.Message));
                            }
                        }
                    },
                    (name, file, script) =>
                    {
                        var xDoc = XDocument.Load(file);
                        if (script.Icon != null)
                            script.Icon.Retrieve();
                        try
                        {
                            script.ImportXml(xDoc, name, false);
                        }
                        catch (FormatException f)
                        {
                            SafeConsole.WriteLineColor(ConsoleColor.Yellow, f.Message);
                            if (AskYesNoAll("Force {0}?".FormatWith(name), ref options.ForceAll))
                                script.ImportXml(xDoc, name, true);
                        }

                        if (script.HasChanges() && AskYesNoAll("Override {0} entity?".FormatWith(name), ref options.OverrideAll))
                        {
                            script.Save();
                            Console.WriteLine("{0} entity overriden.".FormatWith(name));
                        }
                    });
        }
예제 #2
0
        public static bool SynchronizeColumns(this ChartScriptEntity chartScript, IChartBase chart)
        {
            bool result = false;

            if (chartScript == null)
            {
                result = true;
                chart.Columns.Clear();
            }

            for (int i = 0; i < chartScript.Columns.Count; i++)
            {
                if (chart.Columns.Count <= i)
                {
                    chart.Columns.Add(new ChartColumnEmbedded());
                    result = true;
                }

                chart.Columns[i].parentChart  = chart;
                chart.Columns[i].ScriptColumn = chartScript.Columns[i];

                if (!result)
                {
                    result = chart.Columns[i].IntegrityCheck() != null;
                }
            }

            if (chart.Columns.Count > chartScript.Columns.Count)
            {
                chart.Columns.RemoveRange(chartScript.Columns.Count, chart.Columns.Count - chartScript.Columns.Count);
                result = true;
            }

            if (chart.Parameters.Modified != ModifiedState.Sealed)
            {
                if (chart.Parameters.Select(a => a.Name).OrderBy().SequenceEqual(chartScript.Parameters.Select(a => a.Name).OrderBy()))
                {
                    foreach (var cp in chart.Parameters)
                    {
                        var sp = chartScript.Parameters.FirstEx(a => a.Name == cp.Name);

                        cp.parentChart     = chart;
                        cp.ScriptParameter = sp;
                        //if (cp.PropertyCheck(() => cp.Value).HasText())
                        //    cp.Value = sp.DefaultValue(cp.GetToken());
                    }
                }
                else
                {
                    var byName = chart.Parameters.ToDictionary(a => a.Name);
                    chart.Parameters.Clear();
                    foreach (var sp in chartScript.Parameters)
                    {
                        var cp = byName.TryGetC(sp.Name);

                        if (cp != null)
                        {
                            cp.parentChart     = chart;
                            cp.ScriptParameter = sp;

                            //if (cp.PropertyCheck(() => cp.Value).HasText())
                            //    cp.Value = sp.DefaultValue(cp.GetToken());
                        }
                        else
                        {
                            cp = new ChartParameterEmbedded
                            {
                                Name            = sp.Name,
                                parentChart     = chart,
                                ScriptParameter = sp,
                            };

                            cp.Value = sp.DefaultValue(sp.GetToken(chart));
                        }

                        chart.Parameters.Add(cp);
                    }
                }
            }

            if (chartScript.GroupBy == GroupByChart.Always && chart.GroupResults == false)
            {
                chart.GroupResults = true;
                result             = true;
            }
            else if (chartScript.GroupBy == GroupByChart.Never && chart.GroupResults == true)
            {
                chart.GroupResults = false;
                result             = true;
            }

            return(result);
        }