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

Last change on this file was 961, checked in by gav, 12 years ago

Set params.max to 100, 1000 for list views of CostCode, InventoryLocation, InventoryStore, Supplier.

File size: 4.8 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() : 100, 1000 )
15        def associatedPropertyMax = 1000
16        def associatedPropertyValues = [:]
17        def purchasingGroupNameQuery = 'select distinct a.name from PurchasingGroup a where a.isActive = ? order by a.name'
18        associatedPropertyValues.purchasingGroupList = PurchasingGroup.executeQuery(purchasingGroupNameQuery, [true], [max:associatedPropertyMax])
19
20        if(!params.filter) {
21            return [costCodeInstanceList: CostCode.list(params),
22                    costCodeInstanceTotal: CostCode.count(),
23                    associatedPropertyValues: associatedPropertyValues,
24                    filterParams: params]
25        }
26
27        // filterPane:
28        return[ costCodeInstanceList: filterService.filter( params, CostCode ),
29                costCodeInstanceTotal: filterService.count( params, CostCode ),
30                associatedPropertyValues: associatedPropertyValues,
31                filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params),
32                params:params ]
33    }
34
35    def show = {
36
37        // In the case of an actionSubmit button, rewrite action name from 'index'.
38        if(params._action_Show)
39            params.action='show'
40
41        def costCodeInstance = CostCode.get( params.id )
42
43        if(!costCodeInstance) {
44            flash.message = "CostCode not found with id ${params.id}"
45            redirect(action:list)
46        }
47        else { return [ costCodeInstance : costCodeInstance ] }
48    }
49
50    def delete = {
51        def costCodeInstance = CostCode.get( params.id )
52        if(costCodeInstance) {
53            try {
54                costCodeInstance.delete(flush:true)
55                flash.message = "CostCode ${params.id} deleted"
56                redirect(action:list)
57            }
58            catch(org.springframework.dao.DataIntegrityViolationException e) {
59                flash.message = "CostCode ${params.id} could not be deleted"
60                redirect(action:show,id:params.id)
61            }
62        }
63        else {
64            flash.message = "CostCode not found with id ${params.id}"
65            redirect(action:list)
66        }
67    }
68
69    def edit = {
70
71        // In the case of an actionSubmit button, rewrite action name from 'index'.
72        if(params._action_Edit)
73            params.action='edit'
74
75        def costCodeInstance = CostCode.get( params.id )
76
77        if(!costCodeInstance) {
78            flash.message = "CostCode not found with id ${params.id}"
79            redirect(action:list)
80        }
81        else {
82            return [ costCodeInstance : costCodeInstance ]
83        }
84    }
85
86    def update = {
87        def costCodeInstance = CostCode.get( params.id )
88        if(costCodeInstance) {
89            if(params.version) {
90                def version = params.version.toLong()
91                if(costCodeInstance.version > version) {
92
93                    costCodeInstance.errors.rejectValue("version", "default.optimistic.locking.failure")
94                    render(view:'edit',model:[costCodeInstance:costCodeInstance])
95                    return
96                }
97            }
98            costCodeInstance.properties = params
99            // Trim name to avoid spaces.
100            costCodeInstance.name = costCodeInstance.name.trim()
101            if(!costCodeInstance.hasErrors() && costCodeInstance.save(flush: true)) {
102                flash.message = "CostCode ${params.id} updated"
103                redirect(action:show,id:costCodeInstance.id)
104            }
105            else {
106                render(view:'edit',model:[costCodeInstance:costCodeInstance])
107            }
108        }
109        else {
110            flash.message = "CostCode not found with id ${params.id}"
111            redirect(action:list)
112        }
113    }
114
115    def create = {
116        def costCodeInstance = new CostCode()
117        costCodeInstance.properties = params
118        return ['costCodeInstance':costCodeInstance]
119    }
120
121    def save = {
122        def costCodeInstance = new CostCode(params)
123        // Trim name to avoid spaces.
124        costCodeInstance.name = costCodeInstance.name.trim()
125        if(!costCodeInstance.hasErrors() && costCodeInstance.save(flush: true)) {
126            flash.message = "CostCode ${costCodeInstance.id} created"
127            redirect(action:show,id:costCodeInstance.id)
128        }
129        else {
130            render(view:'create',model:[costCodeInstance:costCodeInstance])
131        }
132    } // save
133
134} // end class
Note: See TracBrowser for help on using the repository browser.