DynamicEntityFormGrid.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 : BaseEntityForm<TEntity, TEntityLink, TForm>, new()
  14. where TEntity : Entity
  15. where TEntityLink : BaseObject, IEntityLink<TEntity>, new()
  16. {
  17. public delegate void CustomiseNewFormHandler(TEntity entity, TForm form);
  18. public CustomiseNewFormHandler? CustomiseNewForm;
  19. public Action? OnSaved;
  20. protected virtual TEntity Entity { get; set; }
  21. public bool EditOnAdd { get; set; }
  22. public DynamicEntityFormGrid()
  23. {
  24. OnBeforeSave += BeforeSave;
  25. OnCustomiseEditor += DynamicEntityFormGrid_OnCustomiseEditor;
  26. ActionColumns.Add(new DynamicImageColumn(EditImage, EditClick));
  27. if (DynamicGridUtils.PreviewReport != null)
  28. ActionColumns.Add(new DynamicImageColumn(PrintImage, PrintClick));
  29. HiddenColumns.Add(x => x.FormCompleted);
  30. HiddenColumns.Add(x => x.Form.ID);
  31. }
  32. public DynamicEntityFormGrid(TEntity entity) : this()
  33. {
  34. Entity = entity;
  35. }
  36. private void DynamicEntityFormGrid_OnCustomiseEditor(IDynamicEditorForm sender, TForm[]? items, DynamicGridColumn column, BaseEditor editor)
  37. {
  38. if(column.ColumnName == "Form.ID")
  39. {
  40. editor.Editable = Editable.Disabled;
  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.CopyFrom(digitalForm);
  62. form.Parent.ID = Entity.ID;
  63. CustomiseNewForm?.Invoke(Entity, form);
  64. var refresh = false;
  65. if (EditOnAdd)
  66. {
  67. form.FormStarted = DateTime.Now;
  68. if (DynamicFormEditWindow.EditDigitalForm(form, out var dataModel))
  69. {
  70. dataModel.Update(null);
  71. refresh = true;
  72. }
  73. }
  74. else
  75. {
  76. SaveItem(form);
  77. refresh = true;
  78. }
  79. if (refresh)
  80. {
  81. Refresh(false, true);
  82. DoChanged();
  83. }
  84. }
  85. }
  86. }
  87. public override TForm CreateItem()
  88. {
  89. var result = base.CreateItem();
  90. result.Parent.ID = Entity.ID;
  91. CustomiseNewForm?.Invoke(Entity, result);
  92. return result;
  93. }
  94. protected override void Reload(
  95. Filters<TForm> criteria, Columns<TForm> columns, ref SortOrder<TForm>? sort,
  96. CancellationToken token, Action<CoreTable?, Exception?> action)
  97. {
  98. criteria.Add(Filter<TForm>.Where(x => x.Parent.ID).IsEqualTo(Entity.ID));
  99. base.Reload(criteria, columns, ref sort, token, action);
  100. }
  101. private void BeforeSave(IDynamicEditorForm editor, BaseObject[] items)
  102. {
  103. foreach(var item in items.Cast<TForm>())
  104. {
  105. item.Parent.ID = Entity.ID;
  106. }
  107. }
  108. private BitmapImage PrintImage(CoreRow? arg)
  109. {
  110. return Wpf.Resources.print.AsBitmapImage();
  111. }
  112. private bool PrintClick(CoreRow? arg)
  113. {
  114. if (arg is null) return false;
  115. var formid = arg.Get<TForm, Guid>(x => x.Form.ID);
  116. var model = new DigitalFormReportDataModel<TForm>(new Filter<TForm>("Parent.ID").IsEqualTo(Entity.ID), formid);
  117. var section = formid.ToString();
  118. // TODO: This is a hack
  119. DynamicGridUtils.PrintMenu?.Invoke(null, section, model, true);
  120. return false;
  121. }
  122. private BitmapImage EditImage(CoreRow? arg)
  123. {
  124. if (arg == null)
  125. return Wpf.Resources.pencil.AsBitmapImage();
  126. var completed = arg.Get<TForm, DateTime>(x => x.FormCompleted);
  127. return completed.IsEmpty() ? Wpf.Resources.pencil.AsBitmapImage() : Wpf.Resources.view.AsBitmapImage();
  128. }
  129. private bool EditClick(CoreRow? arg)
  130. {
  131. if (arg is null) return false;
  132. var item = LoadItem(arg);
  133. var form = new Client<TForm>()
  134. .Query(
  135. Filter<TForm>.Where(x => x.ID).IsEqualTo(item.ID),
  136. Columns.None<TForm>().Add(x => x.ID)
  137. .Add(x => x.FormData)
  138. .Add(x => x.BlobData)
  139. .Add(x => x.FormCompleted)
  140. .Add(x => x.FormCompletedBy.ID)
  141. .Add(x => x.Form.ID)
  142. .Add(x => x.Parent.ID))
  143. .Rows.FirstOrDefault()?.ToObject<TForm>();
  144. if (form is null) return false;
  145. if (DynamicFormEditWindow.EditDigitalForm(form, out var dataModel))
  146. {
  147. dataModel.Update(null);
  148. DoChanged();
  149. return true;
  150. }
  151. return false;
  152. }
  153. }