CacheLoader.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 = new Client<JobEmployee>().Query(new Filter<JobEmployee>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID));
  26. if (!table.Rows.Any())
  27. return;
  28. foreach (CoreRow row in table.Rows)
  29. jobShells.Add(AddJobShell(row));
  30. }
  31. else
  32. foreach (CoreRow row in DoJobsQuery(AssignFilter()).Rows)
  33. jobShells.Add(CreateJobShell(row));
  34. AssignJobShells(jobShells);
  35. }
  36. catch
  37. { }
  38. });
  39. }
  40. private static void AssignJobShells(List<JobShell> jobShells)
  41. {
  42. GlobalVariables.JobShells = jobShells;
  43. GlobalVariables.JobShells.Insert(0, new JobShell { ID = Guid.Empty, JobNumber = "No Job", Name = "Empty Job", JobStatusDescription = "Hidden" });
  44. GlobalVariables.JobsLoaded = true;
  45. }
  46. private static Filter<Job> AssignFilter()
  47. {
  48. Filter<Job> filter = new Filter<Job>(x => x.JobStatus.Active).IsEqualTo(true);
  49. if (!PRSSecurity.IsAllowed<CanViewAllJobs>())
  50. {
  51. List<Guid> jobIDs = new List<Guid>();
  52. CoreTable jobEmployees = new Client<JobEmployee>().Query(
  53. new Filter<JobEmployee>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID),
  54. new Columns<JobEmployee>(x => x.JobLink.ID)
  55. );
  56. foreach (CoreRow row in jobEmployees.Rows)
  57. {
  58. jobIDs.Add(row.Get<JobEmployee, Guid>(x => x.JobLink.ID));
  59. }
  60. filter = filter.And(x => x.ID).InList(jobIDs.ToArray());
  61. }
  62. return filter;
  63. }
  64. private static CoreTable DoJobsQuery(Filter<Job> filter)
  65. {
  66. return new Client<Job>().Query(
  67. filter,
  68. new Columns<Job>(x => x.ID, x => x.Name, x => x.JobNumber, x => x.JobStatus.Description, x => x.Color),
  69. new SortOrder<Job>(x => x.JobNumber)
  70. );
  71. }
  72. private static JobShell CreateJobShell(CoreRow row)
  73. {
  74. Job job = row.ToObject<Job>();
  75. if (job.Color == null)
  76. job.Color = "";
  77. JobShell jobshell = new JobShell
  78. {
  79. ID = job.ID,
  80. Name = job.Name,
  81. JobNumber = job.JobNumber,
  82. JobStatusDescription = job.JobStatus.Description,
  83. Color = Color.FromHex(job.Color)
  84. };
  85. if (jobshell.JobStatusDescription.Equals("Active Projects"))
  86. jobshell.JobStatusDescription = "Active";
  87. else if (jobshell.JobStatusDescription.Equals("Projects - Hidden from View"))
  88. jobshell.JobStatusDescription = "Hidden";
  89. else if (jobshell.JobStatusDescription.Equals("Projects in Defect Liability Period"))
  90. jobshell.JobStatusDescription = "Defect Liability";
  91. jobshell.DisplayName = "(" + jobshell.JobNumber + ") " + jobshell.Name;
  92. return jobshell;
  93. }
  94. private static JobShell AddJobShell(CoreRow row)
  95. {
  96. JobShell job = new JobShell();
  97. job.ID = row.Get<JobEmployee, Guid>(x => x.JobLink.ID);
  98. job.JobNumber = row.Get<JobEmployee, string>(x => x.JobLink.JobNumber);
  99. job.Name = row.Get<JobEmployee, string>(x => x.JobLink.Name);
  100. job.JobStatusDescription = row.Get<JobEmployee, string>(x => x.JobLink.JobStatus.Description);
  101. job.DisplayName = "(" + job.JobNumber + ") " + job.Name;
  102. return job;
  103. }
  104. #endregion
  105. }
  106. }