source: trunk/grails-app/controllers/CostCodeDetailedController.groovy @ 932

Last change on this file since 932 was 633, checked in by gav, 14 years ago

Domain change: Add PurchasingGroup?.
Logic and views to suite.

File size: 3.9 KB
RevLine 
[441]1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
[628]3@Secured(['ROLE_AppAdmin', 'ROLE_Manager'])
[441]4class CostCodeDetailedController extends BaseController {
5   
6    def index = { redirect(action:list,params:params) }
7
8    // the delete, save and update actions only accept POST requests
9    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
10
11    def list = {
12        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
13        [ costCodeInstanceList: CostCode.list( params ), costCodeInstanceTotal: CostCode.count() ]
14    }
15
16    def show = {
17
18        // In the case of an actionSubmit button, rewrite action name from 'index'.
19        if(params._action_Show)
20            params.action='show'
21
22        def costCodeInstance = CostCode.get( params.id )
23
24        if(!costCodeInstance) {
25            flash.message = "CostCode not found with id ${params.id}"
26            redirect(action:list)
27        }
28        else { return [ costCodeInstance : costCodeInstance ] }
29    }
30
31    def delete = {
32        def costCodeInstance = CostCode.get( params.id )
33        if(costCodeInstance) {
34            try {
35                costCodeInstance.delete(flush:true)
36                flash.message = "CostCode ${params.id} deleted"
37                redirect(action:list)
38            }
39            catch(org.springframework.dao.DataIntegrityViolationException e) {
40                flash.message = "CostCode ${params.id} could not be deleted"
41                redirect(action:show,id:params.id)
42            }
43        }
44        else {
45            flash.message = "CostCode not found with id ${params.id}"
46            redirect(action:list)
47        }
48    }
49
50    def edit = {
51
52        // In the case of an actionSubmit button, rewrite action name from 'index'.
53        if(params._action_Edit)
54            params.action='edit'
55
56        def costCodeInstance = CostCode.get( params.id )
57
58        if(!costCodeInstance) {
59            flash.message = "CostCode not found with id ${params.id}"
60            redirect(action:list)
61        }
62        else {
63            return [ costCodeInstance : costCodeInstance ]
64        }
65    }
66
67    def update = {
68        def costCodeInstance = CostCode.get( params.id )
69        if(costCodeInstance) {
70            if(params.version) {
71                def version = params.version.toLong()
72                if(costCodeInstance.version > version) {
[632]73
[441]74                    costCodeInstance.errors.rejectValue("version", "default.optimistic.locking.failure")
75                    render(view:'edit',model:[costCodeInstance:costCodeInstance])
76                    return
77                }
78            }
79            costCodeInstance.properties = params
[633]80            // Trim name to avoid spaces.
81            costCodeInstance.name = costCodeInstance.name.trim()
[441]82            if(!costCodeInstance.hasErrors() && costCodeInstance.save(flush: true)) {
83                flash.message = "CostCode ${params.id} updated"
84                redirect(action:show,id:costCodeInstance.id)
85            }
86            else {
87                render(view:'edit',model:[costCodeInstance:costCodeInstance])
88            }
89        }
90        else {
91            flash.message = "CostCode not found with id ${params.id}"
92            redirect(action:list)
93        }
94    }
95
96    def create = {
97        def costCodeInstance = new CostCode()
98        costCodeInstance.properties = params
99        return ['costCodeInstance':costCodeInstance]
100    }
101
102    def save = {
103        def costCodeInstance = new CostCode(params)
[633]104        // Trim name to avoid spaces.
105        costCodeInstance.name = costCodeInstance.name.trim()
[441]106        if(!costCodeInstance.hasErrors() && costCodeInstance.save(flush: true)) {
107            flash.message = "CostCode ${costCodeInstance.id} created"
108            redirect(action:show,id:costCodeInstance.id)
109        }
110        else {
111            render(view:'create',model:[costCodeInstance:costCodeInstance])
112        }
[632]113    } // save
114
115} // end class
Note: See TracBrowser for help on using the repository browser.