using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace InABox.DynamicGrid; public interface IDynamicGridDataComponent where T : BaseObject, new() { DynamicGrid Grid { get; } void Reload(Filters criteria, Columns columns, SortOrder? sort, CancellationToken token, Action action); T LoadItem(CoreRow row); T[] LoadItems(CoreRow[] rows); void SaveItem(T item); void SaveItems(T[] items); void DeleteItems(CoreRow[] rows); /// /// Loads the child tables for an export, based on the filter of the parent table. /// /// Filter for the parent table. /// A list of the child table types, with columns to load for each /// A list of tables, in the same order as they came in IEnumerable> LoadExportTables(Filters filter, IEnumerable> tableColumns); CoreTable LoadImportKeys(string[] fields); } public abstract class BaseDynamicGridDataComponent : IDynamicGridDataComponent where T : BaseObject, new() { public DynamicGrid Grid { get; private set; } public BaseDynamicGridDataComponent(DynamicGrid grid) { Grid = grid; } public abstract void DeleteItems(CoreRow[] rows); public abstract T LoadItem(CoreRow row); public virtual T[] LoadItems(CoreRow[] rows) => rows.ToArray(LoadItem); public abstract void Reload(Filters criteria, Columns columns, SortOrder? sort, CancellationToken token, Action action); public abstract void SaveItem(T item); public virtual void SaveItems(T[] items) { foreach(var item in items) { SaveItem(item); } } public virtual IEnumerable> LoadExportTables(Filters filter, IEnumerable> tableColumns) { return tableColumns.Select(x => { var table = new CoreTable(); table.LoadColumns(x.Item2); return new Tuple(x.Item1, table); }); } public virtual CoreTable LoadImportKeys(String[] fields) { var result = new CoreTable(); result.LoadColumns(Columns.None().Add(fields)); return result; } }