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