FormPickerQueryLoader.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using comal.timesheets.QAForms;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace comal.timesheets
  8. {
  9. public class FormPickerQueryLoader<TEntity, TEntityLink, TInstance> : IFormPickerQueryLoader
  10. where TEntity : Entity, IRemotable, IPersistent, new()
  11. where TEntityLink : EntityLink<TEntity>, new()
  12. where TInstance : Entity, IRemotable, IPersistent, IDigitalFormInstance<TEntityLink>, new()
  13. {
  14. Filter<TInstance> incompleteFilter = new Filter<TInstance>(x => x.FormCompletedBy.ID).IsEqualTo(ClientFactory.UserGuid)
  15. .And(x => x.FormCompleted).IsEqualTo(DateTime.MinValue);
  16. Filter<TInstance> completeFilter = new Filter<TInstance>(x => x.FormCompletedBy.ID).IsEqualTo(ClientFactory.UserGuid)
  17. .And(x => x.FormCompleted).IsNotEqualTo(DateTime.MinValue);
  18. public List<ExistingFormShell> QueryIncomplete()
  19. {
  20. return DoQuery(incompleteFilter);
  21. }
  22. public List<ExistingFormShell> QueryComplete()
  23. {
  24. return DoQuery(completeFilter);
  25. }
  26. private List<ExistingFormShell> DoQuery(Filter<TInstance> filter)
  27. {
  28. List<ExistingFormShell> shells = new List<ExistingFormShell>();
  29. CoreTable table = new Client<TInstance>().Query
  30. (
  31. filter,
  32. new Columns<TInstance>
  33. (
  34. x => x.ID,
  35. x => x.Parent.ID,
  36. x => x.Form.Description,
  37. x => x.FormStarted,
  38. x => x.FormCompleted,
  39. x => x.Form.ID
  40. ));
  41. if (table.Rows.Any())
  42. {
  43. foreach (CoreRow row in table.Rows)
  44. {
  45. shells.Add(CreateShell(row));
  46. }
  47. //OrderShells(shells, filter);
  48. }
  49. return shells;
  50. }
  51. //private List<ExistingFormShell> OrderShells(List<ExistingFormShell> shells, Filter<TInstance> filter)
  52. //{
  53. // if (filter == completeFilter)
  54. // shells.Sort((x, y) => y.DateCompleted.CompareTo(x.DateCompleted)); //descending
  55. // else if (filter == incompleteFilter)
  56. // shells.Sort((x, y) => x.DateStarted.CompareTo(y.DateStarted));
  57. // return shells;
  58. //}
  59. private ExistingFormShell CreateShell(CoreRow row)
  60. {
  61. var instance = row.ToObject<TInstance>();
  62. ExistingFormShell shell = new ExistingFormShell();
  63. shell.ID = instance.ID;
  64. shell.Description = instance.Form.Description;
  65. if (shell.Description == null)
  66. shell.Description = "";
  67. shell.ParentID = instance.Parent.ID;
  68. shell.DateStarted = instance.FormStarted;
  69. shell.DateCompleted = instance.FormCompleted;
  70. shell.Started = "Started: " + shell.DateStarted.ToString("dd MMM yy hh:mm tt");
  71. if (shell.DateCompleted != DateTime.MinValue)
  72. shell.Completed = "Completed: " + shell.DateCompleted.ToString("dd MMM yy hh:mm tt");
  73. shell.Type = typeof(TInstance);
  74. shell.FormID = instance.Form.ID;
  75. return shell;
  76. }
  77. }
  78. }