DynamicManyToManyCrossTab.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.Tasks;
  10. using System.Windows.Controls;
  11. using System.Windows.Media.Imaging;
  12. namespace InABox.DynamicGrid;
  13. public abstract class DynamicManyToManyCrossTab<TManyToMany, TRow, TColumn> : DynamicGrid<TRow>
  14. where TManyToMany : Entity, IRemotable, IPersistent, new()
  15. where TRow : Entity, IRemotable, IPersistent, new()
  16. where TColumn : Entity, IRemotable, IPersistent, new()
  17. {
  18. private static readonly BitmapImage tick = Wpf.Resources.tick.AsBitmapImage();
  19. /// <summary>
  20. /// Property on <typeparamref name="TManyToMany"/> which is an <see cref="EntityLink{T}"/> for <typeparamref name="TRow"/>.
  21. /// </summary>
  22. private readonly PropertyInfo rowProperty;
  23. /// <summary>
  24. /// Property on <typeparamref name="TManyToMany"/> which is an <see cref="EntityLink{T}"/> for <typeparamref name="TColumn"/>.
  25. /// </summary>
  26. private readonly PropertyInfo columnProperty;
  27. private CoreTable? ColumnData;
  28. /// <summary>
  29. /// {ColumnID : { RowID }}
  30. /// </summary>
  31. private Dictionary<Guid, Dictionary<Guid, TManyToMany>>? ManyToManyData;
  32. public DynamicManyToManyCrossTab()
  33. {
  34. rowProperty = CoreUtils.GetManyToManyThisProperty(typeof(TManyToMany), typeof(TRow));
  35. columnProperty = CoreUtils.GetManyToManyThisProperty(typeof(TManyToMany), typeof(TColumn));
  36. HeaderHeight = 125;
  37. }
  38. protected override void Init()
  39. {
  40. }
  41. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  42. {
  43. options.Clear();
  44. }
  45. /// <summary>
  46. /// Load the required columns for <typeparamref name="TRow"/>.
  47. /// </summary>
  48. protected abstract DynamicGridColumns LoadRowColumns();
  49. protected abstract Columns<TColumn>? LoadColumnColumns();
  50. protected abstract SortOrder<TColumn>? LoadColumnSort();
  51. protected abstract string FormatColumnHeader(CoreRow row);
  52. protected virtual Filter<TRow>? RowFilter() => null;
  53. protected virtual Filter<TColumn>? ColumnFilter() => null;
  54. protected override DynamicGridColumns LoadColumns()
  55. {
  56. var columns = LoadRowColumns();
  57. var client = Client.Create(typeof(TColumn));
  58. var columnColumns = new Columns<TColumn>(x => x.ID);
  59. if(LoadColumnColumns() is Columns<TColumn> extra)
  60. {
  61. foreach(var col in extra.GetColumns())
  62. {
  63. columnColumns.Add(col);
  64. }
  65. }
  66. ColumnData = Client.Query(ColumnFilter(), columnColumns, LoadColumnSort());
  67. ActionColumns.Clear();
  68. foreach(var columnRow in ColumnData.Rows)
  69. {
  70. var colID = columnRow.Get<TColumn, Guid>(x => x.ID);
  71. ActionColumns.Add(new DynamicImageColumn(
  72. (row) =>
  73. {
  74. if (row is null || ManyToManyData is null) return null;
  75. if(ManyToManyData.TryGetValue(colID, out var rowSet))
  76. {
  77. var rowID = row.Get<TRow, Guid>(x => x.ID);
  78. if (rowSet.ContainsKey(rowID))
  79. {
  80. return tick;
  81. }
  82. }
  83. return null;
  84. },
  85. (row) =>
  86. {
  87. if (row is null) return false;
  88. var rowID = row.Get<TRow, Guid>(x => x.ID);
  89. return CellClick(colID, rowID);
  90. }
  91. )
  92. {
  93. HeaderText = FormatColumnHeader(columnRow),
  94. ContextMenu = (rows) =>
  95. {
  96. var row = rows?.FirstOrDefault();
  97. if (row is null) return null;
  98. var rowID = row.Get<TRow, Guid>(x => x.ID);
  99. return CellMenu(colID, rowID);
  100. }
  101. });
  102. }
  103. return columns;
  104. }
  105. private ContextMenu? CellMenu(Guid colID, Guid rowID)
  106. {
  107. if (ManyToManyData is null) return null;
  108. var menu = new ContextMenu();
  109. if (ManyToManyData.TryGetValue(colID, out var rowSet)
  110. && rowSet.TryGetValue(rowID, out var obj))
  111. {
  112. if (Security.CanEdit<TManyToMany>() && CanEditCell(obj))
  113. {
  114. menu.AddItem("Edit Item", Wpf.Resources.pencil, obj, (obj) =>
  115. {
  116. if (EditCell(obj))
  117. {
  118. Refresh(false, true);
  119. }
  120. });
  121. }
  122. if (Security.CanDelete<TManyToMany>())
  123. {
  124. menu.AddItem("Delete Item", Wpf.Resources.delete, obj, DeleteCell);
  125. }
  126. }
  127. else
  128. {
  129. if (Security.CanEdit<TManyToMany>())
  130. {
  131. menu.AddItem("Create Item", Wpf.Resources.add, (colID, rowID), CreateCell);
  132. }
  133. }
  134. if(menu.Items.Count > 0)
  135. {
  136. return menu;
  137. }
  138. else
  139. {
  140. return null;
  141. }
  142. }
  143. private void CreateCell((Guid colID, Guid rowID) obj)
  144. {
  145. var manyToMany = CreateManyToMany(obj.rowID, obj.colID);
  146. if (SaveManyToMany(manyToMany))
  147. {
  148. Refresh(false, true);
  149. }
  150. }
  151. private void DeleteCell(TManyToMany obj)
  152. {
  153. if (DeleteManyToMany(obj))
  154. {
  155. Refresh(false, true);
  156. }
  157. }
  158. protected virtual bool CanEditCell(TManyToMany obj) => false;
  159. /// <summary>
  160. /// Code to edit a <typeparamref name="TManyToMany"/> cell; note that this <b>must not</b> allow for changing either
  161. /// the <typeparamref name="TColumn"/> or the <typeparamref name="TRow"/>, otherwise we would easily get duplicate <typeparamref name="TManyToMany"/>s.
  162. /// </summary>
  163. /// <remarks>
  164. /// This method should also save the object.
  165. /// </remarks>
  166. /// <param name="obj"></param>
  167. protected virtual bool EditCell(TManyToMany obj)
  168. {
  169. return false;
  170. }
  171. private bool CellClick(Guid columnID, Guid rowID)
  172. {
  173. if (ManyToManyData is null) return false;
  174. if (ManyToManyData.TryGetValue(columnID, out var rowSet)
  175. && rowSet.TryGetValue(rowID, out var obj))
  176. {
  177. if (Security.CanDelete<TManyToMany>())
  178. {
  179. return DeleteManyToMany(obj);
  180. }
  181. else
  182. {
  183. return false;
  184. }
  185. }
  186. else
  187. {
  188. if (Security.CanEdit<TManyToMany>())
  189. {
  190. obj = CreateManyToMany(rowID, columnID);
  191. return SaveManyToMany(obj);
  192. }
  193. else
  194. {
  195. return false;
  196. }
  197. }
  198. }
  199. protected virtual TManyToMany CreateManyToMany(Guid rowID, Guid columnID)
  200. {
  201. var item = new TManyToMany();
  202. (rowProperty.GetValue(item) as IEntityLink)!.ID = rowID;
  203. (columnProperty.GetValue(item) as IEntityLink)!.ID = columnID;
  204. return item;
  205. }
  206. protected virtual bool SaveManyToMany(TManyToMany obj)
  207. {
  208. Client.Save(obj, "Edited by user");
  209. return true;
  210. }
  211. protected virtual bool DeleteManyToMany(TManyToMany obj)
  212. {
  213. Client.Delete(obj, "Deleted by user");
  214. return true;
  215. }
  216. protected override void Reload(Filters<TRow> criteria, Columns<TRow> columns, ref SortOrder<TRow>? sort, Action<CoreTable?, Exception?> action)
  217. {
  218. var filter = criteria.Add(RowFilter()).Combine();
  219. var manyToManyColumns = new Columns<TManyToMany>(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. }