1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | |
---|
3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager']) |
---|
4 | class AssetSubItemDetailedController extends BaseController { |
---|
5 | |
---|
6 | def assetSubItemService |
---|
7 | |
---|
8 | // the delete, save and update actions only accept POST requests |
---|
9 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
10 | |
---|
11 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
12 | def index = { redirect(controller: "assetDetailed", params:params) } |
---|
13 | |
---|
14 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
15 | def show = { |
---|
16 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
17 | |
---|
18 | if(!assetSubItemInstance) { |
---|
19 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
20 | redirect(controller: "assetDetailed", action: "search") |
---|
21 | } |
---|
22 | else { return [ assetSubItemInstance : assetSubItemInstance ] } |
---|
23 | } |
---|
24 | |
---|
25 | def delete = { |
---|
26 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
27 | if(assetSubItemInstance) { |
---|
28 | try { |
---|
29 | assetSubItemInstance.delete(flush:true) |
---|
30 | flash.message = "AssetSubItem ${params.id} deleted" |
---|
31 | redirect(controller: "assetDetailed", action: "search") |
---|
32 | } |
---|
33 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
34 | flash.errorMessage = "AssetSubItem ${params.id} could not be deleted" |
---|
35 | redirect(action:show,id:params.id) |
---|
36 | } |
---|
37 | } |
---|
38 | else { |
---|
39 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
40 | redirect(controller: "assetDetailed", action: "search") |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | def edit = { |
---|
45 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
46 | |
---|
47 | if(!assetSubItemInstance) { |
---|
48 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
49 | redirect(controller: "assetDetailed", action: "search") |
---|
50 | } |
---|
51 | else { |
---|
52 | return [ assetSubItemInstance : assetSubItemInstance ] |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | def update = { |
---|
57 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
58 | if(assetSubItemInstance) { |
---|
59 | if(params.version) { |
---|
60 | def version = params.version.toLong() |
---|
61 | if(assetSubItemInstance.version > version) { |
---|
62 | |
---|
63 | assetSubItemInstance.errors.rejectValue("version", "assetSubItem.optimistic.locking.failure", "Another user has updated this AssetSubItem while you were editing.") |
---|
64 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
65 | return |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | assetSubItemInstance.properties = params |
---|
70 | |
---|
71 | // if(!assetSubItemInstance.asset && !assetSubItemInstance.parentItem) { |
---|
72 | // flash.message = "Either an asset or parentItem is required." |
---|
73 | // render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
74 | // return |
---|
75 | // } |
---|
76 | // |
---|
77 | // if(assetSubItemInstance.asset && assetSubItemInstance.parentItem) { |
---|
78 | // flash.message = "Both an asset and parentItem may not be specified at the same time." |
---|
79 | // render(view:'edit',model:[parentItem:parentItem]) |
---|
80 | // return |
---|
81 | // } |
---|
82 | |
---|
83 | if(!assetSubItemInstance.hasErrors() && assetSubItemInstance.save(flush: true)) { |
---|
84 | flash.message = "AssetSubItem ${params.id} updated" |
---|
85 | |
---|
86 | if(assetSubItemInstance.assets) |
---|
87 | redirect(controller: "assetDetailed", action:"show", id: assetSubItemInstance.assets.toArray()[0].id) |
---|
88 | else { |
---|
89 | //Get the top parentItem. |
---|
90 | def parentItem = assetSubItemInstance.parentItem |
---|
91 | def i = 0 |
---|
92 | while(parentItem?.parentItem) { |
---|
93 | parentItem = parentItem.parentItem |
---|
94 | // Protect against infinite recurrsion. |
---|
95 | i++ |
---|
96 | if(i > 100) |
---|
97 | break |
---|
98 | } |
---|
99 | |
---|
100 | if(parentItem?.assets) |
---|
101 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
102 | else |
---|
103 | redirect(action:"show", id: assetSubItemInstance.id) // End stop if all else fails. |
---|
104 | } |
---|
105 | } |
---|
106 | else { |
---|
107 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
108 | } |
---|
109 | } |
---|
110 | else { |
---|
111 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
112 | redirect(controller: "assetDetailed", action: "search") |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | def create = { |
---|
117 | def result = assetSubItemService.create(params) |
---|
118 | |
---|
119 | if(!result.error) |
---|
120 | return [assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance] |
---|
121 | |
---|
122 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
123 | redirect(controller: 'assetDetailed', action: 'search') |
---|
124 | } |
---|
125 | |
---|
126 | def save = { |
---|
127 | def result = assetSubItemService.save(params) |
---|
128 | |
---|
129 | if(!result.error) { |
---|
130 | // Success. |
---|
131 | flash.message = g.message(code: "default.create.success", args: ["AssetSubItem", result.assetSubItemInstance.name]) |
---|
132 | |
---|
133 | /// @todo: This just grabs the first parent in an unsorted list and needs improving. |
---|
134 | if(result.assetSubItemInstance.assets) |
---|
135 | redirect(controller: "assetDetailed", action:"show", id: result.assetSubItemInstance.assets.toArray()[0].id) |
---|
136 | else { |
---|
137 | //Get the top parentItem. |
---|
138 | def parentItem = result.assetSubItemInstance.parentItem |
---|
139 | def i = 0 |
---|
140 | while(parentItem?.parentItem) { |
---|
141 | parentItem = parentItem.parentItem |
---|
142 | // Protect against infinite recurrsion. |
---|
143 | i++ |
---|
144 | if(i > 100) |
---|
145 | break |
---|
146 | } |
---|
147 | |
---|
148 | if(parentItem?.assets) |
---|
149 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
150 | else |
---|
151 | redirect(action:"show", id: result.assetSubItemInstance.id) // End stop if all else fails. |
---|
152 | } |
---|
153 | return |
---|
154 | } |
---|
155 | |
---|
156 | if(result.error.code == "assetSubItem.asset.not.found") |
---|
157 | flash.errorMessage = g.message(code: "default.create.success") |
---|
158 | |
---|
159 | render(view:'create', model:[assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance]) |
---|
160 | } |
---|
161 | |
---|
162 | } // end class |
---|