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