[397] | 1 | /** |
---|
| 2 | * Provides a service class with methods to interact with Address domain class. |
---|
| 3 | * Address stores the address for various objects in the database. |
---|
| 4 | */ |
---|
| 5 | class AddressService { |
---|
| 6 | |
---|
| 7 | boolean transactional = false |
---|
| 8 | |
---|
| 9 | def getAddress() { |
---|
| 10 | """${this.street1} |
---|
| 11 | ${this.street2} |
---|
| 12 | ${this.city} |
---|
| 13 | ${this.state} |
---|
| 14 | ${this.postCode} |
---|
| 15 | ${this.country}""" |
---|
| 16 | |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | def create(params) { |
---|
| 20 | def result = [:] |
---|
| 21 | def fail = { Map m -> |
---|
| 22 | result.error = [ code: m.code, args: ["Address", params.id] ] |
---|
| 23 | return result |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | if(!checkForOwner(params)) |
---|
| 27 | return fail(code:"address.owner.not.found") |
---|
| 28 | |
---|
| 29 | result.addressInstance = new Address() |
---|
| 30 | result.addressInstance.properties = params |
---|
| 31 | |
---|
| 32 | // success |
---|
| 33 | return result |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | def save(params) { |
---|
| 37 | def result = [:] |
---|
| 38 | def fail = { Map m -> |
---|
| 39 | if(result.addressInstance && m.field) |
---|
| 40 | result.addressInstance.errors.rejectValue(m.field, m.code) |
---|
| 41 | result.error = [ code: m.code, args: ["Address", params.id] ] |
---|
| 42 | return result |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | if(!checkForOwner(params)) |
---|
| 46 | return fail(code:"address.owner.not.found") |
---|
| 47 | |
---|
| 48 | result.addressInstance = new Address(params) |
---|
| 49 | |
---|
| 50 | if(result.addressInstance.hasErrors() || !result.addressInstance.save(flush: true)) |
---|
| 51 | return fail(code:"default.create.failure") |
---|
| 52 | |
---|
| 53 | // success |
---|
| 54 | return result |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | private checkForOwner(params) { |
---|
| 58 | def manufacturerInstance |
---|
| 59 | def supplierInstance |
---|
| 60 | def personInstance |
---|
| 61 | def siteInstance |
---|
| 62 | |
---|
| 63 | if(params.manufacturer?.id) |
---|
| 64 | manufacturerInstance = Manufacturer.get(params.manufacturer.id) |
---|
| 65 | if(params.supplier?.id) |
---|
| 66 | supplierInstance = Supplier.get(params.supplier.id) |
---|
| 67 | if(params.person?.id) |
---|
| 68 | personInstance = Person.get(params.person.id) |
---|
| 69 | if(params.site?.id) |
---|
| 70 | siteInstance = Site.get(params.site?.id) |
---|
| 71 | |
---|
| 72 | if(!manufacturerInstance && !supplierInstance && !personInstance && !siteInstance) |
---|
| 73 | return false |
---|
| 74 | |
---|
| 75 | return true |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | } // end of class |
---|