CustomProperty.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Runtime.InteropServices;
  6. namespace InABox.Core
  7. {
  8. [UserTracking(typeof(User))]
  9. public class CustomProperty : Entity, IProperty, IPersistent, IRemotable, ISequenceable, ILicense<CoreLicense>
  10. {
  11. public Type? _class;
  12. private Func<object, object> _getter;
  13. private string _name = "";
  14. private Action<object, object?> _setter;
  15. private string _type;
  16. private Type type = typeof(object);
  17. private string _caption;
  18. private long _sequence;
  19. private string _page;
  20. private bool _visible;
  21. private bool _editable;
  22. private Summary _summary;
  23. [ComboLookupEditor(typeof(PropertyClassLookups))]
  24. [EditorSequence(1)]
  25. public string Class
  26. {
  27. get => _class != null ? _class.EntityName() : "";
  28. set
  29. {
  30. CoreUtils.TryGetEntity(value, out _class);
  31. CheckExpressions();
  32. }
  33. }
  34. [EditorSequence(2)]
  35. public string Name
  36. {
  37. get => _name;
  38. set
  39. {
  40. _name = value;
  41. CheckExpressions();
  42. RegenerateEditor();
  43. }
  44. }
  45. [ComboLookupEditor(typeof(PropertyTypeLookups), Visible = Core.Visible.Default)]
  46. [EditorSequence(3)]
  47. public string Type
  48. {
  49. get => _type;
  50. set
  51. {
  52. _type = value;
  53. try
  54. {
  55. type = CoreUtils.GetEntityOrNull(_type) ?? typeof(object);
  56. }
  57. catch
  58. {
  59. type = typeof(object);
  60. }
  61. CheckExpressions();
  62. IsEntityLink = type.GetInterfaces().Contains(typeof(IEntityLink));
  63. IsEnclosedEntity = type.GetInterfaces().Contains(typeof(IEnclosedEntity));
  64. IsParent = IsEntityLink || IsEnclosedEntity;
  65. RegenerateEditor();
  66. }
  67. }
  68. [MemoEditor(Visible = Core.Visible.Default)]
  69. [EditorSequence(4)]
  70. public string Comment { get; set; }
  71. [TextBoxEditor(Visible = Core.Visible.Optional)]
  72. [EditorSequence(5)]
  73. public string Caption
  74. {
  75. get => _caption;
  76. set
  77. {
  78. _caption = value;
  79. RegenerateEditor();
  80. }
  81. }
  82. [TextBoxEditor(Visible = Core.Visible.Optional)]
  83. [EditorSequence(6)]
  84. public string Page
  85. {
  86. get => _page;
  87. set
  88. {
  89. _page = value;
  90. RegenerateEditor();
  91. }
  92. }
  93. [CheckBoxEditor(Visible = Core.Visible.Optional)]
  94. [EditorSequence(7)]
  95. public bool Required { get; set; }
  96. [CheckBoxEditor(Visible = Core.Visible.Default)]
  97. [EditorSequence(8)]
  98. public bool Visible
  99. {
  100. get => _visible;
  101. set
  102. {
  103. _visible = value;
  104. RegenerateEditor();
  105. }
  106. }
  107. [CheckBoxEditor(Visible = Core.Visible.Optional)]
  108. [EditorSequence(9)]
  109. public bool Editable
  110. {
  111. get => _editable;
  112. set
  113. {
  114. _editable = value;
  115. RegenerateEditor();
  116. }
  117. }
  118. [EnumLookupEditor(typeof(Summary))]
  119. [EditorSequence(10)]
  120. public Summary Summary
  121. {
  122. get => _summary;
  123. set
  124. {
  125. _summary = value;
  126. RegenerateEditor();
  127. }
  128. }
  129. protected override void Init()
  130. {
  131. base.Init();
  132. Visible = true;
  133. Editable = true;
  134. Required = false;
  135. HasEditor = true;
  136. Editor = new NullEditor();
  137. _summary = Summary.None;
  138. }
  139. [DoNotPersist]
  140. [DoNotSerialize]
  141. public Type? ClassType
  142. {
  143. get => _class;
  144. set
  145. {
  146. _class = value;
  147. CheckExpressions();
  148. }
  149. }
  150. [DoNotPersist]
  151. [DoNotSerialize]
  152. public Type PropertyType
  153. {
  154. get => type;
  155. set
  156. {
  157. type = value;
  158. _type = value.EntityName();
  159. Editor = null;
  160. IsEntityLink = type.GetInterfaces().Contains(typeof(IEntityLink));
  161. IsEnclosedEntity = type.GetInterfaces().Contains(typeof(IEnclosedEntity));
  162. IsParent = IsEntityLink || IsEnclosedEntity;
  163. RegenerateEditor();
  164. }
  165. }
  166. [NullEditor]
  167. [DoNotPersist]
  168. [DoNotSerialize]
  169. public BaseEditor Editor { get; set; }
  170. [NullEditor]
  171. [DoNotPersist]
  172. [DoNotSerialize]
  173. public bool HasEditor { get; set; }
  174. [NullEditor]
  175. public LoggablePropertyAttribute? Loggable { get; set; }
  176. [NullEditor]
  177. public long Sequence
  178. {
  179. get => _sequence;
  180. set
  181. {
  182. _sequence = value;
  183. RegenerateEditor();
  184. }
  185. }
  186. [NullEditor]
  187. [DoNotPersist]
  188. [DoNotSerialize]
  189. public IProperty? Parent { get; set; }
  190. [NullEditor]
  191. [DoNotPersist]
  192. [DoNotSerialize]
  193. public bool IsEntityLink { get; set; }
  194. [NullEditor]
  195. [DoNotPersist]
  196. [DoNotSerialize]
  197. public bool IsEnclosedEntity { get; set; }
  198. [NullEditor]
  199. [DoNotPersist]
  200. [DoNotSerialize]
  201. public bool IsParent { get; set; }
  202. [NullEditor]
  203. [DoNotPersist]
  204. [DoNotSerialize]
  205. public bool IsCalculated => false;
  206. [NullEditor]
  207. [DoNotPersist]
  208. [DoNotSerialize]
  209. public bool IsDBColumn => true;
  210. private static string? NonWhiteSpaceOrNull(string? name)
  211. {
  212. return string.IsNullOrWhiteSpace(name) ? null : name;
  213. }
  214. public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute
  215. {
  216. return null;
  217. }
  218. private void RegenerateEditor()
  219. {
  220. Editor = EditorUtils.GetEditor(PropertyType) ?? new NullEditor();
  221. Editor.Caption = NonWhiteSpaceOrNull(Caption) ?? NonWhiteSpaceOrNull(Name) ?? string.Empty;
  222. Editor.EditorSequence = (int)Sequence;
  223. Editor.Page = Page;
  224. Editor.Editable = Editable ? Core.Editable.Enabled : Core.Editable.Disabled;
  225. Editor.Visible = Visible ? Core.Visible.Optional : Core.Visible.Hidden;
  226. Editor.Summary = Summary;
  227. }
  228. public Expression Expression()
  229. {
  230. if (_class is null)
  231. throw new Exception("No class for CustomProperty");
  232. return CoreUtils.CreateIndexExpression(_class, "UserProperties", Name);
  233. }
  234. public Func<object, object> Getter()
  235. {
  236. return _getter;
  237. }
  238. public Action<object, object?> Setter()
  239. {
  240. return _setter;
  241. }
  242. public override string ToString()
  243. {
  244. return string.Format("{0}.{1}", Class, Name);
  245. }
  246. private void CheckExpressions()
  247. {
  248. if (_class == null)
  249. return;
  250. if (string.IsNullOrEmpty(Name))
  251. return;
  252. try
  253. {
  254. _getter = Expressions.Getter(_class, "UserProperties", Name);
  255. _setter = Expressions.Setter(_class, "UserProperties", Name);
  256. }
  257. catch (Exception e)
  258. {
  259. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  260. }
  261. }
  262. public decimal PropertySequence() => Sequence;
  263. }
  264. public class CustomPropertyLookups : EntityLookup<CustomProperty>
  265. {
  266. public override Columns<CustomProperty> DefineColumns()
  267. {
  268. return base.DefineColumns()
  269. .Add(x => x.Class)
  270. .Add(x => x.Name);
  271. }
  272. //public string FormatLookup(Dictionary<string, object> values, IEnumerable<string> exclude)
  273. //{
  274. // return LookupFactory.DefaultFormatLookup(values, exclude.Concat(new String[] { "ID" }));
  275. //}
  276. public override Filter<CustomProperty>? DefineFilter()
  277. {
  278. return null;
  279. }
  280. public override SortOrder<CustomProperty> DefineSortOrder()
  281. {
  282. return new SortOrder<CustomProperty>(x => x.Class).ThenBy(x => x.Sequence);
  283. }
  284. }
  285. }