BaseIntegrationGrid.cs 3.7 KB

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