using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; using System.Threading; 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 : IDynamicEditorPage { } public class DynamicManyToManyGrid : DynamicGrid, IDynamicEditorPage, IDynamicManyToManyGrid where TThis : Entity, new() where TManyToMany : Entity, IPersistent, IRemotable, new() { //private Guid ID = Guid.Empty; protected TThis Item; 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 WorkingList = new(); public PageType PageType => PageType.Other; private bool _readOnly; public bool ReadOnly { get => _readOnly; set { if(_readOnly != value) { _readOnly = value; Reconfigure(); } } } protected DynamicGridCustomColumnsComponent ColumnsComponent; 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(otherproperty.Name + ".ID")); ColumnsComponent = new DynamicGridCustomColumnsComponent(this, GetTag()); DataComponent = new DynamicGridMemoryEntityDataComponent(this); base.DataComponent = DataComponent; } protected new DynamicGridMemoryEntityDataComponent DataComponent; protected override void DoReconfigure(DynamicGridOptions options) { options.RecordCount = true; options.SelectColumns = true; options.MultiSelect = true; if (Security.CanEdit() && !ReadOnly) { options.AddRows = true; options.EditRows = true; } if (Security.CanDelete() && !ReadOnly) options.DeleteRows = true; if (Security.CanImport() && !ReadOnly) options.ImportData = true; if (Security.CanExport()) options.ExportData = true; if (Security.CanMerge()) options.MultiSelect = true; } 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? PageDataHandler) { Item = (TThis)item; Refresh(true, false); var data = PageDataHandler?.Invoke(typeof(TManyToMany)); if (data != null) { DataComponent.LoadData(data); } else { if (Item.ID == Guid.Empty) { data = new CoreTable(); data.LoadColumns(typeof(TManyToMany)); DataComponent.LoadData(data); } else { var exp = CoreUtils.GetPropertyExpression(thisproperty.Name + ".ID"); var filter = new Filter(exp).IsEqualTo(Item.ID).And(exp).IsNotEqualTo(Guid.Empty); var sort = LookupFactory.DefineSort(); var columns = DynamicGridUtils.LoadEditorColumns(DataColumns()); DataComponent.LoadData(filter, columns, sort); } } Refresh(false, true); Ready = true; } public void BeforeSave(object item) { // Don't need to do anything here } public void AfterSave(object item) { foreach (var map in DataComponent.Items) { var prop = GetThisLink(map); if (prop.ID != Item.ID) prop.ID = Item.ID; } DataComponent.SaveItems(); } 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(tag).Load()); user.Wait(); //var global = Task.Run(() => new GlobalConfiguration(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(tag).Save(settings); } protected virtual Guid[] CurrentGuids() { var result = new List(); 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.DefineChildFilter(typeof(TThis), OtherType(), new[] { 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))).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); DataComponent.SaveItem(newitem); } } Refresh(false, true); } } else { base.DoAdd(); } } public 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 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 DynamicEditorPages LoadEditorPages(TManyToMany item) { return item.ID != Guid.Empty ? base.LoadEditorPages(item) : new DynamicEditorPages(); } }