Entity.cs 17 KB

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