JobProductMappingsGrid.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.Wpf;
  6. using Syncfusion.Data.Extensions;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace PRSDesktop.Panels.Jobs;
  13. public class JobProductMappingsGrid : DynamicDataGrid<JobProductMapping>, IJobControl, IDataModelSource
  14. {
  15. public Job Job { get; set; }
  16. JobPanelSettings IJobControl.Settings { get; set; }
  17. protected override void Init()
  18. {
  19. base.Init();
  20. HiddenColumns.Add(x => x.JobDocumentSet.ID);
  21. HiddenColumns.Add(x => x.Code);
  22. HiddenColumns.Add(x => x.Description);
  23. ActionColumns.Add(new DynamicMenuColumn(BuildMenu));
  24. }
  25. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  26. {
  27. base.DoReconfigure(options);
  28. options.Add(DynamicGridOption.SelectColumns);
  29. }
  30. private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
  31. {
  32. if (row is not null)
  33. {
  34. var docSet = row.Get<JobProductMapping, Guid>(x => x.JobDocumentSet.ID);
  35. if(docSet != Guid.Empty)
  36. {
  37. var caption = Security.CanEdit<JobDocumentSet>() ? "Edit Document Set" : "View Document Set";
  38. column.AddItem("Edit Document Set", PRSDesktop.Resources.design, (_) =>
  39. {
  40. ViewDocumentSet(docSet);
  41. });
  42. }
  43. else
  44. {
  45. if (Security.CanEdit<JobDocumentSet>())
  46. {
  47. column.AddItem("Create Document Set", PRSDesktop.Resources.design, (_) =>
  48. {
  49. CreateDocumentSet(row);
  50. });
  51. }
  52. }
  53. }
  54. }
  55. private static DynamicDataGrid<JobDocumentSet> GetDocumentSetGrid()
  56. {
  57. var gridType = DynamicGridUtils.FindDynamicGrid(typeof(DynamicDataGrid<>), typeof(JobDocumentSet))
  58. ?? typeof(DynamicDataGrid<JobDocumentSet>);
  59. return (Activator.CreateInstance(gridType) as DynamicDataGrid<JobDocumentSet>)!;
  60. }
  61. private void ViewDocumentSet(Guid docSetID)
  62. {
  63. var docSet = Client.Query(
  64. new Filter<JobDocumentSet>(x => x.ID).IsEqualTo(docSetID),
  65. DynamicGridUtils.LoadEditorColumns<JobDocumentSet>(new Columns<JobDocumentSet>(x => x.ID)))
  66. .ToObjects<JobDocumentSet>()
  67. .FirstOrDefault();
  68. if(docSet is null)
  69. {
  70. MessageWindow.ShowMessage("Document set does not exist.", "Error");
  71. Logger.Send(LogType.Information, "", "Document set does not exist.");
  72. return;
  73. }
  74. GetDocumentSetGrid().EditItems(new JobDocumentSet[] { docSet });
  75. }
  76. private void CreateDocumentSet(CoreRow row)
  77. {
  78. var folder = new MultiSelectDialog<JobDocumentSetFolder>(
  79. new Filter<JobDocumentSetFolder>(x => x.Job.ID).IsEqualTo(Job.ID),
  80. new Columns<JobDocumentSetFolder>(x => x.ID),
  81. multiselect: false);
  82. if (!folder.ShowDialog())
  83. {
  84. MessageWindow.ShowMessage("You need to select a folder to add this document set to.", "Folder required");
  85. return;
  86. }
  87. var code = row.Get<JobProductMapping, string>(x => x.Code);
  88. var docSet = new JobDocumentSet
  89. {
  90. Code = code,
  91. Description = row.Get<JobProductMapping, string>(x => x.Description) + $"\nAttached to JobProductMapping '{code}'"
  92. };
  93. docSet.Job.ID = Job.ID;
  94. docSet.Folder.ID = folder.IDs().First();
  95. if(GetDocumentSetGrid().EditItems(new JobDocumentSet[] { docSet }))
  96. {
  97. var productMapping = row.ToObject<JobProductMapping>();
  98. productMapping.JobDocumentSet.ID = docSet.ID;
  99. Client.Save(productMapping, "Attached to new JobDocumentSet");
  100. Refresh(false, true);
  101. }
  102. }
  103. protected override bool CanCreateItems()
  104. {
  105. return Job.ID != Guid.Empty;
  106. }
  107. protected override JobProductMapping CreateItem()
  108. {
  109. var result = base.CreateItem();
  110. result.Job.ID = Job.ID;
  111. result.Job.Synchronise(Job);
  112. return result;
  113. }
  114. protected override void Reload(Filters<JobProductMapping> criteria, Columns<JobProductMapping> columns, ref SortOrder<JobProductMapping>? sort, Action<CoreTable?, Exception?> action)
  115. {
  116. criteria.Add(new Filter<JobProductMapping>(x => x.Job.ID).IsEqualTo(Job.ID));
  117. base.Reload(criteria, columns, ref sort, action);
  118. }
  119. #region IPanel
  120. public string SectionName => "Job Product Mappings";
  121. public event DataModelUpdateEvent? OnUpdateDataModel;
  122. public DataModel DataModel(Selection selection)
  123. {
  124. var ids = ExtractValues(x => x.ID, selection).ToArray();
  125. return new BaseDataModel<JobProductMapping>(new Filter<JobProductMapping>(x => x.ID).InList(ids));
  126. }
  127. #endregion
  128. }