[402] | 1 | /** |
---|
| 2 | * Provides a service class with methods to interact with the Contact domain class. |
---|
| 3 | * Contact stores contact details for various objects in the database. |
---|
| 4 | */ |
---|
| 5 | class ContactService { |
---|
| 6 | |
---|
| 7 | boolean transactional = false |
---|
| 8 | |
---|
| 9 | def delete(params) { |
---|
| 10 | def result = [:] |
---|
| 11 | def fail = { Map m -> |
---|
| 12 | result.error = [ code: m.code, args: ["Contact", params.id] ] |
---|
| 13 | return result |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | result.contactInstance = Contact.get(params.id) |
---|
| 17 | |
---|
| 18 | if(!result.contactInstance) |
---|
| 19 | return fail(code:"default.not.found") |
---|
| 20 | |
---|
| 21 | result.ownerInstance = getOwner(result.contactInstance) |
---|
| 22 | |
---|
| 23 | try { |
---|
| 24 | result.contactInstance.delete(flush:true) |
---|
| 25 | return result //Success. |
---|
| 26 | } |
---|
| 27 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
| 28 | return fail(code:"default.delete.failure") |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | def create(params) { |
---|
| 34 | def result = [:] |
---|
| 35 | def fail = { Map m -> |
---|
| 36 | result.error = [ code: m.code, args: ["Contact", params.id] ] |
---|
| 37 | return result |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | result.ownerInstance = getOwner(params) |
---|
| 41 | if(!result.ownerInstance) |
---|
| 42 | return fail(code:"contact.owner.not.found") |
---|
| 43 | |
---|
| 44 | result.contactInstance = new Contact() |
---|
| 45 | result.contactInstance.properties = params |
---|
| 46 | |
---|
| 47 | // success |
---|
| 48 | return result |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | def save(params) { |
---|
| 52 | def result = [:] |
---|
| 53 | def fail = { Map m -> |
---|
| 54 | if(result.contactInstance && m.field) |
---|
| 55 | result.contactInstance.errors.rejectValue(m.field, m.code) |
---|
| 56 | result.error = [ code: m.code, args: ["Contact", params.id] ] |
---|
| 57 | return result |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | result.ownerInstance = getOwner(params) |
---|
| 61 | if(!result.ownerInstance) |
---|
| 62 | return fail(code:"contact.owner.not.found") |
---|
| 63 | |
---|
| 64 | result.contactInstance = new Contact(params) |
---|
| 65 | |
---|
| 66 | if(result.contactInstance.hasErrors() || !result.contactInstance.save(flush: true)) |
---|
| 67 | return fail(code:"default.create.failure") |
---|
| 68 | |
---|
| 69 | // success |
---|
| 70 | return result |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | private getOwner(Map params) { |
---|
| 74 | def ownerInstance |
---|
| 75 | |
---|
| 76 | if(params.manufacturer?.id) |
---|
| 77 | return ownerInstance = Manufacturer.get(params.manufacturer.id) |
---|
| 78 | if(params.supplier?.id) |
---|
| 79 | return ownerInstance = Supplier.get(params.supplier.id) |
---|
| 80 | if(params.person?.id) |
---|
| 81 | return ownerInstance = Person.get(params.person.id) |
---|
| 82 | if(params.site?.id) |
---|
| 83 | return ownerInstance = Site.get(params.site.id) |
---|
| 84 | |
---|
| 85 | return false |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | private getOwner(Object object) { |
---|
| 89 | def ownerInstance |
---|
| 90 | |
---|
| 91 | if(object.manufacturer) |
---|
| 92 | return ownerInstance = object.manufacturer |
---|
| 93 | if(object.supplier) |
---|
| 94 | return ownerInstance = object.supplier |
---|
| 95 | if(object.person) |
---|
| 96 | return ownerInstance = object.person |
---|
| 97 | if(object.site) |
---|
| 98 | return ownerInstance = object.site |
---|
| 99 | |
---|
| 100 | return false |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | } // end of class |
---|