BaseIntegrationGrid.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections.Generic;
  3. using Comal.Classes;
  4. using InABox.Core;
  5. using InABox.DynamicGrid;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Input;
  11. using System.Windows.Media.Imaging;
  12. using AutoProperties;
  13. using Comal.Classes.SecurityDescriptors;
  14. using InABox.Clients;
  15. using InABox.Configuration;
  16. using InABox.Wpf;
  17. using InABox.WPF;
  18. namespace PRSDesktop.Integrations.Common;
  19. public class IntegrationGridCreateEntityArgs<TEntity, TType>(TEntity entity, TType mapping)
  20. {
  21. public TEntity Entity { get; private set; } = entity;
  22. public TType Mapping { get; private set; } = mapping;
  23. }
  24. public abstract class BaseIntegrationGrid<TType, TEntity,TLink> : DynamicItemsListGrid<TType>
  25. where TType : BaseIntegrationSource<TEntity,TLink>, new()
  26. where TEntity : Entity, IRemotable, IPersistent, new()
  27. where TLink : EntityLink<TEntity>
  28. {
  29. private static readonly DependencyProperty SourceTypeProperty = DependencyProperty.Register(
  30. nameof(SourceType),
  31. typeof(IntegrationSourceType),
  32. typeof(BaseIntegrationGrid<TType, TEntity,TLink>)
  33. );
  34. public IntegrationSourceType SourceType
  35. {
  36. get => (IntegrationSourceType)GetValue(SourceTypeProperty);
  37. set => SetValue(SourceTypeProperty, value);
  38. }
  39. private static readonly DependencyProperty CreateEntityProperty = DependencyProperty.Register(
  40. nameof(CreateEntity),
  41. typeof(ICommand),
  42. typeof(BaseIntegrationGrid<TType, TEntity,TLink>)
  43. );
  44. public ICommand? CreateEntity
  45. {
  46. get => GetValue(CreateEntityProperty) as ICommand;
  47. set => SetValue(CreateEntityProperty, value);
  48. }
  49. private static readonly DependencyProperty AfterCreateEntityProperty = DependencyProperty.Register(
  50. nameof(AfterCreateEntity),
  51. typeof(ICommand),
  52. typeof(BaseIntegrationGrid<TType, TEntity,TLink>)
  53. );
  54. public ICommand? AfterCreateEntity
  55. {
  56. get => GetValue(AfterCreateEntityProperty) as ICommand;
  57. set => SetValue(AfterCreateEntityProperty, value);
  58. }
  59. private DynamicImageColumn _createordelete;
  60. protected override void Init()
  61. {
  62. base.Init();
  63. var required = Columns.Required<TType>();
  64. foreach (var req in required)
  65. HiddenColumns.Add(req.Property);
  66. HiddenColumns.Add(x => x.DirectMatch);
  67. AddButton("Create All", PRSDesktop.Resources.plus.AsBitmapImage(), CreateAll);
  68. _createordelete = new DynamicImageColumn(CreateImage, CreateOrDeleteOne);
  69. ActionColumns.Add(_createordelete);
  70. }
  71. private BitmapImage? CreateImage(CoreRow? row)
  72. {
  73. return row == null
  74. ? PRSDesktop.Resources.plus.AsBitmapImage()
  75. : row.Get<TType, Guid>(x => x.Entity.ID) == Guid.Empty
  76. ? PRSDesktop.Resources.plus.AsBitmapImage()
  77. : row.Get<TType,bool>(x=>x.DirectMatch)
  78. ? PRSDesktop.Resources.locked.AsBitmapImage()
  79. : PRSDesktop.Resources.minus.AsBitmapImage();
  80. }
  81. private bool CreateOrDeleteOne(CoreRow? row)
  82. {
  83. if (row == null)
  84. return false;
  85. if (row.Get<TType, bool>(x => x.DirectMatch) && row.Get<TType,Guid>(x=>x.Entity.ID) != Guid.Empty)
  86. return false;
  87. var item = Items[row.Index];
  88. if (item.Entity.ID == Guid.Empty)
  89. CreateItem(item);
  90. else
  91. DeleteItem(item);
  92. Refresh(false,true);
  93. DoChanged();
  94. return true;
  95. }
  96. private bool CreateAll(Button button, CoreRow[] rows)
  97. {
  98. foreach (var item in Items.Where(x => !string.IsNullOrWhiteSpace(x.Code) && x.Entity.ID == Guid.Empty))
  99. CreateItem(item);
  100. return true;
  101. }
  102. protected abstract Expression<Func<TEntity,object?>> EntityCode { get; }
  103. private void CreateItem(TType item)
  104. {
  105. var lookup = Client.Query<TEntity>(
  106. new Filter<TEntity>(EntityCode).IsEqualTo(item.Code),
  107. Columns.Required<TEntity>().Add(EntityCode)
  108. ).ToArray<TEntity>();
  109. if (lookup.Any())
  110. {
  111. item.Source = SourceType;
  112. item.Entity.CopyFrom(lookup.First());
  113. item.DirectMatch = true;
  114. return;
  115. }
  116. var entity = new TEntity();
  117. var args = new IntegrationGridCreateEntityArgs<TEntity,TType>(entity,item);
  118. try
  119. {
  120. CreateEntity?.Execute(args);
  121. new Client<TEntity>().Save(entity, "Created by Integration Window");
  122. item.Source = SourceType;
  123. item.Entity.CopyFrom(entity);
  124. item.DirectMatch = true;
  125. try
  126. {
  127. var args2 = new IntegrationGridCreateEntityArgs<TEntity,TType>(entity,item);
  128. AfterCreateEntity?.Execute(args2);
  129. }
  130. catch (Exception e)
  131. {
  132. MessageWindow.ShowError(e.Message, e, "AfterCreate Error");
  133. Logger.Send(LogType.Error,"",$"Exception in AfterCreateEntity: {e.Message}\n{e.StackTrace}");
  134. }
  135. }
  136. catch (Exception e)
  137. {
  138. MessageWindow.ShowError(e.Message, e, "CreateItem Error");
  139. Logger.Send(LogType.Error,"",$"Exception in CreateEntity: {e.Message}\n{e.StackTrace}");
  140. }
  141. }
  142. private void DeleteItem(TType item)
  143. {
  144. MultiQuery query = new();
  145. query.Add(
  146. new Filter<TEntity>(EntityCode).IsEqualTo(item.Code),
  147. Columns.Required<TEntity>().Add(x => x.ID).Add(EntityCode)
  148. );
  149. query.Add(
  150. new Filter<TType>(x=>x.Entity.ID).IsEqualTo(item.Entity.ID).And(x=>x.Code).IsEqualTo(item.Code),
  151. Columns.Required<TType>().Add(x=>x.ID)
  152. );
  153. query.Query();
  154. var mappings = query.Get<TType>().ToArray<TType>();
  155. if (mappings.Any())
  156. {
  157. item.ID = Guid.Empty;
  158. Client.Delete(mappings, "Removed By AWG Mapping Window");
  159. }
  160. var entities = query.Get<TEntity>().ToArray<TEntity>();
  161. if (entities.Any())
  162. {
  163. item.Entity.CopyFrom(entities.First());
  164. item.DirectMatch = true;
  165. }
  166. else
  167. {
  168. item.Entity.CopyFrom(new TEntity());
  169. item.DirectMatch = false;
  170. }
  171. item.CommitChanges();
  172. SaveItem(item);
  173. DoChanged();
  174. }
  175. protected override void DoReconfigure(DynamicGridOptions options)
  176. {
  177. base.DoReconfigure(options);
  178. options.AddRows = false;
  179. options.EditRows = false;
  180. options.DeleteRows = false;
  181. options.FilterRows = true;
  182. options.HideDatabaseFilters = true;
  183. options.DirectEdit = true;
  184. options.HideDirectEditButton = true;
  185. options.SelectColumns = true;
  186. options.MultiSelect = true;
  187. options.RecordCount = true;
  188. }
  189. protected override DynamicGridColumns LoadColumns()
  190. {
  191. var result = new DynamicGridColumns();
  192. result.Add<TType>(x => x.Code, 250, "Code");
  193. result.Add<TType>(x => x.Description, 0, "Description");
  194. result.Add<TType>(x => x.Entity.ID, 200, typeof(TEntity).GetCaption());
  195. return result;
  196. }
  197. protected override BaseEditor CustomiseEditor(DynamicGridColumn column, BaseEditor editor)
  198. {
  199. editor = base.CustomiseEditor(column, editor);
  200. if (String.Equals(CoreUtils.GetFullPropertyName<TType, string>(x => x.Code, "."), column.ColumnName))
  201. editor.Editable = Editable.Disabled;
  202. else if (String.Equals(CoreUtils.GetFullPropertyName<TType, string>(x => x.Description, "."), column.ColumnName))
  203. editor.Editable = Editable.Disabled;
  204. else if (String.Equals(CoreUtils.GetFullPropertyName<TType, Guid>(x => x.Entity.ID, "."), column.ColumnName))
  205. {
  206. var popup = new CodePopupEditor(typeof(TEntity)) { CodeColumn = "Code", CanAdd = Security.CanEdit<TEntity>() };
  207. var required = Columns.Required<TEntity>();
  208. foreach (var req in required)
  209. popup.OtherColumns[$"Entity.{req.Property}"] = req.Property;
  210. popup.OnCreateItem += o =>
  211. {
  212. };
  213. editor = popup;
  214. }
  215. // else if(new Column<TType>(x => x.Entity.ID).IsEqualTo(column.ColumnName) && editor is CodePopupEditor popup)
  216. // popup.CanAdd = Security.CanEdit<TEntity>();
  217. return editor;
  218. }
  219. protected override void OnAfterEditorValueChanged(DynamicEditorGrid? grid, TType[] items, AfterEditorValueChangedArgs args,
  220. Dictionary<string, object?> changes)
  221. {
  222. base.OnAfterEditorValueChanged(grid, items, args, changes);
  223. //DoChanged();
  224. }
  225. public override void SaveItem(TType item)
  226. {
  227. var code = CoreUtils.GetPropertyValue(item.Entity, CoreUtils.GetFullPropertyName(EntityCode, "."))?.ToString() ?? "";
  228. item.DirectMatch = Equals(code, item.Code);
  229. if (!Equals(item.ID, Guid.Empty) && (Equals(code, item.Code) || string.IsNullOrWhiteSpace(code)))
  230. {
  231. Client.Delete(item, "Removed By AWG Mapping Window");
  232. item.ID = Guid.Empty;
  233. item.CommitChanges();
  234. return;
  235. }
  236. if (!string.IsNullOrWhiteSpace(code) && !Equals(code,item.Code))
  237. Client.Save(item,"Updated by Integration Window ");
  238. base.SaveItem(item);
  239. //DoChanged();
  240. }
  241. }