using Comal.Classes; using InABox.Core; namespace Comal.Stores { public class JobStore : ScheduleActionStore { protected override void BeforeSave(Job entity) { base.BeforeSave(entity); if (entity.ID == Guid.Empty) { var defstatus = Provider.Query( new Filter(x => x.Default).IsEqualTo(true), Columns.None().Add(x => x.ID) ).Rows.FirstOrDefault()?.ToObject(); if (defstatus != null) entity.JobStatus.CopyFrom(defstatus); } if (!entity.Account.IsValid() && entity.Customer.IsValid()) { Customer final = null; var customer = Provider.Load(new Filter(x => x.ID).IsEqualTo(entity.Customer.ID)).FirstOrDefault(); if (customer != null) { final = customer; if (customer.Account.IsValid()) { var account = Provider.Load(new Filter(x => x.ID).IsEqualTo(customer.Account.ID)).FirstOrDefault(); if (account != null) final = account; } } entity.Account.Synchronise(final ?? new Customer()); } StoreUtils.Geocode(entity.SiteAddress); } protected override void AfterSave(Job entity) { base.AfterSave(entity); if(entity.HasOriginalValue(x => x.ID)) CreateDefaultScope(entity); } private void CreateDefaultScope(Job job) { var scope = new JobScope { Description = "Default Scope", Type = JobScopeType.Contract }; scope.Job.ID = job.ID; FindSubStore().Save(scope, "Created automatically on job creation."); // 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. var lightJob = new Job().SetID(job.ID); lightJob.DefaultScope.ID = scope.ID; Provider.Save(lightJob); // Update default scope for sending the entity back to the client. job.DefaultScope.ID = scope.ID; } protected override void AfterDelete(Job entity) { base.AfterDelete(entity); var setoutstore = FindSubStore(); var setouts = setoutstore.Query( new Filter(x => x.JobLink.ID).IsEqualTo(entity.ID), null).ToObjects(); setoutstore.Delete(setouts, "Cascaded Delete from Job"); } } }