Procházet zdrojové kódy

Added MissingSetting exception

Kenric Nugteren před 1 rokem
rodič
revize
09ad0ec0d3

+ 31 - 5
InABox.Core/Postable/PostExceptions.cs

@@ -1,20 +1,46 @@
 using System;
 using System.Collections.Generic;
+using System.Linq.Expressions;
 using System.Text;
 
 namespace InABox.Core.Postable
 {
-    public class EmptyPostException : Exception
+    public class PostException : Exception
+    {
+        public PostException() : base() { }
+
+        public PostException(string message) : base(message) { }
+    }
+
+    public class EmptyPostException : PostException
     {
         public EmptyPostException() { }
     }
 
-    public class PostFailedMessageException : Exception
+    public class PostFailedMessageException : PostException
     {
         public PostFailedMessageException(string message): base(message) { }
     }
 
-    public class MissingSettingsException : Exception
+    public class MissingSettingException : PostException
+    {
+        public Type SettingsType { get; }
+
+        public string Setting { get; set; }
+
+        public MissingSettingException(Type settingsType, string setting): base($"Missing setting '{setting}'")
+        {
+            SettingsType = settingsType;
+            Setting = setting;
+        }
+    }
+
+    public class MissingSettingException<T> : MissingSettingException
+    {
+        public MissingSettingException(Expression<Func<T, object?>> setting) : base(typeof(T), CoreUtils.GetFullPropertyName(setting, ".")) { }
+    }
+
+    public class MissingSettingsException : PostException
     {
         public Type PostableType { get; }
 
@@ -23,13 +49,13 @@ namespace InABox.Core.Postable
             PostableType = postableType;
         }
     }
-    public class RepostedException : Exception
+    public class RepostedException : PostException
     {
         public RepostedException() : base("Cannot process an item twice.")
         {
         }
     }
-    public class PostCancelledException : Exception
+    public class PostCancelledException : PostException
     {
         public PostCancelledException() : base("Processing cancelled")
         {

+ 4 - 0
InABox.Core/Postable/PosterEngine.cs

@@ -195,6 +195,10 @@ namespace InABox.Core
                 SetFailed(entities);
                 throw;
             }
+            catch (MissingSettingException)
+            {
+                throw;
+            }
             catch (PostFailedMessageException)
             {
                 throw;

+ 1 - 0
InABox.Poster.MYOB/IMYOBPoster.cs

@@ -1,4 +1,5 @@
 using InABox.Core;
+using InABox.Core.Postable;
 using System;
 using System.Collections.Generic;
 using System.Linq;

+ 0 - 1
InABox.Poster.MYOB/MYOBPosterEngine.cs

@@ -149,7 +149,6 @@ public abstract class MYOBPosterEngine<TPostable, TSettings> :
     {
         return Poster.BeforePost(model);
     }
-
     
     protected override IPostResult<TPostable> DoProcess(IDataModel<TPostable> model)
     {

+ 68 - 56
inabox.wpf/Grids/PostableSettingsGrid.cs

@@ -7,78 +7,90 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace InABox.Wpf
+namespace InABox.Wpf;
+
+public class PostableSettingsGrid : DynamicItemsListGrid<PostableSettings>
 {
-    public class PostableSettingsGrid : DynamicItemsListGrid<PostableSettings>
+    public PostableSettingsGrid()
+    {
+        OnCustomiseEditor += PostableSettingsGrid_OnCustomiseEditor;
+        OnEditorValueChanged += PostableSettingsGrid_OnEditorValueChanged;
+    }
+
+    private Dictionary<string, object?> PostableSettingsGrid_OnEditorValueChanged(object sender, string name, object value)
     {
-        public PostableSettingsGrid()
+        var editorForm = (IDynamicEditorForm)sender;
+        if (name == nameof(PostableSettings.PosterType))
         {
-            OnCustomiseEditor += PostableSettingsGrid_OnCustomiseEditor;
-            OnEditorValueChanged += PostableSettingsGrid_OnEditorValueChanged;
+            var editor = (editorForm.FindEditor(name) as LookupEditorControl)!;
+            (editor.EditorDefinition as ComboLookupEditor)!.Buttons![0].SetEnabled(!string.IsNullOrWhiteSpace(value as string));
+            (editor.EditorDefinition as ComboLookupEditor)!.Buttons![1].SetVisible(
+                CoreUtils.GetEntityOrNull(value as string)?.IsAssignableTo(typeof(IGlobalSettingsPoster)) == true);
         }
+        return new();
+    }
+
+    private void PostableSettingsGrid_OnCustomiseEditor(IDynamicEditorForm sender, PostableSettings[]? items, DynamicGridColumn column, BaseEditor editor)
+    {
+        var settings = items?.FirstOrDefault();
+        if (settings is null) return;
 
-        private Dictionary<string, object?> PostableSettingsGrid_OnEditorValueChanged(object sender, string name, object value)
+        if(column.ColumnName == nameof(PostableSettings.PosterType) && editor is ComboLookupEditor combo)
         {
-            var editorForm = (IDynamicEditorForm)sender;
-            if (name == nameof(PostableSettings.PosterType))
-            {
-                var editor = (editorForm.FindEditor(name) as LookupEditorControl)!;
-                (editor.EditorDefinition as ComboLookupEditor)!.Buttons![0].SetEnabled(!string.IsNullOrWhiteSpace(value as string));
-                (editor.EditorDefinition as ComboLookupEditor)!.Buttons![1].SetVisible(
-                    CoreUtils.GetEntityOrNull(value as string)?.IsAssignableTo(typeof(IGlobalSettingsPoster)) == true);
-            }
-            return new();
+            var settingsButton = new EditorButton(settings, "Settings", 60, ViewSettings, false);
+            settingsButton.SetEnabled(!string.IsNullOrWhiteSpace(settings.PosterType));
+
+            var globalSettingsButton = new EditorButton(settings, "Global Settings", 100, ViewGlobalSettings, false);
+            globalSettingsButton.SetVisible(CoreUtils.GetEntityOrNull(settings.PosterType)?.IsAssignableTo(typeof(IGlobalSettingsPoster)) == true);
+
+            combo.Buttons = [settingsButton, globalSettingsButton];
         }
+    }
 
-        private void PostableSettingsGrid_OnCustomiseEditor(IDynamicEditorForm sender, PostableSettings[]? items, DynamicGridColumn column, BaseEditor editor)
-        {
-            var settings = items?.FirstOrDefault();
-            if (settings is null) return;
+    private void ViewGlobalSettings(object editor, object? item)
+    {
+        if (item is not PostableSettings settings) return;
+
+        var posterType = CoreUtils.GetEntityOrNull(settings.PosterType);
+        var globalSettingsType = posterType?.GetInterfaceDefinition(typeof(IGlobalSettingsPoster<>))?.GenericTypeArguments[0];
+        if (globalSettingsType is null) return;
 
-            if(column.ColumnName == nameof(PostableSettings.PosterType) && editor is ComboLookupEditor combo)
-            {
-                var settingsButton = new EditorButton(settings, "Settings", 60, ViewSettings, false);
-                settingsButton.SetEnabled(!string.IsNullOrWhiteSpace(settings.PosterType));
+        ConfigureGlobalPosterSettings(globalSettingsType);
+    }
+
+    private void ViewSettings(object editor, object? item)
+    {
+        if (item is not PostableSettings settings) return;
 
-                var globalSettingsButton = new EditorButton(settings, "Global Settings", 100, ViewGlobalSettings, false);
-                globalSettingsButton.SetVisible(CoreUtils.GetEntityOrNull(settings.PosterType)?.IsAssignableTo(typeof(IGlobalSettingsPoster)) == true);
+        var entityType = CoreUtils.GetEntityOrNull(settings.PostableType);
+        if (entityType is null) return;
 
-                combo.Buttons = [settingsButton, globalSettingsButton];
-            }
-        }
+        var posterType = CoreUtils.GetEntityOrNull(settings.PosterType);
+        var settingsType = posterType?.GetInterfaceDefinition(typeof(IPoster<,>))!.GenericTypeArguments[1];
 
-        private void ViewGlobalSettings(object editor, object? item)
+        ConfigurePosterSettings(entityType, settingsType);
+    }
+
+    public static void ConfigureGlobalPosterSettings(Type globalSettingsType)
+    {
+        var globalPosterSettings = PosterUtils.LoadGlobalPosterSettings(globalSettingsType);
+        var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicItemsListGrid<>), globalSettingsType);
+        if(grid.EditItems(new object[] { globalPosterSettings }))
         {
-            if (item is not PostableSettings settings) return;
-
-            var posterType = CoreUtils.GetEntityOrNull(settings.PosterType);
-            var globalSettingsType = posterType?.GetInterfaceDefinition(typeof(IGlobalSettingsPoster<>))?.GenericTypeArguments[0];
-            if (globalSettingsType is null) return;
-
-            var globalPosterSettings = PosterUtils.LoadGlobalPosterSettings(globalSettingsType);
-            var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicItemsListGrid<>), globalSettingsType);
-            if(grid.EditItems(new object[] { globalPosterSettings }))
-            {
-                PosterUtils.SaveGlobalPosterSettings(globalSettingsType, globalPosterSettings);
-            }
+            PosterUtils.SaveGlobalPosterSettings(globalSettingsType, globalPosterSettings);
         }
+    }
 
-        private void ViewSettings(object editor, object? item)
+    public static void ConfigurePosterSettings(Type entityType, Type settingsType)
+    {
+        var posterSettings = PosterUtils.LoadPosterSettings(entityType, settingsType);
+        var grid = DynamicGridUtils.CreateDynamicGrid(typeof(PosterSettingsGrid<>), settingsType);
+        if(grid.EditItems(new object[] { posterSettings }))
         {
-            if (item is not PostableSettings settings) return;
-
-            var entityType = CoreUtils.GetEntityOrNull(settings.PostableType);
-            if (entityType is null) return;
-
-            var posterType = CoreUtils.GetEntityOrNull(settings.PosterType);
-            var settingsType = posterType?.GetInterfaceDefinition(typeof(IPoster<,>))!.GenericTypeArguments[1];
-
-            var posterSettings = PosterUtils.LoadPosterSettings(entityType, settingsType);
-            var grid = DynamicGridUtils.CreateDynamicGrid(typeof(PosterSettingsGrid<>), settingsType);
-            if(grid.EditItems(new object[] { posterSettings }))
-            {
-                PosterUtils.SavePosterSettings(entityType, settingsType, posterSettings);
-            }
+            PosterUtils.SavePosterSettings(entityType, settingsType, posterSettings);
         }
     }
+
+    public static void ConfigurePosterSettings<T>(Type settingsType)
+        where T : Entity, IPostable, IRemotable, IPersistent, new() => ConfigurePosterSettings(typeof(T), settingsType);
 }