JobScopeGrid.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  17. {
  18. base.DoReconfigure(options);
  19. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows);
  20. HiddenColumns.Add(x=>x.Job.DefaultScope.ID);
  21. HiddenColumns.Add(x=>x.Job.ID);
  22. ActionColumns.Add(new DynamicImageColumn(IsDefaultImage, SelectDefaultAction) { Position = DynamicActionColumnPosition.Start });
  23. }
  24. private BitmapImage? IsDefaultImage(CoreRow? row)
  25. {
  26. return row == null
  27. ? tick
  28. : row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID)
  29. ? tick
  30. : null;
  31. }
  32. private bool SelectDefaultAction(CoreRow? row)
  33. {
  34. if ((row == null) ||row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID))
  35. return false;
  36. using (new WaitCursor())
  37. {
  38. var job = new Client<Job>().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. new Client<Job>().Save(job, "Updated Default Scope");
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. public Job Job { get; set; }
  52. protected override bool CanCreateItems()
  53. {
  54. return Job.ID != Guid.Empty;
  55. }
  56. protected override JobScope CreateItem()
  57. {
  58. var result = base.CreateItem();
  59. result.Job.ID = Job.ID;
  60. result.Job.Synchronise(Job);
  61. result.Type = Data.Rows.Any() ? JobScopeType.Variation : JobScopeType.Contract;
  62. return result;
  63. }
  64. protected override void Reload(Filters<JobScope> criteria, Columns<JobScope> columns, ref SortOrder<JobScope> sort,
  65. Action<CoreTable, Exception> action)
  66. {
  67. criteria.Add(new Filter<JobScope>(x => x.Job.ID).IsEqualTo(Job.ID));
  68. base.Reload(criteria, columns, ref sort, action);
  69. }
  70. }
  71. }