JobScopeGrid.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Media.Imaging;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. using InABox.Wpf;
  12. namespace PRSDesktop
  13. {
  14. internal class JobScopeGrid : DynamicDataGrid<JobScope>, IMasterDetailControl<Job,JobScope>
  15. {
  16. private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
  17. public Job? Master { get; set; }
  18. public Filter<JobScope> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
  19. ? new Filter<JobScope>(x => x.Job.ID).IsEqualTo(Master.ID)
  20. : new Filter<JobScope>().None();
  21. public JobScopeGrid()
  22. {
  23. HiddenColumns.Add(x=>x.Job.DefaultScope.ID);
  24. HiddenColumns.Add(x=>x.Job.ID);
  25. ActionColumns.Add(new DynamicImageColumn(IsDefaultImage, SelectDefaultAction) { Position = DynamicActionColumnPosition.End });
  26. }
  27. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  28. {
  29. base.DoReconfigure(options);
  30. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows);
  31. }
  32. public override void DeleteItems(params CoreRow[] rows)
  33. {
  34. base.DeleteItems(rows);
  35. var row = rows.FirstOrDefault(x => x.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == x.Get<JobScope, Guid>(x => x.ID));
  36. if (row is not null)
  37. {
  38. var job = Client.Query(
  39. new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
  40. new Columns<Job>(x => x.ID).Add(x => x.DefaultScope.ID)
  41. ).Rows.FirstOrDefault()?.ToObject<Job>();
  42. if (job != null)
  43. {
  44. job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
  45. Client.Save(job, "Updated Default Scope");
  46. }
  47. }
  48. }
  49. private BitmapImage? IsDefaultImage(CoreRow? row)
  50. {
  51. return row == null
  52. ? tick
  53. : row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID)
  54. ? tick
  55. : null;
  56. }
  57. private bool SelectDefaultAction(CoreRow? row)
  58. {
  59. if ((row == null) ||row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID))
  60. return false;
  61. using (new WaitCursor())
  62. {
  63. var job = new Client<Job>().Query(
  64. new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
  65. new Columns<Job>(x => x.ID).Add(x => x.DefaultScope.ID)
  66. ).Rows.FirstOrDefault()?.ToObject<Job>();
  67. if (job != null)
  68. {
  69. job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
  70. new Client<Job>().Save(job, "Updated Default Scope");
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. protected override bool CanCreateItems()
  77. {
  78. return base.CanCreateItems() && (Master?.ID ?? Guid.Empty) != Guid.Empty;
  79. }
  80. public override JobScope CreateItem()
  81. {
  82. var result = base.CreateItem();
  83. result.Job.ID = Master?.ID ?? Guid.Empty;
  84. result.Job.Synchronise(Master ?? new Job());
  85. result.Type = Data.Rows.Any() ? JobScopeType.Variation : JobScopeType.Contract;
  86. return result;
  87. }
  88. protected override void Reload(Filters<JobScope> criteria, Columns<JobScope> columns, ref SortOrder<JobScope>? sort, Action<CoreTable?, Exception?> action)
  89. {
  90. criteria.Add(MasterDetailFilter);
  91. base.Reload(criteria, columns, ref sort, action);
  92. }
  93. }
  94. }