DynamicOneToManyGrid.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media.Imaging;
  11. using InABox.Clients;
  12. using InABox.Configuration;
  13. using InABox.Core;
  14. using InABox.Core.Reports;
  15. using InABox.WPF;
  16. namespace InABox.DynamicGrid
  17. {
  18. public interface IDynamicOneToManyGrid<TOne, TMany> : IDynamicEditorPage
  19. {
  20. List<TMany> Items { get; }
  21. void LoadItems(TMany[] items);
  22. }
  23. public class DynamicOneToManyGrid<TOne, TMany> : DynamicGrid<TMany>, IDynamicEditorPage, IDynamicOneToManyGrid<TOne, TMany>
  24. where TOne : Entity, new() where TMany : Entity, IPersistent, IRemotable, new()
  25. {
  26. private TMany[] MasterList = Array.Empty<TMany>();
  27. private readonly PropertyInfo property;
  28. public PageType PageType => PageType.Other;
  29. protected DynamicGridCustomColumnsComponent<TMany> ColumnsComponent;
  30. public DynamicOneToManyGrid()
  31. {
  32. Ready = false;
  33. Items = new List<TMany>();
  34. Criteria = new Filters<TMany>();
  35. property = CoreUtils.GetOneToManyProperty(typeof(TMany), typeof(TOne));
  36. ColumnsComponent = new DynamicGridCustomColumnsComponent<TMany>(this, GetTag());
  37. }
  38. protected override void Init()
  39. {
  40. }
  41. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  42. {
  43. options.BeginUpdate();
  44. options.Add(DynamicGridOption.RecordCount)
  45. .Add(DynamicGridOption.SelectColumns);
  46. if (Security.CanEdit<TMany>() && !ReadOnly)
  47. options.Add(DynamicGridOption.AddRows).Add(DynamicGridOption.EditRows);
  48. if (Security.CanDelete<TMany>() && !ReadOnly)
  49. options.Add(DynamicGridOption.DeleteRows);
  50. if (Security.CanImport<TMany>() && !ReadOnly)
  51. options.Add(DynamicGridOption.ImportData);
  52. if (Security.CanExport<TMany>())
  53. options.Add(DynamicGridOption.ExportData);
  54. if (Security.CanMerge<TMany>())
  55. options.Add(DynamicGridOption.MultiSelect);
  56. options.EndUpdate();
  57. }
  58. private bool _readOnly;
  59. public bool ReadOnly
  60. {
  61. get => _readOnly;
  62. set
  63. {
  64. if(_readOnly != value)
  65. {
  66. _readOnly = value;
  67. Reconfigure();
  68. }
  69. }
  70. }
  71. private static bool IsAutoEntity => typeof(TMany).HasAttribute<AutoEntity>();
  72. protected Filters<TMany> Criteria { get; } = new Filters<TMany>();
  73. public TOne Item { get; protected set; }
  74. public bool Ready { get; set; }
  75. public DynamicEditorGrid EditorGrid { get; set; }
  76. public string Caption()
  77. {
  78. var caption = typeof(TMany).GetCustomAttribute(typeof(Caption));
  79. if (caption != null)
  80. return ((Caption)caption).Text;
  81. var result = new Inflector.Inflector(new CultureInfo("en")).Pluralize(typeof(TMany).Name);
  82. return result;
  83. }
  84. public virtual int Order()
  85. {
  86. return int.MinValue;
  87. }
  88. public virtual void Load(object item, Func<Type, CoreTable?>? PageDataHandler)
  89. {
  90. Reconfigure();
  91. Item = (TOne)item;
  92. var data = PageDataHandler?.Invoke(typeof(TMany));
  93. if (data == null)
  94. {
  95. if (Item.ID == Guid.Empty)
  96. {
  97. data = new CoreTable();
  98. data.LoadColumns(typeof(TMany));
  99. }
  100. else
  101. {
  102. var criteria = new Filters<TMany>();
  103. var exp = CoreUtils.GetPropertyExpression<TMany>(property.Name + ".ID");
  104. criteria.Add(new Filter<TMany>(exp).IsEqualTo(Item.ID).And(exp).IsNotEqualTo(Guid.Empty));
  105. criteria.AddRange(Criteria.Items);
  106. var sort = LookupFactory.DefineSort<TMany>();
  107. data = new Client<TMany>().Query(criteria.Combine(), null, sort);
  108. }
  109. }
  110. MasterList = data.Rows.Select(x => x.ToObject<TMany>()).ToArray();
  111. Items = MasterList.ToList();
  112. Refresh(true, true);
  113. Ready = true;
  114. }
  115. public virtual void BeforeSave(object item)
  116. {
  117. // Don't need to do anything here
  118. }
  119. protected virtual void OnDeleteItem(TMany item)
  120. {
  121. if (IsAutoEntity)
  122. {
  123. return;
  124. }
  125. new Client<TMany>().Delete(item, typeof(TMany).Name + " Deleted by User");
  126. }
  127. public void AfterSave(object item)
  128. {
  129. if (IsAutoEntity)
  130. {
  131. return;
  132. }
  133. // First remove any deleted files
  134. foreach (var map in MasterList)
  135. if (!Items.Contains(map))
  136. OnDeleteItem(map);
  137. foreach (var map in Items)
  138. {
  139. var prop = (property.GetValue(map) as IEntityLink)!;
  140. prop.ID = Item.ID;
  141. prop.Synchronise(Item);
  142. }
  143. new Client<TMany>().Save(Items.Where(x => x.IsChanged()), "Updated by User");
  144. }
  145. protected override CoreTable LoadImportKeys(string[] fields)
  146. {
  147. var result = base.LoadImportKeys(fields);
  148. result.LoadRows(MasterList);
  149. return result;
  150. }
  151. protected override bool CustomiseImportItem(TMany item)
  152. {
  153. var result = base.CustomiseImportItem(item);
  154. if (result)
  155. {
  156. var prop = (property.GetValue(item) as IEntityLink)!;
  157. prop.ID = Item.ID;
  158. prop.Synchronise(Item);
  159. }
  160. return result;
  161. }
  162. public Size MinimumSize()
  163. {
  164. return new Size(400, 400);
  165. }
  166. public List<TMany> Items { get; private set; }
  167. public void LoadItems(TMany[] items)
  168. {
  169. Items.Clear();
  170. Items.AddRange(items);
  171. Refresh(false, true);
  172. }
  173. private static string GetTag()
  174. {
  175. return typeof(TOne).Name + "." + typeof(TMany).Name;
  176. }
  177. protected override DynamicGridColumns LoadColumns()
  178. {
  179. return ColumnsComponent.LoadColumns();
  180. }
  181. protected override void SaveColumns(DynamicGridColumns columns)
  182. {
  183. ColumnsComponent.SaveColumns(columns);
  184. }
  185. protected override void LoadColumnsMenu(ContextMenu menu)
  186. {
  187. base.LoadColumnsMenu(menu);
  188. ColumnsComponent.LoadColumnsMenu(menu);
  189. }
  190. protected override DynamicGridSettings LoadSettings()
  191. {
  192. var tag = GetTag();
  193. var user = Task.Run(() => new UserConfiguration<DynamicGridSettings>(tag).Load());
  194. user.Wait();
  195. return user.Result;
  196. }
  197. protected override void SaveSettings(DynamicGridSettings settings)
  198. {
  199. var tag = GetTag();
  200. new UserConfiguration<DynamicGridSettings>(tag).Save(settings);
  201. }
  202. protected override TMany CreateItem()
  203. {
  204. var result = new TMany();
  205. var prop = (property.GetValue(result) as IEntityLink)!;
  206. prop.ID = Item.ID;
  207. prop.Synchronise(Item);
  208. return result;
  209. }
  210. protected override TMany LoadItem(CoreRow row)
  211. {
  212. return Items[_recordmap[row].Index];
  213. }
  214. protected override TMany[] LoadItems(CoreRow[] rows)
  215. {
  216. var result = new List<TMany>();
  217. foreach (var row in rows)
  218. result.Add(LoadItem(row));
  219. return result.ToArray();
  220. }
  221. public override void SaveItem(TMany item)
  222. {
  223. if (!Items.Contains(item))
  224. Items.Add(item);
  225. if (item is ISequenceable) Items = Items.AsQueryable().OrderBy(x => (x as ISequenceable)!.Sequence).ToList();
  226. }
  227. protected override void DeleteItems(params CoreRow[] rows)
  228. {
  229. var items = rows.Select(LoadItem).ToList();
  230. foreach(var item in items)
  231. {
  232. Items.Remove(item);
  233. }
  234. }
  235. protected override void Reload(Filters<TMany> criteria, Columns<TMany> columns, ref SortOrder<TMany>? sort,
  236. Action<CoreTable?, Exception?> action)
  237. {
  238. var results = new CoreTable();
  239. results.LoadColumns(typeof(TMany));
  240. if (sort != null)
  241. {
  242. var exp = IQueryableExtensions.ToLambda<TMany>(sort.Expression);
  243. var sorted = sort.Direction == SortDirection.Ascending
  244. ? Items.AsQueryable().OrderBy(exp)
  245. : Items.AsQueryable().OrderByDescending(exp);
  246. foreach (var then in sort.Thens)
  247. {
  248. var thexp = IQueryableExtensions.ToLambda<TMany>(then.Expression);
  249. sorted = sort.Direction == SortDirection.Ascending ? sorted.ThenBy(exp) : sorted.ThenByDescending(exp);
  250. }
  251. Items = sorted.ToList();
  252. }
  253. results.LoadRows(Items);
  254. action.Invoke(results, null);
  255. }
  256. protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
  257. {
  258. var type = CoreUtils.GetProperty(typeof(TMany), column.ColumnName).DeclaringType;
  259. if (type.GetInterfaces().Contains(typeof(IEntityLink)) && type.ContainsInheritedGenericType(typeof(TOne)))
  260. return new NullEditor();
  261. return base.GetEditor(item, column);
  262. }
  263. public override DynamicEditorPages LoadEditorPages(TMany item)
  264. {
  265. return item.ID != Guid.Empty ? base.LoadEditorPages(item) : new DynamicEditorPages();
  266. }
  267. protected override bool BeforePaste(IEnumerable<TMany> items, ClipAction action)
  268. {
  269. if(action == ClipAction.Copy)
  270. {
  271. foreach(var item in items)
  272. {
  273. item.ID = Guid.Empty;
  274. }
  275. }
  276. return base.BeforePaste(items, action);
  277. }
  278. }
  279. }