CacheLoader.cs 3.5 KB

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