CacheLoader.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using comal.timesheets.CustomControls;
  2. using Comal.Classes;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Xamarin.Forms;
  10. using PRSSecurity = InABox.Core.Security;
  11. namespace comal.timesheets
  12. {
  13. public static class CacheLoader
  14. {
  15. #region Jobs
  16. public static void LoadJobs()
  17. {
  18. Task.Run(() =>
  19. {
  20. try
  21. {
  22. List<JobShell> jobShells = new List<JobShell>();
  23. if (GlobalVariables.IsJobOnlyEmployee)
  24. {
  25. CoreTable table = DoJobEmployeeQuery();
  26. while (table == null)
  27. table = DoJobEmployeeQuery();
  28. if (!table.Rows.Any())
  29. return;
  30. foreach (CoreRow row in table.Rows)
  31. jobShells.Add(AddJobShell(row));
  32. }
  33. else
  34. {
  35. CoreTable table = DoJobsQuery(AssignFilter());
  36. while (table == null)
  37. table = DoJobsQuery(AssignFilter());
  38. foreach (CoreRow row in table.Rows)
  39. jobShells.Add(CreateJobShell(row));
  40. }
  41. AssignJobShells(jobShells);
  42. }
  43. catch
  44. { }
  45. });
  46. }
  47. private static CoreTable DoJobEmployeeQuery()
  48. {
  49. try
  50. {
  51. return new Client<JobEmployee>().Query(new Filter<JobEmployee>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID));
  52. }
  53. catch (Exception ex)
  54. {
  55. var log = new MobileLogging(LogType.Query, "DoJobEmployeeQuery()", ex.Message + ex.StackTrace, "CacheLoader");
  56. return null;
  57. }
  58. }
  59. private static void AssignJobShells(List<JobShell> jobShells)
  60. {
  61. GlobalVariables.JobShells = jobShells;
  62. GlobalVariables.JobShells.Insert(0, new JobShell { ID = Guid.Empty, JobNumber = "No Job", Name = "Empty Job", JobStatusDescription = "Hidden" });
  63. GlobalVariables.JobsLoaded = true;
  64. }
  65. private static Filter<Job> AssignFilter()
  66. {
  67. Filter<Job> filter = new Filter<Job>(x => x.JobStatus.Active).IsEqualTo(true);
  68. if (!PRSSecurity.IsAllowed<CanViewAllJobs>())
  69. {
  70. List<Guid> jobIDs = new List<Guid>();
  71. CoreTable jobEmployees = new Client<JobEmployee>().Query(
  72. new Filter<JobEmployee>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID),
  73. new Columns<JobEmployee>(x => x.JobLink.ID)
  74. );
  75. foreach (CoreRow row in jobEmployees.Rows)
  76. {
  77. jobIDs.Add(row.Get<JobEmployee, Guid>(x => x.JobLink.ID));
  78. }
  79. filter = filter.And(x => x.ID).InList(jobIDs.ToArray());
  80. }
  81. return filter;
  82. }
  83. private static CoreTable DoJobsQuery(Filter<Job> filter)
  84. {
  85. try
  86. {
  87. return new Client<Job>().Query(
  88. filter,
  89. new Columns<Job>(x => x.ID, x => x.Name, x => x.JobNumber, x => x.JobStatus.Description, x => x.Color),
  90. new SortOrder<Job>(x => x.JobNumber)
  91. );
  92. }
  93. catch (Exception ex)
  94. {
  95. var log = new MobileLogging(LogType.Query, "DoJobsQuery", ex.Message + ex.StackTrace, "CacheLoader");
  96. return null;
  97. }
  98. }
  99. private static JobShell CreateJobShell(CoreRow row)
  100. {
  101. Job job = row.ToObject<Job>();
  102. if (job.Color == null)
  103. job.Color = "";
  104. JobShell jobshell = new JobShell
  105. {
  106. ID = job.ID,
  107. Name = job.Name,
  108. JobNumber = job.JobNumber,
  109. JobStatusDescription = job.JobStatus.Description,
  110. Color = Color.FromHex(job.Color)
  111. };
  112. if (jobshell.JobStatusDescription.Equals("Active Projects"))
  113. jobshell.JobStatusDescription = "Active";
  114. else if (jobshell.JobStatusDescription.Equals("Projects - Hidden from View"))
  115. jobshell.JobStatusDescription = "Hidden";
  116. else if (jobshell.JobStatusDescription.Equals("Projects in Defect Liability Period"))
  117. jobshell.JobStatusDescription = "Defect Liability";
  118. jobshell.DisplayName = "(" + jobshell.JobNumber + ") " + jobshell.Name;
  119. return jobshell;
  120. }
  121. private static JobShell AddJobShell(CoreRow row)
  122. {
  123. JobShell job = new JobShell();
  124. job.ID = row.Get<JobEmployee, Guid>(x => x.JobLink.ID);
  125. job.JobNumber = row.Get<JobEmployee, string>(x => x.JobLink.JobNumber);
  126. job.Name = row.Get<JobEmployee, string>(x => x.JobLink.Name);
  127. job.JobStatusDescription = row.Get<JobEmployee, string>(x => x.JobLink.JobStatus.Description);
  128. job.DisplayName = "(" + job.JobNumber + ") " + job.Name;
  129. return job;
  130. }
  131. #endregion
  132. }
  133. }