BaseIntegrationGrid.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. try
  63. {
  64. new Client<TEntity>().Save(entity, "Created by Integration Window");
  65. item.Source = SourceType;
  66. item.Entity.CopyFrom(entity);
  67. }
  68. catch (Exception e)
  69. {
  70. Logger.Send(LogType.Error,"",$"{e.Message}\n{e.StackTrace}");
  71. }
  72. }
  73. }
  74. return true;
  75. }
  76. protected override void DoReconfigure(DynamicGridOptions options)
  77. {
  78. base.DoReconfigure(options);
  79. options.AddRows = false;
  80. options.EditRows = false;
  81. options.DeleteRows = false;
  82. options.FilterRows = true;
  83. options.HideDatabaseFilters = true;
  84. options.DirectEdit = true;
  85. options.HideDirectEditButton = true;
  86. options.SelectColumns = true;
  87. options.MultiSelect = true;
  88. options.RecordCount = true;
  89. }
  90. protected override DynamicGridColumns LoadColumns()
  91. {
  92. var result = new DynamicGridColumns();
  93. result.Add<TType>(x => x.Code, 200, "Code");
  94. result.Add<TType>(x => x.Description, 0, "Description");
  95. result.Add<TType>(x => x.Entity.ID, 200, typeof(TEntity).GetCaption());
  96. return result;
  97. }
  98. protected override BaseEditor CustomiseEditor(DynamicGridColumn column, BaseEditor editor)
  99. {
  100. editor = base.CustomiseEditor(column, editor);
  101. if (String.Equals(CoreUtils.GetFullPropertyName<TType, string>(x => x.Code, "."), column.ColumnName))
  102. editor.Editable = Editable.Disabled;
  103. else if (String.Equals(CoreUtils.GetFullPropertyName<TType, string>(x => x.Description, "."), column.ColumnName))
  104. editor.Editable = Editable.Disabled;
  105. else if (String.Equals(CoreUtils.GetFullPropertyName<TType, Guid>(x => x.Entity.ID, "."), column.ColumnName))
  106. {
  107. var popup = new CodePopupEditor(typeof(TEntity)) { CodeColumn = "Code", CanAdd = Security.CanEdit<TEntity>() };
  108. var required = Columns.Required<TEntity>();
  109. foreach (var req in required)
  110. popup.OtherColumns[$"Entity.{req.Property}"] = req.Property;
  111. editor = popup;
  112. }
  113. // else if(new Column<TType>(x => x.Entity.ID).IsEqualTo(column.ColumnName) && editor is CodePopupEditor popup)
  114. // popup.CanAdd = Security.CanEdit<TEntity>();
  115. return editor;
  116. }
  117. public override void SaveItem(TType item)
  118. {
  119. base.SaveItem(item);
  120. Client.Save(item,"Updated by Integration Window ");
  121. }
  122. }