瀏覽代碼

Fixed integer summing aggregates. Made issues column more generic

Kenric Nugteren 1 年之前
父節點
當前提交
b75bf0a749

+ 6 - 0
InABox.Core/Security/Security.cs

@@ -248,6 +248,12 @@ namespace InABox.Core
             return ClientFactory.IsSupported<TEntity>() && IsAllowed<AutoSecurityDescriptor<TEntity, CanDelete<TEntity>>>();
         }
 
+        public static bool CanManageIssues(Type TEntity)
+        {
+            return ClientFactory.IsSupported(TEntity)
+                && IsAllowed(typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanManageIssues<>).MakeGenericType(TEntity)));
+        }
+
         public static bool CanManageIssues<TEntity>() where TEntity : Entity, IIssues, new()
         {
             return ClientFactory.IsSupported<TEntity>() && IsAllowed<AutoSecurityDescriptor<TEntity, CanManageIssues<TEntity>>>();

+ 2 - 2
inabox.database.sqlite/SQLiteProvider.cs

@@ -1595,7 +1595,7 @@ namespace InABox.Database.SQLite
         {
             return attribute.Calculation switch
             {
-                AggregateCalculation.Sum => "TOTAL",
+                AggregateCalculation.Sum => "SUM",
                 AggregateCalculation.Count => "COUNT",
                 AggregateCalculation.Maximum => "MAX",
                 AggregateCalculation.Minimum => "MIN",
@@ -2432,7 +2432,7 @@ namespace InABox.Database.SQLite
                                                 ReadAndDecodeValue(result, reader, row, i);
                                             }
                                         }
-                                        catch (Exception)
+                                        catch (Exception e)
                                         {
                                             row.Values.Add(result.Columns[i].DataType.GetDefault());
                                         }

+ 4 - 0
inabox.wpf/DynamicGrid/Columns/DynamicImageColumn.cs

@@ -11,6 +11,10 @@ public class DynamicImageColumn : DynamicActionColumn
 
     public delegate BitmapImage? GetImageDelegate(CoreRow? row);
 
+    protected DynamicImageColumn()
+    {
+    }
+
     public DynamicImageColumn(GetImageDelegate image, ActionDelegate? action = null)
     {
         Image = image;

+ 112 - 86
inabox.wpf/DynamicGrid/Columns/DynamicIssuesColumn.cs

@@ -1,5 +1,7 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Linq;
+using System.Linq.Expressions;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Media.Imaging;
@@ -7,121 +9,145 @@ using InABox.Clients;
 using InABox.Core;
 using InABox.WPF;
 
-namespace InABox.DynamicGrid
+namespace InABox.DynamicGrid;
+
+public class DynamicIssuesColumn<TIssues> : DynamicImageColumn
+    where TIssues : Entity, IRemotable, IPersistent, IIssues, new()
 {
-    public class DynamicIssuesColumn<T> : DynamicImageColumn
-        where T : Entity, IRemotable, IPersistent, IIssues, new()
+    private static readonly BitmapImage _warning = Wpf.Resources.warning.AsBitmapImage();
+
+    private readonly IDynamicGrid _parent;
+
+    public string IssuesProperty;
+    public Func<CoreRow[], TIssues[]> LoadIssues;
+
+    public DynamicIssuesColumn(IDynamicGrid parent, string issuesProperty, Func<CoreRow[], TIssues[]> loadIssues)
     {
-        private static readonly BitmapImage _warning =Wpf.Resources.warning.AsBitmapImage();
+        Image = IssuesImage;
+        Filters = new[] { "Active Issues", "No Issues" };
+        FilterRecord = DoFilterIssues;
+        ContextMenu = CreateIssuesMenu;
+        ToolTip = CreateIssuesToolTip;
+        _parent = parent;
+        Position = DynamicActionColumnPosition.Start;
+
+        IssuesProperty = issuesProperty;
+        LoadIssues = loadIssues;
+    }
 
-        private readonly IDynamicGrid _parent;
+    public DynamicIssuesColumn(IDynamicGrid parent):
+        this(parent, CoreUtils.GetFullPropertyName<TIssues, string>(x => x.Issues, ""), (rows) => rows.ToObjects<TIssues>().ToArray())
+    {
+    }
 
-        public DynamicIssuesColumn(IDynamicGrid parent) : base(IssuesImage)
-        {
-            Filters = new[] { "Active Issues", "No Issues" };
-            FilterRecord = DoFilterIssues;
-            ContextMenu = CreateIssuesMenu;
-            ToolTip = CreateIssuesToolTip;
-            _parent = parent;
-            Position = DynamicActionColumnPosition.Start;
-        }
+    private string? GetIssues(CoreRow? row)
+    {
+        return row?.Get<string>(IssuesProperty);
+    }
 
-        private static BitmapImage? IssuesImage(CoreRow? row)
-        {
-            if (row == null || string.IsNullOrWhiteSpace(row.Get<T, string>(x => x.Issues)))
-                return null;
-            return _warning;
-        }
+    private BitmapImage? IssuesImage(CoreRow? row)
+    {
+        if (GetIssues(row).IsNullOrWhiteSpace())
+            return null;
+        return _warning;
+    }
+
+    private FrameworkElement? CreateIssuesToolTip(DynamicActionColumn column, CoreRow? row)
+    {
+        var text = GetIssues(row);
+        if (text.IsNullOrWhiteSpace())
+            text = "No Issues Found";
+        return TextToolTip(text);
+    }
+
+    private bool DoFilterIssues(CoreRow row, string[] filter)
+    {
+        var noissues = GetIssues(row).IsNullOrWhiteSpace();
+        if (filter.Contains("No Issues") && noissues)
+            return true;
+        if (filter.Contains("Active Issues") && !noissues)
+            return true;
+        return false;
+    }
 
-        private FrameworkElement? CreateIssuesToolTip(DynamicActionColumn column, CoreRow? row)
+    private MenuItem CreateMenu(string caption, RoutedEventHandler click)
+    {
+        var item = new MenuItem();
+        item.Header = caption;
+        item.Click += click;
+        return item;
+    }
+
+    private ContextMenu? CreateIssuesMenu(CoreRow[]? rows)
+    {
+        if (!Security.CanManageIssues<TIssues>())
+            return null;
+
+        var issues = rows?.Select(GetIssues).Where(x => !string.IsNullOrWhiteSpace(x)).Any();
+        var result = new ContextMenu();
+
+        if (issues != true)
         {
-            var text = row?.Get<T, string>(x => x.Issues);
-            if (string.IsNullOrWhiteSpace(text))
-                text = "No Issues Found";
-            return TextToolTip(text);
+            result.Items.Add(CreateMenu("Create Issue", (o, e) => EditIssues(rows)));
         }
-
-        private bool DoFilterIssues(CoreRow row, string[] filter)
+        else
         {
-            var noissues = string.IsNullOrWhiteSpace(row?.Get<T, string>(x => x.Issues));
-            if (filter.Contains("No Issues") && noissues)
-                return true;
-            if (filter.Contains("Active Issues") && !noissues)
-                return true;
-            return false;
+            result.Items.Add(CreateMenu("Update Issues", (o, e) => EditIssues(rows)));
+            result.Items.Add(CreateMenu("Clear Issues", (o, e) => ClearIssues(rows)));
         }
 
-        private MenuItem CreateMenu(string caption, RoutedEventHandler click)
+        return result;
+    }
+
+    private void ClearIssues(CoreRow[]? rows)
+    {
+        if (rows is null)
         {
-            var item = new MenuItem();
-            item.Header = caption;
-            item.Click += click;
-            return item;
+            return;
         }
-
-        private ContextMenu? CreateIssuesMenu(CoreRow[]? rows)
+        if (MessageBox.Show("This will clear the flagged issues for these items!\n\nAre you sure you wish to continue?", "Confirm",
+                MessageBoxButton.YesNo) == MessageBoxResult.Yes)
         {
-            if (!Security.CanManageIssues<T>())
-                return null;
-
-            var issues = rows?.Select(r => r.Get<T, string>(x => x.Issues)).Where(x => !string.IsNullOrWhiteSpace(x)).Any();
-            var result = new ContextMenu();
-
-            if (issues != true)
+            var _updates = LoadIssues(rows).ToArray();
+            foreach (var update in _updates)
             {
-                result.Items.Add(CreateMenu("Create Issue", (o, e) => EditIssues(rows)));
+                update.Issues = "";
             }
-            else
+            using (new WaitCursor())
             {
-                result.Items.Add(CreateMenu("Update Issues", (o, e) => EditIssues(rows)));
-                result.Items.Add(CreateMenu("Clear Issues", (o, e) => ClearIssues(rows)));
+                Client.Save(_updates, "Clearing Issues", (o, e) => { });
             }
 
-            return result;
+            // False here to prevent Refreshing and losing the selected row record
+            foreach (var row in rows)
+                _parent.UpdateRow(row, IssuesProperty, "");
         }
+    }
+
+    private void EditIssues(CoreRow[]? rows)
+    {
+        var objects = LoadIssues(rows ?? Array.Empty<CoreRow>());
 
-        private void ClearIssues(CoreRow[]? rows)
+        var map = new Dictionary<CoreRow, TIssues>();
+        if (rows is not null)
         {
-            if(rows is null)
+            var i = 0;
+            foreach (var row in rows)
             {
-                return;
-            }
-            if (MessageBox.Show("This will clear the flagged issues for these items!\n\nAre you sure you wish to continue?", "Confirm",
-                    MessageBoxButton.YesNo) == MessageBoxResult.Yes)
-            {
-                var _updates = rows.Select(x => x.ToObject<T>()).ToArray();
-                foreach (var update in _updates)
-                    update.Issues = "";
-                using (new WaitCursor())
-                {
-                    new Client<T>().Save(_updates, "Clearing Issues", (o, e) => { });
-                }
-
-                // False here to prevent Refreshing and losing the selected row record
-                foreach (var row in rows)
-                    _parent.UpdateRow<T, string>(row, x => x.Issues, "");
+                map[row] = objects[i];
+                ++i;
             }
         }
 
-        private void EditIssues(CoreRow[]? rows)
+        if (new DynamicIssuesEditor(map.Values.ToArray()).ShowDialog() == true)
         {
-            var map = new Dictionary<CoreRow, T>();
-            if(rows is not null)
+            using (new WaitCursor())
             {
-                foreach (var row in rows)
-                    map[row] = row.ToObject<T>();
+                Client.Save(map.Values, "Updating Issues", (o, e) => { });
             }
 
-            if (new DynamicIssuesEditor(map.Values.ToArray()).ShowDialog() == true)
-            {
-                using (new WaitCursor())
-                {
-                    new Client<T>().Save(map.Values, "Updating Issues", (o, e) => { });
-                }
-
-                foreach (var row in map.Keys)
-                    _parent.UpdateRow<T, string>(row, x => x.Issues, map[row].Issues);
-            }
+            foreach (var row in map.Keys)
+                _parent.UpdateRow(row, IssuesProperty, map[row].Issues);
         }
     }
 }