DynamicManyToManyGrid.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using InABox.Clients;
  12. using InABox.Configuration;
  13. using InABox.Core;
  14. using InABox.Wpf;
  15. using InABox.WPF;
  16. namespace InABox.DynamicGrid;
  17. public interface IDynamicManyToManyGrid<TManyToMany, TThis> : IDynamicEditorPage
  18. {
  19. }
  20. public class DynamicManyToManyGrid<TManyToMany, TThis> : DynamicGrid<TManyToMany>,
  21. IDynamicEditorPage,
  22. IDynamicManyToManyGrid<TManyToMany, TThis>
  23. where TThis : Entity, new()
  24. where TManyToMany : Entity, IPersistent, IRemotable, new()
  25. {
  26. //private Guid ID = Guid.Empty;
  27. protected TThis Item;
  28. protected PropertyInfo otherproperty;
  29. protected IEntityLink GetOtherLink(TManyToMany item) => (otherproperty.GetValue(item) as IEntityLink)!;
  30. protected PropertyInfo thisproperty;
  31. protected IEntityLink GetThisLink(TManyToMany item) => (thisproperty.GetValue(item) as IEntityLink)!;
  32. protected List<TManyToMany> WorkingList = new();
  33. public PageType PageType => PageType.Other;
  34. private bool _readOnly;
  35. public bool ReadOnly
  36. {
  37. get => _readOnly;
  38. set
  39. {
  40. if(_readOnly != value)
  41. {
  42. _readOnly = value;
  43. Reconfigure();
  44. }
  45. }
  46. }
  47. protected DynamicGridCustomColumnsComponent<TManyToMany> ColumnsComponent;
  48. public DynamicManyToManyGrid()
  49. {
  50. MultiSelect = true;
  51. thisproperty = CoreUtils.GetManyToManyThisProperty(typeof(TManyToMany), typeof(TThis));
  52. otherproperty = CoreUtils.GetManyToManyOtherProperty(typeof(TManyToMany), typeof(TThis));
  53. HiddenColumns.Add(x => x.ID);
  54. HiddenColumns.Add(CoreUtils.CreateLambdaExpression<TManyToMany>(otherproperty.Name + ".ID"));
  55. ColumnsComponent = new DynamicGridCustomColumnsComponent<TManyToMany>(this, GetTag());
  56. DataComponent = new DynamicGridMemoryEntityDataComponent<TManyToMany>(this);
  57. base.DataComponent = DataComponent;
  58. }
  59. protected new DynamicGridMemoryEntityDataComponent<TManyToMany> DataComponent;
  60. protected override void DoReconfigure(DynamicGridOptions options)
  61. {
  62. options.RecordCount = true;
  63. options.SelectColumns = true;
  64. options.MultiSelect = true;
  65. if (Security.CanEdit<TManyToMany>() && !ReadOnly)
  66. {
  67. options.AddRows = true;
  68. options.EditRows = true;
  69. }
  70. if (Security.CanDelete<TManyToMany>() && !ReadOnly)
  71. options.DeleteRows = true;
  72. if (Security.CanImport<TManyToMany>() && !ReadOnly)
  73. options.ImportData = true;
  74. if (Security.CanExport<TManyToMany>())
  75. options.ExportData = true;
  76. if (Security.CanMerge<TManyToMany>())
  77. options.MultiSelect = true;
  78. }
  79. public bool MultiSelect { get; set; }
  80. public DynamicEditorGrid EditorGrid { get; set; }
  81. public string Caption()
  82. {
  83. //var m2m = typeof(TManyToMany).GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IManyToMany<,>) && i.GenericTypeArguments.Contains(typeof(TThis)));
  84. //Type other = m2m.GenericTypeArguments.FirstOrDefault(x => x != typeof(TThis));
  85. //var serv = System.Data.Entity PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));
  86. //var plural = serv.Pluralize(source);
  87. //return MvcHtmlString.Create(plural);
  88. var result = new Inflector.Inflector(new CultureInfo("en")).Pluralize(OtherType().Name);
  89. return result;
  90. }
  91. public virtual int Order()
  92. {
  93. return int.MinValue;
  94. }
  95. public bool Ready { get; set; }
  96. public void Load(object item, Func<Type, CoreTable?>? PageDataHandler)
  97. {
  98. Item = (TThis)item;
  99. Refresh(true, false);
  100. var data = PageDataHandler?.Invoke(typeof(TManyToMany));
  101. if (data != null)
  102. {
  103. DataComponent.LoadData(data);
  104. }
  105. else
  106. {
  107. if (Item.ID == Guid.Empty)
  108. {
  109. data = new CoreTable();
  110. data.LoadColumns(typeof(TManyToMany));
  111. DataComponent.LoadData(data);
  112. }
  113. else
  114. {
  115. var exp = CoreUtils.GetPropertyExpression<TManyToMany>(thisproperty.Name + ".ID");
  116. var filter = new Filter<TManyToMany>(exp).IsEqualTo(Item.ID).And(exp).IsNotEqualTo(Guid.Empty);
  117. var sort = LookupFactory.DefineSort<TManyToMany>();
  118. var columns = DynamicGridUtils.LoadEditorColumns(DataColumns());
  119. DataComponent.LoadData(filter, columns, sort);
  120. }
  121. }
  122. Refresh(false, true);
  123. Ready = true;
  124. }
  125. public void BeforeSave(object item)
  126. {
  127. // Don't need to do anything here
  128. }
  129. public void AfterSave(object item)
  130. {
  131. foreach (var map in DataComponent.Items)
  132. {
  133. var prop = GetThisLink(map);
  134. if (prop.ID != Item.ID)
  135. prop.ID = Item.ID;
  136. }
  137. DataComponent.SaveItems();
  138. }
  139. public Size MinimumSize()
  140. {
  141. return new Size(400, 400);
  142. }
  143. private static Type OtherType() =>
  144. CoreUtils.GetManyToManyOtherType(typeof(TManyToMany), typeof(TThis));
  145. private static string GetTag()
  146. {
  147. return typeof(TManyToMany).Name + "." + typeof(TThis).Name;
  148. }
  149. public override DynamicGridColumns GenerateColumns()
  150. {
  151. var cols = new DynamicGridColumns();
  152. cols.AddRange(base.GenerateColumns().Where(x => !x.ColumnName.StartsWith(thisproperty.Name + ".")));
  153. return cols;
  154. }
  155. protected override DynamicGridColumns LoadColumns()
  156. {
  157. return ColumnsComponent.LoadColumns();
  158. }
  159. protected override void SaveColumns(DynamicGridColumns columns)
  160. {
  161. ColumnsComponent.SaveColumns(columns);
  162. }
  163. protected override void LoadColumnsMenu(ContextMenu menu)
  164. {
  165. base.LoadColumnsMenu(menu);
  166. ColumnsComponent.LoadColumnsMenu(menu);
  167. }
  168. protected override DynamicGridSettings LoadSettings()
  169. {
  170. var tag = GetTag();
  171. var user = Task.Run(() => new UserConfiguration<DynamicGridSettings>(tag).Load());
  172. user.Wait();
  173. //var global = Task.Run(() => new GlobalConfiguration<DynamicGridSettings>(tag).Load());
  174. //global.Wait();
  175. //Task.WaitAll(user, global);
  176. //var columns = user.Result.Any() ? user.Result : global.Result;
  177. return user.Result;
  178. }
  179. protected override void SaveSettings(DynamicGridSettings settings)
  180. {
  181. var tag = GetTag();
  182. new UserConfiguration<DynamicGridSettings>(tag).Save(settings);
  183. }
  184. protected virtual Guid[] CurrentGuids()
  185. {
  186. var result = new List<Guid>();
  187. foreach (var item in WorkingList)
  188. {
  189. //var prop = GetOtherLink(item);
  190. var prop = GetThisLink(item);
  191. result.Add(prop.ID);
  192. }
  193. return result.ToArray();
  194. }
  195. protected virtual IFilter? GetFilter()
  196. {
  197. var result = LookupFactory.DefineChildFilter(typeof(TThis), OtherType(), new[] { Item });
  198. var filtertype = typeof(Filter<>).MakeGenericType(OtherType());
  199. var filtermethod = filtertype.GetMethods(BindingFlags.Public | BindingFlags.Static).Where(x =>
  200. x.Name.Equals("List") && x.GetParameters().Last().ParameterType.IsAssignableFrom(typeof(IEnumerable<Guid>))).First();
  201. var filterexpression = CoreUtils.GetPropertyExpression(OtherType(), "ID");
  202. var filtervalues = CurrentGuids();
  203. var filter = filtermethod.Invoke(null, new object[] { filterexpression, ListOperator.Excludes, filtervalues }) as IFilter;
  204. if (filter != null)
  205. {
  206. if (result != null)
  207. {
  208. filter.And(result);
  209. }
  210. return filter;
  211. }
  212. if (result != null) return result;
  213. return null;
  214. }
  215. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  216. {
  217. if (MultiSelect)
  218. {
  219. var filter = GetFilter();
  220. var dlgtype = typeof(MultiSelectDialog<>).MakeGenericType(OtherType());
  221. var dlg = (Activator.CreateInstance(dlgtype, filter, null, true) as IMultiSelectDialog)!;
  222. if (dlg.ShowDialog())
  223. {
  224. var guids = CurrentGuids();
  225. foreach (var entity in dlg.Items(null))
  226. {
  227. if (!guids.Contains(entity.ID))
  228. {
  229. var newitem = CreateItem();
  230. var prop = GetOtherLink(newitem);
  231. prop.ID = entity.ID;
  232. prop.Synchronise(entity);
  233. DataComponent.SaveItem(newitem);
  234. }
  235. }
  236. Refresh(false, true);
  237. }
  238. }
  239. else
  240. {
  241. base.DoAdd();
  242. }
  243. }
  244. public override TManyToMany CreateItem()
  245. {
  246. var result = new TManyToMany();
  247. if (Item != null)
  248. {
  249. var prop = GetThisLink(result);
  250. prop.ID = Item.ID;
  251. prop.Synchronise(Item);
  252. }
  253. return result;
  254. }
  255. protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
  256. {
  257. var type = CoreUtils.GetProperty(typeof(TManyToMany), column.ColumnName).DeclaringType;
  258. if (type.GetInterfaces().Contains(typeof(IEntityLink)) && type.ContainsInheritedGenericType(typeof(TThis)))
  259. return new NullEditor();
  260. return base.GetEditor(item, column);
  261. }
  262. public override DynamicEditorPages LoadEditorPages(TManyToMany item)
  263. {
  264. return item.ID != Guid.Empty ? base.LoadEditorPages(item) : new DynamicEditorPages();
  265. }
  266. }