Entity.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. namespace InABox.Core
  10. {
  11. public class Credentials
  12. {
  13. public virtual string UserID { get; set; }
  14. public virtual string Password { get; set; }
  15. }
  16. public interface IEnclosedEntity
  17. {
  18. }
  19. public interface IEntity
  20. {
  21. Guid ID { get; set; }
  22. Guid Deleted { get; set; }
  23. bool IsChanged();
  24. }
  25. public interface ITaxable
  26. {
  27. double ExTax { get; set; }
  28. double TaxRate { get; set; }
  29. double Tax { get; set; }
  30. double IncTax { get; set; }
  31. }
  32. public interface IIssues
  33. {
  34. string Issues { get; set; }
  35. }
  36. public interface IExportable
  37. {
  38. }
  39. public interface IImportable
  40. {
  41. }
  42. public interface IMergeable
  43. {
  44. }
  45. public interface ISecure { }
  46. public interface IDuplicatable
  47. {
  48. IEntityDuplicator GetDuplicator();
  49. }
  50. public interface IEntityDuplicator
  51. {
  52. void Duplicate(IFilter filter);
  53. }
  54. public class EntityDuplicator<TEntity> : IEntityDuplicator where TEntity : Entity, IRemotable, IPersistent
  55. {
  56. private readonly List<Tuple<Type, Type, Expression>> _relationships = new List<Tuple<Type, Type, Expression>>();
  57. public void Duplicate(IFilter filter)
  58. {
  59. Duplicate(typeof(TEntity), filter);
  60. }
  61. private void Duplicate(Type parent, IFilter filter)
  62. {
  63. var table = ClientFactory.CreateClient(parent).Query(filter);
  64. foreach (var row in table.Rows)
  65. {
  66. var update = (row.ToObject(parent) as Entity)!;
  67. var id = update.ID;
  68. update.ID = Guid.Empty;
  69. update.CommitChanges();
  70. ClientFactory.CreateClient(parent).Save(update, "Duplicated Record");
  71. foreach (var relationship in _relationships.Where(x => x.Item1 == parent))
  72. {
  73. var filtertype = typeof(Filter<>).MakeGenericType(relationship.Item2);
  74. var newfilter = Activator.CreateInstance(filtertype) as IFilter;
  75. filter.Expression = relationship.Item3;
  76. filter.Operator = Operator.IsEqualTo;
  77. filter.Value = id;
  78. Duplicate(relationship.Item2, filter);
  79. }
  80. }
  81. }
  82. public void AddChild<TParent, TChild>(Expression<Func<TChild, IEntityLink<TParent>>> childkey) where TParent : Entity where TChild : Entity
  83. {
  84. _relationships.Add(new Tuple<Type, Type, Expression>(typeof(TParent), typeof(TChild), childkey));
  85. }
  86. }
  87. /// <summary>
  88. /// An <see cref="IProperty"/> is required if it has the <see cref="RequiredColumnAttribute"/> defined on it.<br/>
  89. /// If it is part of an <see cref="IEntityLink"/> (or <see cref="IEnclosedEntity"/>), then it is only required
  90. /// if the <see cref="IEntityLink"/> property on the parent class also has <see cref="RequiredColumnAttribute"/>.
  91. /// </summary>
  92. public class RequiredColumnAttribute : Attribute { }
  93. public abstract class Entity : BaseObject, IEntity
  94. {
  95. private bool bTaxing;
  96. //public String Name { get; set; }
  97. [TimestampEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  98. [RequiredColumn]
  99. public virtual DateTime LastUpdate { get; set; }
  100. [CodeEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  101. [RequiredColumn]
  102. public string LastUpdateBy { get; set; }
  103. [NullEditor]
  104. [RequiredColumn]
  105. public virtual DateTime Created { get; set; }
  106. [NullEditor]
  107. [RequiredColumn]
  108. public virtual string CreatedBy { get; set; }
  109. [NullEditor]
  110. [RequiredColumn]
  111. public Guid ID { get; set; }
  112. /// <summary>
  113. /// If the entity is deleted, holds the ID of the Deletion. Otherwise, it holds Guid.Empty
  114. /// </summary>
  115. [NullEditor]
  116. [DoNotSerialize]
  117. public Guid Deleted { get; set; }
  118. public static Type ClassVersion(Type t)
  119. {
  120. //Type t = MethodBase.GetCurrentMethod().DeclaringType;
  121. var ti = t.GetTypeInfo();
  122. var interfaces = ti.GetInterfaces();
  123. if (ti.GetInterfaces().Contains(typeof(IPersistent)))
  124. {
  125. if (ti.BaseType != null)
  126. throw new Exception(t.Name + " hase no Base Type");
  127. if (ti.BaseType.Equals(typeof(Entity)))
  128. throw new Exception(t.Name + " may not derive directly from TEntity");
  129. var props = t.GetTypeInfo().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
  130. if (props.Count() > 0)
  131. throw new Exception(t.Name + "may not declare properties");
  132. }
  133. return t.GetTypeInfo().BaseType;
  134. }
  135. //[NullEditor]
  136. //public List<EntityHistory> History { get; set; }
  137. protected override void Init()
  138. {
  139. base.Init();
  140. ID = Guid.Empty;
  141. Deleted = Guid.Empty;
  142. Created = DateTime.Now;
  143. CreatedBy = ClientFactory.UserID;
  144. LastUpdate = DateTime.Now;
  145. LastUpdateBy = ClientFactory.UserID;
  146. //History = new List<EntityHistory>();
  147. CheckSequence();
  148. }
  149. //public Entity() : base()
  150. //{
  151. // CommitChanges();
  152. //}
  153. //public Entity(Guid id) : base()
  154. //{
  155. // ID = id;
  156. // History = new List<EntityHistory>();
  157. // UserProperties = new Dictionary<string, Object>();
  158. // DataModel.InitializeEntity(this);
  159. // CheckSequence();
  160. // CommitChanges();
  161. //}
  162. public static bool IsEntityLinkValid<T, U>(Expression<Func<T, U>> expression, CoreRow arg) where U : IEntityLink
  163. {
  164. return arg.IsEntityLinkValid(expression);
  165. }
  166. /// <summary>
  167. /// Gets the ID of an entity link of an entity, doing a validity check (see <see cref="IsEntityLinkValid{T, U}(Expression{Func{T, U}}, CoreRow)"/>)
  168. /// </summary>
  169. /// <typeparam name="T">The entity type</typeparam>
  170. /// <typeparam name="U">The entity link type</typeparam>
  171. /// <param name="expression">An expression to the entity link of type <typeparamref name="U"/></param>
  172. /// <param name="arg">The row representing the entity of type <typeparamref name="T"/></param>
  173. /// <returns>The ID on the entity link, or <c>null</c> if the entity link is invalid</returns>
  174. public static Guid? EntityLinkID<T, U>(Expression<Func<T, U>> expression, CoreRow arg) where U : IEntityLink
  175. {
  176. var col = CoreUtils.GetFullPropertyName(expression, ".");
  177. var id = arg.Get<Guid>(col + ".ID");
  178. if (id != Guid.Empty && arg.Get<Guid>(col + ".Deleted") == Guid.Empty)
  179. return id;
  180. return null;
  181. }
  182. private void CheckSequence()
  183. {
  184. if (this is ISequenceable)
  185. {
  186. var seq = (ISequenceable)this;
  187. if (seq.Sequence <= 0)
  188. seq.Sequence = CoreUtils.GenerateSequence();
  189. }
  190. }
  191. protected override void SetChanged(string name, object before, object after)
  192. {
  193. base.SetChanged(name, before, after);
  194. CheckTax(name, before, after);
  195. }
  196. private void CheckTax(string name, object before, object after)
  197. {
  198. if (this is ITaxable)
  199. {
  200. if (bTaxing)
  201. return;
  202. bTaxing = true;
  203. var taxable = this as ITaxable;
  204. if (name.Equals("ExTax"))
  205. {
  206. taxable.Tax = (double)after * (taxable.TaxRate / 100.0F);
  207. taxable.IncTax = (double)after + taxable.Tax;
  208. }
  209. else if (name.Equals("TaxRate"))
  210. {
  211. taxable.Tax = taxable.ExTax * ((double)after / 100.0F);
  212. taxable.IncTax = taxable.ExTax + taxable.Tax;
  213. }
  214. else if (name.Equals("Tax"))
  215. {
  216. taxable.IncTax = (double)after + taxable.Tax;
  217. }
  218. else if (name.Equals("IncTax"))
  219. {
  220. taxable.ExTax = (double)after / ((100.0F + taxable.TaxRate) / 100.0F);
  221. taxable.Tax = (double)after - taxable.ExTax;
  222. }
  223. bTaxing = false;
  224. }
  225. }
  226. protected override void DoPropertyChanged(string name, object before, object after)
  227. {
  228. if (!IsObserving())
  229. return;
  230. //CheckSequence();
  231. if (!name.Equals("LastUpdate"))
  232. LastUpdate = DateTime.Now;
  233. LastUpdateBy = ClientFactory.UserID;
  234. // This doesn;t work - keeps being updated to current date
  235. // Created => null ::Set ID = guid.empty -> now :: any other change -> unchanged!
  236. // Moved to Create(), should not simply be overwritten on deserialise from json
  237. //if (Created.Equals(DateTime.MinValue))
  238. //{
  239. // Created = DateTime.Now;
  240. // CreatedBy = ClientFactory.UserID;
  241. //}
  242. }
  243. #region Linked Properties
  244. // Why?
  245. [DoNotSerialize]
  246. private static readonly List<LinkedProperty> _LinkedProperties = new List<LinkedProperty>();
  247. public virtual void LinkProperty<TEntityLink, TLinkedEntity>(Expression<Func<TEntityLink, object>> source,
  248. Expression<Func<TLinkedEntity, object>> target)
  249. {
  250. var map = LinkedProperty.Create(source, target);
  251. if (!_LinkedProperties.Any(x =>
  252. x.Source.DeclaringType.Equals(typeof(TEntityLink)) && x.Source.Name.Equals(map.Source.Name) &&
  253. x.Source.Name.Equals(map.Source.Name) && x.Target.DeclaringType.Equals(typeof(TLinkedEntity)) &&
  254. x.Target.Name.Equals(map.Target.Name)))
  255. _LinkedProperties.Add(LinkedProperty.Create(source, target));
  256. }
  257. public IEnumerable<LinkedProperty> LinkedProperties(Type type)
  258. {
  259. var result = _LinkedProperties.Where(x =>
  260. (x.Source.DeclaringType.Equals(type) || x.Source.DeclaringType.IsSubclassOf(type)) &&
  261. (x.Target.DeclaringType.Equals(GetType()) || x.Target.DeclaringType.IsSubclassOf(GetType()))
  262. );
  263. return result;
  264. }
  265. #endregion
  266. }
  267. public interface ILicense<TLicenseToken> where TLicenseToken : LicenseToken
  268. {
  269. }
  270. public interface IPersistent
  271. {
  272. }
  273. public interface IRemotable
  274. {
  275. }
  276. //public interface IRemoteQuery
  277. //{
  278. //}
  279. //public interface IRemoteUpdate
  280. //{
  281. //}
  282. //public interface IRemoteDelete
  283. //{
  284. //}
  285. public interface ISequenceable
  286. {
  287. long Sequence { get; set; }
  288. }
  289. public interface IAutoIncrement<T, TType>
  290. {
  291. Expression<Func<T, TType>> AutoIncrementField();
  292. Filter<T> AutoIncrementFilter();
  293. }
  294. public interface INumericAutoIncrement<T> : IAutoIncrement<T, int>
  295. {
  296. }
  297. public interface IStringAutoIncrement<T> : IAutoIncrement<T, string>
  298. {
  299. string AutoIncrementFormat();
  300. }
  301. /// <summary>
  302. /// Used to flag an entity as exhibiting the properties of a ManyToMany relationship, allowing PRS to auto-generate things like grids and datamodels based on
  303. /// entity relationships.
  304. /// </summary>
  305. /// <remarks>
  306. /// This will cause a ManyToMany grid of <typeparamref name="TRight"/> to appear on all <typeparamref name="TLeft"/> editors.
  307. /// Hence, if one wishes to cause both grids to appear (that is, for <typeparamref name="TLeft"/> to appear for <typeparamref name="TRight"/> <i>and</i>
  308. /// vice versa, one must flag the entity with both <c>IManyToMany&lt;<typeparamref name="TLeft"/>, <typeparamref name="TRight"/>&gt;</c> and
  309. /// <c>IManyToMany&lt;<typeparamref name="TRight"/>, <typeparamref name="TLeft"/>&gt;</c>.
  310. /// </remarks>
  311. /// <typeparam name="TLeft"></typeparam>
  312. /// <typeparam name="TRight"></typeparam>
  313. public interface IManyToMany<TLeft, TRight> where TLeft : Entity where TRight : Entity
  314. {
  315. }
  316. public interface IOneToMany<TOne> where TOne : Entity
  317. {
  318. }
  319. public static class EntityFactory
  320. {
  321. public delegate object ObjectActivator(params object[] args);
  322. private static readonly Dictionary<Type, ObjectActivator> _cache = new Dictionary<Type, ObjectActivator>();
  323. public static ObjectActivator GetActivator<T>(ConstructorInfo ctor)
  324. {
  325. var type = ctor.DeclaringType;
  326. var paramsInfo = ctor.GetParameters();
  327. //create a single param of type object[]
  328. var param =
  329. Expression.Parameter(typeof(object[]), "args");
  330. var argsExp =
  331. new Expression[paramsInfo.Length];
  332. //pick each arg from the params array
  333. //and create a typed expression of them
  334. for (var i = 0; i < paramsInfo.Length; i++)
  335. {
  336. Expression index = Expression.Constant(i);
  337. var paramType = paramsInfo[i].ParameterType;
  338. Expression paramAccessorExp =
  339. Expression.ArrayIndex(param, index);
  340. Expression paramCastExp =
  341. Expression.Convert(paramAccessorExp, paramType);
  342. argsExp[i] = paramCastExp;
  343. }
  344. //make a NewExpression that calls the
  345. //ctor with the args we just created
  346. var newExp = Expression.New(ctor, argsExp);
  347. //create a lambda with the New
  348. //Expression as body and our param object[] as arg
  349. var lambda =
  350. Expression.Lambda(typeof(ObjectActivator), newExp, param);
  351. //compile it
  352. var compiled = (ObjectActivator)lambda.Compile();
  353. return compiled;
  354. }
  355. public static T CreateEntity<T>() where T : BaseObject
  356. {
  357. if (!_cache.ContainsKey(typeof(T)))
  358. {
  359. var ctor = typeof(T).GetConstructors().First();
  360. var activator = typeof(EntityFactory).GetMethod("GetActivator").MakeGenericMethod(typeof(T));
  361. _cache[typeof(T)] = GetActivator<T>(ctor);
  362. }
  363. var createdActivator = _cache[typeof(T)];
  364. return (T)createdActivator();
  365. }
  366. public static object CreateEntity(Type type)
  367. {
  368. if (!_cache.ContainsKey(type))
  369. {
  370. var ctor = type.GetConstructors().First();
  371. var activator = typeof(EntityFactory).GetMethod("GetActivator").MakeGenericMethod(type);
  372. _cache[type] = (ObjectActivator)activator.Invoke(null, new object[] { ctor });
  373. }
  374. var createdActivator = _cache[type];
  375. return createdActivator();
  376. }
  377. }
  378. }