DatabaseSchema.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace InABox.Core
  7. {
  8. public static class DatabaseSchema
  9. {
  10. // {className: {propertyName: property}}
  11. private static Dictionary<string, Dictionary<string, IProperty>> _properties
  12. = new Dictionary<string, Dictionary<string, IProperty>>();
  13. private struct SubObject
  14. {
  15. public Type PropertyType { get; set; }
  16. public string Name { get; set; }
  17. public Action<object, object> Setter { get; set; }
  18. public SubObject(Type objectType, Type propertyType, string name)
  19. {
  20. PropertyType = propertyType;
  21. Name = name;
  22. Setter = Expressions.Setter(objectType, name);
  23. }
  24. }
  25. private static Dictionary<Type, List<SubObject>> _subObjects { get; } = new Dictionary<Type, List<SubObject>>();
  26. private static List<SubObject>? GetSubObjects(Type t)
  27. {
  28. CheckProperties(t);
  29. return _subObjects.GetValueOrDefault(t);
  30. }
  31. public static void InitializeSubObjects(BaseObject obj)
  32. {
  33. var objs = GetSubObjects(obj.GetType());
  34. if(objs is null)
  35. {
  36. return;
  37. }
  38. foreach (var subObjectDef in objs)
  39. {
  40. var subObj = (Activator.CreateInstance(subObjectDef.PropertyType) as ISubObject)!;
  41. subObjectDef.Setter(obj, subObj);
  42. subObj.SetLinkedParent(obj);
  43. subObj.SetLinkedPath(subObjectDef.Name);
  44. }
  45. }
  46. private static void RegisterSubObject(Type objectType, Type propertyType, string name)
  47. {
  48. lock (_updatelock)
  49. {
  50. if (!_subObjects.TryGetValue(objectType, out var properties))
  51. {
  52. properties = new List<SubObject>();
  53. _subObjects[objectType] = properties;
  54. }
  55. if (!properties.Any(x => x.Name == name && x.PropertyType == propertyType))
  56. {
  57. properties.Add(new SubObject(objectType, propertyType, name));
  58. }
  59. }
  60. }
  61. private static void RegisterProperties(Type master, Type type, string prefix, StandardProperty? parent)
  62. {
  63. try
  64. {
  65. var classname = master.EntityName();
  66. var properties = CoreUtils.PropertyList(
  67. type,
  68. x => !x.PropertyType.IsInterface &&
  69. x.GetGetMethod()?.IsPublic == true &&
  70. (x.DeclaringType.IsSubclassOf(typeof(BaseObject))
  71. || x.DeclaringType.IsSubclassOf(typeof(BaseEditor)))
  72. );
  73. var classProps = _properties.GetValueOrDefault(classname);
  74. foreach (var prop in properties)
  75. {
  76. var name = string.IsNullOrWhiteSpace(prefix) ? prop.Name : string.Format("{0}.{1}", prefix, prop.Name);
  77. var p = classProps?.GetValueOrDefault(name);
  78. if (p == null)
  79. {
  80. var isstatic = prop.GetAccessors(true)[0].IsStatic;
  81. if (!isstatic)
  82. {
  83. BaseEditor? editor;
  84. if (parent != null && parent.HasEditor && parent.Editor is NullEditor)
  85. {
  86. editor = parent.Editor;
  87. }
  88. else
  89. {
  90. editor = prop.GetEditor();
  91. }
  92. var captionAttr = prop.GetCustomAttribute<Caption>();
  93. var subCaption = captionAttr != null ? captionAttr.Text : prop.Name;
  94. var path = captionAttr == null || captionAttr.IncludePath; // If no caption attribute, we should always include the path
  95. var caption = parent?.Caption ?? ""; // We default to the parent caption if subCaption doesn't exist
  96. if (!string.IsNullOrWhiteSpace(subCaption))
  97. {
  98. if (!string.IsNullOrWhiteSpace(caption) && path)
  99. {
  100. caption = $"{caption} {subCaption}";
  101. }
  102. else
  103. {
  104. caption = subCaption;
  105. }
  106. }
  107. // Once the parent page has been found, this property is cemented to that page - it cannot change page to its parent
  108. // The same goes for sequence
  109. var page = parent?.Page;
  110. var sequence = parent?.Sequence;
  111. if (string.IsNullOrWhiteSpace(page))
  112. {
  113. var sequenceAttribute = prop.GetCustomAttribute<EditorSequence>();
  114. if (sequenceAttribute != null)
  115. {
  116. page = sequenceAttribute.Page;
  117. sequence = sequenceAttribute.Sequence;
  118. }
  119. }
  120. editor = editor?.Clone() as BaseEditor;
  121. if (editor != null)
  122. {
  123. editor.Page = page;
  124. editor.Caption = caption;
  125. editor.EditorSequence = (int)(sequence ?? 999);
  126. editor.Security = prop.GetCustomAttributes<SecurityAttribute>().ToArray();
  127. }
  128. bool required = false;
  129. if (parent == null || parent.Required)
  130. {
  131. required = prop.GetCustomAttribute<RequiredColumnAttribute>() != null;
  132. }
  133. LoggablePropertyAttribute? loggable = null;
  134. if (parent == null || parent.Loggable != null)
  135. {
  136. loggable = prop.GetCustomAttribute<LoggablePropertyAttribute>();
  137. }
  138. var newProperty = new StandardProperty
  139. {
  140. _class = master,
  141. //Class = classname,
  142. Name = name,
  143. PropertyType = prop.PropertyType,
  144. Editor = editor ?? new NullEditor(),
  145. HasEditor = editor != null,
  146. Caption = caption,
  147. Sequence = sequence ?? 999,
  148. Page = page ?? "",
  149. Required = required,
  150. Loggable = loggable,
  151. Parent = parent,
  152. Property = prop
  153. };
  154. var isLink = prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink));
  155. var isEnclosedEntity = prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity));
  156. var isBaseEditor = prop.PropertyType.Equals(typeof(BaseEditor)) ||
  157. prop.PropertyType.IsSubclassOf(typeof(BaseEditor));
  158. if ((isLink || isEnclosedEntity) && !isBaseEditor)
  159. {
  160. RegisterSubObject(type, prop.PropertyType, prop.Name);
  161. }
  162. if (isLink || isEnclosedEntity || isBaseEditor)
  163. {
  164. RegisterProperties(master, prop.PropertyType, name, newProperty);
  165. }
  166. else
  167. {
  168. RegisterProperty(newProperty);
  169. }
  170. }
  171. }
  172. }
  173. if (type.IsSubclassOf(typeof(BaseObject)))
  174. RegisterProperties(master, type.BaseType, prefix, parent);
  175. }
  176. catch (Exception e)
  177. {
  178. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  179. }
  180. }
  181. private static void RegisterProperties(Type type)
  182. {
  183. RegisterProperties(type, type, "", null);
  184. }
  185. public static object? DefaultValue(Type type)
  186. {
  187. if (type.IsValueType)
  188. return Activator.CreateInstance(type);
  189. if (type.Equals(typeof(string)))
  190. return "";
  191. return null;
  192. }
  193. private static readonly object _updatelock = new object();
  194. public static void RegisterProperty(IProperty entry)
  195. {
  196. lock (_updatelock)
  197. {
  198. if (!_properties.ContainsKey(entry.Class))
  199. _properties[entry.Class] = new Dictionary<string, IProperty>();
  200. _properties[entry.Class][entry.Name] = entry;
  201. }
  202. }
  203. public static void Load(CustomProperty[] customproperties)
  204. {
  205. foreach (var prop in customproperties)
  206. RegisterProperty(prop);
  207. }
  208. private static void CheckProperties(Type type)
  209. {
  210. var entityName = type.EntityName();
  211. var props = _properties.GetValueOrDefault(entityName);
  212. var hasprops = props?.Any(x => x.Value is StandardProperty) == true;
  213. if (type.IsSubclassOf(typeof(BaseObject)) && !hasprops)
  214. RegisterProperties(type);
  215. }
  216. public static IEnumerable<IProperty> Properties(Type type)
  217. {
  218. CheckProperties(type);
  219. var entityName = type.EntityName();
  220. return _properties.GetValueOrDefault(entityName)?.Select(x => x.Value) ?? Array.Empty<IProperty>();
  221. }
  222. public static IProperty? Property(Type type, string name)
  223. {
  224. CheckProperties(type);
  225. var entityName = type.EntityName();
  226. var prop = _properties.GetValueOrDefault(entityName)?.GetValueOrDefault(name);
  227. // Walk up the inheritance tree, see if an ancestor has this property
  228. if (prop == null && type.BaseType != null)
  229. prop = Property(type.BaseType, name);
  230. return prop;
  231. }
  232. public static void InitializeObject<TObject>(TObject entity) where TObject : BaseObject
  233. {
  234. entity.UserProperties.Clear();
  235. var props = Properties(entity.GetType()).Where(x => x is CustomProperty).ToArray();
  236. foreach (var field in props) entity.UserProperties[field.Name] = DefaultValue(field.PropertyType);
  237. }
  238. }
  239. }