123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- using InABox.Clients;
- using InABox.Core;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace InABox.DynamicGrid;
- public class DynamicGridMemoryEntityDataComponent<T> : BaseDynamicGridDataComponent<T>
- where T : Entity, IRemotable, IPersistent, new()
- {
- /// <summary>
- /// Keeps a cache of initially loaded objects, so that we can figure out which guys to delete when we save.
- /// </summary>
- private T[] MasterList = Array.Empty<T>();
- public List<T> Items { get; private set; }
- /// <summary>
- /// A set of columns representing which columns have been loaded from the database.
- /// </summary>
- /// <remarks>
- /// This is used to refresh the data when the columns change.<br/>
- ///
- /// It is <see langword="null"/> if no data has been loaded from the database (that is, the data was gotten from
- /// a page data handler instead.)
- /// </remarks>
- public HashSet<string>? LoadedColumns { get; private set; }
- private static bool IsAutoEntity => typeof(T).HasAttribute<AutoEntity>();
- public DynamicGridMemoryEntityDataComponent(DynamicGrid<T> grid): base(grid)
- {
- Items = new List<T>();
- }
- #region MemoryEntityGrid
- public void LoadData(T[] items)
- {
- MasterList = items;
- Items = MasterList.ToList();
- LoadedColumns = null;
- }
- public void LoadData(CoreTable data)
- {
- MasterList = data.ToArray<T>();
- Items = MasterList.ToList();
- LoadedColumns = null;
- }
- public void LoadData(Filter<T>? filter, Columns<T> columns, SortOrder<T>? sort)
- {
- var data = Client.Query(filter, columns, sort);
- MasterList = data.ToArray<T>();
- Items = MasterList.ToList();
- LoadedColumns = columns.ColumnNames().ToHashSet();
- }
- public void SaveItems()
- {
- if (IsAutoEntity)
- {
- return;
- }
- // First remove any deleted files
- foreach (var map in MasterList)
- if (!Items.Contains(map))
- Client.Delete(map, typeof(T).Name + " Deleted by User");
- Client.Save(Items.Where(x => x.IsChanged()), "Updated by User");
- }
- #endregion
- public override CoreTable LoadImportKeys(string[] fields)
- {
- var result = new CoreTable();
- result.LoadColumns(Columns.None<T>().Add(fields));
- result.LoadRows(MasterList);
- return result;
- }
- public override T LoadItem(CoreRow row)
- {
- return Items[Grid.GetMasterRow(row).Index];
- }
- public override void SaveItem(T item)
- {
- if (!Items.Contains(item))
- Items.Add(item);
- if (item is ISequenceable) Items = Items.AsQueryable().OrderBy(x => (x as ISequenceable)!.Sequence).ToList();
- }
- public override void SaveItems(T[] items)
- {
- foreach(var item in items)
- {
- if (!Items.Contains(item))
- Items.Add(item);
- }
- if (typeof(T).HasInterface<ISequenceable>())
- Items = Items.AsQueryable().OrderBy(x => (x as ISequenceable)!.Sequence).ToList();
- }
- public override void DeleteItems(params CoreRow[] rows)
- {
- var items = rows.Select(LoadItem).ToList();
- foreach (var item in items)
- {
- Items.Remove(item);
- }
- }
- public override void Reload(
- Filters<T> criteria, Columns<T> columns, SortOrder<T>? sort,
- CancellationToken token, Action<QueryResult> action)
- {
- var results = new CoreTable();
- results.LoadColumns(typeof(T));
- EnsureColumns(columns);
- if (sort != null)
- {
- var exp = IQueryableExtensions.ToLambda<T>(sort.Expression);
- var sorted = sort.Direction == SortDirection.Ascending
- ? Items.AsQueryable().OrderBy(exp)
- : Items.AsQueryable().OrderByDescending(exp);
- foreach (var then in sort.Thens)
- {
- var thexp = IQueryableExtensions.ToLambda<T>(then.Expression);
- sorted = sort.Direction == SortDirection.Ascending ? sorted.ThenBy(exp) : sorted.ThenByDescending(exp);
- }
- Items = sorted.ToList();
- }
- results.LoadRows(Items);
- action.Invoke(new(results));
- }
- public void EnsureColumns(Columns<T> columns)
- {
- RequireColumns(columns);
- LoadForeignProperties(columns);
- }
- /// <summary>
- /// 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"/>.
- /// 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
- /// linked table with the required columns.
- /// </summary>
- /// <param name="columns"></param>
- public void LoadForeignProperties(Columns<T> columns)
- {
- // Lists of properties that we need, arranged by the entity link property which is their parent.
- // LinkIDProperty : (Type, Properties: [(columnName, property)], Objects)
- var newData = new Dictionary<IProperty, Tuple<Type, List<Tuple<string, IProperty>>, HashSet<T>>>();
- foreach (var column in columns)
- {
- var property = DatabaseSchema.Property(typeof(T), column.Property);
- if (property?.GetOuterParent(x => x.IsEntityLink) is IProperty linkProperty)
- {
- var remaining = column.Property[(linkProperty.Name.Length + 1)..];
- if (remaining.Equals(nameof(IEntityLink.ID)))
- {
- // This guy isn't foreign, so we don't pull him.
- continue;
- }
- var idProperty = DatabaseSchema.Property(typeof(T), linkProperty.Name + "." + nameof(IEntityLink.ID))!;
- var linkType = linkProperty.PropertyType.GetInterfaceDefinition(typeof(IEntityLink<>))!.GenericTypeArguments[0];
- if (!newData.TryGetValue(idProperty, out var data))
- {
- data = new Tuple<Type, List<Tuple<string, IProperty>>, HashSet<T>>(
- linkType,
- new List<Tuple<string, IProperty>>(),
- new HashSet<T>());
- newData.Add(idProperty, data);
- }
- var any = false;
- foreach (var item in Items)
- {
- if (!item.LoadedColumns.Contains(column.Property))
- {
- var linkID = (Guid)idProperty.Getter()(item);
- if (linkID != Guid.Empty)
- {
- any = true;
- data.Item3.Add(item);
- }
- }
- }
- if (any)
- {
- data.Item2.Add(new(remaining, property));
- }
- }
- }
- var queryDefs = new List<IKeyedQueryDef>();
- foreach (var (prop, data) in newData)
- {
- if (data.Item2.Count != 0)
- {
- var ids = data.Item3.Select(prop.Getter()).Cast<Guid>().ToArray();
- queryDefs.Add(new KeyedQueryDef(prop.Name, data.Item1,
- Filter.Create<Entity>(data.Item1, x => x.ID).InList(ids),
- Columns.None(data.Item1)
- .Add(data.Item2.Select(x => x.Item1))
- .Add<Entity>(x => x.ID)));
- }
- }
- var results = Client.QueryMultiple(queryDefs);
- foreach(var (prop, data) in newData)
- {
- var table = results.GetOrDefault(prop.Name);
- if(table is null)
- {
- continue;
- }
- foreach (var entity in data.Item3)
- {
- var linkID = (Guid)prop.Getter()(entity);
- var row = table.Rows.FirstOrDefault(x => x.Get<Entity, Guid>(x => x.ID) == linkID);
- if (row is not null)
- {
- foreach (var (name, property) in data.Item2)
- {
- if (!entity.LoadedColumns.Contains(property.Name))
- {
- property.Setter()(entity, row[name]);
- entity.LoadedColumns.Add(property.Name);
- }
- }
- }
- }
- }
- }
- public void RequireColumns(Columns<T> columns)
- {
- if (LoadedColumns is null) return;
- // Figure out which columns we still need.
- var newColumns = columns.Where(x => !LoadedColumns.Contains(x.Property)).ToColumns(ColumnTypeFlags.None);
- if (newColumns.Count > 0 && typeof(T).GetCustomAttribute<AutoEntity>() is null)
- {
- var data = Client.Query(
- new Filter<T>(x => x.ID).InList(Items.Select(x => x.ID).Where(x => x != Guid.Empty).ToArray()),
- // We also need to add ID, so we know which item to fill.
- newColumns.Add(x => x.ID));
- foreach (var row in data.Rows)
- {
- var item = Items.FirstOrDefault(x => x.ID == row.Get<T, Guid>(y => y.ID));
- if (item is not null)
- {
- row.FillObject(item, overrideExisting: false);
- }
- }
- // Remember that we have now loaded this data.
- foreach (var column in newColumns)
- {
- LoadedColumns.Add(column.Property);
- }
- }
- }
- }
|