1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | |
---|
3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager']) |
---|
4 | class AddressDetailedController extends BaseController { |
---|
5 | |
---|
6 | def addressService |
---|
7 | |
---|
8 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AppUser']) |
---|
9 | def index = { redirect(action:list,params:params) } |
---|
10 | |
---|
11 | // the delete, save and update actions only accept POST requests |
---|
12 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
13 | |
---|
14 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AppUser']) |
---|
15 | def list = { |
---|
16 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
17 | [ addressInstanceList: Address.list( params ), addressInstanceTotal: Address.count() ] |
---|
18 | } |
---|
19 | |
---|
20 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AppUser']) |
---|
21 | def show = { |
---|
22 | def addressInstance = Address.get( params.id ) |
---|
23 | |
---|
24 | if(!addressInstance) { |
---|
25 | flash.message = "Address not found with id ${params.id}" |
---|
26 | redirect(action:list) |
---|
27 | } |
---|
28 | else { return [ addressInstance : addressInstance ] } |
---|
29 | } |
---|
30 | |
---|
31 | def delete = { |
---|
32 | def addressInstance = Address.get( params.id ) |
---|
33 | if(addressInstance) { |
---|
34 | try { |
---|
35 | addressInstance.delete(flush:true) |
---|
36 | flash.message = "Address ${params.id} deleted" |
---|
37 | redirect(action:list) |
---|
38 | } |
---|
39 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
40 | flash.message = "Address ${params.id} could not be deleted" |
---|
41 | redirect(action:show,id:params.id) |
---|
42 | } |
---|
43 | } |
---|
44 | else { |
---|
45 | flash.message = "Address not found with id ${params.id}" |
---|
46 | redirect(action:list) |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | def edit = { |
---|
51 | def addressInstance = Address.get( params.id ) |
---|
52 | |
---|
53 | if(!addressInstance) { |
---|
54 | flash.message = "Address not found with id ${params.id}" |
---|
55 | redirect(action:list) |
---|
56 | } |
---|
57 | else { |
---|
58 | return [ addressInstance : addressInstance ] |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | def update = { |
---|
63 | def addressInstance = Address.get( params.id ) |
---|
64 | if(addressInstance) { |
---|
65 | if(params.version) { |
---|
66 | def version = params.version.toLong() |
---|
67 | if(addressInstance.version > version) { |
---|
68 | |
---|
69 | addressInstance.errors.rejectValue("version", "default.optimistic.locking.failure") |
---|
70 | render(view:'edit',model:[addressInstance:addressInstance]) |
---|
71 | return |
---|
72 | } |
---|
73 | } |
---|
74 | addressInstance.properties = params |
---|
75 | if(!addressInstance.hasErrors() && addressInstance.save(flush: true)) { |
---|
76 | flash.message = "Address ${params.id} updated" |
---|
77 | redirect(action:show,id:addressInstance.id) |
---|
78 | } |
---|
79 | else { |
---|
80 | render(view:'edit',model:[addressInstance:addressInstance]) |
---|
81 | } |
---|
82 | } |
---|
83 | else { |
---|
84 | flash.message = "Address not found with id ${params.id}" |
---|
85 | redirect(action:list) |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | def create = { |
---|
90 | def result = addressService.create(params) |
---|
91 | |
---|
92 | if(!result.error) |
---|
93 | return [addressInstance: result.addressInstance] |
---|
94 | |
---|
95 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
96 | redirect(action: list) |
---|
97 | } |
---|
98 | |
---|
99 | def save = { |
---|
100 | def result = addressService.save(params) |
---|
101 | |
---|
102 | if(!result.error) { |
---|
103 | flash.message = g.message(code: "default.create.success", args: ["Address", result.addressInstance.id]) |
---|
104 | redirect(action:show, id: result.addressInstance.id) |
---|
105 | return |
---|
106 | } |
---|
107 | |
---|
108 | render(view:'create', model:[addressInstance: result.addressInstance]) |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|