DynamicManyToManyCrossTab.cs 10 KB

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