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

Last change on this file since 967 was 722, checked in by gav, 9 years ago

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

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