StandardProperty.cs 6.7 KB

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