DatabaseSchema.cs 13 KB

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