source: trunk/src/templates/scaffolding/Controller.groovy @ 158

Revision 158, 3.6 KB checked in by gav, 5 years ago (diff)

Update templates to Grails-1.1.1

Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3<%=packageName ? "package ${packageName}\n\n" : ''%>class ${className}Controller extends BaseAppAdminController {
4   
5    def index = { redirect(action:list,params:params) }
6
7    // the delete, save and update actions only accept POST requests
8    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
9
10    def list = {
11        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
12        [ ${propertyName}List: ${className}.list( params ), ${propertyName}Total: ${className}.count() ]
13    }
14
15    def show = {
16        def ${propertyName} = ${className}.get( params.id )
17
18        if(!${propertyName}) {
19            flash.message = "${className} not found with id \${params.id}"
20            redirect(action:list)
21        }
22        else { return [ ${propertyName} : ${propertyName} ] }
23    }
24
25    def delete = {
26        def ${propertyName} = ${className}.get( params.id )
27        if(${propertyName}) {
28            try {
29                ${propertyName}.delete(flush:true)
30                flash.message = "${className} \${params.id} deleted"
31                redirect(action:list)
32            }
33            catch(org.springframework.dao.DataIntegrityViolationException e) {
34                flash.message = "${className} \${params.id} could not be deleted"
35                redirect(action:show,id:params.id)
36            }
37        }
38        else {
39            flash.message = "${className} not found with id \${params.id}"
40            redirect(action:list)
41        }
42    }
43
44    def edit = {
45        def ${propertyName} = ${className}.get( params.id )
46
47        if(!${propertyName}) {
48            flash.message = "${className} not found with id \${params.id}"
49            redirect(action:list)
50        }
51        else {
52            return [ ${propertyName} : ${propertyName} ]
53        }
54    }
55
56    def update = {
57        def ${propertyName} = ${className}.get( params.id )
58        if(${propertyName}) {
59            if(params.version) {
60                def version = params.version.toLong()
61                if(${propertyName}.version > version) {
62                    <%def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className)%>
63                    ${propertyName}.errors.rejectValue("version", "${lowerCaseName}.optimistic.locking.failure", "Another user has updated this ${className} while you were editing.")
64                    render(view:'edit',model:[${propertyName}:${propertyName}])
65                    return
66                }
67            }
68            ${propertyName}.properties = params
69            if(!${propertyName}.hasErrors() && ${propertyName}.save()) {
70                flash.message = "${className} \${params.id} updated"
71                redirect(action:show,id:${propertyName}.id)
72            }
73            else {
74                render(view:'edit',model:[${propertyName}:${propertyName}])
75            }
76        }
77        else {
78            flash.message = "${className} not found with id \${params.id}"
79            redirect(action:list)
80        }
81    }
82
83    def create = {
84        def ${propertyName} = new ${className}()
85        ${propertyName}.properties = params
86        return ['${propertyName}':${propertyName}]
87    }
88
89    def save = {
90        def ${propertyName} = new ${className}(params)
91        if(!${propertyName}.hasErrors() && ${propertyName}.save()) {
92            flash.message = "${className} \${${propertyName}.id} created"
93            redirect(action:show,id:${propertyName}.id)
94        }
95        else {
96            render(view:'create',model:[${propertyName}:${propertyName}])
97        }
98    }
99}
Note: See TracBrowser for help on using the repository browser.