123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.Wpf;
- using InABox.WPF;
- namespace InABox.DynamicGrid;
- public interface IDynamicManyToManyGrid<TManyToMany, TThis> : IDynamicEditorPage
- {
- }
- public class DynamicManyToManyGrid<TManyToMany, TThis> : DynamicGrid<TManyToMany>, IDynamicEditorPage, IDynamicManyToManyGrid<TManyToMany, TThis>
- where TThis : Entity, new()
- where TManyToMany : Entity, IPersistent, IRemotable, new()
- {
- //private Guid ID = Guid.Empty;
- protected TThis Item;
- /// <summary>
- /// Keeps a cache of initially loaded objects, so that we can figure out which guys to delete when we save.
- /// </summary>
- private TManyToMany[] MasterList = Array.Empty<TManyToMany>();
- protected PropertyInfo otherproperty;
- protected IEntityLink GetOtherLink(TManyToMany item) => (otherproperty.GetValue(item) as IEntityLink)!;
- protected PropertyInfo thisproperty;
- protected IEntityLink GetThisLink(TManyToMany item) => (thisproperty.GetValue(item) as IEntityLink)!;
-
- protected List<TManyToMany> WorkingList = new();
- public PageType PageType => PageType.Other;
- private bool _readOnly;
- public bool ReadOnly
- {
- get => _readOnly;
- set
- {
- if(_readOnly != value)
- {
- _readOnly = value;
- Reconfigure();
- }
- }
- }
- private static bool IsAutoEntity => typeof(TManyToMany).HasAttribute<AutoEntity>();
- protected DynamicGridCustomColumnsComponent<TManyToMany> ColumnsComponent;
- /// <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;
- public DynamicManyToManyGrid()
- {
- MultiSelect = true;
- thisproperty = CoreUtils.GetManyToManyThisProperty(typeof(TManyToMany), typeof(TThis));
- otherproperty = CoreUtils.GetManyToManyOtherProperty(typeof(TManyToMany), typeof(TThis));
- HiddenColumns.Add(x => x.ID);
- HiddenColumns.Add(CoreUtils.CreateLambdaExpression<TManyToMany>(otherproperty.Name + ".ID"));
- ColumnsComponent = new DynamicGridCustomColumnsComponent<TManyToMany>(this, GetTag());
- }
- protected override void Init()
- {
- }
- protected override void DoReconfigure(FluentList<DynamicGridOption> options)
- {
- options.BeginUpdate();
- options.Add(DynamicGridOption.RecordCount)
- .Add(DynamicGridOption.SelectColumns)
- .Add(DynamicGridOption.MultiSelect);
- if (Security.CanEdit<TManyToMany>() && !ReadOnly)
- options.Add(DynamicGridOption.AddRows).Add(DynamicGridOption.EditRows);
- if (Security.CanDelete<TManyToMany>() && !ReadOnly)
- options.Add(DynamicGridOption.DeleteRows);
- if (Security.CanImport<TManyToMany>() && !ReadOnly)
- options.Add(DynamicGridOption.ImportData);
- if (Security.CanExport<TManyToMany>())
- options.Add(DynamicGridOption.ExportData);
- if (Security.CanMerge<TManyToMany>())
- options.Add(DynamicGridOption.MultiSelect);
- options.EndUpdate();
- }
- public bool MultiSelect { get; set; }
- public DynamicEditorGrid EditorGrid { get; set; }
- public string Caption()
- {
- //var m2m = typeof(TManyToMany).GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IManyToMany<,>) && i.GenericTypeArguments.Contains(typeof(TThis)));
- //Type other = m2m.GenericTypeArguments.FirstOrDefault(x => x != typeof(TThis));
- //var serv = System.Data.Entity PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));
- //var plural = serv.Pluralize(source);
- //return MvcHtmlString.Create(plural);
- var result = new Inflector.Inflector(new CultureInfo("en")).Pluralize(OtherType().Name);
- return result;
- }
- public virtual int Order()
- {
- return int.MinValue;
- }
- public bool Ready { get; set; }
- public void Load(object item, Func<Type, CoreTable?>? PageDataHandler)
- {
- Item = (TThis)item;
- var data = PageDataHandler?.Invoke(typeof(TManyToMany));
- if (data != null)
- {
- RefreshData(data);
- }
- else
- {
- if (Item.ID == Guid.Empty)
- {
- data = new CoreTable();
- data.LoadColumns(typeof(TManyToMany));
- RefreshData(data);
- }
- else
- {
- var exp = CoreUtils.GetPropertyExpression<TManyToMany>(thisproperty.Name + ".ID");
- var filter = new Filter<TManyToMany>(exp).IsEqualTo(Item.ID).And(exp).IsNotEqualTo(Guid.Empty);
- var sort = LookupFactory.DefineSort<TManyToMany>();
- var columns = DynamicGridUtils.LoadEditorColumns(DataColumns());
- Client.Query(filter, columns, sort, (o, e) =>
- {
- if (o != null)
- {
- LoadedColumns = columns.ColumnNames().ToHashSet();
- Dispatcher.Invoke(() => RefreshData(o));
- }
- else if(e != null)
- {
- Dispatcher.Invoke(() =>
- {
- MessageWindow.ShowError("An error occurred while loading data.", e);
- });
- }
- });
- }
- }
- }
- public void BeforeSave(object item)
- {
- // Don't need to do anything here
- }
- public void AfterSave(object item)
- {
- if (IsAutoEntity)
- {
- return;
- }
- // First remove any deleted files
- foreach (var map in MasterList)
- if (!WorkingList.Contains(map))
- Client.Delete(map, typeof(TManyToMany).Name + " Deleted by User");
- foreach (var map in WorkingList)
- {
- var prop = GetThisLink(map);
- if (prop.ID != Item.ID)
- prop.ID = Item.ID;
- }
- if (WorkingList.Any(x => x.IsChanged()))
- Client.Save(WorkingList.Where(x => x.IsChanged()), "Updated by User");
- }
- public Size MinimumSize()
- {
- return new Size(400, 400);
- }
- private static Type OtherType() =>
- CoreUtils.GetManyToManyOtherType(typeof(TManyToMany), typeof(TThis));
- private static string GetTag()
- {
- return typeof(TManyToMany).Name + "." + typeof(TThis).Name;
- }
- public override DynamicGridColumns GenerateColumns()
- {
- var cols = new DynamicGridColumns();
- cols.AddRange(base.GenerateColumns().Where(x => !x.ColumnName.StartsWith(thisproperty.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();
- //var global = Task.Run(() => new GlobalConfiguration<DynamicGridSettings>(tag).Load());
- //global.Wait();
- //Task.WaitAll(user, global);
- //var columns = user.Result.Any() ? user.Result : global.Result;
- return user.Result;
- }
- protected override void SaveSettings(DynamicGridSettings settings)
- {
- var tag = GetTag();
- new UserConfiguration<DynamicGridSettings>(tag).Save(settings);
- }
- protected virtual Guid[] CurrentGuids()
- {
- var result = new List<Guid>();
- foreach (var item in WorkingList)
- {
- //var prop = GetOtherLink(item);
- var prop = GetThisLink(item);
- result.Add(prop.ID);
- }
- return result.ToArray();
- }
- protected virtual IFilter? GetFilter()
- {
- var result = LookupFactory.DefineFilter(OtherType(), typeof(TThis), new[] { (TThis)Item });
- var filtertype = typeof(Filter<>).MakeGenericType(OtherType());
- var filtermethod = filtertype.GetMethods(BindingFlags.Public | BindingFlags.Static).Where(x =>
- x.Name.Equals("List") && x.GetParameters().Last().ParameterType.IsAssignableFrom(typeof(IEnumerable<Guid>))).First();
- var filterexpression = CoreUtils.GetPropertyExpression(OtherType(), "ID");
- var filtervalues = CurrentGuids();
- var filter = filtermethod.Invoke(null, new object[] { filterexpression, ListOperator.Excludes, filtervalues }) as IFilter;
- if (filter != null)
- {
- if (result != null)
- {
- filter.And(result);
- }
- return filter;
- }
- if (result != null) return result;
- return null;
- }
- protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
- {
- if (MultiSelect)
- {
- var filter = GetFilter();
- var dlgtype = typeof(MultiSelectDialog<>).MakeGenericType(OtherType());
- var dlg = (Activator.CreateInstance(dlgtype, filter, null, true) as IMultiSelectDialog)!;
- if (dlg.ShowDialog())
- {
- var guids = CurrentGuids();
- foreach (var entity in dlg.Items(null))
- {
- if (!guids.Contains(entity.ID))
- {
- var newitem = CreateItem();
- var prop = GetOtherLink(newitem);
- prop.ID = entity.ID;
- prop.Synchronise(entity);
- SaveItem(newitem);
- }
- }
- Refresh(false, true);
- }
- }
- else
- {
- base.DoAdd();
- }
- }
- protected override TManyToMany CreateItem()
- {
- var result = new TManyToMany();
- if (Item != null)
- {
- var prop = GetThisLink(result);
- prop.ID = Item.ID;
- prop.Synchronise(Item);
- }
- return result;
- }
- protected override TManyToMany LoadItem(CoreRow row)
- {
- return WorkingList[_recordmap[row].Index];
- }
- public override void SaveItem(TManyToMany item)
- {
- if (!WorkingList.Contains(item))
- WorkingList.Add(item);
- }
- protected override void DeleteItems(params CoreRow[] rows)
- {
- foreach (var row in rows)
- {
- var id = row.Get<TManyToMany, Guid>(c => c.ID);
- var item = WorkingList.FirstOrDefault(x => x.ID.Equals(id));
- if (item != null)
- WorkingList.Remove(item);
- }
- }
- private void RefreshData(CoreTable data)
- {
- MasterList = data.ToArray<TManyToMany>();
- WorkingList = MasterList.ToList();
- Refresh(true, true);
- Ready = true;
- }
- protected override void Reload(Filters<TManyToMany> criteria, Columns<TManyToMany> columns, ref SortOrder<TManyToMany>? sort,
- Action<CoreTable?, Exception?> action)
- {
- var results = new CoreTable();
- results.LoadColumns(typeof(TManyToMany));
- 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(TManyToMany).GetCustomAttribute<AutoEntity>() is null)
- {
- var data = Client.Query(
- new Filter<TManyToMany>(x => x.ID).InList(WorkingList.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 = WorkingList.FirstOrDefault(x => x.ID == row.Get<TManyToMany, 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<TManyToMany>(sort.Expression);
- var sorted = sort.Direction == SortDirection.Ascending
- ? WorkingList.AsQueryable().OrderBy(exp)
- : WorkingList.AsQueryable().OrderByDescending(exp);
- foreach (var then in sort.Thens)
- {
- var thexp = IQueryableExtensions.ToLambda<TManyToMany>(then.Expression);
- sorted = sort.Direction == SortDirection.Ascending ? sorted.ThenBy(exp) : sorted.ThenByDescending(exp);
- }
- WorkingList = sorted.ToList();
- }
- results.LoadRows(WorkingList);
- //results.LoadRows(WorkingList);
- action.Invoke(results, null);
- }
- protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
- {
- var type = CoreUtils.GetProperty(typeof(TManyToMany), column.ColumnName).DeclaringType;
- if (type.GetInterfaces().Contains(typeof(IEntityLink)) && type.ContainsInheritedGenericType(typeof(TThis)))
- return new NullEditor();
- return base.GetEditor(item, column);
- }
- public override void LoadEditorButtons(TManyToMany 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 TManyToMany entity) return;
- var window = new AuditWindow(entity.ID);
- window.ShowDialog();
- }
- public override DynamicEditorPages LoadEditorPages(TManyToMany item)
- {
- return item.ID != Guid.Empty ? base.LoadEditorPages(item) : new DynamicEditorPages();
- }
- protected override bool BeforePaste(IEnumerable<TManyToMany> items, ClipAction action)
- {
- if (action == ClipAction.Copy)
- {
- foreach (var item in items)
- {
- item.ID = Guid.Empty;
- }
- }
- return base.BeforePaste(items, action);
- }
- }
|