CustomProperty.cs 7.1 KB

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