IDynamicMemoryEntityGrid.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace InABox.DynamicGrid;
  10. /// <summary>
  11. /// Defines a common interface for dealing with grids like <see cref="DynamicOneToManyGrid{TOne, TMany}"/>
  12. /// or <see cref="DynamicManyToManyGrid{TManyToMany, TThis}"/>, which display <see cref="Entity"/>s, but do not load them necessarily from the database,
  13. /// instead keeping them in memory.
  14. /// <br/>
  15. /// This interface then allows other functions, like
  16. /// <see cref="DynamicMemoryEntityGridExtensions.EnsureColumns{T}(InABox.DynamicGrid.IDynamicMemoryEntityGrid{T}, Columns{T})"/>, to work based on <see cref="IDynamicMemoryEntityGrid{T}.LoadedColumns"/> and manage our column handling better.
  17. /// </summary>
  18. /// <typeparam name="T"></typeparam>
  19. public interface IDynamicMemoryEntityGrid<T>
  20. where T : Entity, IRemotable, IPersistent, new()
  21. {
  22. /// <summary>
  23. /// A set of columns representing which columns have been loaded from the database.
  24. /// </summary>
  25. /// <remarks>
  26. /// This is used to refresh the data when the columns change.<br/>
  27. ///
  28. /// It is <see langword="null"/> if no data has been loaded from the database (that is, the data was gotten from
  29. /// a page data handler instead.)
  30. /// </remarks>
  31. public HashSet<string>? LoadedColumns { get; }
  32. public IEnumerable<T> Items { get; }
  33. }
  34. public static class DynamicMemoryEntityGridExtensions
  35. {
  36. public static void EnsureColumns<T>(this IDynamicMemoryEntityGrid<T> grid, Columns<T> columns)
  37. where T : Entity, IRemotable, IPersistent, new()
  38. {
  39. RequireColumns(grid, columns);
  40. LoadForeignProperties(grid, columns);
  41. }
  42. /// <summary>
  43. /// Load the properties of any <see cref="EntityLink{T}"/>s on this <see cref="TMany"/> where the <see cref="IEntityLink.ID"/> is not <see cref="Guid.Empty"/>.
  44. /// This allows us to populate columns of transient objects, as long as they are linked by the ID. What this actually then does is query each
  45. /// linked table with the required columns.
  46. /// </summary>
  47. /// <param name="columns"></param>
  48. public static void LoadForeignProperties<T>(this IDynamicMemoryEntityGrid<T> grid, Columns<T> columns)
  49. where T : Entity, IRemotable, IPersistent, new()
  50. {
  51. grid.Items.LoadForeignProperties(columns);
  52. }
  53. public static void RequireColumns<T>(this IDynamicMemoryEntityGrid<T> grid, Columns<T> columns)
  54. where T : Entity, IRemotable, IPersistent, new()
  55. {
  56. if (grid.LoadedColumns is null) return;
  57. // Figure out which columns we still need.
  58. var newColumns = columns.Where(x => !grid.LoadedColumns.Contains(x.Property)).ToColumns(ColumnTypeFlags.None);
  59. if (newColumns.Count > 0 && typeof(T).GetCustomAttribute<AutoEntity>() is null)
  60. {
  61. var data = Client.Query(
  62. new Filter<T>(x => x.ID).InList(grid.Items.Select(x => x.ID).Where(x => x != Guid.Empty).ToArray()),
  63. // We also need to add ID, so we know which item to fill.
  64. newColumns.Add(x => x.ID));
  65. foreach (var row in data.Rows)
  66. {
  67. var item = grid.Items.FirstOrDefault(x => x.ID == row.Get<T, Guid>(y => y.ID));
  68. if (item is not null)
  69. {
  70. row.FillObject(item, overrideExisting: false);
  71. }
  72. }
  73. // Remember that we have now loaded this data.
  74. foreach (var column in newColumns)
  75. {
  76. grid.LoadedColumns.Add(column.Property);
  77. }
  78. }
  79. }
  80. }