DatabaseSchema.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. RegisterProperties(master, prop.PropertyType, name, newProperty);
  152. }
  153. else
  154. {
  155. RegisterProperty(newProperty);
  156. }
  157. }
  158. }
  159. }
  160. if (type.IsSubclassOf(typeof(BaseObject)))
  161. //if ((type.BaseType != null) && (type.BaseType != typeof(BaseObject)))
  162. RegisterProperties(master, type.BaseType, prefix, parent);
  163. }
  164. catch (Exception e)
  165. {
  166. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  167. }
  168. }
  169. private static void RegisterProperties(Type type)
  170. {
  171. RegisterProperties(type, type, "", null);
  172. }
  173. public static object? DefaultValue(Type type)
  174. {
  175. if (type.IsValueType)
  176. return Activator.CreateInstance(type);
  177. if (type.Equals(typeof(string)))
  178. return "";
  179. return null;
  180. }
  181. private static readonly object _updatelock = new object();
  182. public static void RegisterProperty(IProperty entry)
  183. {
  184. lock (_updatelock)
  185. {
  186. if (!_Properties.ContainsKey(entry.Class))
  187. _Properties[entry.Class] = new Dictionary<string, IProperty>();
  188. _Properties[entry.Class][entry.Name] = entry;
  189. //var dict = _Properties.GetOrAdd(entry.Class, className => new Dictionary<string, IProperty>());
  190. //dict.TryAdd(entry.Name, entry);
  191. }
  192. }
  193. public static void Load(CustomProperty[] customproperties)
  194. {
  195. foreach (var prop in customproperties)
  196. RegisterProperty(prop);
  197. }
  198. private static void CheckProperties(Type type)
  199. {
  200. var entityName = type.EntityName();
  201. if (type.IsSubclassOf(typeof(BaseObject)) && _Properties.GetValueOrDefault(entityName)?.Any(x => x.Value is StandardProperty) != true)
  202. RegisterProperties(type);
  203. }
  204. public static IEnumerable<IProperty> Properties(Type type)
  205. {
  206. CheckProperties(type);
  207. var entityName = type.EntityName();
  208. return _Properties.GetValueOrDefault(entityName)?.Select(x => x.Value) ?? Array.Empty<IProperty>();
  209. }
  210. public static IProperty? Property(Type type, string name)
  211. {
  212. CheckProperties(type);
  213. var entityName = type.EntityName();
  214. //IProperty prop = _Properties.ToArray().FirstOrDefault(x => (x.Class.Equals(type.EntityName())) && (x.Name.Equals(name)));
  215. var prop = _Properties.GetValueOrDefault(entityName)?.GetValueOrDefault(name);
  216. // Walk up the inheritance tree, see if an ancestor has this property
  217. if (prop == null && type.BaseType != null)
  218. prop = Property(type.BaseType, name);
  219. return prop;
  220. }
  221. public static void InitializeObject<TObject>(TObject entity) where TObject : BaseObject
  222. {
  223. entity.UserProperties.Clear();
  224. var props = Properties(entity.GetType()).Where(x => x is CustomProperty).ToArray();
  225. foreach (var field in props) entity.UserProperties[field.Name] = DefaultValue(field.PropertyType);
  226. }
  227. }
  228. }