source: trunk/grails-app/controllers/SupplierDetailedController.groovy @ 956

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

Add filterpane search to Supplier list view.

File size: 4.6 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager'])
4class SupplierDetailedController 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        def associatedPropertyMax = 1000
16        def associatedPropertyValues = [:]
17        def supplierTypeNameQuery = 'select distinct a.name from SupplierType a where a.isActive = ? order by a.name'
18        associatedPropertyValues.supplierTypeList = SupplierType.executeQuery(supplierTypeNameQuery, [true], [max:associatedPropertyMax])
19
20        if(!params.filter) {
21            return [supplierInstanceList: Supplier.list(params),
22                    supplierInstanceTotal: Supplier.count(),
23                    associatedPropertyValues: associatedPropertyValues,
24                    filterParams: params]
25        }
26
27        // filterPane:
28        return[ supplierInstanceList: filterService.filter( params, Supplier ),
29                supplierInstanceTotal: filterService.count( params, Supplier ),
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 supplierInstance = Supplier.get( params.id )
42
43        if(!supplierInstance) {
44            flash.message = "Supplier not found with id ${params.id}"
45            redirect(action:list)
46        }
47        else { return [ supplierInstance : supplierInstance ] }
48    }
49
50    def delete = {
51        def supplierInstance = Supplier.get( params.id )
52        if(supplierInstance) {
53            try {
54                supplierInstance.delete(flush:true)
55                flash.message = "Supplier ${params.id} deleted"
56                redirect(action:list)
57            }
58            catch(org.springframework.dao.DataIntegrityViolationException e) {
59                flash.message = "Supplier ${params.id} could not be deleted"
60                redirect(action:show,id:params.id)
61            }
62        }
63        else {
64            flash.message = "Supplier 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 supplierInstance = Supplier.get( params.id )
76
77        if(!supplierInstance) {
78            flash.message = "Supplier not found with id ${params.id}"
79            redirect(action:list)
80        }
81        else {
82            return [ supplierInstance : supplierInstance ]
83        }
84    }
85
86    def update = {
87        def supplierInstance = Supplier.get( params.id )
88        if(supplierInstance) {
89            if(params.version) {
90                def version = params.version.toLong()
91                if(supplierInstance.version > version) {
92
93                    supplierInstance.errors.rejectValue("version", "default.optimistic.locking.failure")
94                    render(view:'edit',model:[supplierInstance:supplierInstance])
95                    return
96                }
97            }
98            supplierInstance.properties = params
99            if(!supplierInstance.hasErrors() && supplierInstance.save(flush: true)) {
100                flash.message = "Supplier ${params.id} updated"
101                redirect(action:show,id:supplierInstance.id)
102            }
103            else {
104                render(view:'edit',model:[supplierInstance:supplierInstance])
105            }
106        }
107        else {
108            flash.message = "Supplier not found with id ${params.id}"
109            redirect(action:list)
110        }
111    }
112
113    def create = {
114        def supplierInstance = new Supplier()
115        supplierInstance.properties = params
116        return ['supplierInstance':supplierInstance]
117    }
118
119    def save = {
120        def supplierInstance = new Supplier(params)
121        if(!supplierInstance.hasErrors() && supplierInstance.save(flush: true)) {
122            flash.message = "Supplier ${supplierInstance.id} created"
123            redirect(action:show,id:supplierInstance.id)
124        }
125        else {
126            render(view:'create',model:[supplierInstance:supplierInstance])
127        }
128    }
129}
Note: See TracBrowser for help on using the repository browser.