StandardProperty.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. }
  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 IsCalculated
  110. {
  111. get
  112. {
  113. if (!_calculated.HasValue)
  114. {
  115. _calculated = Property.GetCustomAttribute<AggregateAttribute>() != null
  116. || Property.GetCustomAttribute<FormulaAttribute>() != null
  117. || Property.GetCustomAttribute<ConditionAttribute>() != null
  118. || Property.GetCustomAttribute<ChildEntityAttribute>() != null;
  119. }
  120. return _calculated.Value;
  121. }
  122. }
  123. public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute
  124. {
  125. return Property.GetCustomAttribute<TAttribute>();
  126. }
  127. public Expression Expression()
  128. {
  129. return CoreUtils.CreateMemberExpression(_class, Name);
  130. }
  131. public Func<object, object> Getter()
  132. {
  133. return _getter;
  134. }
  135. public Action<object, object?> Setter()
  136. {
  137. return _setter;
  138. }
  139. public decimal PropertySequence() => _propertySequence;
  140. private decimal CalculatePropertySequence()
  141. {
  142. var sequence = 0.0M;
  143. IProperty? property = this;
  144. while(property != null)
  145. {
  146. sequence = property.Sequence + sequence / 1000.0M;
  147. property = property.Parent;
  148. }
  149. return sequence;
  150. }
  151. public override string ToString()
  152. {
  153. return string.Format("{0}.{1}", Class, Name);
  154. }
  155. private void CheckExpressions()
  156. {
  157. if (_class == null)
  158. return;
  159. if (string.IsNullOrEmpty(_name))
  160. return;
  161. try
  162. {
  163. _propertyType = CoreUtils.GetProperty(_class, _name).PropertyType;
  164. }
  165. catch (Exception e)
  166. {
  167. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  168. }
  169. try
  170. {
  171. _getter = Expressions.Getter(_class, _name);
  172. }
  173. catch (Exception e)
  174. {
  175. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  176. }
  177. try
  178. {
  179. _setter = Expressions.Setter(_class, _name);
  180. }
  181. catch (Exception e)
  182. {
  183. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  184. }
  185. }
  186. private class StandardPropertyClassLookups : LookupGenerator<object>
  187. {
  188. public StandardPropertyClassLookups(object[] items) : base(items)
  189. {
  190. var types = CoreUtils.TypeList(
  191. AppDomain.CurrentDomain.GetAssemblies(),
  192. myType =>
  193. myType.IsClass
  194. && !myType.IsAbstract
  195. && myType.IsSubclassOf(typeof(BaseEditor))
  196. );
  197. foreach (var type in types)
  198. AddValue(type.EntityName(), type.Name);
  199. }
  200. }
  201. }
  202. }