DynamicEntityFormGrid.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 : 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.ID = digitalForm.ID;
  59. form.Parent.ID = Entity.ID;
  60. var refresh = false;
  61. if (EditOnAdd)
  62. {
  63. if (DynamicFormEditWindow.EditDigitalForm(form, out var dataModel))
  64. {
  65. dataModel.Update(null);
  66. refresh = true;
  67. }
  68. }
  69. else
  70. {
  71. SaveItem(form);
  72. refresh = true;
  73. }
  74. if (refresh)
  75. {
  76. Refresh(false, true);
  77. DoChanged();
  78. }
  79. }
  80. }
  81. }
  82. public override TForm CreateItem()
  83. {
  84. var result = base.CreateItem();
  85. result.Parent.ID = Entity.ID;
  86. return result;
  87. }
  88. protected override void Reload(
  89. Filters<TForm> criteria, Columns<TForm> columns, ref SortOrder<TForm>? sort,
  90. CancellationToken token, Action<CoreTable?, Exception?> action)
  91. {
  92. criteria.Add(new Filter<TForm>(x => x.Parent.ID).IsEqualTo(Entity.ID));
  93. base.Reload(criteria, columns, ref sort, token, action);
  94. }
  95. private void BeforeSave(IDynamicEditorForm editor, BaseObject[] items)
  96. {
  97. foreach(var item in items.Cast<TForm>())
  98. {
  99. item.Parent.ID = Entity.ID;
  100. }
  101. }
  102. private BitmapImage PrintImage(CoreRow? arg)
  103. {
  104. return Wpf.Resources.print.AsBitmapImage();
  105. }
  106. private bool PrintClick(CoreRow? arg)
  107. {
  108. if (arg is null) return false;
  109. var formid = arg.Get<TForm, Guid>(x => x.Form.ID);
  110. var model = new DigitalFormReportDataModel<TForm>(new Filter<TForm>("Parent.ID").IsEqualTo(Entity.ID), formid);
  111. var section = formid.ToString();
  112. // TODO: This is a hack
  113. DynamicGridUtils.PrintMenu?.Invoke(null, section, model, true);
  114. return false;
  115. }
  116. private BitmapImage EditImage(CoreRow? arg)
  117. {
  118. if (arg == null)
  119. return Wpf.Resources.pencil.AsBitmapImage();
  120. var completed = arg.Get<TForm, DateTime>(x => x.FormCompleted);
  121. return completed.IsEmpty() ? Wpf.Resources.pencil.AsBitmapImage() : Wpf.Resources.view.AsBitmapImage();
  122. }
  123. private bool EditClick(CoreRow? arg)
  124. {
  125. if (arg is null) return false;
  126. var item = LoadItem(arg);
  127. var form = new Client<TForm>()
  128. .Query(
  129. new Filter<TForm>(x => x.ID).IsEqualTo(item.ID),
  130. Columns.None<TForm>().Add(x => x.ID)
  131. .Add(x => x.FormData)
  132. .Add(x => x.BlobData)
  133. .Add(x => x.FormCompleted)
  134. .Add(x => x.FormCompletedBy.ID)
  135. .Add(x => x.Form.ID)
  136. .Add(x => x.Parent.ID))
  137. .Rows.FirstOrDefault()?.ToObject<TForm>();
  138. if (form is null) return false;
  139. if (DynamicFormEditWindow.EditDigitalForm(form, out var dataModel))
  140. {
  141. dataModel.Update(null);
  142. return true;
  143. }
  144. return false;
  145. }
  146. }