Browse Source

Editor sequence improvements for the Import Profile list

Kenric Nugteren 1 năm trước cách đây
mục cha
commit
e39b536725

+ 37 - 42
InABox.Core/CoreUtils.cs

@@ -16,6 +16,7 @@ using System.Text.RegularExpressions;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
 using System.Runtime.CompilerServices;
+using System.Data.Common;
 
 //using Serialize.Linq.Serializers;
 
@@ -1691,55 +1692,49 @@ namespace InABox.Core
             return sanitisedNamePart;
         }
 
-        private static List<Tuple<Type, String, decimal>> _columnsequences = new List<Tuple<Type, string, decimal>>();
+        private static Dictionary<(Type type, string column), decimal> _columnsequences = new Dictionary<(Type, string), decimal>();
 
-        public static decimal GetPropertySequence(Type type, string column)
+        private static decimal CalculatePropertySequence(IProperty? property)
         {
-            var tuple = _columnsequences.FirstOrDefault(x => Type.Equals(x.Item1, type) && String.Equals(x.Item2, column));
-            if (tuple == null)
+            var sequence = 0.0M;
+            if(property is null)
             {
-                decimal result = 0.0M;
-                var customprop = DatabaseSchema.Property(type, column);
-                if (customprop != null && customprop is CustomProperty)
-                {
-                    result = customprop.Sequence;
-                }
-                else
+                sequence = 999;
+            }
+            else if (property is CustomProperty)
+            {
+                sequence = property.Sequence;
+            }
+            else
+            {
+                while(property != null)
                 {
-                    var bits = column.Split('.');
-                    for (var i = 0; i < bits.Length; i++)
-                    {
-                        var sProp = string.Join(".", bits.Take(bits.Length - i));
-                        PropertyInfo? prop = null;
-                        try
-                        {
-                            prop = GetProperty(type, sProp);
-                        }
-                        catch (Exception e)
-                        {
-                            Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
-                        }
+                    sequence = property.Sequence + sequence / 1000.0M;
 
-                        if (prop != null)
-                        {
-                            result = prop.GetSequence() + (result / 1000.0M);
-                        }
-                        else
-                        {
-                            var cprop = DatabaseSchema.Property(type, sProp);
-                            if (cprop != null)
-                                result = cprop.Sequence;
-                            else
-                                result = result / 1000.0M;
-                        }
-                    }
-                    //result = double.MinValue + result;
+                    property = property.Parent;
                 }
-                _columnsequences.Add(new Tuple<Type, string, decimal>(type, column, result));
-                return result;
             }
-            else
-                return tuple.Item3;
+            return sequence;
+        }
+
+        public static decimal GetPropertySequence(IProperty property)
+        {
+            if (!_columnsequences.TryGetValue((property.ClassType!, property.Name), out var sequence))
+            {
+                sequence = CalculatePropertySequence(property);
+                _columnsequences.Add((property.ClassType!, property.Name), sequence);
+            }
+            return sequence;
+        }
+
+        public static decimal GetPropertySequence(Type type, string column)
+        {
+            if(!_columnsequences.TryGetValue((type, column), out var sequence))
+            {
+                sequence = CalculatePropertySequence(DatabaseSchema.Property(type, column));
+                _columnsequences.Add((type, column), sequence);
+            }
+            return sequence;
         }
 
         /// <summary>

+ 2 - 56
InABox.Core/DatabaseSchema/DatabaseSchema.cs

@@ -9,7 +9,7 @@ namespace InABox.Core
     public static class DatabaseSchema
     {
         // {className: {propertyName: property}}
-        private static Dictionary<string, Dictionary<string, IProperty>> _properties { get; }
+        private static Dictionary<string, Dictionary<string, IProperty>> _properties
             = new Dictionary<string, Dictionary<string, IProperty>>();
 
         private struct SubObject
@@ -52,54 +52,6 @@ namespace InABox.Core
             }
         }
 
-        /*private static BaseEditor GetPropertyEditor(Type type, PropertyInfo property, string propertyName)
-        {
-            BaseEditor editor = new NullEditor();
-
-            var parts = propertyName.Split('.');
-            var caption = "";
-            var page = "";
-            int? sequence = null;
-
-            for (var i = 0; i < parts.Length; i++)
-            {
-                var column = string.Join(".", parts.Take(i + 1));
-                var prop = CoreUtils.GetProperty(type, column);
-                if (column.Equals(propertyName))
-                {
-                    editor = property.GetEditor() ?? new NullEditor();
-                }
-                else
-                {
-                    var pedit = prop.GetEditor();
-                    if (pedit is NullEditor) return pedit;
-                }
-
-                editor = editor == null ? new NullEditor() : (editor.Clone() as BaseEditor)!;
-
-                var capattr = prop.GetCustomAttribute<Caption>();
-                var subcap = capattr != null ? capattr.Text : parts[i];
-                var path = capattr == null || capattr.IncludePath;
-                if (!string.IsNullOrWhiteSpace(subcap))
-                    caption = string.IsNullOrWhiteSpace(caption) || path == false ? subcap : string.Format("{0} {1}", caption, subcap);
-
-                if (string.IsNullOrWhiteSpace(page))
-                {
-                    var pageattr = prop.GetCustomAttribute<EditorSequence>();
-                    if (pageattr != null)
-                    {
-                        page = pageattr.Page;
-                        sequence = pageattr.Sequence;
-                    }
-                }
-            }
-
-            editor.Caption = caption;
-            editor.Page = page;
-            editor.EditorSequence = sequence ?? 999;
-            return editor;
-        }*/
-
         private static void RegisterSubObject(Type objectType, Type propertyType, string name)
         {
             lock (_updatelock)
@@ -127,14 +79,13 @@ namespace InABox.Core
                         x.GetGetMethod()?.IsPublic == true &&
                         (x.DeclaringType.IsSubclassOf(typeof(BaseObject)) 
                         || x.DeclaringType.IsSubclassOf(typeof(BaseEditor)))
-                ); //.OrderBy(x=>x.Name);
+                );
                 var classProps = _properties.GetValueOrDefault(classname);
 
                 foreach (var prop in properties)
                 {
                     var name = string.IsNullOrWhiteSpace(prefix) ? prop.Name : string.Format("{0}.{1}", prefix, prop.Name);
                     var p = classProps?.GetValueOrDefault(name);
-                        //.ToArray().FirstOrDefault(x => x.Class == classname && x.Name == name);
                     if (p == null)
                     {
                         var isstatic = prop.GetAccessors(true)[0].IsStatic;
@@ -240,7 +191,6 @@ namespace InABox.Core
                 }
 
                 if (type.IsSubclassOf(typeof(BaseObject)))
-                    //if ((type.BaseType != null) && (type.BaseType != typeof(BaseObject)))
                     RegisterProperties(master, type.BaseType, prefix, parent);
             }
             catch (Exception e)
@@ -272,9 +222,6 @@ namespace InABox.Core
                 if (!_properties.ContainsKey(entry.Class))
                     _properties[entry.Class] = new Dictionary<string, IProperty>();
                 _properties[entry.Class][entry.Name] = entry;
-
-                //var dict = _Properties.GetOrAdd(entry.Class, className => new Dictionary<string, IProperty>());
-                //dict.TryAdd(entry.Name, entry);
             }
         }
 
@@ -305,7 +252,6 @@ namespace InABox.Core
             CheckProperties(type);
             var entityName = type.EntityName();
 
-            //IProperty prop = _Properties.ToArray().FirstOrDefault(x => (x.Class.Equals(type.EntityName())) && (x.Name.Equals(name)));
             var prop = _properties.GetValueOrDefault(entityName)?.GetValueOrDefault(name);
 
             // Walk up the inheritance tree, see if an ancestor has this property

+ 1 - 1
InABox.Core/Imports/ImportFactory.cs

@@ -56,7 +56,7 @@ namespace InABox.Core
         {
             var results = new List<ImportMapping>();
             
-            var props = DatabaseSchema.Properties(type).OrderBy(x=>x.Sequence);
+            var props = DatabaseSchema.Properties(type).OrderBy(x => CoreUtils.GetPropertySequence(x));
             
             //var keys = CoreUtils.PropertyList(type, x => true, true).Keys.ToArray();
             foreach (var prop in props)