[274] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
[336] | 2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
[387] | 3 | import org.apache.commons.lang.WordUtils |
---|
[274] | 4 | |
---|
[298] | 5 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager']) |
---|
[274] | 6 | class AssetSubItemDetailedController extends BaseController { |
---|
[285] | 7 | |
---|
[336] | 8 | def filterService |
---|
| 9 | def exportService |
---|
[285] | 10 | def assetSubItemService |
---|
| 11 | |
---|
[274] | 12 | // the delete, save and update actions only accept POST requests |
---|
| 13 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
| 14 | |
---|
[298] | 15 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[342] | 16 | def index = { redirect(action:search,params:params) } |
---|
[298] | 17 | |
---|
[359] | 18 | /** |
---|
| 19 | * Set session.assetSubItemSearchParamsMax |
---|
| 20 | */ |
---|
[298] | 21 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[359] | 22 | def setSearchParamsMax = { |
---|
| 23 | def max = 1000 |
---|
| 24 | if(params.newMax.isInteger()) { |
---|
| 25 | def i = params.newMax.toInteger() |
---|
| 26 | if(i > 0 && i <= max) |
---|
| 27 | session.assetSubItemSearchParamsMax = params.newMax |
---|
| 28 | if(i > max) |
---|
| 29 | session.assetSubItemSearchParamsMax = max |
---|
| 30 | } |
---|
| 31 | forward(action: 'search', params: params) |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[336] | 35 | def search = { |
---|
| 36 | |
---|
[359] | 37 | if(session.assetSubItemSearchParamsMax) |
---|
| 38 | params.max = session.assetSubItemSearchParamsMax |
---|
[336] | 39 | |
---|
| 40 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
| 41 | |
---|
| 42 | def assetSubItemInstanceList = [] |
---|
| 43 | def assetSubItemInstanceTotal |
---|
| 44 | def filterParams = [:] |
---|
| 45 | |
---|
| 46 | // Quick Search: |
---|
| 47 | if(!params.filter) { |
---|
| 48 | assetSubItemInstanceList = AssetSubItem.list( params ) |
---|
| 49 | assetSubItemInstanceTotal = AssetSubItem.count() |
---|
| 50 | filterParams.quickSearch = params.quickSearch |
---|
| 51 | } |
---|
| 52 | else { |
---|
| 53 | // filterPane: |
---|
| 54 | assetSubItemInstanceList = filterService.filter( params, AssetSubItem ) |
---|
| 55 | assetSubItemInstanceTotal = filterService.count( params, AssetSubItem ) |
---|
| 56 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | // export plugin: |
---|
| 60 | if(params?.format && params.format != "html") { |
---|
| 61 | |
---|
| 62 | def dateFmt = { date -> |
---|
| 63 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | String title = "AssetSubItem List." |
---|
| 67 | |
---|
| 68 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
| 69 | response.setHeader("Content-disposition", "attachment; filename=assets.${params.extension}") |
---|
| 70 | List fields = ["name", |
---|
| 71 | "description"] |
---|
| 72 | Map labels = ["name": "AssetSubItem", |
---|
| 73 | "description": "Description"] |
---|
| 74 | |
---|
| 75 | Map formatters = [:] |
---|
| 76 | Map parameters = [title: title, separator: ","] |
---|
| 77 | |
---|
| 78 | exportService.export(params.format, |
---|
| 79 | response.outputStream, |
---|
| 80 | assetSubItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
| 81 | fields, |
---|
| 82 | labels, |
---|
| 83 | formatters, |
---|
| 84 | parameters) |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | // Add some basic params to filterParams. |
---|
| 88 | filterParams.max = params.max |
---|
| 89 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
| 90 | filterParams.sort = params.sort ?: "id" |
---|
| 91 | filterParams.order = params.order ?: "desc" |
---|
| 92 | |
---|
| 93 | return[ assetSubItemInstanceList: assetSubItemInstanceList, |
---|
| 94 | assetSubItemInstanceTotal: assetSubItemInstanceTotal, |
---|
| 95 | filterParams: filterParams ] |
---|
| 96 | |
---|
| 97 | } // end search() |
---|
| 98 | |
---|
| 99 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[274] | 100 | def show = { |
---|
[369] | 101 | |
---|
| 102 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 103 | if(params._action_Show) |
---|
[375] | 104 | params.action='show' |
---|
[369] | 105 | |
---|
[274] | 106 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 107 | |
---|
| 108 | if(!assetSubItemInstance) { |
---|
| 109 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
[342] | 110 | redirect(action: "search") |
---|
[274] | 111 | } |
---|
| 112 | else { return [ assetSubItemInstance : assetSubItemInstance ] } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | def delete = { |
---|
[347] | 116 | def result = assetSubItemService.delete(params) |
---|
| 117 | |
---|
| 118 | if(!result.error) { |
---|
| 119 | flash.message = g.message(code: "default.delete.success", args: ["AssetSubItem", params.id]) |
---|
| 120 | redirect(action: search) |
---|
| 121 | return |
---|
[274] | 122 | } |
---|
[347] | 123 | |
---|
| 124 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 125 | |
---|
| 126 | if(result.error.code == "default.not.found") { |
---|
| 127 | redirect(action: search) |
---|
| 128 | return |
---|
[274] | 129 | } |
---|
[347] | 130 | |
---|
| 131 | redirect(action:show, id: params.id) |
---|
[274] | 132 | } |
---|
| 133 | |
---|
| 134 | def edit = { |
---|
[369] | 135 | |
---|
| 136 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 137 | if(params._action_Edit) |
---|
[375] | 138 | params.action='edit' |
---|
[369] | 139 | |
---|
[274] | 140 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 141 | |
---|
| 142 | if(!assetSubItemInstance) { |
---|
| 143 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
[342] | 144 | redirect(action: "search") |
---|
[352] | 145 | return |
---|
[274] | 146 | } |
---|
[352] | 147 | |
---|
| 148 | def possibleParentItems = assetSubItemService.possibleParentItems(assetSubItemInstance) |
---|
| 149 | |
---|
| 150 | return [ assetSubItemInstance : assetSubItemInstance, possibleParentItems: possibleParentItems ] |
---|
[274] | 151 | } |
---|
| 152 | |
---|
| 153 | def update = { |
---|
| 154 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 155 | if(assetSubItemInstance) { |
---|
| 156 | if(params.version) { |
---|
| 157 | def version = params.version.toLong() |
---|
| 158 | if(assetSubItemInstance.version > version) { |
---|
| 159 | |
---|
| 160 | assetSubItemInstance.errors.rejectValue("version", "assetSubItem.optimistic.locking.failure", "Another user has updated this AssetSubItem while you were editing.") |
---|
| 161 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
| 162 | return |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | assetSubItemInstance.properties = params |
---|
| 167 | |
---|
[387] | 168 | use(WordUtils) { |
---|
| 169 | assetSubItemInstance.name = assetSubItemInstance.name.capitalize() |
---|
| 170 | assetSubItemInstance.description = assetSubItemInstance.description.capitalize() |
---|
| 171 | } |
---|
| 172 | |
---|
[352] | 173 | if(assetSubItemInstance.assets) { |
---|
| 174 | assetSubItemInstance.parentItem = null |
---|
| 175 | } |
---|
[274] | 176 | |
---|
| 177 | if(!assetSubItemInstance.hasErrors() && assetSubItemInstance.save(flush: true)) { |
---|
[387] | 178 | flash.message = "Sub Item '${assetSubItemInstance.name}' updated" |
---|
[274] | 179 | |
---|
[276] | 180 | if(assetSubItemInstance.assets) |
---|
| 181 | redirect(controller: "assetDetailed", action:"show", id: assetSubItemInstance.assets.toArray()[0].id) |
---|
[274] | 182 | else { |
---|
| 183 | //Get the top parentItem. |
---|
| 184 | def parentItem = assetSubItemInstance.parentItem |
---|
| 185 | def i = 0 |
---|
| 186 | while(parentItem?.parentItem) { |
---|
| 187 | parentItem = parentItem.parentItem |
---|
| 188 | // Protect against infinite recurrsion. |
---|
| 189 | i++ |
---|
| 190 | if(i > 100) |
---|
| 191 | break |
---|
| 192 | } |
---|
| 193 | |
---|
[276] | 194 | if(parentItem?.assets) |
---|
| 195 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
[274] | 196 | else |
---|
| 197 | redirect(action:"show", id: assetSubItemInstance.id) // End stop if all else fails. |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | else { |
---|
| 201 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | else { |
---|
| 205 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
[342] | 206 | redirect(action: "search") |
---|
[274] | 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | def create = { |
---|
[285] | 211 | def result = assetSubItemService.create(params) |
---|
| 212 | |
---|
| 213 | if(!result.error) |
---|
| 214 | return [assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance] |
---|
| 215 | |
---|
| 216 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
| 217 | redirect(controller: 'assetDetailed', action: 'search') |
---|
[274] | 218 | } |
---|
| 219 | |
---|
| 220 | def save = { |
---|
[285] | 221 | def result = assetSubItemService.save(params) |
---|
[274] | 222 | |
---|
[285] | 223 | if(!result.error) { |
---|
| 224 | // Success. |
---|
[387] | 225 | flash.message = g.message(code: "default.create.success", args: ["Sub Item", "'${result.assetSubItemInstance.name}'"]) |
---|
[274] | 226 | |
---|
[285] | 227 | /// @todo: This just grabs the first parent in an unsorted list and needs improving. |
---|
| 228 | if(result.assetSubItemInstance.assets) |
---|
| 229 | redirect(controller: "assetDetailed", action:"show", id: result.assetSubItemInstance.assets.toArray()[0].id) |
---|
[274] | 230 | else { |
---|
| 231 | //Get the top parentItem. |
---|
[285] | 232 | def parentItem = result.assetSubItemInstance.parentItem |
---|
[274] | 233 | def i = 0 |
---|
| 234 | while(parentItem?.parentItem) { |
---|
| 235 | parentItem = parentItem.parentItem |
---|
| 236 | // Protect against infinite recurrsion. |
---|
| 237 | i++ |
---|
| 238 | if(i > 100) |
---|
| 239 | break |
---|
| 240 | } |
---|
| 241 | |
---|
[276] | 242 | if(parentItem?.assets) |
---|
| 243 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
[274] | 244 | else |
---|
[285] | 245 | redirect(action:"show", id: result.assetSubItemInstance.id) // End stop if all else fails. |
---|
[274] | 246 | } |
---|
[285] | 247 | return |
---|
[274] | 248 | } |
---|
| 249 | |
---|
[285] | 250 | if(result.error.code == "assetSubItem.asset.not.found") |
---|
| 251 | flash.errorMessage = g.message(code: "default.create.success") |
---|
| 252 | |
---|
| 253 | render(view:'create', model:[assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance]) |
---|
| 254 | } |
---|
| 255 | |
---|
[274] | 256 | } // end class |
---|