[118] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
[271] | 2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
[387] | 3 | import org.apache.commons.lang.WordUtils |
---|
[118] | 4 | |
---|
[298] | 5 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager']) |
---|
[124] | 6 | class AssetDetailedController extends BaseController { |
---|
[156] | 7 | |
---|
[414] | 8 | def assetCsvService |
---|
[156] | 9 | def filterService |
---|
[271] | 10 | def exportService |
---|
[278] | 11 | def assetService |
---|
[312] | 12 | def assetTreeService |
---|
[156] | 13 | |
---|
[118] | 14 | // the delete, save and update actions only accept POST requests |
---|
[271] | 15 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', saveCopy:'POST'] |
---|
[118] | 16 | |
---|
[298] | 17 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[306] | 18 | def index = { redirect(action:search,params:params) } |
---|
[298] | 19 | |
---|
[325] | 20 | /** |
---|
[326] | 21 | * Set session.assetSearchParamsMax |
---|
| 22 | */ |
---|
| 23 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
| 24 | def setSearchParamsMax = { |
---|
| 25 | def max = 1000 |
---|
| 26 | if(params.newMax.isInteger()) { |
---|
| 27 | def i = params.newMax.toInteger() |
---|
| 28 | if(i > 0 && i <= max) |
---|
| 29 | session.assetSearchParamsMax = params.newMax |
---|
| 30 | if(i > max) |
---|
| 31 | session.assetSearchParamsMax = max |
---|
| 32 | } |
---|
| 33 | forward(action: 'search', params: params) |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /** |
---|
[325] | 37 | * Build and return the asset tree response for the AJAX request. |
---|
| 38 | */ |
---|
| 39 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[312] | 40 | def assetTree = { |
---|
| 41 | def s = assetTreeService.buildAssetTree(params, session) |
---|
| 42 | render s |
---|
| 43 | } |
---|
| 44 | |
---|
[459] | 45 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
| 46 | def exportAssetTreeHtml = { |
---|
| 47 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 48 | response.setHeader("Content-disposition", "attachment; filename=AssetTree.html") |
---|
| 49 | def s = assetTreeService.buildAssetTree(params, session) |
---|
| 50 | render s |
---|
| 51 | } |
---|
| 52 | |
---|
[318] | 53 | /** |
---|
| 54 | * Save the asset tree status in the current http session. |
---|
| 55 | */ |
---|
[325] | 56 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[318] | 57 | def saveAssetTreeStatus = { |
---|
| 58 | session.assetTreeVisibleBranches = params.assetTreeVisibleBranches |
---|
| 59 | } |
---|
| 60 | |
---|
[325] | 61 | /** |
---|
| 62 | * Disaply the import view. |
---|
| 63 | */ |
---|
| 64 | def importAssetTree = { |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * Handle the import save. |
---|
| 69 | */ |
---|
[271] | 70 | def importAssetTreeSave = { |
---|
[414] | 71 | def result = assetCsvService.importAssetTree(request) |
---|
[271] | 72 | |
---|
[421] | 73 | if(!result.error) { |
---|
[271] | 74 | flash.message = g.message(code: "asset.tree.import.success") |
---|
[421] | 75 | redirect(action:search) |
---|
| 76 | return |
---|
| 77 | } |
---|
[271] | 78 | |
---|
[421] | 79 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[271] | 80 | redirect(action: importAssetTree) |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Export a csv template. |
---|
| 85 | * NOTE: IE has a 'validating' bug in dev mode that causes the export to take a long time! |
---|
| 86 | * This does not appear to be a problem once deployed to Tomcat. |
---|
| 87 | */ |
---|
[298] | 88 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[271] | 89 | def exportAssetTreeTemplate = { |
---|
| 90 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 91 | response.setHeader("Content-disposition", "attachment; filename=AssetTreeTemplate.csv") |
---|
[414] | 92 | def s = assetCsvService.buildAssetTreeTemplate() |
---|
[271] | 93 | render s |
---|
| 94 | } |
---|
| 95 | |
---|
[325] | 96 | /** |
---|
| 97 | * Export a csv test file. |
---|
| 98 | */ |
---|
[302] | 99 | def exportAssetTreeTest = { |
---|
| 100 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 101 | response.setHeader("Content-disposition", "attachment; filename=AssetTreeTestFile.csv") |
---|
[414] | 102 | def s = assetCsvService.buildAssetTreeTest() |
---|
[302] | 103 | render s |
---|
| 104 | } |
---|
| 105 | |
---|
[298] | 106 | /** |
---|
[325] | 107 | * Export the entire asset tree as a csv file. |
---|
[298] | 108 | */ |
---|
| 109 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[271] | 110 | def exportAssetTree = { |
---|
| 111 | |
---|
| 112 | def assetList = Asset.list() |
---|
| 113 | |
---|
| 114 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 115 | response.setHeader("Content-disposition", "attachment; filename=AssetTree.csv") |
---|
[414] | 116 | def s = assetCsvService.buildAssetTree(assetList) |
---|
[271] | 117 | render s |
---|
| 118 | } |
---|
| 119 | |
---|
[298] | 120 | /** |
---|
| 121 | * Search action. |
---|
| 122 | */ |
---|
| 123 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[139] | 124 | def search = { |
---|
[156] | 125 | |
---|
[325] | 126 | if(session.assetSearchParamsMax) |
---|
| 127 | params.max = session.assetSearchParamsMax |
---|
[271] | 128 | |
---|
| 129 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
| 130 | |
---|
| 131 | def assetInstanceList = [] |
---|
| 132 | def assetInstanceTotal |
---|
| 133 | def filterParams = [:] |
---|
| 134 | |
---|
[156] | 135 | // Quick Search: |
---|
| 136 | if(!params.filter) { |
---|
[271] | 137 | assetInstanceList = Asset.list( params ) |
---|
| 138 | assetInstanceTotal = Asset.count() |
---|
| 139 | filterParams.quickSearch = params.quickSearch |
---|
[156] | 140 | } |
---|
[271] | 141 | else { |
---|
[156] | 142 | // filterPane: |
---|
[271] | 143 | assetInstanceList = filterService.filter( params, Asset ) |
---|
| 144 | assetInstanceTotal = filterService.count( params, Asset ) |
---|
| 145 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 146 | } |
---|
[139] | 147 | |
---|
[271] | 148 | // export plugin: |
---|
| 149 | if(params?.format && params.format != "html") { |
---|
| 150 | |
---|
| 151 | def dateFmt = { date -> |
---|
| 152 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | // def fmtAsset = { m -> |
---|
| 156 | // def r = '' |
---|
| 157 | // def assetInstance = Asset.findByName(m) |
---|
| 158 | // |
---|
| 159 | // r += assetInstance |
---|
| 160 | // r += ", " |
---|
| 161 | // |
---|
| 162 | // def lastSubAsset = assetInstance.subAssets.size() - 1 |
---|
| 163 | // assetInstance.subAssets.eachWithIndex() { obj, i -> |
---|
| 164 | // r += "\"" + obj + "\"" |
---|
| 165 | // if( i < lastSubAsset ) |
---|
| 166 | // r += ", " |
---|
| 167 | // } |
---|
| 168 | // return r |
---|
| 169 | // } |
---|
| 170 | |
---|
| 171 | // def fmtSubAsset = { m -> |
---|
| 172 | // def r = '' |
---|
| 173 | // m.each() { |
---|
| 174 | // def machine = Machine.findByName(it) |
---|
| 175 | // def assemblies = machine.assemblies |
---|
| 176 | // r += machine.name |
---|
| 177 | // r += " " |
---|
| 178 | // r += assemblies |
---|
| 179 | // r += " " |
---|
| 180 | // } |
---|
| 181 | // return r |
---|
| 182 | // } |
---|
| 183 | |
---|
| 184 | String title = "Asset List." |
---|
| 185 | |
---|
| 186 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
[390] | 187 | response.setHeader("Content-disposition", "attachment; filename=Assets.${params.extension}") |
---|
[271] | 188 | List fields = ["section.site", |
---|
| 189 | "section", |
---|
| 190 | "name", |
---|
| 191 | "description"] |
---|
| 192 | Map labels = ["section.site": "Site", |
---|
| 193 | "section": "Section", |
---|
| 194 | "name": "Asset", |
---|
| 195 | "description": "Description"] |
---|
| 196 | // Map labels |
---|
| 197 | // Map formatters = ["subAsset.name": fmtSubAsset] |
---|
| 198 | Map formatters = [:] |
---|
| 199 | Map parameters = [title: title, separator: ","] |
---|
| 200 | |
---|
| 201 | exportService.export(params.format, |
---|
| 202 | response.outputStream, |
---|
| 203 | assetInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
| 204 | fields, |
---|
| 205 | labels, |
---|
| 206 | formatters, |
---|
| 207 | parameters) |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | // Add some basic params to filterParams. |
---|
| 211 | filterParams.max = params.max |
---|
| 212 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
| 213 | filterParams.sort = params.sort ?: "id" |
---|
| 214 | filterParams.order = params.order ?: "desc" |
---|
| 215 | |
---|
| 216 | return[ assetInstanceList: assetInstanceList, |
---|
| 217 | assetInstanceTotal: assetInstanceTotal, |
---|
| 218 | filterParams: filterParams ] |
---|
| 219 | |
---|
| 220 | } // end search() |
---|
| 221 | |
---|
[298] | 222 | /** |
---|
| 223 | * Show action. |
---|
| 224 | */ |
---|
| 225 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[118] | 226 | def show = { |
---|
[283] | 227 | |
---|
[139] | 228 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 229 | if(params._action_Show) |
---|
[375] | 230 | params.action='show' |
---|
[283] | 231 | |
---|
[118] | 232 | def assetInstance = Asset.get( params.id ) |
---|
| 233 | |
---|
| 234 | if(!assetInstance) { |
---|
| 235 | flash.message = "Asset not found with id ${params.id}" |
---|
[139] | 236 | redirect(action:search) |
---|
[118] | 237 | } |
---|
| 238 | else { return [ assetInstance : assetInstance ] } |
---|
| 239 | } |
---|
| 240 | |
---|
[298] | 241 | /** |
---|
| 242 | * Delete action. |
---|
| 243 | */ |
---|
[118] | 244 | def delete = { |
---|
[350] | 245 | def result = assetService.delete(params) |
---|
| 246 | |
---|
| 247 | if(!result.error) { |
---|
| 248 | flash.message = g.message(code: "default.delete.success", args: ["Asset", params.id]) |
---|
| 249 | redirect(action:search) |
---|
| 250 | return |
---|
[118] | 251 | } |
---|
[350] | 252 | |
---|
| 253 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 254 | |
---|
| 255 | if(result.error.code == "default.not.found") { |
---|
[139] | 256 | redirect(action:search) |
---|
[350] | 257 | return |
---|
[118] | 258 | } |
---|
[350] | 259 | |
---|
| 260 | redirect(action:show, id: params.id) |
---|
[118] | 261 | } |
---|
| 262 | |
---|
[298] | 263 | /** |
---|
| 264 | * Edit action. |
---|
| 265 | */ |
---|
[118] | 266 | def edit = { |
---|
[283] | 267 | |
---|
[139] | 268 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 269 | if(params._action_Edit) |
---|
[375] | 270 | params.action='edit' |
---|
[283] | 271 | |
---|
[118] | 272 | def assetInstance = Asset.get( params.id ) |
---|
| 273 | |
---|
| 274 | if(!assetInstance) { |
---|
| 275 | flash.message = "Asset not found with id ${params.id}" |
---|
[139] | 276 | redirect(action:search) |
---|
[118] | 277 | } |
---|
| 278 | else { |
---|
[305] | 279 | return [ assetInstance : assetInstance, possibleAssetSubItems: assetService.possibleAssetSubItems() ] |
---|
[118] | 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
[298] | 283 | /** |
---|
| 284 | * Update action. |
---|
| 285 | */ |
---|
[118] | 286 | def update = { |
---|
| 287 | def assetInstance = Asset.get( params.id ) |
---|
| 288 | if(assetInstance) { |
---|
| 289 | if(params.version) { |
---|
| 290 | def version = params.version.toLong() |
---|
| 291 | if(assetInstance.version > version) { |
---|
[283] | 292 | |
---|
[403] | 293 | assetInstance.errors.rejectValue("version", "default.optimistic.locking.failure") |
---|
[305] | 294 | render(view:'edit',model:[assetInstance:assetInstance, possibleAssetSubItems: assetService.possibleAssetSubItems()]) |
---|
[118] | 295 | return |
---|
| 296 | } |
---|
| 297 | } |
---|
[286] | 298 | |
---|
[118] | 299 | assetInstance.properties = params |
---|
[286] | 300 | |
---|
[387] | 301 | use(WordUtils) { |
---|
| 302 | assetInstance.name = assetInstance.name.capitalize() |
---|
| 303 | assetInstance.description = assetInstance.description.capitalize() |
---|
| 304 | } |
---|
| 305 | |
---|
[286] | 306 | assetInstance.setAssetSubItemsFromCheckBoxList(params.assetSubItems) |
---|
| 307 | |
---|
[178] | 308 | if(!assetInstance.hasErrors() && assetInstance.save(flush: true)) { |
---|
[387] | 309 | flash.message = "Asset '${assetInstance.name}' updated" |
---|
[118] | 310 | redirect(action:show,id:assetInstance.id) |
---|
| 311 | } |
---|
| 312 | else { |
---|
[305] | 313 | render(view:'edit',model:[assetInstance:assetInstance, possibleAssetSubItems: assetService.possibleAssetSubItems()]) |
---|
[118] | 314 | } |
---|
| 315 | } |
---|
| 316 | else { |
---|
| 317 | flash.message = "Asset not found with id ${params.id}" |
---|
[334] | 318 | redirect(action:search) |
---|
[118] | 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[298] | 322 | /** |
---|
| 323 | * Create action. |
---|
| 324 | */ |
---|
[118] | 325 | def create = { |
---|
[284] | 326 | def result = assetService.create(params) |
---|
| 327 | |
---|
| 328 | if(!result.error) |
---|
| 329 | return [assetInstance: result.assetInstance] |
---|
| 330 | |
---|
| 331 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 332 | redirect(action: search) |
---|
[118] | 333 | } |
---|
| 334 | |
---|
[298] | 335 | /** |
---|
| 336 | * Copy action. |
---|
| 337 | */ |
---|
[271] | 338 | def copy = { |
---|
[284] | 339 | def result = assetService.copy(params) |
---|
[271] | 340 | |
---|
[284] | 341 | if(!result.error) |
---|
| 342 | return [assetInstance: result.assetInstance, assetToCopy: result.assetToCopy] |
---|
[271] | 343 | |
---|
[284] | 344 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 345 | redirect(action: search) |
---|
[271] | 346 | } |
---|
| 347 | |
---|
[298] | 348 | /** |
---|
| 349 | * Save action. |
---|
| 350 | */ |
---|
[118] | 351 | def save = { |
---|
[284] | 352 | def result = assetService.save(params) |
---|
| 353 | |
---|
| 354 | if(!result.error) { |
---|
[387] | 355 | flash.message = g.message(code: "default.create.success", args: ["Asset", "'${result.assetInstance.name}'"]) |
---|
[284] | 356 | redirect(action:show, id: result.assetInstance.id) |
---|
| 357 | return |
---|
[118] | 358 | } |
---|
[284] | 359 | |
---|
| 360 | render(view:'create', model:[assetInstance: result.assetInstance]) |
---|
[118] | 361 | } |
---|
[271] | 362 | |
---|
[298] | 363 | /** |
---|
| 364 | * Copy save action. |
---|
| 365 | */ |
---|
[271] | 366 | def saveCopy = { |
---|
[278] | 367 | def result = assetService.saveCopy(params) |
---|
[271] | 368 | |
---|
| 369 | if(!result.error) { |
---|
| 370 | flash.message = g.message(code: "default.create.success", args: ["Asset", result.assetInstance.id]) |
---|
| 371 | redirect(action:show, id: result.assetInstance.id) |
---|
| 372 | return |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | if(result.error.code == "default.not.found") { |
---|
| 376 | flash.message = g.message(code: result.error.code, args: ["Asset", params.assetToCopy?.id]) |
---|
[334] | 377 | redirect(action: search) |
---|
[271] | 378 | return |
---|
| 379 | } |
---|
| 380 | |
---|
[429] | 381 | flash.errorMessage = g.message(code: result.error.code, args: ["Asset"]) |
---|
[278] | 382 | |
---|
[271] | 383 | render(view:'copy', model:[assetInstance: result.assetInstance, assetToCopy: result.assetToCopy]) |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | } // end class |
---|