JobScopeDocumentGrid.cs 2.6 KB

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