JobProductStylesGrid.cs 4.7 KB

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