CacheLoader.cs 3.6 KB

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