JobScopeDocumentGrid.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.Wpf;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. namespace PRSDesktop;
  15. public class JobScopeDocumentGrid : DynamicDocumentGrid<JobScopeDocument, JobScope, JobScopeLink>, IJobScopeGrid
  16. {
  17. public JobScope? Scope
  18. {
  19. get => Item;
  20. set
  21. {
  22. Load(value ?? new JobScope(), null);
  23. }
  24. }
  25. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  26. {
  27. if((Scope?.ID ?? Guid.Empty) == Guid.Empty)
  28. {
  29. MessageWindow.ShowMessage("Please select a scope.", "No scope selected.");
  30. return;
  31. }
  32. base.DoAdd(OpenEditorOnDirectEdit);
  33. }
  34. public override void SaveItem(JobScopeDocument item)
  35. {
  36. Client.Save(item, "Updated by user.");
  37. }
  38. public override void SaveItems(IEnumerable<JobScopeDocument> items)
  39. {
  40. Client.Save(items, "Updated by user.");
  41. }
  42. public override void DeleteItems(params CoreRow[] rows)
  43. {
  44. Client.Delete(rows.Select(x => new JobScopeDocument { ID = x.Get<JobScopeDocument, Guid>(x => x.ID) }), "Deleted by user.");
  45. }
  46. public override JobScopeDocument LoadItem(CoreRow row)
  47. {
  48. var id = row.Get<JobScopeDocument, Guid>(x => x.ID);
  49. return Client.Query(
  50. new Filter<JobScopeDocument>(x => x.ID).IsEqualTo(id),
  51. DynamicGridUtils.LoadEditorColumns(DataColumns()))
  52. .ToObjects<JobScopeDocument>()
  53. .FirstOrDefault()
  54. ?? throw new Exception($"No {nameof(JobScopeDocument)} with ID {id}");
  55. }
  56. public override JobScopeDocument[] LoadItems(IList<CoreRow> rows)
  57. {
  58. var ids = rows.ToArray(x => x.Get<JobScopeDocument, Guid>(x => x.ID));
  59. return Client.Query(
  60. new Filter<JobScopeDocument>(x => x.ID).InList(ids),
  61. DynamicGridUtils.LoadEditorColumns(DataColumns()))
  62. .ToObjects<JobScopeDocument>().ToArray();
  63. }
  64. protected override void Reload(
  65. Filters<JobScopeDocument> criteria, Columns<JobScopeDocument> columns, ref SortOrder<JobScopeDocument>? sort,
  66. CancellationToken token, Action<CoreTable?, Exception?> action)
  67. {
  68. if(Scope is null || Scope.ID == Guid.Empty)
  69. {
  70. criteria.Add(new Filter<JobScopeDocument>().None());
  71. }
  72. else
  73. {
  74. criteria.Add(new Filter<JobScopeDocument>(x => x.EntityLink.ID).IsEqualTo(Scope?.ID ?? Guid.Empty));
  75. }
  76. Client.Query(criteria.Combine(), columns, sort, CoreRange.All, action);
  77. }
  78. }