StandardProperty.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. namespace InABox.Core
  6. {
  7. public class StandardProperty : IProperty
  8. {
  9. public Type _class;
  10. private Func<object, object> _getter;
  11. private string _name = "";
  12. private bool? _calculated;
  13. private Action<object, object?> _setter;
  14. private bool _checkedExpressions;
  15. public string _type = "";
  16. public Type _propertyType = typeof(object);
  17. public StandardProperty()
  18. {
  19. Editor = new NullEditor();
  20. HasEditor = false;
  21. }
  22. public string Class
  23. {
  24. get => _class.EntityName();
  25. set
  26. {
  27. _class = CoreUtils.GetEntity(value);
  28. ClearExpressions();
  29. }
  30. }
  31. public Type? ClassType
  32. {
  33. get => _class;
  34. set
  35. {
  36. _class = value ?? typeof(object);
  37. }
  38. }
  39. public string Name
  40. {
  41. get => _name;
  42. set
  43. {
  44. _name = value;
  45. ClearExpressions();
  46. }
  47. }
  48. public string Type
  49. {
  50. get => _type;
  51. set
  52. {
  53. Type propType;
  54. try
  55. {
  56. propType = CoreUtils.GetEntityOrNull(_type) ?? typeof(object);
  57. }
  58. catch
  59. {
  60. propType = typeof(object);
  61. }
  62. PropertyType = propType; // Invoke other setter to ensure consistency of functionality
  63. }
  64. }
  65. public Type PropertyType
  66. {
  67. get => _propertyType;
  68. set
  69. {
  70. _propertyType = value;
  71. _type = value.EntityName();
  72. IsEntityLink = _propertyType.HasInterface(typeof(IEntityLink));
  73. IsEnclosedEntity = _propertyType.HasInterface(typeof(IEnclosedEntity));
  74. IsParent = IsEnclosedEntity || IsEntityLink;
  75. }
  76. }
  77. public PropertyInfo Property { get; set; }
  78. //[ComboLookupEditor(typeof(PropertyEditorLookups))]
  79. public BaseEditor Editor { get; set; }
  80. public bool HasEditor { get; set; }
  81. public string Caption { get; set; }
  82. private decimal _propertySequence;
  83. private long _sequence;
  84. public long Sequence
  85. {
  86. get => _sequence;
  87. set
  88. {
  89. _sequence = value;
  90. _propertySequence = CalculatePropertySequence();
  91. }
  92. }
  93. [NullEditor]
  94. public string Page { get; set; }
  95. public bool Required { get; set; }
  96. public LoggablePropertyAttribute? Loggable { get; set; }
  97. private IProperty? _parent;
  98. public IProperty? Parent
  99. {
  100. get => _parent;
  101. set
  102. {
  103. _parent = value;
  104. _propertySequence = CalculatePropertySequence();
  105. }
  106. }
  107. public bool IsEntityLink { get; set; }
  108. public bool IsEnclosedEntity { get; set; }
  109. public bool IsParent { get; set; }
  110. public bool IsCalculated
  111. {
  112. get
  113. {
  114. if (!_calculated.HasValue)
  115. {
  116. _calculated = Property.GetCustomAttribute<AggregateAttribute>() != null
  117. || Property.GetCustomAttribute<FormulaAttribute>() != null
  118. || Property.GetCustomAttribute<ConditionAttribute>() != null
  119. || Property.GetCustomAttribute<ChildEntityAttribute>() != null;
  120. }
  121. return _calculated.Value;
  122. }
  123. }
  124. public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute
  125. {
  126. return Property.GetCustomAttribute<TAttribute>();
  127. }
  128. public Expression Expression()
  129. {
  130. return CoreUtils.CreateMemberExpression(_class, Name);
  131. }
  132. public Func<object, object> Getter()
  133. {
  134. if (_getter is null) CheckExpressions();
  135. return _getter;
  136. }
  137. public Action<object, object?> Setter()
  138. {
  139. if (_setter is null) CheckExpressions();
  140. return _setter;
  141. }
  142. public decimal PropertySequence() => _propertySequence;
  143. private decimal CalculatePropertySequence()
  144. {
  145. var sequence = 0.0M;
  146. IProperty? property = this;
  147. while(property != null)
  148. {
  149. sequence = property.Sequence + sequence / 1000.0M;
  150. property = property.Parent;
  151. }
  152. return sequence;
  153. }
  154. public override string ToString()
  155. {
  156. return string.Format("{0}.{1}", Class, Name);
  157. }
  158. private void ClearExpressions()
  159. {
  160. _checkedExpressions = false;
  161. _getter = null;
  162. _setter = null;
  163. }
  164. private void CheckExpressions()
  165. {
  166. if(_checkedExpressions) return;
  167. if (_class == null)
  168. return;
  169. if (string.IsNullOrEmpty(_name))
  170. return;
  171. try
  172. {
  173. _getter = Expressions.Getter(_class, _name);
  174. }
  175. catch (Exception e)
  176. {
  177. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  178. }
  179. try
  180. {
  181. _setter = Expressions.Setter(_class, _name);
  182. }
  183. catch (Exception e)
  184. {
  185. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  186. }
  187. _checkedExpressions = true;
  188. }
  189. }
  190. }