BaseIntegrationGrid.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using AutoProperties;
  11. using InABox.Clients;
  12. using InABox.WPF;
  13. namespace PRSDesktop.Integrations.Common;
  14. public class IntegrationGridCreateEntityArgs<TEntity, TType>(TEntity entity, TType mapping)
  15. {
  16. public TEntity Entity { get; private set; } = entity;
  17. public TType Mapping { get; private set; } = mapping;
  18. public bool Cancel { get; set; }
  19. }
  20. public abstract class BaseIntegrationGrid<TType, TEntity,TLink> : DynamicItemsListGrid<TType>
  21. where TType : BaseIntegrationSource<TEntity,TLink>, new()
  22. where TEntity : Entity, IRemotable, IPersistent, new()
  23. where TLink : EntityLink<TEntity>
  24. {
  25. private static readonly DependencyProperty CreateEntityProperty = DependencyProperty.Register(
  26. nameof(CreateEntity),
  27. typeof(ICommand),
  28. typeof(BaseIntegrationGrid<TType, TEntity,TLink>)
  29. );
  30. public ICommand? CreateEntity
  31. {
  32. get => GetValue(CreateEntityProperty) as ICommand;
  33. set => SetValue(CreateEntityProperty, value);
  34. }
  35. protected override void Init()
  36. {
  37. base.Init();
  38. var required = Columns.Required<TType>();
  39. foreach (var req in required)
  40. HiddenColumns.Add(req.Property);
  41. AddButton("Create All", PRSDesktop.Resources.plus.AsBitmapImage(), CreateAll);
  42. }
  43. private bool CreateAll(Button button, CoreRow[] rows)
  44. {
  45. foreach (var item in Items.Where(x => !string.IsNullOrWhiteSpace(x.Code) && x.Entity.ID == Guid.Empty))
  46. {
  47. var entity = new TEntity();
  48. var args = new IntegrationGridCreateEntityArgs<TEntity,TType>(entity,item);
  49. CreateEntity?.Execute(args);
  50. if (!args.Cancel)
  51. {
  52. new Client<TEntity>().Save(entity, "Created by Integration Window");
  53. item.Entity.CopyFrom(entity);
  54. }
  55. }
  56. return true;
  57. }
  58. protected override void DoReconfigure(DynamicGridOptions options)
  59. {
  60. base.DoReconfigure(options);
  61. options.AddRows = false;
  62. options.EditRows = false;
  63. options.DeleteRows = false;
  64. options.FilterRows = true;
  65. options.HideDatabaseFilters = true;
  66. options.DirectEdit = true;
  67. options.HideDirectEditButton = true;
  68. options.SelectColumns = true;
  69. options.MultiSelect = true;
  70. }
  71. protected override DynamicGridColumns LoadColumns()
  72. {
  73. var result = new DynamicGridColumns();
  74. result.Add<TType>(x => x.Code, 200, "Code");
  75. result.Add<TType>(x => x.Description, 0, "Description");
  76. result.Add<TType>(x => x.Entity.ID, 200, typeof(TEntity).GetCaption());
  77. return result;
  78. }
  79. protected override BaseEditor CustomiseEditor(DynamicGridColumn column, BaseEditor editor)
  80. {
  81. editor = base.CustomiseEditor(column, editor);
  82. if (String.Equals(CoreUtils.GetFullPropertyName<TType, string>(x => x.Code, "."), column.ColumnName))
  83. editor.Editable = Editable.Disabled;
  84. else if (String.Equals(CoreUtils.GetFullPropertyName<TType, string>(x => x.Description, "."), column.ColumnName))
  85. editor.Editable = Editable.Disabled;
  86. else if (String.Equals(CoreUtils.GetFullPropertyName<TType, Guid>(x => x.Entity.ID, "."), column.ColumnName))
  87. {
  88. var popup = new CodePopupEditor(typeof(TEntity)) { CodeColumn = "Code", CanAdd = Security.CanEdit<TEntity>() };
  89. var required = Columns.Required<TEntity>();
  90. foreach (var req in required)
  91. popup.OtherColumns[$"Entity.{req.Property}"] = req.Property;
  92. editor = popup;
  93. }
  94. // else if(new Column<TType>(x => x.Entity.ID).IsEqualTo(column.ColumnName) && editor is CodePopupEditor popup)
  95. // popup.CanAdd = Security.CanEdit<TEntity>();
  96. return editor;
  97. }
  98. public override void SaveItem(TType item)
  99. {
  100. base.SaveItem(item);
  101. Client.Save(item,"Updated by Integration Window ");
  102. }
  103. }