source: branches/features/purchaseOrders/grails-app/services/ContactService.groovy @ 923

Last change on this file since 923 was 722, checked in by gav, 14 years ago

Domain change: as per ticket #97 - Drop the entire Manufacturer domain concept.

File size: 2.7 KB
Line 
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 */
5class 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.supplier?.id)
77            return ownerInstance = Supplier.get(params.supplier.id)
78        if(params.person?.id)
79            return ownerInstance = Person.get(params.person.id)
80        if(params.site?.id)
81            return ownerInstance = Site.get(params.site.id)
82
83        return false
84    }
85
86    private getOwner(Object object) {
87        def ownerInstance
88
89        if(object.supplier)
90            return ownerInstance = object.supplier
91        if(object.person)
92            return ownerInstance = object.person
93        if(object.site)
94            return ownerInstance = object.site
95
96        return false
97    }
98
99} // end of class
Note: See TracBrowser for help on using the repository browser.