123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- using System;
- using System.Collections.Generic;
- using System.Collections.Immutable;
- using System.Globalization;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.Core.Reports;
- using InABox.WPF;
- namespace InABox.DynamicGrid
- {
- public interface IDynamicOneToManyGrid<TOne, TMany> : IDynamicEditorPage
- {
- List<TMany> Items { get; }
- void LoadItems(TMany[] items);
- }
- public class DynamicOneToManyGrid<TOne, TMany> : DynamicGrid<TMany>, IDynamicEditorPage, IDynamicOneToManyGrid<TOne, TMany>
- where TOne : Entity, new() where TMany : Entity, IPersistent, IRemotable, new()
- {
- private TMany[] MasterList = Array.Empty<TMany>();
- private readonly PropertyInfo property;
- /// <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>
- private HashSet<string>? LoadedColumns;
- protected DynamicGridCustomColumnsComponent<TMany> ColumnsComponent;
- public DynamicOneToManyGrid()
- {
- Ready = false;
- Items = new List<TMany>();
- Criteria = new Filters<TMany>();
- property = CoreUtils.GetOneToManyProperty(typeof(TMany), typeof(TOne));
- AddHiddenColumn(property.Name);
- foreach (var col in LookupFactory.RequiredColumns<TMany>())
- HiddenColumns.Add(col);
- ColumnsComponent = new DynamicGridCustomColumnsComponent<TMany>(this, GetTag());
- }
- protected override void Init()
- {
- }
- protected override void DoReconfigure(FluentList<DynamicGridOption> options)
- {
- options.BeginUpdate();
- options.Add(DynamicGridOption.RecordCount)
- .Add(DynamicGridOption.SelectColumns);
- if (Security.CanEdit<TMany>() && !ReadOnly)
- options.Add(DynamicGridOption.AddRows).Add(DynamicGridOption.EditRows);
- if (Security.CanDelete<TMany>() && !ReadOnly)
- options.Add(DynamicGridOption.DeleteRows);
- if (Security.CanImport<TMany>() && !ReadOnly)
- options.Add(DynamicGridOption.ImportData);
- if (Security.CanExport<TMany>())
- options.Add(DynamicGridOption.ExportData);
- if (Security.CanMerge<TMany>())
- options.Add(DynamicGridOption.MultiSelect);
- options.EndUpdate();
- }
- private static bool IsAutoEntity => typeof(TMany).HasAttribute<AutoEntity>();
- protected Filters<TMany> Criteria { get; } = new Filters<TMany>();
- public TOne Item { get; protected set; }
- public List<TMany> Items { get; private set; }
- public void LoadItems(TMany[] items)
- {
- Items.Clear();
- Items.AddRange(items);
- Refresh(false, true);
- }
- private static string GetTag()
- {
- return typeof(TOne).Name + "." + typeof(TMany).Name;
- }
- #region IDynamicEditorPage
- public DynamicEditorGrid EditorGrid { get; set; }
- public PageType PageType => PageType.Other;
- public bool Ready { get; set; }
- private bool _readOnly;
- public bool ReadOnly
- {
- get => _readOnly;
- set
- {
- if (_readOnly != value)
- {
- _readOnly = value;
- Reconfigure();
- }
- }
- }
- public virtual void Load(object item, Func<Type, CoreTable?>? PageDataHandler)
- {
- Reconfigure();
- Item = (TOne)item;
- Refresh(true, false);
- var data = PageDataHandler?.Invoke(typeof(TMany));
- if (data == null)
- {
- if (Item.ID == Guid.Empty)
- {
- data = new CoreTable();
- data.LoadColumns(typeof(TMany));
- }
- else
- {
- var criteria = new Filters<TMany>();
- var exp = CoreUtils.GetPropertyExpression<TMany>(property.Name + ".ID");
- criteria.Add(new Filter<TMany>(exp).IsEqualTo(Item.ID).And(exp).IsNotEqualTo(Guid.Empty));
- criteria.AddRange(Criteria.Items);
- var sort = LookupFactory.DefineSort<TMany>();
- var columns = DynamicGridUtils.LoadEditorColumns(DataColumns());
- data = Client.Query(criteria.Combine(), columns, sort);
- LoadedColumns = columns.ColumnNames().ToHashSet();
- }
- }
- MasterList = data.Rows.Select(x => x.ToObject<TMany>()).ToArray();
- Items = MasterList.ToList();
- Refresh(false, true);
- Ready = true;
- }
- public virtual void BeforeSave(object item)
- {
- // Don't need to do anything here
- }
- public virtual void AfterSave(object item)
- {
- if (IsAutoEntity)
- {
- return;
- }
- // First remove any deleted files
- foreach (var map in MasterList)
- if (!Items.Contains(map))
- OnDeleteItem(map);
- foreach (var map in Items)
- {
- var prop = (property.GetValue(map) as IEntityLink)!;
- prop.ID = Item.ID;
- prop.Synchronise(Item);
- }
- new Client<TMany>().Save(Items.Where(x => x.IsChanged()), "Updated by User");
- }
- public Size MinimumSize()
- {
- return new Size(400, 400);
- }
- public string Caption()
- {
- var caption = typeof(TMany).GetCustomAttribute(typeof(Caption));
- if (caption != null)
- return ((Caption)caption).Text;
- var result = new Inflector.Inflector(new CultureInfo("en")).Pluralize(typeof(TMany).Name);
- return result;
- }
- public virtual int Order()
- {
- return int.MinValue;
- }
- #endregion
- #region DynamicGrid
- protected virtual void OnDeleteItem(TMany item)
- {
- if (IsAutoEntity)
- {
- return;
- }
- Client.Delete(item, typeof(TMany).Name + " Deleted by User");
- }
- protected override CoreTable LoadImportKeys(string[] fields)
- {
- var result = base.LoadImportKeys(fields);
- result.LoadRows(MasterList);
- return result;
- }
- protected override bool CustomiseImportItem(TMany item)
- {
- var result = base.CustomiseImportItem(item);
- if (result)
- {
- var prop = (property.GetValue(item) as IEntityLink)!;
- prop.ID = Item.ID;
- prop.Synchronise(Item);
- }
- return result;
- }
- public override DynamicGridColumns GenerateColumns()
- {
- var cols = new DynamicGridColumns();
- cols.AddRange(base.GenerateColumns().Where(x => !x.ColumnName.StartsWith(property.Name + ".")));
- return cols;
- }
- protected override DynamicGridColumns LoadColumns()
- {
- return ColumnsComponent.LoadColumns();
- }
- protected override void SaveColumns(DynamicGridColumns columns)
- {
- ColumnsComponent.SaveColumns(columns);
- }
- protected override void LoadColumnsMenu(ContextMenu menu)
- {
- base.LoadColumnsMenu(menu);
- ColumnsComponent.LoadColumnsMenu(menu);
- }
- protected override DynamicGridSettings LoadSettings()
- {
- var tag = GetTag();
- var user = Task.Run(() => new UserConfiguration<DynamicGridSettings>(tag).Load());
- user.Wait();
- return user.Result;
- }
- protected override void SaveSettings(DynamicGridSettings settings)
- {
- var tag = GetTag();
- new UserConfiguration<DynamicGridSettings>(tag).Save(settings);
- }
- protected override TMany CreateItem()
- {
- var result = new TMany();
- var prop = (property.GetValue(result) as IEntityLink)!;
- prop.ID = Item.ID;
- prop.Synchronise(Item);
- return result;
- }
- protected override TMany LoadItem(CoreRow row)
- {
- return Items[_recordmap[row].Index];
- }
- protected override TMany[] LoadItems(CoreRow[] rows)
- {
- var result = new List<TMany>();
- foreach (var row in rows)
- result.Add(LoadItem(row));
- return result.ToArray();
- }
- public override void SaveItem(TMany item)
- {
- if (!Items.Contains(item))
- Items.Add(item);
- if (item is ISequenceable) Items = Items.AsQueryable().OrderBy(x => (x as ISequenceable)!.Sequence).ToList();
- }
- protected override void DeleteItems(params CoreRow[] rows)
- {
- var items = rows.Select(LoadItem).ToList();
- foreach (var item in items)
- {
- Items.Remove(item);
- }
- }
- protected override void Reload(Filters<TMany> criteria, Columns<TMany> columns, ref SortOrder<TMany>? sort,
- Action<CoreTable?, Exception?> action)
- {
- var results = new CoreTable();
- results.LoadColumns(typeof(TMany));
- if (LoadedColumns is not null)
- {
- // Figure out which columns we still need.
- var newColumns = columns.Where(x => !LoadedColumns.Contains(x.Property)).ToColumns();
- if (newColumns.Any() && typeof(TMany).GetCustomAttribute<AutoEntity>() is null)
- {
- var data = Client.Query(
- new Filter<TMany>(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<TMany, 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);
- }
- }
- }
- if (sort != null)
- {
- var exp = IQueryableExtensions.ToLambda<TMany>(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<TMany>(then.Expression);
- sorted = sort.Direction == SortDirection.Ascending ? sorted.ThenBy(exp) : sorted.ThenByDescending(exp);
- }
- Items = sorted.ToList();
- }
- results.LoadRows(Items);
- action.Invoke(results, null);
- }
- protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
- {
- var type = CoreUtils.GetProperty(typeof(TMany), column.ColumnName).DeclaringType;
- if (type.GetInterfaces().Contains(typeof(IEntityLink)) && type.ContainsInheritedGenericType(typeof(TOne)))
- return new NullEditor();
- return base.GetEditor(item, column);
- }
- public override void LoadEditorButtons(TMany item, DynamicEditorButtons buttons)
- {
- base.LoadEditorButtons(item, buttons);
- if (ClientFactory.IsSupported<AuditTrail>())
- buttons.Add("Audit Trail", Wpf.Resources.view.AsBitmapImage(), item, AuditTrailClick);
- }
- private void AuditTrailClick(object sender, object? item)
- {
- if (item is not TMany entity) return;
- var window = new AuditWindow(entity.ID);
- window.ShowDialog();
- }
- public override DynamicEditorPages LoadEditorPages(TMany item)
- {
- return item.ID != Guid.Empty ? base.LoadEditorPages(item) : new DynamicEditorPages();
- }
- protected override bool BeforePaste(IEnumerable<TMany> items, ClipAction action)
- {
- if (action == ClipAction.Copy)
- {
- foreach (var item in items)
- {
- item.ID = Guid.Empty;
- }
- }
- return base.BeforePaste(items, action);
- }
- #endregion
- }
- }
|