StandardProperty.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. public string _type = "";
  15. public Type _propertyType = typeof(object);
  16. public StandardProperty()
  17. {
  18. Editor = new NullEditor();
  19. HasEditor = false;
  20. }
  21. [ComboLookupEditor(typeof(StandardPropertyClassLookups))]
  22. public string Class
  23. {
  24. get => _class.EntityName();
  25. set
  26. {
  27. _class = CoreUtils.GetEntity(value);
  28. CheckExpressions();
  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. CheckExpressions();
  46. }
  47. }
  48. [ComboLookupEditor(typeof(PropertyTypeLookups))]
  49. public string Type
  50. {
  51. get => _type;
  52. set
  53. {
  54. Type propType;
  55. try
  56. {
  57. propType = CoreUtils.GetEntityOrNull(_type) ?? typeof(object);
  58. }
  59. catch
  60. {
  61. propType = typeof(object);
  62. }
  63. PropertyType = propType; // Invoke other setter to ensure consistency of functionality
  64. }
  65. }
  66. public Type PropertyType
  67. {
  68. get => _propertyType;
  69. set
  70. {
  71. _propertyType = value;
  72. _type = value.EntityName();
  73. IsEntityLink = _propertyType.GetInterfaces().Contains(typeof(IEntityLink));
  74. IsEnclosedEntity = _propertyType.GetInterfaces().Contains(typeof(IEnclosedEntity));
  75. IsParent = IsEnclosedEntity || IsEntityLink;
  76. }
  77. }
  78. public PropertyInfo Property { get; set; }
  79. //[ComboLookupEditor(typeof(PropertyEditorLookups))]
  80. public BaseEditor Editor { get; set; }
  81. public bool HasEditor { get; set; }
  82. public string Caption { 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. public bool IsCalculated
  112. {
  113. get
  114. {
  115. if (!_calculated.HasValue)
  116. {
  117. _calculated = Property.GetCustomAttribute<AggregateAttribute>() != null
  118. || Property.GetCustomAttribute<FormulaAttribute>() != null
  119. || Property.GetCustomAttribute<ConditionAttribute>() != null
  120. || Property.GetCustomAttribute<ChildEntityAttribute>() != null;
  121. }
  122. return _calculated.Value;
  123. }
  124. }
  125. public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute
  126. {
  127. return Property.GetCustomAttribute<TAttribute>();
  128. }
  129. public Expression Expression()
  130. {
  131. return CoreUtils.CreateMemberExpression(_class, Name);
  132. }
  133. public Func<object, object> Getter()
  134. {
  135. return _getter;
  136. }
  137. public Action<object, object?> Setter()
  138. {
  139. return _setter;
  140. }
  141. public decimal PropertySequence() => _propertySequence;
  142. private decimal CalculatePropertySequence()
  143. {
  144. var sequence = 0.0M;
  145. IProperty? property = this;
  146. while(property != null)
  147. {
  148. sequence = property.Sequence + sequence / 1000.0M;
  149. property = property.Parent;
  150. }
  151. return sequence;
  152. }
  153. public override string ToString()
  154. {
  155. return string.Format("{0}.{1}", Class, Name);
  156. }
  157. private void CheckExpressions()
  158. {
  159. if (_class == null)
  160. return;
  161. if (string.IsNullOrEmpty(_name))
  162. return;
  163. try
  164. {
  165. _propertyType = CoreUtils.GetProperty(_class, _name).PropertyType;
  166. }
  167. catch (Exception e)
  168. {
  169. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  170. }
  171. try
  172. {
  173. _getter = Expressions.Getter(_class, _name);
  174. }
  175. catch (Exception e)
  176. {
  177. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  178. }
  179. try
  180. {
  181. _setter = Expressions.Setter(_class, _name);
  182. }
  183. catch (Exception e)
  184. {
  185. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  186. }
  187. }
  188. private class StandardPropertyClassLookups : LookupGenerator<object>
  189. {
  190. public StandardPropertyClassLookups(object[] items) : base(items)
  191. {
  192. var types = CoreUtils.TypeList(
  193. AppDomain.CurrentDomain.GetAssemblies(),
  194. myType =>
  195. myType.IsClass
  196. && !myType.IsAbstract
  197. && myType.IsSubclassOf(typeof(BaseEditor))
  198. );
  199. foreach (var type in types)
  200. AddValue(type.EntityName(), type.Name);
  201. }
  202. }
  203. }
  204. }