DynamicEntityFormGrid.cs 5.0 KB

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