FormPickerQueryLoader.cs 3.7 KB

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