DynamicEntityFormGrid.cs 5.4 KB

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