JobStore.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Linq;
  2. using Comal.Classes;
  3. using InABox.Core;
  4. namespace Comal.Stores
  5. {
  6. public class JobStore : ScheduleActionStore<Job>
  7. {
  8. protected override void BeforeSave(Job entity)
  9. {
  10. base.BeforeSave(entity);
  11. if (!entity.Account.IsValid() && entity.Customer.IsValid())
  12. {
  13. Customer final = null;
  14. var customer = Provider.Load(new Filter<Customer>(x => x.ID).IsEqualTo(entity.Customer.ID)).FirstOrDefault();
  15. if (customer != null)
  16. {
  17. final = customer;
  18. if (customer.Account.IsValid())
  19. {
  20. var account = Provider.Load(new Filter<Customer>(x => x.ID).IsEqualTo(customer.Account.ID)).FirstOrDefault();
  21. if (account != null)
  22. final = account;
  23. }
  24. }
  25. entity.Account.Synchronise(final ?? new Customer());
  26. }
  27. StoreUtils.Geocode(entity.SiteAddress);
  28. }
  29. protected override void AfterSave(Job entity)
  30. {
  31. base.AfterSave(entity);
  32. if(entity.HasOriginalValue(x => x.ID))
  33. {
  34. CreateDefaultScope(entity);
  35. }
  36. }
  37. private void CreateDefaultScope(Job job)
  38. {
  39. var scope = new JobScope
  40. {
  41. Description = "Default Scope",
  42. Type = JobScopeType.Contract
  43. };
  44. scope.Job.ID = job.ID;
  45. FindSubStore<JobScope>().Save(scope, "Created automatically on job creation.");
  46. // Save the job again, with a default scope set. We create a lighter job so we're not saving all the same properties over again.
  47. var lightJob = new Job
  48. {
  49. ID = job.ID
  50. };
  51. lightJob.DefaultScope.ID = scope.ID;
  52. Provider.Save(lightJob);
  53. // Update default scope for sending the entity back to the client.
  54. job.DefaultScope.ID = scope.ID;
  55. }
  56. protected override void AfterDelete(Job entity)
  57. {
  58. base.AfterDelete(entity);
  59. var setoutstore = FindSubStore<Setout>();
  60. var setouts = setoutstore.Query(
  61. new Filter<Setout>(x => x.JobLink.ID).IsEqualTo(entity.ID),
  62. null).ToObjects<Setout>();
  63. setoutstore.Delete(setouts, "Cascaded Delete from Job");
  64. }
  65. }
  66. }