StandardProperty.cs 6.6 KB

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