BaseIntegrationGrid.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 SourceTypeProperty = DependencyProperty.Register(
  26. nameof(SourceType),
  27. typeof(IntegrationSourceType),
  28. typeof(BaseIntegrationGrid<TType, TEntity,TLink>)
  29. );
  30. public IntegrationSourceType SourceType
  31. {
  32. get => (IntegrationSourceType)GetValue(SourceTypeProperty);
  33. set => SetValue(SourceTypeProperty, value);
  34. }
  35. private static readonly DependencyProperty CreateEntityProperty = DependencyProperty.Register(
  36. nameof(CreateEntity),
  37. typeof(ICommand),
  38. typeof(BaseIntegrationGrid<TType, TEntity,TLink>)
  39. );
  40. public ICommand? CreateEntity
  41. {
  42. get => GetValue(CreateEntityProperty) as ICommand;
  43. set => SetValue(CreateEntityProperty, value);
  44. }
  45. protected override void Init()
  46. {
  47. base.Init();
  48. var required = Columns.Required<TType>();
  49. foreach (var req in required)
  50. HiddenColumns.Add(req.Property);
  51. AddButton("Create All", PRSDesktop.Resources.plus.AsBitmapImage(), CreateAll);
  52. }
  53. private bool CreateAll(Button button, CoreRow[] rows)
  54. {
  55. foreach (var item in Items.Where(x => !string.IsNullOrWhiteSpace(x.Code) && x.Entity.ID == Guid.Empty))
  56. {
  57. var entity = new TEntity();
  58. var args = new IntegrationGridCreateEntityArgs<TEntity,TType>(entity,item);
  59. CreateEntity?.Execute(args);
  60. if (!args.Cancel)
  61. {
  62. item.Source = SourceType;
  63. new Client<TEntity>().Save(entity, "Created by Integration Window");
  64. item.Entity.CopyFrom(entity);
  65. }
  66. }
  67. return true;
  68. }
  69. protected override void DoReconfigure(DynamicGridOptions options)
  70. {
  71. base.DoReconfigure(options);
  72. options.AddRows = false;
  73. options.EditRows = false;
  74. options.DeleteRows = false;
  75. options.FilterRows = true;
  76. options.HideDatabaseFilters = true;
  77. options.DirectEdit = true;
  78. options.HideDirectEditButton = true;
  79. options.SelectColumns = true;
  80. options.MultiSelect = true;
  81. }
  82. protected override DynamicGridColumns LoadColumns()
  83. {
  84. var result = new DynamicGridColumns();
  85. result.Add<TType>(x => x.Code, 200, "Code");
  86. result.Add<TType>(x => x.Description, 0, "Description");
  87. result.Add<TType>(x => x.Entity.ID, 200, typeof(TEntity).GetCaption());
  88. return result;
  89. }
  90. protected override BaseEditor CustomiseEditor(DynamicGridColumn column, BaseEditor editor)
  91. {
  92. editor = base.CustomiseEditor(column, editor);
  93. if (String.Equals(CoreUtils.GetFullPropertyName<TType, string>(x => x.Code, "."), column.ColumnName))
  94. editor.Editable = Editable.Disabled;
  95. else if (String.Equals(CoreUtils.GetFullPropertyName<TType, string>(x => x.Description, "."), column.ColumnName))
  96. editor.Editable = Editable.Disabled;
  97. else if (String.Equals(CoreUtils.GetFullPropertyName<TType, Guid>(x => x.Entity.ID, "."), column.ColumnName))
  98. {
  99. var popup = new CodePopupEditor(typeof(TEntity)) { CodeColumn = "Code", CanAdd = Security.CanEdit<TEntity>() };
  100. var required = Columns.Required<TEntity>();
  101. foreach (var req in required)
  102. popup.OtherColumns[$"Entity.{req.Property}"] = req.Property;
  103. editor = popup;
  104. }
  105. // else if(new Column<TType>(x => x.Entity.ID).IsEqualTo(column.ColumnName) && editor is CodePopupEditor popup)
  106. // popup.CanAdd = Security.CanEdit<TEntity>();
  107. return editor;
  108. }
  109. public override void SaveItem(TType item)
  110. {
  111. base.SaveItem(item);
  112. Client.Save(item,"Updated by Integration Window ");
  113. }
  114. }