CustomProperty.cs 8.2 KB

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