CustomProperty.cs 8.9 KB

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