DynamicEntityFormGrid.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using InABox.WPF;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Media.Imaging;
  11. namespace InABox.DynamicGrid
  12. {
  13. public class DynamicEntityFormGrid<TForm, TEntity, TEntityLink> : DynamicDataGrid<TForm>
  14. where TForm : EntityForm<TEntity, TEntityLink, TForm>, new()
  15. where TEntity : Entity
  16. where TEntityLink : IEntityLink<TEntity>, new()
  17. {
  18. private TEntity Entity { get; set; }
  19. public DynamicEntityFormGrid(TEntity entity)
  20. {
  21. Entity = entity;
  22. OnBeforeSave += BeforeSave;
  23. OnCustomiseEditor += DynamicEntityFormGrid_OnCustomiseEditor;
  24. ActionColumns.Add(new DynamicImageColumn(EditImage, EditClick));
  25. if (DynamicGridUtils.PreviewReport != null)
  26. ActionColumns.Add(new DynamicImageColumn(PrintImage, PrintClick));
  27. HiddenColumns.Add(x => x.FormCompleted);
  28. HiddenColumns.Add(x => x.Form.ID);
  29. }
  30. private void DynamicEntityFormGrid_OnCustomiseEditor(IDynamicEditorForm sender, TForm[]? items, DynamicGridColumn column, BaseEditor editor)
  31. {
  32. if(column.ColumnName == "Form.ID")
  33. {
  34. editor.Editable = Editable.Disabled;
  35. }
  36. }
  37. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  38. {
  39. var filter = LookupFactory.DefineChildFilter<TEntity, DigitalForm>(new TEntity[] { Entity })
  40. ?? LookupFactory.DefineLookupFilter<TForm, DigitalForm, DigitalFormLink>(x => x.Form, Array.Empty<TForm>());
  41. var columns = LookupFactory.DefineLookupColumns<TForm, DigitalForm, DigitalFormLink>(x => x.Form);
  42. var select = new MultiSelectDialog<DigitalForm>(
  43. filter,
  44. columns.Add(x => x.Description),
  45. false);
  46. if (select.ShowDialog() == true)
  47. {
  48. var digitalForm = select.Items().FirstOrDefault();
  49. if(digitalForm is not null)
  50. {
  51. var form = new TForm
  52. {
  53. Description = digitalForm.Description
  54. };
  55. form.Form.ID = digitalForm.ID;
  56. form.Parent.ID = Entity.ID;
  57. SaveItem(form);
  58. Refresh(false, true);
  59. DoChanged();
  60. }
  61. }
  62. }
  63. public override TForm CreateItem()
  64. {
  65. var result = base.CreateItem();
  66. result.Parent.ID = Entity.ID;
  67. return result;
  68. }
  69. protected override void Reload(
  70. Filters<TForm> criteria, Columns<TForm> columns, ref SortOrder<TForm>? sort,
  71. CancellationToken token, Action<CoreTable?, Exception?> action)
  72. {
  73. criteria.Add(new Filter<TForm>(x => x.Parent.ID).IsEqualTo(Entity.ID));
  74. base.Reload(criteria, columns, ref sort, token, action);
  75. }
  76. private void BeforeSave(IDynamicEditorForm editor, BaseObject[] items)
  77. {
  78. foreach(var item in items.Cast<TForm>())
  79. {
  80. item.Parent.ID = Entity.ID;
  81. }
  82. }
  83. private BitmapImage PrintImage(CoreRow? arg)
  84. {
  85. return Wpf.Resources.print.AsBitmapImage();
  86. }
  87. private bool PrintClick(CoreRow? arg)
  88. {
  89. if (arg is null) return false;
  90. var formid = arg.Get<TForm, Guid>(x => x.Form.ID);
  91. var model = new DigitalFormReportDataModel<TForm>(new Filter<TForm>("Parent.ID").IsEqualTo(Entity.ID), formid);
  92. var section = formid.ToString();
  93. // TODO: This is a hack
  94. DynamicGridUtils.PrintMenu?.Invoke(null, section, model, true);
  95. return false;
  96. }
  97. private BitmapImage EditImage(CoreRow? arg)
  98. {
  99. if (arg == null)
  100. return Wpf.Resources.pencil.AsBitmapImage();
  101. var completed = arg.Get<TForm, DateTime>(x => x.FormCompleted);
  102. return completed.IsEmpty() ? Wpf.Resources.pencil.AsBitmapImage() : Wpf.Resources.view.AsBitmapImage();
  103. }
  104. private bool EditClick(CoreRow? arg)
  105. {
  106. if (arg is null) return false;
  107. var item = LoadItem(arg);
  108. var form = new Client<TForm>()
  109. .Query(
  110. new Filter<TForm>(x => x.ID).IsEqualTo(item.ID),
  111. Columns.None<TForm>().Add(x => x.ID)
  112. .Add(x => x.FormData)
  113. .Add(x => x.BlobData)
  114. .Add(x => x.FormCompleted)
  115. .Add(x => x.FormCompletedBy.ID)
  116. .Add(x => x.Form.ID)
  117. .Add(x => x.Parent.ID))
  118. .Rows.FirstOrDefault()?.ToObject<TForm>();
  119. if (form is null) return false;
  120. if (DynamicFormEditWindow.EditDigitalForm(form, out var dataModel))
  121. {
  122. dataModel.Update(null);
  123. return true;
  124. }
  125. return false;
  126. }
  127. }
  128. }