DynamicManyToManyCrossTab.cs 9.5 KB

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