[441] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
| 2 | |
---|
[628] | 3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager']) |
---|
[441] | 4 | class CostCodeDetailedController extends BaseController { |
---|
[953] | 5 | |
---|
| 6 | def filterService |
---|
| 7 | |
---|
[441] | 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) |
---|
[954] | 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]) |
---|
[953] | 19 | |
---|
| 20 | if(!params.filter) { |
---|
| 21 | return [costCodeInstanceList: CostCode.list(params), |
---|
| 22 | costCodeInstanceTotal: CostCode.count(), |
---|
[954] | 23 | associatedPropertyValues: associatedPropertyValues, |
---|
[953] | 24 | filterParams: params] |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | // filterPane: |
---|
| 28 | return[ costCodeInstanceList: filterService.filter( params, CostCode ), |
---|
| 29 | costCodeInstanceTotal: filterService.count( params, CostCode ), |
---|
[954] | 30 | associatedPropertyValues: associatedPropertyValues, |
---|
[953] | 31 | filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params), |
---|
| 32 | params:params ] |
---|
[441] | 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) { |
---|
[632] | 92 | |
---|
[441] | 93 | costCodeInstance.errors.rejectValue("version", "default.optimistic.locking.failure") |
---|
| 94 | render(view:'edit',model:[costCodeInstance:costCodeInstance]) |
---|
| 95 | return |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | costCodeInstance.properties = params |
---|
[633] | 99 | // Trim name to avoid spaces. |
---|
| 100 | costCodeInstance.name = costCodeInstance.name.trim() |
---|
[441] | 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) |
---|
[633] | 123 | // Trim name to avoid spaces. |
---|
| 124 | costCodeInstance.name = costCodeInstance.name.trim() |
---|
[441] | 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 | } |
---|
[632] | 132 | } // save |
---|
| 133 | |
---|
| 134 | } // end class |
---|