JobScopeGrid.cs 3.7 KB

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