1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Linq;
- using Comal.Classes;
- using InABox.Core;
- namespace Comal.Stores
- {
- public class JobStore : ScheduleActionStore<Job>
- {
- protected override void BeforeSave(Job entity)
- {
- base.BeforeSave(entity);
- if (!entity.Account.IsValid() && entity.Customer.IsValid())
- {
- Customer final = null;
- var customer = Provider.Load(new Filter<Customer>(x => x.ID).IsEqualTo(entity.Customer.ID)).FirstOrDefault();
- if (customer != null)
- {
- final = customer;
- if (customer.Account.IsValid())
- {
- var account = Provider.Load(new Filter<Customer>(x => x.ID).IsEqualTo(customer.Account.ID)).FirstOrDefault();
- if (account != null)
- final = account;
- }
- }
- entity.Account.Synchronise(final);
- }
- 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<JobScope>().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
- {
- ID = 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<Setout>();
- var setouts = setoutstore.Query(
- new Filter<Setout>(x => x.JobLink.ID).IsEqualTo(entity.ID),
- null).ToObjects<Setout>();
- setoutstore.Delete(setouts, "Cascaded Delete from Job");
- }
- }
- }
|