Entity.cs 15 KB

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