StandardProperty.cs 7.6 KB

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