JobStore.cs 2.5 KB

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