CacheLoader.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using comal.timesheets.CustomControls;
  2. using Comal.Classes;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. using iText.Layout.Element;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Xamarin.Forms;
  11. namespace comal.timesheets
  12. {
  13. public static class CacheLoader
  14. {
  15. #region Jobs
  16. public static async Task LoadJobs()
  17. {
  18. await Task.Run(() =>
  19. {
  20. try
  21. {
  22. List<JobShell> jobShells = new List<JobShell>();
  23. Filter<Job> filter = AssignFilter();
  24. CoreTable table = DoJobsQuery(filter);
  25. foreach (CoreRow row in table.Rows)
  26. jobShells.Add(CreateJobShell(row));
  27. AssignJobShells(jobShells);
  28. }
  29. catch
  30. { }
  31. });
  32. }
  33. private static void AssignJobShells(List<JobShell> jobShells)
  34. {
  35. GlobalVariables.JobShells = jobShells;
  36. GlobalVariables.JobShells.Insert(0, new JobShell { ID = Guid.Empty, JobNumber = "No Job", Name = "Empty Job", JobStatusDescription = "Hidden" });
  37. GlobalVariables.JobsLoaded = true;
  38. }
  39. private static Filter<Job> AssignFilter()
  40. {
  41. Filter<Job> filter = new Filter<Job>(x => x.JobStatus.Active).IsEqualTo(true);
  42. if (!Security.IsAllowed<CanViewAllJobs>())
  43. {
  44. List<Guid> jobIDs = new List<Guid>();
  45. CoreTable jobEmployees = new Client<JobEmployee>().Query(
  46. new Filter<JobEmployee>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID),
  47. new Columns<JobEmployee>(x => x.JobLink.ID)
  48. );
  49. foreach (CoreRow row in jobEmployees.Rows)
  50. {
  51. jobIDs.Add(row.Get<JobEmployee, Guid>(x => x.JobLink.ID));
  52. }
  53. filter = filter.And(x => x.ID).InList(jobIDs.ToArray());
  54. }
  55. return filter;
  56. }
  57. private static CoreTable DoJobsQuery(Filter<Job> filter)
  58. {
  59. return new Client<Job>().Query(
  60. filter,
  61. new Columns<Job>(x => x.ID, x => x.Name, x => x.JobNumber, x => x.JobStatus.Description, x => x.Color),
  62. new SortOrder<Job>(x => x.JobNumber)
  63. );
  64. }
  65. private static JobShell CreateJobShell(CoreRow row)
  66. {
  67. Job job = row.ToObject<Job>();
  68. if (job.Color == null)
  69. job.Color = "";
  70. JobShell jobshell = new JobShell
  71. {
  72. ID = job.ID,
  73. Name = job.Name,
  74. JobNumber = job.JobNumber,
  75. JobStatusDescription = job.JobStatus.Description,
  76. Color = Color.FromHex(job.Color)
  77. };
  78. if (jobshell.JobStatusDescription.Equals("Active Projects"))
  79. jobshell.JobStatusDescription = "Active";
  80. else if (jobshell.JobStatusDescription.Equals("Projects - Hidden from View"))
  81. jobshell.JobStatusDescription = "Hidden";
  82. else if (jobshell.JobStatusDescription.Equals("Projects in Defect Liability Period"))
  83. jobshell.JobStatusDescription = "Defect Liability";
  84. jobshell.DisplayName = "(" + jobshell.JobNumber + ") " + jobshell.Name;
  85. return jobshell;
  86. }
  87. #endregion
  88. }
  89. }