source: trunk/grails-app/controllers/ContactDetailedController.groovy @ 403

Last change on this file since 403 was 402, checked in by gav, 14 years ago

Add contacts to Person, Supplier, Manufacturer and Site.

File size: 4.2 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3@Secured(['ROLE_AppAdmin', 'ROLE_Manager'])
4class ContactDetailedController extends BaseController {
5
6    def contactService
7
8    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AppUser'])
9    def index = { redirect(action:list,params:params) }
10
11    // the delete, save and update actions only accept POST requests
12    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
13
14    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AppUser'])
15    def list = {
16        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
17        [ contactInstanceList: Contact.list( params ), contactInstanceTotal: Contact.count() ]
18    }
19
20    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AppUser'])
21    def show = {
22        def contactInstance = Contact.get( params.id )
23
24        if(!contactInstance) {
25            flash.message = "Contact not found with id ${params.id}"
26            redirect(action:list)
27        }
28        else { return [ contactInstance : contactInstance ] }
29    }
30
31    def delete = {
32        def result = contactService.delete(params)
33
34        if(!result.error) {
35            if(result.ownerInstance) {
36                def c = getControllerName(result.ownerInstance)
37                flash.message = g.message(code: "default.delete.success", args: ["Contact", ''])
38                redirect(controller:c, action:'edit', id: result.ownerInstance.id)
39                return
40            }
41            flash.message = g.message(code: "default.delete.success", args: ["Contact", params.id])
42            redirect(action:list)
43            return
44        }
45
46        flash.message = g.message(code: result.error.code, args: result.error.args)
47
48        if(result.error.code == "default.not.found") {
49            redirect(action:list)
50            return
51        }
52
53        redirect(action:show, id: params.id)
54    }
55
56    def edit = {
57        def contactInstance = Contact.get( params.id )
58
59        if(!contactInstance) {
60            flash.message = "Contact not found with id ${params.id}"
61            redirect(action:list)
62        }
63        else {
64            return [ contactInstance : contactInstance ]
65        }
66    }
67
68    def update = {
69        def contactInstance = Contact.get( params.id )
70        if(contactInstance) {
71            if(params.version) {
72                def version = params.version.toLong()
73                if(contactInstance.version > version) {
74
75                    contactInstance.errors.rejectValue("version", "default.optimistic.locking.failure")
76                    render(view:'edit',model:[contactInstance:contactInstance])
77                    return
78                }
79            }
80            contactInstance.properties = params
81            if(!contactInstance.hasErrors() && contactInstance.save(flush: true)) {
82                flash.message = "Contact ${params.id} updated"
83                redirect(action:show,id:contactInstance.id)
84            }
85            else {
86                render(view:'edit',model:[contactInstance:contactInstance])
87            }
88        }
89        else {
90            flash.message = "Contact not found with id ${params.id}"
91            redirect(action:list)
92        }
93    }
94
95    def create = {
96        def result = contactService.create(params)
97
98        if(!result.error)
99            return [contactInstance: result.contactInstance]
100
101        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
102        redirect(action: list)
103    }
104
105    def save = {
106        def result = contactService.save(params)
107
108        if(!result.error) {
109            def c = getControllerName(result.ownerInstance)
110            flash.message = g.message(code: "default.create.success", args: ["Contact", ''])
111            redirect(controller:c, action:'edit', id: result.ownerInstance.id)
112            return
113        }
114
115        render(view:'create', model:[contactInstance: result.contactInstance])
116    }
117
118    private getControllerName(ownerInstance) {
119        if(ownerInstance.class.name == 'Person')
120            return "${ownerInstance.class.name[0].toLowerCase() + ownerInstance.class.name[1..-1]}"
121        else
122            return "${ownerInstance.class.name[0].toLowerCase() + ownerInstance.class.name[1..-1]}"+"Detailed"
123    }
124
125}
Note: See TracBrowser for help on using the repository browser.