StandardProperty.cs 7.5 KB

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