Entity.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. public class RequiredColumnAttribute : Attribute { }
  164. public abstract class Entity : BaseObject, IEntity
  165. {
  166. private bool bTaxing;
  167. //public String Name { get; set; }
  168. [TimestampEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  169. [RequiredColumn]
  170. public virtual DateTime LastUpdate { get; set; } = DateTime.Now;
  171. [CodeEditor(Visible = Visible.Optional, Editable = Editable.Hidden)]
  172. [RequiredColumn]
  173. public string LastUpdateBy { get; set; } = ClientFactory.UserID;
  174. [NullEditor]
  175. [RequiredColumn]
  176. public virtual DateTime Created { get; set; } = DateTime.Now;
  177. [NullEditor]
  178. [RequiredColumn]
  179. public virtual string CreatedBy { get; set; } = ClientFactory.UserID;
  180. [NullEditor]
  181. [RequiredColumn]
  182. public Guid ID { get; set; } = Guid.Empty;
  183. /// <summary>
  184. /// If the entity is deleted, holds the ID of the Deletion. Otherwise, it holds Guid.Empty
  185. /// </summary>
  186. [NullEditor]
  187. [DoNotSerialize]
  188. [Obsolete]
  189. public Guid Deleted { get; set; } = Guid.Empty;
  190. public static Type ClassVersion(Type t)
  191. {
  192. //Type t = MethodBase.GetCurrentMethod().DeclaringType;
  193. var ti = t.GetTypeInfo();
  194. var interfaces = ti.GetInterfaces();
  195. if (ti.GetInterfaces().Contains(typeof(IPersistent)))
  196. {
  197. if (ti.BaseType != null)
  198. throw new Exception(t.Name + " hase no Base Type");
  199. if (ti.BaseType.Equals(typeof(Entity)))
  200. throw new Exception(t.Name + " may not derive directly from TEntity");
  201. var props = t.GetTypeInfo().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
  202. if (props.Count() > 0)
  203. throw new Exception(t.Name + "may not declare properties");
  204. }
  205. return t.GetTypeInfo().BaseType;
  206. }
  207. //[NullEditor]
  208. //public List<EntityHistory> History { get; set; }
  209. //public Entity() : base()
  210. //{
  211. // CommitChanges();
  212. //}
  213. //public Entity(Guid id) : base()
  214. //{
  215. // ID = id;
  216. // History = new List<EntityHistory>();
  217. // UserProperties = new Dictionary<string, Object>();
  218. // DataModel.InitializeEntity(this);
  219. // CheckSequence();
  220. // CommitChanges();
  221. //}
  222. public static bool IsEntityLinkValid<T, U>(Expression<Func<T, U>> expression, CoreRow arg) where U : IEntityLink
  223. {
  224. return arg.IsEntityLinkValid(expression);
  225. }
  226. /// <summary>
  227. /// 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)"/>)
  228. /// </summary>
  229. /// <typeparam name="T">The entity type</typeparam>
  230. /// <typeparam name="U">The entity link type</typeparam>
  231. /// <param name="expression">An expression to the entity link of type <typeparamref name="U"/></param>
  232. /// <param name="arg">The row representing the entity of type <typeparamref name="T"/></param>
  233. /// <returns>The ID on the entity link, or <c>null</c> if the entity link is invalid</returns>
  234. public static Guid? EntityLinkID<T, U>(Expression<Func<T, U>> expression, CoreRow arg) where U : IEntityLink
  235. {
  236. var col = CoreUtils.GetFullPropertyName(expression, ".");
  237. var id = arg.Get<Guid>(col + ".ID");
  238. if (id != Guid.Empty && arg.Get<Guid>(col + ".Deleted") == Guid.Empty)
  239. return id;
  240. return null;
  241. }
  242. protected override void SetChanged(string name, object? before, object? after)
  243. {
  244. base.SetChanged(name, before, after);
  245. CheckTax(name, before, after);
  246. }
  247. private void CheckTax(string name, object? before, object? after)
  248. {
  249. if (this is ITaxable taxable && !bTaxing)
  250. {
  251. bTaxing = true;
  252. try
  253. {
  254. if (name.Equals("ExTax"))
  255. {
  256. taxable.Tax = (double)after * (taxable.TaxRate / 100.0F);
  257. taxable.IncTax = (double)after + taxable.Tax;
  258. }
  259. else if (name.Equals("TaxRate"))
  260. {
  261. taxable.Tax = taxable.ExTax * ((double)after / 100.0F);
  262. taxable.IncTax = taxable.ExTax + taxable.Tax;
  263. }
  264. else if (name.Equals("Tax"))
  265. {
  266. taxable.ExTax = taxable.IncTax - (double)after;
  267. }
  268. else if (name.Equals("IncTax"))
  269. {
  270. taxable.ExTax = (double)after / ((100.0F + taxable.TaxRate) / 100.0F);
  271. taxable.Tax = (double)after - taxable.ExTax;
  272. }
  273. }
  274. catch (Exception e)
  275. {
  276. Logger.Send(LogType.Error, "", String.Join("\n",e.Message,e.StackTrace));
  277. }
  278. bTaxing = false;
  279. }
  280. }
  281. protected override void DoPropertyChanged(string name, object? before, object? after)
  282. {
  283. if (!IsObserving())
  284. return;
  285. //CheckSequence();
  286. if (!name.Equals("LastUpdate"))
  287. LastUpdate = DateTime.Now;
  288. LastUpdateBy = ClientFactory.UserID;
  289. // This doesn;t work - keeps being updated to current date
  290. // Created => null ::Set ID = guid.empty -> now :: any other change -> unchanged!
  291. // Moved to Create(), should not simply be overwritten on deserialise from json
  292. //if (Created.Equals(DateTime.MinValue))
  293. //{
  294. // Created = DateTime.Now;
  295. // CreatedBy = ClientFactory.UserID;
  296. //}
  297. }
  298. }
  299. public interface ILicense<TLicenseToken> where TLicenseToken : LicenseToken
  300. {
  301. }
  302. public interface IPersistent
  303. {
  304. }
  305. public interface IRemotable
  306. {
  307. }
  308. //public interface IRemoteQuery
  309. //{
  310. //}
  311. //public interface IRemoteUpdate
  312. //{
  313. //}
  314. //public interface IRemoteDelete
  315. //{
  316. //}
  317. public interface ISequenceable
  318. {
  319. long Sequence { get; set; }
  320. }
  321. public interface IAutoIncrement<T, TType>
  322. {
  323. Expression<Func<T, TType>> AutoIncrementField();
  324. Filter<T>? AutoIncrementFilter();
  325. }
  326. public interface INumericAutoIncrement<T> : IAutoIncrement<T, int>
  327. {
  328. }
  329. public interface IStringAutoIncrement
  330. {
  331. string AutoIncrementPrefix();
  332. string AutoIncrementFormat();
  333. }
  334. public interface IStringAutoIncrement<T> : IAutoIncrement<T, string>, IStringAutoIncrement
  335. {
  336. }
  337. /// <summary>
  338. /// 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
  339. /// entity relationships.
  340. /// </summary>
  341. /// <remarks>
  342. /// This will cause a ManyToMany grid of <typeparamref name="TRight"/> to appear on all <typeparamref name="TLeft"/> editors.
  343. /// 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>
  344. /// vice versa, one must flag the entity with both <c>IManyToMany&lt;<typeparamref name="TLeft"/>, <typeparamref name="TRight"/>&gt;</c> and
  345. /// <c>IManyToMany&lt;<typeparamref name="TRight"/>, <typeparamref name="TLeft"/>&gt;</c>.
  346. /// </remarks>
  347. /// <typeparam name="TLeft"></typeparam>
  348. /// <typeparam name="TRight"></typeparam>
  349. public interface IManyToMany<TLeft, TRight> where TLeft : Entity where TRight : Entity
  350. {
  351. }
  352. public interface IOneToMany<TOne> where TOne : Entity
  353. {
  354. }
  355. public static class EntityFactory
  356. {
  357. public delegate object ObjectActivator(params object[] args);
  358. private static readonly Dictionary<Type, ObjectActivator> _cache = new Dictionary<Type, ObjectActivator>();
  359. public static ObjectActivator GetActivator<T>(ConstructorInfo ctor)
  360. {
  361. var type = ctor.DeclaringType;
  362. var paramsInfo = ctor.GetParameters();
  363. //create a single param of type object[]
  364. var param =
  365. Expression.Parameter(typeof(object[]), "args");
  366. var argsExp =
  367. new Expression[paramsInfo.Length];
  368. //pick each arg from the params array
  369. //and create a typed expression of them
  370. for (var i = 0; i < paramsInfo.Length; i++)
  371. {
  372. Expression index = Expression.Constant(i);
  373. var paramType = paramsInfo[i].ParameterType;
  374. Expression paramAccessorExp =
  375. Expression.ArrayIndex(param, index);
  376. Expression paramCastExp =
  377. Expression.Convert(paramAccessorExp, paramType);
  378. argsExp[i] = paramCastExp;
  379. }
  380. //make a NewExpression that calls the
  381. //ctor with the args we just created
  382. var newExp = Expression.New(ctor, argsExp);
  383. //create a lambda with the New
  384. //Expression as body and our param object[] as arg
  385. var lambda =
  386. Expression.Lambda(typeof(ObjectActivator), newExp, param);
  387. //compile it
  388. var compiled = (ObjectActivator)lambda.Compile();
  389. return compiled;
  390. }
  391. public static T CreateEntity<T>() where T : BaseObject
  392. {
  393. if (!_cache.ContainsKey(typeof(T)))
  394. {
  395. var ctor = typeof(T).GetConstructors().First();
  396. _cache[typeof(T)] = GetActivator<T>(ctor);
  397. }
  398. var createdActivator = _cache[typeof(T)];
  399. return (T)createdActivator();
  400. }
  401. public static object CreateEntity(Type type)
  402. {
  403. if (!_cache.ContainsKey(type))
  404. {
  405. var ctor = type.GetConstructors().First();
  406. var activator = typeof(EntityFactory).GetMethod("GetActivator").MakeGenericMethod(type);
  407. _cache[type] = (ObjectActivator)activator.Invoke(null, new object[] { ctor });
  408. }
  409. var createdActivator = _cache[type];
  410. return createdActivator();
  411. }
  412. }
  413. }