StandardProperty.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 IsPropertyPersistable(StandardProperty? prop)
  127. {
  128. if (prop == null)
  129. return true;
  130. if (prop.Property.GetCustomAttribute<DoNotPersist>() != null)
  131. return false;
  132. return IsPropertyPersistable(prop.Parent as StandardProperty);
  133. }
  134. private bool IsPropertySerializable(StandardProperty? prop)
  135. {
  136. if (prop == null)
  137. return true;
  138. if (prop.Property.GetCustomAttribute<DoNotSerialize>() != null)
  139. return false;
  140. return IsPropertySerializable(prop.Parent as StandardProperty);
  141. }
  142. public bool IsPersistable => IsPropertyPersistable(this);
  143. public bool IsSerializable => IsPropertySerializable(this);
  144. private bool? _dbColumn;
  145. public bool IsDBColumn
  146. {
  147. get
  148. {
  149. if (!_dbColumn.HasValue)
  150. {
  151. _dbColumn = !IsCalculated
  152. && IsPersistable
  153. && Property.CanWrite
  154. && (!this.HasParentEntityLink() || (Parent?.HasParentEntityLink() != true && Property.Name == "ID"));
  155. }
  156. return _dbColumn.Value;
  157. }
  158. }
  159. public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute
  160. {
  161. return Property.GetCustomAttribute<TAttribute>();
  162. }
  163. public Expression Expression()
  164. {
  165. return CoreUtils.CreateMemberExpression(_class, Name);
  166. }
  167. public Func<object, object> Getter()
  168. {
  169. if (_getter is null) CheckExpressions();
  170. return _getter;
  171. }
  172. public Action<object, object?> Setter()
  173. {
  174. if (_setter is null) CheckExpressions();
  175. return _setter;
  176. }
  177. public decimal PropertySequence() => _propertySequence;
  178. private decimal CalculatePropertySequence()
  179. {
  180. var sequence = 0.0M;
  181. IProperty? property = this;
  182. while(property != null)
  183. {
  184. sequence = property.Sequence + sequence / 1000.0M;
  185. property = property.Parent;
  186. }
  187. return sequence;
  188. }
  189. public override string ToString()
  190. {
  191. return string.Format("{0}.{1}", Class, Name);
  192. }
  193. private void ClearExpressions()
  194. {
  195. _checkedExpressions = false;
  196. _getter = null;
  197. _setter = null;
  198. }
  199. private void CheckExpressions()
  200. {
  201. if(_checkedExpressions) return;
  202. if (_class == null)
  203. return;
  204. if (string.IsNullOrEmpty(_name))
  205. return;
  206. try
  207. {
  208. _getter = Expressions.Getter(_class, _name);
  209. }
  210. catch (Exception e)
  211. {
  212. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  213. }
  214. try
  215. {
  216. _setter = Expressions.Setter(_class, _name);
  217. }
  218. catch (Exception e)
  219. {
  220. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  221. }
  222. _checkedExpressions = true;
  223. }
  224. }
  225. }