DynamicOneToManyGrid.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media.Imaging;
  12. using InABox.Clients;
  13. using InABox.Configuration;
  14. using InABox.Core;
  15. using InABox.Core.Reports;
  16. using InABox.WPF;
  17. namespace InABox.DynamicGrid;
  18. public interface IDynamicOneToManyGrid<TOne, TMany> : IDynamicEditorPage
  19. {
  20. void LoadItems(TMany[] items);
  21. }
  22. public class DynamicOneToManyGrid<TOne, TMany> : DynamicGrid<TMany>,
  23. IDynamicEditorPage,
  24. IDynamicOneToManyGrid<TOne, TMany>
  25. where TOne : Entity, new() where TMany : Entity, IPersistent, IRemotable, new()
  26. {
  27. private readonly PropertyInfo property;
  28. protected DynamicGridCustomColumnsComponent<TMany> ColumnsComponent;
  29. public DynamicOneToManyGrid()
  30. {
  31. Ready = false;
  32. Criteria = new Filters<TMany>();
  33. property = CoreUtils.GetOneToManyProperty(typeof(TMany), typeof(TOne));
  34. AddHiddenColumn(property.Name + "." + nameof(IEntityLink.ID));
  35. foreach (var col in LookupFactory.RequiredColumns<TMany>())
  36. HiddenColumns.Add(col);
  37. ColumnsComponent = new DynamicGridCustomColumnsComponent<TMany>(this, GetTag());
  38. DataComponent = new DynamicGridMemoryEntityDataComponent<TMany>(this);
  39. base.DataComponent = DataComponent;
  40. }
  41. protected new DynamicGridMemoryEntityDataComponent<TMany> DataComponent;
  42. protected override void Init()
  43. {
  44. }
  45. protected override void DoReconfigure(DynamicGridOptions options)
  46. {
  47. options.RecordCount = true;
  48. options.SelectColumns = true;
  49. if (Security.CanEdit<TMany>() && !ReadOnly)
  50. {
  51. options.AddRows = true;
  52. options.EditRows = true;
  53. }
  54. if (Security.CanDelete<TMany>() && !ReadOnly)
  55. options.DeleteRows = true;
  56. if (Security.CanImport<TMany>() && !ReadOnly)
  57. options.ImportData = true;
  58. if (Security.CanExport<TMany>())
  59. options.ExportData = true;
  60. if (Security.CanMerge<TMany>())
  61. options.MultiSelect = true;
  62. }
  63. private static bool IsAutoEntity => typeof(TMany).HasAttribute<AutoEntity>();
  64. protected Filters<TMany> Criteria { get; } = new Filters<TMany>();
  65. public TOne Item { get; protected set; }
  66. public void LoadItems(TMany[] items)
  67. {
  68. DataComponent.LoadData(items);
  69. Refresh(false, true);
  70. }
  71. private static string GetTag()
  72. {
  73. return typeof(TOne).Name + "." + typeof(TMany).Name;
  74. }
  75. #region IDynamicEditorPage
  76. public DynamicEditorGrid EditorGrid { get; set; }
  77. public PageType PageType => PageType.Other;
  78. public bool Ready { get; set; }
  79. private bool _readOnly;
  80. public bool ReadOnly
  81. {
  82. get => _readOnly;
  83. set
  84. {
  85. if (_readOnly != value)
  86. {
  87. _readOnly = value;
  88. Reconfigure();
  89. }
  90. }
  91. }
  92. public virtual void Load(object item, Func<Type, CoreTable?>? PageDataHandler)
  93. {
  94. Reconfigure();
  95. Item = (TOne)item;
  96. Refresh(true, false);
  97. var data = PageDataHandler?.Invoke(typeof(TMany));
  98. if(data is not null)
  99. {
  100. DataComponent.LoadData(data);
  101. }
  102. {
  103. if (Item.ID == Guid.Empty)
  104. {
  105. data = new CoreTable();
  106. data.LoadColumns(typeof(TMany));
  107. DataComponent.LoadData(data);
  108. }
  109. else
  110. {
  111. var criteria = new Filters<TMany>();
  112. var exp = CoreUtils.GetPropertyExpression<TMany>(property.Name + ".ID");
  113. criteria.Add(new Filter<TMany>(exp).IsEqualTo(Item.ID).And(exp).IsNotEqualTo(Guid.Empty));
  114. criteria.AddRange(Criteria.Items);
  115. var sort = LookupFactory.DefineSort<TMany>();
  116. var columns = DynamicGridUtils.LoadEditorColumns(DataColumns());
  117. DataComponent.LoadData(criteria.Combine(), columns, sort);
  118. }
  119. }
  120. Refresh(false, true);
  121. Ready = true;
  122. }
  123. public virtual void BeforeSave(object item)
  124. {
  125. // Don't need to do anything here
  126. }
  127. public virtual void AfterSave(object item)
  128. {
  129. foreach (var map in DataComponent.Items)
  130. {
  131. var prop = (property.GetValue(map) as IEntityLink)!;
  132. prop.ID = Item.ID;
  133. prop.Synchronise(Item);
  134. }
  135. DataComponent.SaveItems();
  136. }
  137. public Size MinimumSize()
  138. {
  139. return new Size(400, 400);
  140. }
  141. public string Caption()
  142. {
  143. var caption = typeof(TMany).GetCustomAttribute(typeof(Caption));
  144. if (caption != null)
  145. return ((Caption)caption).Text;
  146. var result = new Inflector.Inflector(new CultureInfo("en")).Pluralize(typeof(TMany).Name);
  147. return result;
  148. }
  149. public virtual int Order()
  150. {
  151. return int.MinValue;
  152. }
  153. #endregion
  154. #region DynamicGrid
  155. protected override bool CustomiseImportItem(TMany item)
  156. {
  157. var result = base.CustomiseImportItem(item);
  158. if (result)
  159. {
  160. var prop = (property.GetValue(item) as IEntityLink)!;
  161. prop.ID = Item.ID;
  162. prop.Synchronise(Item);
  163. }
  164. return result;
  165. }
  166. public override DynamicGridColumns GenerateColumns()
  167. {
  168. var cols = new DynamicGridColumns();
  169. cols.AddRange(base.GenerateColumns().Where(x => !x.ColumnName.StartsWith(property.Name + ".")));
  170. return cols;
  171. }
  172. protected override DynamicGridColumns LoadColumns()
  173. {
  174. return ColumnsComponent.LoadColumns();
  175. }
  176. protected override void SaveColumns(DynamicGridColumns columns)
  177. {
  178. ColumnsComponent.SaveColumns(columns);
  179. }
  180. protected override void LoadColumnsMenu(ContextMenu menu)
  181. {
  182. base.LoadColumnsMenu(menu);
  183. ColumnsComponent.LoadColumnsMenu(menu);
  184. }
  185. protected override DynamicGridSettings LoadSettings()
  186. {
  187. var tag = GetTag();
  188. var user = Task.Run(() => new UserConfiguration<DynamicGridSettings>(tag).Load());
  189. user.Wait();
  190. return user.Result;
  191. }
  192. protected override void SaveSettings(DynamicGridSettings settings)
  193. {
  194. var tag = GetTag();
  195. new UserConfiguration<DynamicGridSettings>(tag).Save(settings);
  196. }
  197. public override TMany CreateItem()
  198. {
  199. var result = new TMany();
  200. var prop = (property.GetValue(result) as IEntityLink)!;
  201. prop.ID = Item.ID;
  202. prop.Synchronise(Item);
  203. return result;
  204. }
  205. protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
  206. {
  207. var type = CoreUtils.GetProperty(typeof(TMany), column.ColumnName).DeclaringType;
  208. if (type.GetInterfaces().Contains(typeof(IEntityLink)) && type.ContainsInheritedGenericType(typeof(TOne)))
  209. return new NullEditor();
  210. return base.GetEditor(item, column);
  211. }
  212. public override DynamicEditorPages LoadEditorPages(TMany item)
  213. {
  214. return item.ID != Guid.Empty ? base.LoadEditorPages(item) : new DynamicEditorPages();
  215. }
  216. #endregion
  217. }