DynamicOneToManyGrid.cs 11 KB

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