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

Last change on this file since 953 was 953, checked in by gav, 13 years ago

Add filterpane search to CostCode? list view.

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