DynamicEntityFormGrid.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. protected virtual TEntity Entity { get; set; }
  19. public DynamicEntityFormGrid()
  20. {
  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. public DynamicEntityFormGrid(TEntity entity) : this()
  30. {
  31. Entity = entity;
  32. }
  33. private void DynamicEntityFormGrid_OnCustomiseEditor(IDynamicEditorForm sender, TForm[]? items, DynamicGridColumn column, BaseEditor editor)
  34. {
  35. if(column.ColumnName == "Form.ID")
  36. {
  37. editor.Editable = Editable.Disabled;
  38. }
  39. }
  40. protected virtual void OnFormCreated(TForm form)
  41. {
  42. }
  43. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  44. {
  45. var filter = LookupFactory.DefineChildFilter<TEntity, DigitalForm>(new TEntity[] { Entity })
  46. ?? LookupFactory.DefineLookupFilter<TForm, DigitalForm, DigitalFormLink>(x => x.Form, Array.Empty<TForm>());
  47. var columns = LookupFactory.DefineLookupColumns<TForm, DigitalForm, DigitalFormLink>(x => x.Form);
  48. var select = new MultiSelectDialog<DigitalForm>(
  49. filter,
  50. columns.Add(x => x.Description),
  51. false);
  52. if (select.ShowDialog() == true)
  53. {
  54. var digitalForm = select.Items().FirstOrDefault();
  55. if(digitalForm is not null)
  56. {
  57. var form = new TForm
  58. {
  59. Description = digitalForm.Description
  60. };
  61. form.Form.ID = digitalForm.ID;
  62. form.Parent.ID = Entity.ID;
  63. SaveItem(form);
  64. OnFormCreated(form);
  65. Refresh(false, true);
  66. DoChanged();
  67. }
  68. }
  69. }
  70. public override TForm CreateItem()
  71. {
  72. var result = base.CreateItem();
  73. result.Parent.ID = Entity.ID;
  74. return result;
  75. }
  76. protected override void Reload(
  77. Filters<TForm> criteria, Columns<TForm> columns, ref SortOrder<TForm>? sort,
  78. CancellationToken token, Action<CoreTable?, Exception?> action)
  79. {
  80. criteria.Add(new Filter<TForm>(x => x.Parent.ID).IsEqualTo(Entity.ID));
  81. base.Reload(criteria, columns, ref sort, token, action);
  82. }
  83. private void BeforeSave(IDynamicEditorForm editor, BaseObject[] items)
  84. {
  85. foreach(var item in items.Cast<TForm>())
  86. {
  87. item.Parent.ID = Entity.ID;
  88. }
  89. }
  90. private BitmapImage PrintImage(CoreRow? arg)
  91. {
  92. return Wpf.Resources.print.AsBitmapImage();
  93. }
  94. private bool PrintClick(CoreRow? arg)
  95. {
  96. if (arg is null) return false;
  97. var formid = arg.Get<TForm, Guid>(x => x.Form.ID);
  98. var model = new DigitalFormReportDataModel<TForm>(new Filter<TForm>("Parent.ID").IsEqualTo(Entity.ID), formid);
  99. var section = formid.ToString();
  100. // TODO: This is a hack
  101. DynamicGridUtils.PrintMenu?.Invoke(null, section, model, true);
  102. return false;
  103. }
  104. private BitmapImage EditImage(CoreRow? arg)
  105. {
  106. if (arg == null)
  107. return Wpf.Resources.pencil.AsBitmapImage();
  108. var completed = arg.Get<TForm, DateTime>(x => x.FormCompleted);
  109. return completed.IsEmpty() ? Wpf.Resources.pencil.AsBitmapImage() : Wpf.Resources.view.AsBitmapImage();
  110. }
  111. private bool EditClick(CoreRow? arg)
  112. {
  113. if (arg is null) return false;
  114. var item = LoadItem(arg);
  115. var form = new Client<TForm>()
  116. .Query(
  117. new Filter<TForm>(x => x.ID).IsEqualTo(item.ID),
  118. Columns.None<TForm>().Add(x => x.ID)
  119. .Add(x => x.FormData)
  120. .Add(x => x.BlobData)
  121. .Add(x => x.FormCompleted)
  122. .Add(x => x.FormCompletedBy.ID)
  123. .Add(x => x.Form.ID)
  124. .Add(x => x.Parent.ID))
  125. .Rows.FirstOrDefault()?.ToObject<TForm>();
  126. if (form is null) return false;
  127. if (DynamicFormEditWindow.EditDigitalForm(form, out var dataModel))
  128. {
  129. dataModel.Update(null);
  130. return true;
  131. }
  132. return false;
  133. }
  134. }
  135. }