FormPickerQueryLoader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = QueryTable(filter);
  30. while(table == null)
  31. table = QueryTable(filter);
  32. if (table.Rows.Any())
  33. {
  34. foreach (CoreRow row in table.Rows)
  35. {
  36. shells.Add(CreateShell(row));
  37. }
  38. //OrderShells(shells, filter);
  39. }
  40. return shells;
  41. }
  42. private CoreTable QueryTable(Filter<TInstance> filter)
  43. {
  44. try
  45. {
  46. return new Client<TInstance>().Query
  47. (
  48. filter,
  49. new Columns<TInstance>
  50. (
  51. x => x.ID,
  52. x => x.Parent.ID,
  53. x => x.Form.Description,
  54. x => x.FormStarted,
  55. x => x.FormCompleted,
  56. x => x.Form.ID
  57. ));
  58. }
  59. catch (Exception ex)
  60. {
  61. InABox.Mobile.MobileLogging.Log(ex);
  62. return null;
  63. }
  64. }
  65. //private List<ExistingFormShell> OrderShells(List<ExistingFormShell> shells, Filter<TInstance> filter)
  66. //{
  67. // if (filter == completeFilter)
  68. // shells.Sort((x, y) => y.DateCompleted.CompareTo(x.DateCompleted)); //descending
  69. // else if (filter == incompleteFilter)
  70. // shells.Sort((x, y) => x.DateStarted.CompareTo(y.DateStarted));
  71. // return shells;
  72. //}
  73. private ExistingFormShell CreateShell(CoreRow row)
  74. {
  75. var instance = row.ToObject<TInstance>();
  76. ExistingFormShell shell = new ExistingFormShell();
  77. shell.ID = instance.ID;
  78. shell.Description = instance.Form.Description;
  79. if (shell.Description == null)
  80. shell.Description = "";
  81. shell.ParentID = instance.Parent.ID;
  82. shell.DateStarted = instance.FormStarted;
  83. shell.DateCompleted = instance.FormCompleted;
  84. shell.Started = "Started: " + shell.DateStarted.ToString("dd MMM yy hh:mm tt");
  85. if (shell.DateCompleted != DateTime.MinValue)
  86. shell.Completed = "Completed: " + shell.DateCompleted.ToString("dd MMM yy hh:mm tt");
  87. shell.Type = typeof(TInstance);
  88. shell.FormID = instance.Form.ID;
  89. return shell;
  90. }
  91. }
  92. }