JobScopeGrid.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.Start });
  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. private BitmapImage? IsDefaultImage(CoreRow? row)
  28. {
  29. return row == null
  30. ? tick
  31. : row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID)
  32. ? tick
  33. : null;
  34. }
  35. private bool SelectDefaultAction(CoreRow? row)
  36. {
  37. if ((row == null) ||row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID))
  38. return false;
  39. using (new WaitCursor())
  40. {
  41. var job = new Client<Job>().Query(
  42. new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
  43. new Columns<Job>(x => x.ID).Add(x => x.DefaultScope.ID)
  44. ).Rows.FirstOrDefault()?.ToObject<Job>();
  45. if (job != null)
  46. {
  47. job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
  48. new Client<Job>().Save(job, "Updated Default Scope");
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. public Job Job { get; set; }
  55. protected override bool CanCreateItems()
  56. {
  57. return Job.ID != Guid.Empty;
  58. }
  59. protected override JobScope CreateItem()
  60. {
  61. var result = base.CreateItem();
  62. result.Job.ID = Job.ID;
  63. result.Job.Synchronise(Job);
  64. result.Type = Data.Rows.Any() ? JobScopeType.Variation : JobScopeType.Contract;
  65. return result;
  66. }
  67. protected override void Reload(Filters<JobScope> criteria, Columns<JobScope> columns, ref SortOrder<JobScope>? sort, Action<CoreTable?, Exception?> action)
  68. {
  69. criteria.Add(new Filter<JobScope>(x => x.Job.ID).IsEqualTo(Job.ID));
  70. base.Reload(criteria, columns, ref sort, action);
  71. }
  72. }
  73. }