source: trunk/grails-app/services/AddressService.groovy @ 477

Last change on this file since 477 was 401, checked in by gav, 15 years ago

Small improvement to AddressService checkForOwner().

File size: 1.9 KB
Line 
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 */
5class 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
59        if(params.manufacturer?.id)
60            return Manufacturer.exists(params.manufacturer.id)
61        if(params.supplier?.id)
62            return Supplier.exists(params.supplier.id)
63        if(params.person?.id)
64            return Person.exists(params.person.id)
65        if(params.site?.id)
66            return Site.exists(params.site.id)
67
68        return false
69    }
70
71
72} // end of class
Note: See TracBrowser for help on using the repository browser.