Entity.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 IEntity
  17. {
  18. Guid ID { get; set; }
  19. Guid Deleted { get; set; }
  20. bool IsChanged();
  21. void CommitChanges();
  22. void CancelChanges();
  23. }
  24. public interface ITaxable
  25. {
  26. double ExTax { get; set; }
  27. double TaxRate { get; set; }
  28. double Tax { get; set; }
  29. double IncTax { get; set; }
  30. }
  31. public interface IProblem
  32. {
  33. string[] Notes { get; set; }
  34. DateTime Resolved { get; set; }
  35. }
  36. public abstract class Problem : EnclosedEntity, IProblem
  37. {
  38. [NotesEditor]
  39. [EditorSequence(1)]
  40. [Caption("Notes", IncludePath = false)]
  41. public string[] Notes { get; set; }
  42. [TimestampEditor]
  43. [EditorSequence(999)]
  44. [Caption("Resolved", IncludePath = false)]
  45. public DateTime Resolved { get; set; }
  46. }
  47. public interface IProblems
  48. {
  49. IProblem Problem { get; }
  50. }
  51. public interface IProblems<T>: IProblems where T : Problem
  52. {
  53. new T Problem { get; set; }
  54. IProblem IProblems.Problem => Problem;
  55. }
  56. public interface IIssues
  57. {
  58. string Issues { get; set; }
  59. }
  60. public interface IExportable
  61. {
  62. }
  63. public interface IImportable
  64. {
  65. }
  66. /// <summary>
  67. /// Indicate that an <see cref="Entity"/> is able to be merged together.
  68. /// </summary>
  69. /// <remarks>
  70. /// It is recommended that an <see cref="Entity"/> that implements this should provide a <see cref="object.ToString"/> implementation.
  71. /// </remarks>
  72. public interface IMergeable
  73. {
  74. }
  75. public interface ISecure { }
  76. public interface IDuplicatable
  77. {
  78. IEntityDuplicator GetDuplicator();
  79. }
  80. public interface IEntityDuplicator
  81. {
  82. //void Duplicate(IFilter filter);
  83. void Duplicate(IEnumerable<BaseObject> entities);
  84. }
  85. public class EntityDuplicator<TEntity> : IEntityDuplicator where TEntity : Entity, IRemotable, IPersistent
  86. {
  87. private interface IRelationship
  88. {
  89. Type ParentType { get; }
  90. Type ChildType { get; }
  91. IFilter GetFilter(Entity parent);
  92. }
  93. private class EntityLinkRelationship<TParent, TChild> : IRelationship
  94. {
  95. public Type ParentType => typeof(TParent);
  96. public Type ChildType => typeof(TChild);
  97. public Column<TChild> Column { get; set; }
  98. public IFilter GetFilter(Entity parent)
  99. {
  100. return new Filter<TChild>(Column).IsEqualTo(parent.ID);
  101. }
  102. }
  103. private class GenericRelationship<TParent, TChild> : IRelationship
  104. where TParent : Entity
  105. {
  106. public Type ParentType => typeof(TParent);
  107. public Type ChildType => typeof(TChild);
  108. public Column<TChild> Column { get; set; }
  109. public Func<TParent, object?> Func { get; set; }
  110. public IFilter GetFilter(Entity parent)
  111. {
  112. return new Filter<TChild>(Column).IsEqualTo(Func(parent as TParent));
  113. }
  114. }
  115. private readonly List<IRelationship> _relationships = new List<IRelationship>();
  116. public void Duplicate(IEnumerable<TEntity> entites) =>
  117. Duplicate(typeof(TEntity),
  118. new Filter<TEntity>(x => x.ID).InList(entites.Select(x => x.ID).ToArray()));
  119. private void Duplicate(Type parent, IFilter filter)
  120. {
  121. var table = ClientFactory.CreateClient(parent)
  122. .Query(filter, Columns.Local(parent));
  123. foreach (var row in table.Rows)
  124. {
  125. var update = (row.ToObject(parent) as Entity)!;
  126. var id = update.ID;
  127. update.ID = Guid.Empty;
  128. update.CommitChanges();
  129. ClientFactory.CreateClient(parent).Save(update, "Duplicated Record");
  130. foreach (var relationship in _relationships.Where(x => x.ParentType == parent))
  131. {
  132. Duplicate(relationship.ChildType, relationship.GetFilter(update));
  133. }
  134. }
  135. }
  136. public void AddChild<TParent, TChild, TParentLink>(Expression<Func<TChild, TParentLink>> childkey)
  137. where TParent : Entity, IRemotable, IPersistent
  138. where TChild : Entity, IRemotable, IPersistent
  139. where TParentLink : IEntityLink<TParent>
  140. {
  141. _relationships.Add(new EntityLinkRelationship<TParent, TChild>
  142. {
  143. Column = new Column<TChild>(CoreUtils.GetFullPropertyName(childkey, ".") + ".ID")
  144. });
  145. }
  146. public void AddChild<TParent, TChild>(Column<TChild> linkColumn, Func<TParent, object?> value)
  147. where TParent : Entity, IRemotable, IPersistent
  148. where TChild : Entity, IRemotable, IPersistent
  149. {
  150. _relationships.Add(new GenericRelationship<TParent, TChild>
  151. {
  152. Column = linkColumn,
  153. Func = value
  154. });
  155. }
  156. void IEntityDuplicator.Duplicate(IEnumerable<BaseObject> entities) => Duplicate(entities.Cast<TEntity>());
  157. }
  158. /// <summary>
  159. /// An <see cref="IProperty"/> is required if it has the <see cref="RequiredColumnAttribute"/> defined on it.<br/>
  160. /// If it is part of an <see cref="IEntityLink"/> (or <see cref="IEnclosedEntity"/>), then it is only required
  161. /// if the <see cref="IEntityLink"/> property on the parent class also has <see cref="RequiredColumnAttribute"/>.
  162. /// </summary>
  163. /// <remarks>
  164. /// In general, this should be used in one of two places - either if the property is required by the store for the entity, or if it is used by the <see cref="BaseObject.DoPropertyChanged(string, object?, object?)"/> function.
  165. /// </remarks>
  166. public class RequiredColumnAttribute : Attribute { }
  167. public abstract class Entity : BaseObject, IEntity
  168. {
  169. private bool bTaxing;
  170. //public String Name { get; set; }
  171. [TimestampEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  172. [RequiredColumn]
  173. public virtual DateTime LastUpdate { get; set; } = DateTime.Now;
  174. [CodeEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  175. [RequiredColumn]
  176. public string LastUpdateBy { get; set; } = ClientFactory.UserID;
  177. [NullEditor]
  178. [RequiredColumn]
  179. public virtual DateTime Created { get; set; } = DateTime.Now;
  180. [NullEditor]
  181. [RequiredColumn]
  182. public virtual string CreatedBy { get; set; } = ClientFactory.UserID;
  183. [NullEditor]
  184. [RequiredColumn]
  185. public Guid ID { get; set; } = Guid.Empty;
  186. /// <summary>
  187. /// If the entity is deleted, holds the ID of the Deletion. Otherwise, it holds Guid.Empty
  188. /// </summary>
  189. [NullEditor]
  190. [DoNotSerialize]
  191. [Obsolete]
  192. public Guid Deleted { get; set; } = Guid.Empty;
  193. public static Type ClassVersion(Type t)
  194. {
  195. //Type t = MethodBase.GetCurrentMethod().DeclaringType;
  196. var ti = t.GetTypeInfo();
  197. var interfaces = ti.GetInterfaces();
  198. if (ti.GetInterfaces().Contains(typeof(IPersistent)))
  199. {
  200. if (ti.BaseType != null)
  201. throw new Exception(t.Name + " hase no Base Type");
  202. if (ti.BaseType.Equals(typeof(Entity)))
  203. throw new Exception(t.Name + " may not derive directly from TEntity");
  204. var props = t.GetTypeInfo().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
  205. if (props.Count() > 0)
  206. throw new Exception(t.Name + "may not declare properties");
  207. }
  208. return t.GetTypeInfo().BaseType;
  209. }
  210. //[NullEditor]
  211. //public List<EntityHistory> History { get; set; }
  212. //public Entity() : base()
  213. //{
  214. // CommitChanges();
  215. //}
  216. //public Entity(Guid id) : base()
  217. //{
  218. // ID = id;
  219. // History = new List<EntityHistory>();
  220. // UserProperties = new Dictionary<string, Object>();
  221. // DataModel.InitializeEntity(this);
  222. // CheckSequence();
  223. // CommitChanges();
  224. //}
  225. public static bool IsEntityLinkValid<T, U>(Expression<Func<T, U>> expression, CoreRow arg) where U : IEntityLink
  226. {
  227. return arg.IsEntityLinkValid(expression);
  228. }
  229. /// <summary>
  230. /// 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)"/>)
  231. /// </summary>
  232. /// <typeparam name="T">The entity type</typeparam>
  233. /// <typeparam name="U">The entity link type</typeparam>
  234. /// <param name="expression">An expression to the entity link of type <typeparamref name="U"/></param>
  235. /// <param name="arg">The row representing the entity of type <typeparamref name="T"/></param>
  236. /// <returns>The ID on the entity link, or <c>null</c> if the entity link is invalid</returns>
  237. public static Guid? EntityLinkID<T, U>(Expression<Func<T, U>> expression, CoreRow arg) where U : IEntityLink
  238. {
  239. var col = CoreUtils.GetFullPropertyName(expression, ".");
  240. var id = arg.Get<Guid>(col + ".ID");
  241. if (id != Guid.Empty && arg.Get<Guid>(col + ".Deleted") == Guid.Empty)
  242. return id;
  243. return null;
  244. }
  245. protected override void SetChanged(string name, object? before, object? after)
  246. {
  247. base.SetChanged(name, before, after);
  248. CheckTax(name, before, after);
  249. }
  250. private void CheckTax(string name, object? before, object? after)
  251. {
  252. if (this is ITaxable taxable && !bTaxing)
  253. {
  254. bTaxing = true;
  255. try
  256. {
  257. if (name.Equals("ExTax"))
  258. {
  259. taxable.Tax = (double)after * (taxable.TaxRate / 100.0F);
  260. taxable.IncTax = (double)after + taxable.Tax;
  261. }
  262. else if (name.Equals("TaxRate"))
  263. {
  264. taxable.Tax = taxable.ExTax * ((double)after / 100.0F);
  265. taxable.IncTax = taxable.ExTax + taxable.Tax;
  266. }
  267. else if (name.Equals("Tax"))
  268. {
  269. taxable.ExTax = taxable.IncTax - (double)after;
  270. }
  271. else if (name.Equals("IncTax"))
  272. {
  273. taxable.ExTax = (double)after / ((100.0F + taxable.TaxRate) / 100.0F);
  274. taxable.Tax = (double)after - taxable.ExTax;
  275. }
  276. }
  277. catch (Exception e)
  278. {
  279. Logger.Send(LogType.Error, "", String.Join("\n",e.Message,e.StackTrace));
  280. }
  281. bTaxing = false;
  282. }
  283. }
  284. protected override void DoPropertyChanged(string name, object? before, object? after)
  285. {
  286. if (!IsObserving())
  287. return;
  288. //CheckSequence();
  289. if (!name.Equals("LastUpdate"))
  290. LastUpdate = DateTime.Now;
  291. LastUpdateBy = ClientFactory.UserID;
  292. // This doesn;t work - keeps being updated to current date
  293. // Created => null ::Set ID = guid.empty -> now :: any other change -> unchanged!
  294. // Moved to Create(), should not simply be overwritten on deserialise from json
  295. //if (Created.Equals(DateTime.MinValue))
  296. //{
  297. // Created = DateTime.Now;
  298. // CreatedBy = ClientFactory.UserID;
  299. //}
  300. }
  301. }
  302. public interface ILicense<TLicenseToken> where TLicenseToken : LicenseToken
  303. {
  304. }
  305. public interface IPersistent
  306. {
  307. }
  308. public interface IRemotable
  309. {
  310. }
  311. //public interface IRemoteQuery
  312. //{
  313. //}
  314. //public interface IRemoteUpdate
  315. //{
  316. //}
  317. //public interface IRemoteDelete
  318. //{
  319. //}
  320. public interface ISequenceable
  321. {
  322. long Sequence { get; set; }
  323. }
  324. public interface IAutoIncrement<T, TType>
  325. {
  326. Expression<Func<T, TType>> AutoIncrementField();
  327. Filter<T>? AutoIncrementFilter();
  328. }
  329. public interface INumericAutoIncrement<T> : IAutoIncrement<T, int>
  330. {
  331. }
  332. public interface IStringAutoIncrement
  333. {
  334. string AutoIncrementPrefix();
  335. string AutoIncrementFormat();
  336. }
  337. public interface IStringAutoIncrement<T> : IAutoIncrement<T, string>, IStringAutoIncrement
  338. {
  339. }
  340. /// <summary>
  341. /// 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
  342. /// entity relationships.
  343. /// </summary>
  344. /// <remarks>
  345. /// This will cause a ManyToMany grid of <typeparamref name="TRight"/> to appear on all <typeparamref name="TLeft"/> editors.
  346. /// 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>
  347. /// vice versa, one must flag the entity with both <c>IManyToMany&lt;<typeparamref name="TLeft"/>, <typeparamref name="TRight"/>&gt;</c> and
  348. /// <c>IManyToMany&lt;<typeparamref name="TRight"/>, <typeparamref name="TLeft"/>&gt;</c>.
  349. /// </remarks>
  350. /// <typeparam name="TLeft"></typeparam>
  351. /// <typeparam name="TRight"></typeparam>
  352. public interface IManyToMany<TLeft, TRight> where TLeft : Entity where TRight : Entity
  353. {
  354. }
  355. public interface IOneToMany<TOne> where TOne : Entity
  356. {
  357. }
  358. public static class EntityFactory
  359. {
  360. public delegate object ObjectActivator(params object[] args);
  361. private static readonly Dictionary<Type, ObjectActivator> _cache = new Dictionary<Type, ObjectActivator>();
  362. public static ObjectActivator GetActivator<T>(ConstructorInfo ctor)
  363. {
  364. var type = ctor.DeclaringType;
  365. var paramsInfo = ctor.GetParameters();
  366. //create a single param of type object[]
  367. var param =
  368. Expression.Parameter(typeof(object[]), "args");
  369. var argsExp =
  370. new Expression[paramsInfo.Length];
  371. //pick each arg from the params array
  372. //and create a typed expression of them
  373. for (var i = 0; i < paramsInfo.Length; i++)
  374. {
  375. Expression index = Expression.Constant(i);
  376. var paramType = paramsInfo[i].ParameterType;
  377. Expression paramAccessorExp =
  378. Expression.ArrayIndex(param, index);
  379. Expression paramCastExp =
  380. Expression.Convert(paramAccessorExp, paramType);
  381. argsExp[i] = paramCastExp;
  382. }
  383. //make a NewExpression that calls the
  384. //ctor with the args we just created
  385. var newExp = Expression.New(ctor, argsExp);
  386. //create a lambda with the New
  387. //Expression as body and our param object[] as arg
  388. var lambda =
  389. Expression.Lambda(typeof(ObjectActivator), newExp, param);
  390. //compile it
  391. var compiled = (ObjectActivator)lambda.Compile();
  392. return compiled;
  393. }
  394. public static T CreateEntity<T>() where T : BaseObject
  395. {
  396. if (!_cache.ContainsKey(typeof(T)))
  397. {
  398. var ctor = typeof(T).GetConstructors().First();
  399. _cache[typeof(T)] = GetActivator<T>(ctor);
  400. }
  401. var createdActivator = _cache[typeof(T)];
  402. return (T)createdActivator();
  403. }
  404. public static object CreateEntity(Type type)
  405. {
  406. if (!_cache.ContainsKey(type))
  407. {
  408. var ctor = type.GetConstructors().First();
  409. var activator = typeof(EntityFactory).GetMethod("GetActivator").MakeGenericMethod(type);
  410. _cache[type] = (ObjectActivator)activator.Invoke(null, new object[] { ctor });
  411. }
  412. var createdActivator = _cache[type];
  413. return createdActivator();
  414. }
  415. }
  416. }