소스 검색

Fixes to entitylink system

Kenric Nugteren 1 년 전
부모
커밋
cffa48059d
3개의 변경된 파일56개의 추가작업 그리고 54개의 파일을 삭제
  1. 45 31
      InABox.Core/DatabaseSchema/DatabaseSchema.cs
  2. 0 2
      InABox.Core/DigitalForms/Obsolete/QAQuestion.cs
  3. 11 21
      inabox.scripting/FileReader/ExcelFileReader.cs

+ 45 - 31
InABox.Core/DatabaseSchema/DatabaseSchema.cs

@@ -12,21 +12,43 @@ namespace InABox.Core
         private static Dictionary<string, Dictionary<string, IProperty>> _properties { get; }
             = new Dictionary<string, Dictionary<string, IProperty>>();
 
-        private static Dictionary<Type, List<IProperty>> _subObjects { get; } = new Dictionary<Type, List<IProperty>>();
+        private struct SubObject
+        {
+            public Type PropertyType { get; set; }
+
+            public string Name { get; set; }
+
+            public Action<object, object> Setter { get; set; }
+
+            public SubObject(Type objectType, Type propertyType, string name)
+            {
+                PropertyType = propertyType;
+                Name = name;
+                Setter = Expressions.Setter(objectType, name);
+            }
+        }
+
+        private static Dictionary<Type, List<SubObject>> _subObjects { get; } = new Dictionary<Type, List<SubObject>>();
 
-        private static List<IProperty> GetSubObjects(Type t)
+        private static List<SubObject>? GetSubObjects(Type t)
         {
-            return _subObjects.GetValueOrDefault(t) ?? new List<IProperty>();
+            CheckProperties(t);
+            return _subObjects.GetValueOrDefault(t);
         }
 
         public static void InitializeSubObjects(BaseObject obj)
         {
-            foreach(var linkProp in GetSubObjects(obj.GetType()))
+            var objs = GetSubObjects(obj.GetType());
+            if(objs is null)
+            {
+                return;
+            }
+            foreach (var subObjectDef in objs)
             {
-                var subObj = (Activator.CreateInstance(linkProp.PropertyType) as ISubObject)!;
-                linkProp.Setter()(subObj, subObj);
+                var subObj = (Activator.CreateInstance(subObjectDef.PropertyType) as ISubObject)!;
+                subObjectDef.Setter(obj, subObj);
                 subObj.SetLinkedParent(obj);
-                subObj.SetLinkedPath(linkProp.Name);
+                subObj.SetLinkedPath(subObjectDef.Name);
             }
         }
 
@@ -78,19 +100,18 @@ namespace InABox.Core
             return editor;
         }*/
 
-        private static void RegisterSubObject(IProperty property)
+        private static void RegisterSubObject(Type objectType, Type propertyType, string name)
         {
             lock (_updatelock)
             {
-                var type = property.Parent?.PropertyType ?? property.ClassType;
-                if(type != null)
+                if (!_subObjects.TryGetValue(objectType, out var properties))
                 {
-                    if (!_subObjects.TryGetValue(type, out var properties))
-                    {
-                        properties = new List<IProperty>();
-                        _subObjects[type] = properties;
-                    }
-                    properties.Add(property);
+                    properties = new List<SubObject>();
+                    _subObjects[objectType] = properties;
+                }
+                if (!properties.Any(x => x.Name == name && x.PropertyType == propertyType))
+                {
+                    properties.Add(new SubObject(objectType, propertyType, name));
                 }
             }
         }
@@ -194,20 +215,18 @@ namespace InABox.Core
                                 Parent = parent,
                                 Property = prop
                             };
-                            if (newProperty.IsEntityLink || newProperty.IsEnclosedEntity)
+
+                            var isLink = prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink));
+                            var isEnclosedEntity = prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity));
+                            var isBaseEditor = prop.PropertyType.Equals(typeof(BaseEditor)) ||
+                                prop.PropertyType.IsSubclassOf(typeof(BaseEditor));
+                            if ((isLink || isEnclosedEntity) && !isBaseEditor)
                             {
-                                RegisterSubObject(newProperty);
+                                RegisterSubObject(type, prop.PropertyType, prop.Name);
                             }
 
-                            if (prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)) ||
-                                prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)) ||
-                                prop.PropertyType.Equals(typeof(BaseEditor)) ||
-                                prop.PropertyType.IsSubclassOf(typeof(BaseEditor)))
+                            if (isLink || isEnclosedEntity || isBaseEditor)
                             {
-                                if (prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)))
-                                {
-                                    //RegisterProperty(newProperty);
-                                }
                                 RegisterProperties(master, prop.PropertyType, name, newProperty);
                             }
                             else
@@ -255,11 +274,6 @@ namespace InABox.Core
                 //var dict = _Properties.GetOrAdd(entry.Class, className => new Dictionary<string, IProperty>());
                 //dict.TryAdd(entry.Name, entry);
             }
-
-            if (entry.IsEntityLink)
-            {
-                RegisterSubObject(entry);
-            }
         }
 
         public static void Load(CustomProperty[] customproperties)

+ 0 - 2
InABox.Core/DigitalForms/Obsolete/QAQuestion.cs

@@ -11,8 +11,6 @@ namespace InABox.Core
         public QAQuestion()
         {
             Answer = QAAnswer.Comment;
-            QAForm = new QAFormLink();
-            Form = new DigitalFormLink();
         }
 
         [NullEditor]

+ 11 - 21
inabox.scripting/FileReader/ExcelFileReader.cs

@@ -9,6 +9,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using static NPOI.HSSF.UserModel.HeaderFooter;
+using System.Diagnostics.CodeAnalysis;
 
 namespace InABox.Scripting
 {
@@ -18,9 +19,10 @@ namespace InABox.Scripting
 
         public Dictionary<string, int> Columns { get; set; }
 
+        [MemberNotNullWhen(false, nameof(_row))]
         public bool EndOfData { get; private set; }
 
-        private IRow _row;
+        private IRow? _row;
 
         public ExcelFileReader(Stream stream)
         {
@@ -87,27 +89,15 @@ namespace InABox.Scripting
                     }
                     else
                     {
-                        switch(cell.GetCellType())
+                        result = cell.GetCellType() switch
                         {
-                            case CellType.Formula:
-                                result = cell.GetValue();
-                                break;
-                            case CellType.Numeric:
-                                result = cell.GetDoubleValue();
-                                break;
-                            case CellType.Date:
-                                result = cell.GetDateTimeValue();
-                                break;
-                            case CellType.String:
-                                result = cell.GetValue();
-                                break;
-                            case CellType.Boolean:
-                                result = cell.GetBoolValue();
-                                break;
-                            default:
-                                result = null;
-                                break;
-                        }
+                            CellType.Formula => cell.GetValue(),
+                            CellType.Numeric => cell.GetDoubleValue(),
+                            CellType.Date => cell.GetDateTimeValue(),
+                            CellType.String => cell.GetValue(),
+                            CellType.Boolean => cell.GetBoolValue(),
+                            _ => null,
+                        };
                     }
                     return result;
                 });