123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- namespace InABox.Core
- {
- public static class DatabaseSchema
- {
- // {className: {propertyName: property}}
- private static Dictionary<string, Dictionary<string, IProperty>> _properties
- = new Dictionary<string, Dictionary<string, 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<SubObject>? GetSubObjects(Type t)
- {
- CheckProperties(t);
- return _subObjects.GetValueOrDefault(t);
- }
- public static void InitializeSubObjects(BaseObject obj)
- {
- var objs = GetSubObjects(obj.GetType());
- if(objs is null)
- {
- return;
- }
- foreach (var subObjectDef in objs)
- {
- var subObj = (Activator.CreateInstance(subObjectDef.PropertyType) as ISubObject)!;
- subObjectDef.Setter(obj, subObj);
- subObj.SetLinkedParent(obj);
- subObj.SetLinkedPath(subObjectDef.Name);
- }
- }
- private static void RegisterSubObject(Type objectType, Type propertyType, string name)
- {
- lock (_updatelock)
- {
- if (!_subObjects.TryGetValue(objectType, out var properties))
- {
- properties = new List<SubObject>();
- _subObjects[objectType] = properties;
- }
- if (!properties.Any(x => x.Name == name && x.PropertyType == propertyType))
- {
- properties.Add(new SubObject(objectType, propertyType, name));
- }
- }
- }
- private static void RegisterProperties(Type master, Type type, string prefix, StandardProperty? parent)
- {
- try
- {
- var classname = master.EntityName();
- var properties = CoreUtils.PropertyList(
- type,
- x => !x.PropertyType.IsInterface &&
- x.GetGetMethod()?.IsPublic == true &&
- (x.DeclaringType.IsSubclassOf(typeof(BaseObject))
- || x.DeclaringType.IsSubclassOf(typeof(BaseEditor)))
- );
- 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);
- if (p == null)
- {
- var isstatic = prop.GetAccessors(true)[0].IsStatic;
- if (!isstatic)
- {
- BaseEditor? editor;
- if (parent != null && parent.HasEditor && parent.Editor is NullEditor)
- {
- editor = parent.Editor;
- }
- else
- {
- editor = prop.GetEditor();
- }
- var captionAttr = prop.GetCustomAttribute<Caption>();
- var subCaption = captionAttr != null ? captionAttr.Text : prop.Name;
- var path = captionAttr == null || captionAttr.IncludePath; // If no caption attribute, we should always include the path
- var caption = parent?.Caption ?? ""; // We default to the parent caption if subCaption doesn't exist
- if (!string.IsNullOrWhiteSpace(subCaption))
- {
- if (!string.IsNullOrWhiteSpace(caption) && path)
- {
- caption = $"{caption} {subCaption}";
- }
- else
- {
- caption = subCaption;
- }
- }
- // Once the parent page has been found, this property is cemented to that page - it cannot change page to its parent
- // The same goes for sequence
- var page = parent?.Page;
- var sequence = parent?.Sequence;
- if (string.IsNullOrWhiteSpace(page))
- {
- var sequenceAttribute = prop.GetCustomAttribute<EditorSequence>();
- if (sequenceAttribute != null)
- {
- page = sequenceAttribute.Page;
- sequence = sequenceAttribute.Sequence;
- }
- }
- editor = editor?.Clone() as BaseEditor;
- if (editor != null)
- {
- editor.Page = page;
- editor.Caption = caption;
- editor.EditorSequence = (int)(sequence ?? 999);
- editor.Security = prop.GetCustomAttributes<SecurityAttribute>().ToArray();
- }
- bool required = false;
- if (parent == null || parent.Required)
- {
- required = prop.GetCustomAttribute<RequiredColumnAttribute>() != null;
- }
- LoggablePropertyAttribute? loggable = null;
- if (parent == null || parent.Loggable != null)
- {
- loggable = prop.GetCustomAttribute<LoggablePropertyAttribute>();
- }
- var newProperty = new StandardProperty
- {
- _class = master,
- //Class = classname,
- Name = name,
- PropertyType = prop.PropertyType,
- Editor = editor ?? new NullEditor(),
- HasEditor = editor != null,
- Caption = caption,
- Sequence = sequence ?? 999,
- Page = page ?? "",
- Required = required,
- Loggable = loggable,
- Parent = parent,
- Property = prop
- };
- var parentWithEditable = newProperty.GetOuterParent(x =>
- x is StandardProperty st
- && st.Property.GetCustomAttribute<EditableAttribute>() != null);
- if(parentWithEditable != null)
- {
- var attr = (parentWithEditable as StandardProperty)!.Property.GetCustomAttribute<EditableAttribute>()!;
- newProperty.Editor.Editable = newProperty.Editor.Editable.Combine(attr.Editable);
- }
- else if(prop.GetCustomAttribute<EditableAttribute>() is EditableAttribute attr)
- {
- newProperty.Editor.Editable = newProperty.Editor.Editable.Combine(attr.Editable);
- }
- 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(type, prop.PropertyType, prop.Name);
- }
- if (isLink || isEnclosedEntity || isBaseEditor)
- {
- RegisterProperties(master, prop.PropertyType, name, newProperty);
- }
- else
- {
- RegisterProperty(newProperty);
- }
- }
- }
- }
- if (type.IsSubclassOf(typeof(BaseObject)))
- RegisterProperties(master, type.BaseType, prefix, parent);
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
- }
- }
- private static void RegisterProperties(Type type)
- {
- RegisterProperties(type, type, "", null);
- }
- public static object? DefaultValue(Type type)
- {
- if (type.IsValueType)
- return Activator.CreateInstance(type);
- if (type.Equals(typeof(string)))
- return "";
- return null;
- }
-
- private static readonly object _updatelock = new object();
-
- public static void RegisterProperty(IProperty entry)
- {
- lock (_updatelock)
- {
- if (!_properties.ContainsKey(entry.Class))
- _properties[entry.Class] = new Dictionary<string, IProperty>();
- _properties[entry.Class][entry.Name] = entry;
- }
- }
- public static void Load(CustomProperty[] customproperties)
- {
- foreach (var prop in customproperties)
- RegisterProperty(prop);
- }
- private static void CheckProperties(Type type)
- {
- var entityName = type.EntityName();
- try
- {
- var props = _properties.GetValueOrDefault(entityName);
- var hasprops = props?.Any(x => x.Value is StandardProperty) == true;
- if (type.IsSubclassOf(typeof(BaseObject)) && !hasprops)
- RegisterProperties(type);
- }
- catch (Exception e)
- {
- // This seems to be an intermittent error "Collection has been modified" when checking if the Dictionary has been populated already
- // I've added a .ToArray() to concretise the list, but who knows?
- Logger.Send(LogType.Error,"",$"Error Checking Properties for Type: {entityName}\n{e.Message}\n{e.StackTrace}");
- }
- }
- public static IEnumerable<IProperty> Properties(Type type)
- {
- CheckProperties(type);
- var entityName = type.EntityName();
- return _properties.GetValueOrDefault(entityName)?.Select(x => x.Value) ?? Array.Empty<IProperty>();
- }
-
- public static IProperty? Property(Type type, string name)
- {
- CheckProperties(type);
- var entityName = type.EntityName();
- var prop = _properties.GetValueOrDefault(entityName)?.GetValueOrDefault(name);
- // Walk up the inheritance tree, see if an ancestor has this property
- if (prop == null && type.BaseType != null)
- prop = Property(type.BaseType, name);
- return prop;
- }
- public static IProperty? Property<T>(Expression<Func<T, object?>> expression) => Property(typeof(T), CoreUtils.GetFullPropertyName(expression, "."));
- public static void InitializeObject<TObject>(TObject entity) where TObject : BaseObject
- {
- entity.UserProperties.Clear();
- var props = Properties(entity.GetType()).Where(x => x is CustomProperty).ToArray();
- foreach (var field in props) entity.UserProperties[field.Name] = DefaultValue(field.PropertyType);
- }
- }
- }
|