DatabaseSchema.cs 11 KB

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