DynamicManyToManyCrossTab.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using InABox.WPF;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Controls;
  12. using System.Windows.Media.Imaging;
  13. namespace InABox.DynamicGrid;
  14. public abstract class DynamicManyToManyCrossTab<TManyToMany, TRow, TColumn> : DynamicGrid<TRow>
  15. where TManyToMany : Entity, IRemotable, IPersistent, new()
  16. where TRow : Entity, IRemotable, IPersistent, new()
  17. where TColumn : Entity, IRemotable, IPersistent, new()
  18. {
  19. private static readonly BitmapImage tick = Wpf.Resources.tick.AsBitmapImage();
  20. /// <summary>
  21. /// Property on <typeparamref name="TManyToMany"/> which is an <see cref="EntityLink{T}"/> for <typeparamref name="TRow"/>.
  22. /// </summary>
  23. private readonly PropertyInfo rowProperty;
  24. /// <summary>
  25. /// Property on <typeparamref name="TManyToMany"/> which is an <see cref="EntityLink{T}"/> for <typeparamref name="TColumn"/>.
  26. /// </summary>
  27. private readonly PropertyInfo columnProperty;
  28. private CoreTable? ColumnData;
  29. /// <summary>
  30. /// {ColumnID : { RowID }}
  31. /// </summary>
  32. private Dictionary<Guid, Dictionary<Guid, TManyToMany>>? ManyToManyData;
  33. protected new CrossTabDataComponent DataComponent;
  34. public DynamicManyToManyCrossTab()
  35. {
  36. rowProperty = CoreUtils.GetManyToManyThisProperty(typeof(TManyToMany), typeof(TRow));
  37. columnProperty = CoreUtils.GetManyToManyThisProperty(typeof(TManyToMany), typeof(TColumn));
  38. HeaderHeight = 125;
  39. DataComponent = new CrossTabDataComponent(this);
  40. base.DataComponent = DataComponent;
  41. }
  42. protected override void Init()
  43. {
  44. }
  45. protected override void DoReconfigure(DynamicGridOptions options)
  46. {
  47. options.Clear();
  48. }
  49. /// <summary>
  50. /// Load the required columns for <typeparamref name="TRow"/>.
  51. /// </summary>
  52. protected abstract DynamicGridColumns LoadRowColumns();
  53. protected abstract Columns<TColumn>? LoadColumnColumns();
  54. protected abstract SortOrder<TColumn>? LoadColumnSort();
  55. protected abstract string FormatColumnHeader(CoreRow row);
  56. protected virtual Filter<TRow>? RowFilter() => null;
  57. protected virtual Filter<TColumn>? ColumnFilter() => null;
  58. protected override DynamicGridColumns LoadColumns()
  59. {
  60. var columns = LoadRowColumns();
  61. var client = Client.Create(typeof(TColumn));
  62. var columnColumns = Columns.None<TColumn>().Add(x => x.ID);
  63. if(LoadColumnColumns() is Columns<TColumn> extra)
  64. {
  65. foreach(var col in extra)
  66. {
  67. columnColumns.Add(col);
  68. }
  69. }
  70. ColumnData = Client.Query(ColumnFilter(), columnColumns, LoadColumnSort());
  71. ActionColumns.Clear();
  72. foreach(var columnRow in ColumnData.Rows)
  73. {
  74. var colID = columnRow.Get<TColumn, Guid>(x => x.ID);
  75. ActionColumns.Add(new DynamicImageColumn(
  76. (row) =>
  77. {
  78. if (row is null || ManyToManyData is null) return null;
  79. if(ManyToManyData.TryGetValue(colID, out var rowSet))
  80. {
  81. var rowID = row.Get<TRow, Guid>(x => x.ID);
  82. if (rowSet.ContainsKey(rowID))
  83. {
  84. return tick;
  85. }
  86. }
  87. return null;
  88. },
  89. (row) =>
  90. {
  91. if (row is null) return false;
  92. var rowID = row.Get<TRow, Guid>(x => x.ID);
  93. return CellClick(colID, rowID);
  94. }
  95. )
  96. {
  97. HeaderText = FormatColumnHeader(columnRow),
  98. ContextMenu = (rows) =>
  99. {
  100. var row = rows?.FirstOrDefault();
  101. if (row is null) return null;
  102. var rowID = row.Get<TRow, Guid>(x => x.ID);
  103. return CellMenu(colID, rowID);
  104. }
  105. });
  106. }
  107. return columns;
  108. }
  109. private ContextMenu? CellMenu(Guid colID, Guid rowID)
  110. {
  111. if (ManyToManyData is null) return null;
  112. var menu = new ContextMenu();
  113. if (ManyToManyData.TryGetValue(colID, out var rowSet)
  114. && rowSet.TryGetValue(rowID, out var obj))
  115. {
  116. if (Security.CanEdit<TManyToMany>() && CanEditCell(obj))
  117. {
  118. menu.AddItem("Edit Item", Wpf.Resources.pencil, obj, (obj) =>
  119. {
  120. if (EditCell(obj))
  121. {
  122. Refresh(false, true);
  123. }
  124. });
  125. }
  126. if (Security.CanDelete<TManyToMany>())
  127. {
  128. menu.AddItem("Delete Item", Wpf.Resources.delete, obj, DeleteCell);
  129. }
  130. }
  131. else
  132. {
  133. if (Security.CanEdit<TManyToMany>())
  134. {
  135. menu.AddItem("Create Item", Wpf.Resources.add, (colID, rowID), CreateCell);
  136. }
  137. }
  138. if(menu.Items.Count > 0)
  139. {
  140. return menu;
  141. }
  142. else
  143. {
  144. return null;
  145. }
  146. }
  147. private void CreateCell((Guid colID, Guid rowID) obj)
  148. {
  149. var manyToMany = CreateManyToMany(obj.rowID, obj.colID);
  150. if (SaveManyToMany(manyToMany))
  151. {
  152. Refresh(false, true);
  153. }
  154. }
  155. private void DeleteCell(TManyToMany obj)
  156. {
  157. if (DeleteManyToMany(obj))
  158. {
  159. Refresh(false, true);
  160. }
  161. }
  162. protected virtual bool CanEditCell(TManyToMany obj) => false;
  163. /// <summary>
  164. /// Code to edit a <typeparamref name="TManyToMany"/> cell; note that this <b>must not</b> allow for changing either
  165. /// the <typeparamref name="TColumn"/> or the <typeparamref name="TRow"/>, otherwise we would easily get duplicate <typeparamref name="TManyToMany"/>s.
  166. /// </summary>
  167. /// <remarks>
  168. /// This method should also save the object.
  169. /// </remarks>
  170. /// <param name="obj"></param>
  171. protected virtual bool EditCell(TManyToMany obj)
  172. {
  173. return false;
  174. }
  175. private bool CellClick(Guid columnID, Guid rowID)
  176. {
  177. if (ManyToManyData is null) return false;
  178. if (ManyToManyData.TryGetValue(columnID, out var rowSet)
  179. && rowSet.TryGetValue(rowID, out var obj))
  180. {
  181. if (Security.CanDelete<TManyToMany>())
  182. {
  183. return DeleteManyToMany(obj);
  184. }
  185. else
  186. {
  187. return false;
  188. }
  189. }
  190. else
  191. {
  192. if (Security.CanEdit<TManyToMany>())
  193. {
  194. obj = CreateManyToMany(rowID, columnID);
  195. return SaveManyToMany(obj);
  196. }
  197. else
  198. {
  199. return false;
  200. }
  201. }
  202. }
  203. protected virtual TManyToMany CreateManyToMany(Guid rowID, Guid columnID)
  204. {
  205. var item = new TManyToMany();
  206. (rowProperty.GetValue(item) as IEntityLink)!.ID = rowID;
  207. (columnProperty.GetValue(item) as IEntityLink)!.ID = columnID;
  208. return item;
  209. }
  210. protected virtual bool SaveManyToMany(TManyToMany obj)
  211. {
  212. Client.Save(obj, "Edited by user");
  213. return true;
  214. }
  215. protected virtual bool DeleteManyToMany(TManyToMany obj)
  216. {
  217. Client.Delete(obj, "Deleted by user");
  218. return true;
  219. }
  220. protected class CrossTabDataComponent : BaseDynamicGridDataComponent<TRow>
  221. {
  222. private new DynamicManyToManyCrossTab<TManyToMany, TRow, TColumn> Grid;
  223. public CrossTabDataComponent(DynamicManyToManyCrossTab<TManyToMany, TRow, TColumn> grid): base(grid)
  224. {
  225. Grid = grid;
  226. }
  227. public override TRow LoadItem(CoreRow row)
  228. {
  229. var id = row.Get<TRow, Guid>(x => x.ID);
  230. return Client.Query(
  231. new Filter<TRow>(x => x.ID).IsEqualTo(id),
  232. DynamicGridUtils.LoadEditorColumns(Grid.DataColumns()))
  233. .ToObjects<TRow>()
  234. .FirstOrDefault() ?? throw new Exception($"No {typeof(TRow).Name} with ID {id}");
  235. }
  236. public override void Reload(Filters<TRow> criteria, Columns<TRow> columns, SortOrder<TRow>? sort, CancellationToken token, Action<QueryResult> action)
  237. {
  238. var filter = criteria.Add(Grid.RowFilter()).Combine();
  239. var manyToManyColumns = Columns.None<TManyToMany>().Add(x => x.ID);
  240. manyToManyColumns.Add(Grid.rowProperty.Name + ".ID").Add(Grid.columnProperty.Name + ".ID");
  241. Client.QueryMultiple(
  242. (results, e) =>
  243. {
  244. if(e is not null)
  245. {
  246. action(new(e));
  247. }
  248. else
  249. {
  250. var manyToManyTable = results!.Get<TManyToMany>();
  251. Grid.ManyToManyData = new Dictionary<Guid, Dictionary<Guid, TManyToMany>>();
  252. foreach(var row in manyToManyTable.Rows)
  253. {
  254. var obj = row.ToObject<TManyToMany>();
  255. var rowID = (Grid.rowProperty.GetValue(obj) as IEntityLink)!.ID;
  256. var colID = (Grid.columnProperty.GetValue(obj) as IEntityLink)!.ID;
  257. var rowSet = Grid.ManyToManyData.GetValueOrAdd(colID);
  258. rowSet[rowID] = obj;
  259. }
  260. action(new(results!.Get<TRow>()));
  261. }
  262. },
  263. new KeyedQueryDef<TRow>(filter, columns, sort),
  264. new KeyedQueryDef<TManyToMany>(
  265. new Filter<TManyToMany>(Grid.rowProperty.Name + ".ID").InQuery(filter, x => x.ID)
  266. .And(Grid.columnProperty.Name + ".ID").InQuery(Grid.ColumnFilter(), x => x.ID),
  267. manyToManyColumns));
  268. }
  269. public override void SaveItem(TRow item)
  270. {
  271. // Never should get called.
  272. }
  273. public override void DeleteItems(CoreRow[] rows)
  274. {
  275. // Never should get called.
  276. }
  277. }
  278. }