Browse Source

Setout customisation script

Kenric Nugteren 1 year ago
parent
commit
5427332be2

+ 1 - 1
prs.desktop/Panels/Staging/StagingPanel.xaml

@@ -23,7 +23,7 @@
                     <RowDefinition Height="2*"/>
                     <RowDefinition Height="0"/>
                 </Grid.RowDefinitions>
-                <local:StagingSetoutGrid Grid.Row="0" x:Name="stagingSetoutGrid"/>
+                <local:StagingSetoutGrid Grid.Row="0" x:Name="stagingSetoutGrid" OnCustomiseSetouts="stagingSetoutGrid_OnCustomiseSetouts"/>
                 <local:StagingMaterialListGrid Grid.Row="1" x:Name="stagingMaterialListGrid"/>
             </Grid>
         </dynamicgrid:DynamicSplitPanel.Master>

+ 107 - 114
prs.desktop/Panels/Staging/StagingPanel.xaml.cs

@@ -12,9 +12,14 @@ using System.Diagnostics;
 using System.IO;
 using InABox.WPF;
 using System.ComponentModel;
+using InABox.Scripting;
+using System.Reflection;
+using System.Collections.Immutable;
+using com.sun.istack.@internal.localization;
 
 namespace PRSDesktop
 {
+    [Caption("Staging Panel Settings")]
     public class StagingPanellSettings : BaseObject, IGlobalConfigurationSettings
     {
         [Caption("PDF Markup Program Pathway", IncludePath = false)]
@@ -24,10 +29,40 @@ namespace PRSDesktop
         [FolderEditor(Environment.SpecialFolder.CommonDocuments)]
         public string SetoutsFolder { get; set; }
 
+        [ScriptEditor]
+        public string? Script { get; set; }
+
         public StagingPanellSettings()
         {
             MarkupPathway = "";
             SetoutsFolder = "";
+            Script = null;
+        }
+
+        public string DefaultScript()
+        {
+            return @"
+using PRSDesktop;
+using InABox.Core;
+using System.Collections.Generic;
+
+public class Module
+{
+    /*public void CustomiseSetouts(CustomiseSetoutsArgs args)
+    {
+        // Perform customisation on the setouts when they are added to the 'Staged Documents' grid.
+    }*/
+}";
+        }
+    }
+
+    public class CustomiseSetoutsArgs
+    {
+        public IReadOnlyList<Tuple<StagingSetout, Document>> Setouts;
+
+        public CustomiseSetoutsArgs(IReadOnlyList<Tuple<StagingSetout, Document>> setouts)
+        {
+            Setouts = setouts;
         }
     }
 
@@ -48,6 +83,47 @@ namespace PRSDesktop
         /// </summary>
         private List<StagingSetout> selectedSetouts = new();
 
+        private MethodInfo? _customiseSetoutsMethod;
+        private MethodInfo? CustomiseSetoutsMethod
+        {
+            get
+            {
+                EnsureScript();
+                return _customiseSetoutsMethod;
+            }
+        }
+        private object? _scriptObject;
+        private object? ScriptObject
+        {
+            get
+            {
+                EnsureScript();
+                return _scriptObject;
+            }
+        }
+        private ScriptDocument? _script;
+        private ScriptDocument? Script
+        {
+            get
+            {
+                EnsureScript();
+                return _script;
+            }
+        }
+        private void EnsureScript()
+        {
+            if (_script is null && !_settings.Script.IsNullOrWhiteSpace())
+            {
+                _script = new ScriptDocument(_settings.Script);
+                if (!_script.Compile())
+                {
+                    throw new Exception("Script in Staging Panel Settings failed to compile!");
+                }
+                _scriptObject = _script?.GetObject();
+                _customiseSetoutsMethod = _script?.GetMethod(methodName: "CustomiseSetouts");
+            }
+        }
+
         public StagingPanel()
         {
             InitializeComponent();
@@ -206,118 +282,6 @@ namespace PRSDesktop
             new Client<SetoutDocument>().Save(setoutdoc, "Added from staging screen");
         }
 
-        /*private void CreatePackets(Setout setout)
-        {
-            List<ManufacturingPacket> packets = new List<ManufacturingPacket>();
-            List<StagingManufacturingPacket> stagingPackets = new List<StagingManufacturingPacket>();
-
-            //create new manufacturing packets from the staging packets
-            foreach (var row in manufacturingControl.Data.Rows)
-            {
-                var staging = row.ToObject<StagingManufacturingPacket>();
-                if (staging.ManufacturingPacket.ID != Guid.Empty)
-                    continue;
-
-                var packet = new ManufacturingPacket();
-                packet.SetoutLink.ID = setout.ID;
-                packet.Serial = staging.Serial;
-                packet.ITP.ID = staging.ITP.ID;
-                packet.JobLink.ID = staging.Job.ID;
-                packet.ManufacturingTemplateLink.ID = staging.Template.ID;
-                packet.Title = staging.Title;
-                packet.Quantity = staging.Quantity;
-                packet.BarcodeQty = staging.BarcodeQuantity != 0 ? staging.BarcodeQuantity : packet.Quantity;
-                packet.WaterMark = staging.Watermark.ToString();
-                packet.Location = staging.Location;
-
-                packets.Add(packet);
-                stagingPackets.Add(staging);
-            }
-
-            //save the newly created packets to provide an ID
-            new Client<ManufacturingPacket>().Save(packets, "Created from Design Management Panel");
-
-            //now work on their stages with the provided packet.ID
-            List<ManufacturingPacketStage> stages = new List<ManufacturingPacketStage>();
-
-            Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent> stagingToActualComponents = new Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent>();
-
-            //Handled in this loop:
-            // - staging packets (update packet.ID)
-            // - creation of template stages
-            // - creation of components from staging components
-            foreach (var stagingPacket in stagingPackets)
-            {
-                var packet = packets.FirstOrDefault(x => x.Serial == stagingPacket.Serial);
-                stagingPacket.ManufacturingPacket.ID = packet.ID;
-
-                stages.AddRange(CreateStagesForTemplate(packet.ManufacturingTemplateLink.ID, packet.ID));
-
-                CreateComponents(stagingPacket.ID, packet.ID, stagingToActualComponents);
-            }
-
-            //save everything
-            MultiSave save = new MultiSave();
-            save.Add(typeof(ManufacturingPacketStage), stages.ToArray());
-            save.Add(typeof(StagingManufacturingPacket), stagingPackets.ToArray());
-            save.Add(typeof(ManufacturingPacketComponent), stagingToActualComponents.Values.ToArray());
-            save.Save(null, "Updated from setout staging screen");
-
-            foreach (var pair in stagingToActualComponents)
-            {
-                var stagingComponent = pair.Key;
-                stagingComponent.ComponentID = pair.Value.ID;
-            }
-
-            new Client<StagingManufacturingPacketComponent>().Save(stagingToActualComponents.Keys.ToArray(), "Updated from setout staging screen");
-        }*/
-
-        /*private void CreateComponents(Guid stagingPacketID, Guid packetID, Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent> stagingToActualComponents)
-        {
-            var components = new List<ManufacturingPacketComponent>();
-            CoreTable table = new Client<StagingManufacturingPacketComponent>().Query(
-                new Filter<StagingManufacturingPacketComponent>(x => x.StagingPacket.ID).IsEqualTo(stagingPacketID)
-                .And(x => x.ComponentID).IsEqualTo(Guid.Empty),
-                new Columns<StagingManufacturingPacketComponent>(
-                    x => x.Product.ID,
-                    x => x.Quantity,
-                    x => x.Length,
-                    x => x.Height,
-                    x => x.Width
-                ));
-            foreach (var row in table.Rows)
-            {
-                var stagingComponent = row.ToObject<StagingManufacturingPacketComponent>();
-                var component = stagingComponent.CreateComponent(packetID);
-                components.Add(component);
-
-                stagingToActualComponents.Add(stagingComponent, component);
-            }
-        }*/
-
-        /*private List<ManufacturingPacketStage> CreateStagesForTemplate(Guid templateID, Guid packetID)
-        {
-            var stages = new List<ManufacturingPacketStage>();
-            CoreTable table = new Client<ManufacturingTemplateStage>().Query(
-            new Filter<ManufacturingTemplateStage>(x => x.Template.ID).IsEqualTo(templateID),
-                new Columns<ManufacturingTemplateStage>
-                (
-                    x => x.Time,
-                    x => x.Sequence,
-                    x => x.SequenceType,
-                    x => x.Section.ID,
-                    x => x.Section.Name
-                    ));
-            foreach (var row in table.Rows)
-            {
-                var templateStage = row.ToObject<ManufacturingTemplateStage>();
-                var packetStage = templateStage.CreateManufacturingPacketStage();
-                packetStage.ManufacturingPacketLink.ID = packetID;
-                stages.Add(packetStage);
-            }
-            return stages;
-        }*/
-
         private void DocumentPreviewer_OnRejected(IEntityDocument? stagingsetoutdocument)
         {
             if(selectedSetout is null || stagingsetoutdocument is null)
@@ -440,19 +404,37 @@ namespace PRSDesktop
                 CoreUtils.SetPropertyValue(_settings, name, value);
                 return new Dictionary<string, object?>();
             };
-            propertyEditor.OnFormCustomiseEditor += PropertyEditor_OnFormCustomiseEditor;
+            propertyEditor.OnFormCustomiseEditor += Settings_OnFormCustomiseEditor;
 
             propertyEditor.Items = new BaseObject[] { _settings };
 
             if (propertyEditor.ShowDialog() == true)
             {
                 new GlobalConfiguration<StagingPanellSettings>().Save(_settings);
+                _script = null;
             }
         }
 
-        private void PropertyEditor_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
+        private void Settings_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
         {
+            if (items?.FirstOrDefault() is not StagingPanellSettings settings) return;
 
+            if (column.ColumnName == nameof(StagingPanellSettings.Script) && editor is ScriptEditor scriptEditor)
+            {
+                scriptEditor.Type = ScriptEditorType.TemplateEditor;
+                scriptEditor.OnEditorClicked += () =>
+                {
+                    var script = settings.Script.NotWhiteSpaceOr()
+                        ?? settings.DefaultScript();
+
+                    var editor = new ScriptEditorWindow(script, SyntaxLanguage.CSharp);
+                    if (editor.ShowDialog() == true)
+                    {
+                        sender.SetEditorValue(column.ColumnName, editor.Script);
+                        settings.Script = editor.Script;
+                    }
+                };
+            }
         }
 
         public void Heartbeat(TimeSpan time)
@@ -582,5 +564,16 @@ namespace PRSDesktop
                 CollapsePacketsButton.Content = "Collapse";
             }
         }
+
+        private void stagingSetoutGrid_OnCustomiseSetouts(IReadOnlyList<StagingSetoutGrid.SetoutDocument> setouts)
+        {
+            if(CustomiseSetoutsMethod != null && ScriptObject != null)
+            {
+                CustomiseSetoutsMethod?.Invoke(ScriptObject, new object?[]
+                {
+                    new CustomiseSetoutsArgs(setouts.Select(x => new Tuple<StagingSetout, Document>(x.Setout, x.Document)).ToImmutableList())
+                });
+            }
+        }
     }
 }