| 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
|---|
| 2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
|---|
| 3 | import org.apache.commons.lang.WordUtils |
|---|
| 4 | |
|---|
| 5 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager']) |
|---|
| 6 | class AssetDetailedController extends BaseController { |
|---|
| 7 | |
|---|
| 8 | def assetCsvService |
|---|
| 9 | def filterService |
|---|
| 10 | def exportService |
|---|
| 11 | def assetService |
|---|
| 12 | def assetTreeService |
|---|
| 13 | def taskService |
|---|
| 14 | |
|---|
| 15 | // the delete, save and update actions only accept POST requests |
|---|
| 16 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', saveCopy:'POST'] |
|---|
| 17 | |
|---|
| 18 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
|---|
| 19 | def index = { redirect(action:search,params:params) } |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * Set session.assetSearchParamsMax |
|---|
| 23 | */ |
|---|
| 24 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
|---|
| 25 | def setSearchParamsMax = { |
|---|
| 26 | def max = 1000 |
|---|
| 27 | if(params.newMax?.isInteger()) { |
|---|
| 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 | /** |
|---|
| 38 | * Build and return the asset tree response for the AJAX request. |
|---|
| 39 | */ |
|---|
| 40 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
|---|
| 41 | def assetTree = { |
|---|
| 42 | def s = assetTreeService.buildAssetTree(params, session) |
|---|
| 43 | render s |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 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 | |
|---|
| 54 | /** |
|---|
| 55 | * Save the asset tree status in the current http session. |
|---|
| 56 | */ |
|---|
| 57 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
|---|
| 58 | def saveAssetTreeStatus = { |
|---|
| 59 | session.assetTreeVisibleBranches = params.assetTreeVisibleBranches |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Disaply the import view. |
|---|
| 64 | */ |
|---|
| 65 | def importAssetTree = { |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Handle the import save. |
|---|
| 70 | */ |
|---|
| 71 | def importAssetTreeSave = { |
|---|
| 72 | def result = assetCsvService.importAssetTree(request) |
|---|
| 73 | |
|---|
| 74 | if(!result.error) { |
|---|
| 75 | flash.message = g.message(code: "asset.tree.import.success") |
|---|
| 76 | redirect(action:search) |
|---|
| 77 | return |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
|---|
| 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 | */ |
|---|
| 89 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
|---|
| 90 | def exportAssetTreeTemplate = { |
|---|
| 91 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
|---|
| 92 | response.setHeader("Content-disposition", "attachment; filename=AssetTreeTemplate.csv") |
|---|
| 93 | def s = assetCsvService.buildAssetTreeTemplate() |
|---|
| 94 | render s |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Export a csv test file. |
|---|
| 99 | */ |
|---|
| 100 | def exportAssetTreeTest = { |
|---|
| 101 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
|---|
| 102 | response.setHeader("Content-disposition", "attachment; filename=AssetTreeTestFile.csv") |
|---|
| 103 | def s = assetCsvService.buildAssetTreeTest() |
|---|
| 104 | render s |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * Export the entire asset tree as a csv file. |
|---|
| 109 | */ |
|---|
| 110 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
|---|
| 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") |
|---|
| 117 | def s = assetCsvService.buildAssetTree(assetList) |
|---|
| 118 | render s |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Search action. |
|---|
| 123 | */ |
|---|
| 124 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
|---|
| 125 | def search = { |
|---|
| 126 | |
|---|
| 127 | if(session.assetSearchParamsMax) |
|---|
| 128 | params.max = session.assetSearchParamsMax |
|---|
| 129 | |
|---|
| 130 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
|---|
| 131 | |
|---|
| 132 | def assetInstanceList = [] |
|---|
| 133 | def assetInstanceTotal |
|---|
| 134 | def filterParams = [:] |
|---|
| 135 | |
|---|
| 136 | // Quick Search: |
|---|
| 137 | if(!params.filter) { |
|---|
| 138 | assetInstanceList = Asset.list( params ) |
|---|
| 139 | assetInstanceTotal = Asset.count() |
|---|
| 140 | filterParams.quickSearch = params.quickSearch |
|---|
| 141 | } |
|---|
| 142 | else { |
|---|
| 143 | // filterPane: |
|---|
| 144 | assetInstanceList = filterService.filter( params, Asset ) |
|---|
| 145 | assetInstanceTotal = filterService.count( params, Asset ) |
|---|
| 146 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 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] |
|---|
| 188 | response.setHeader("Content-disposition", "attachment; filename=Assets.${params.extension}") |
|---|
| 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 | |
|---|
| 223 | /** |
|---|
| 224 | * Show action. |
|---|
| 225 | */ |
|---|
| 226 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
|---|
| 227 | def show = { |
|---|
| 228 | |
|---|
| 229 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
|---|
| 230 | if(params._action_Show) |
|---|
| 231 | params.action='show' |
|---|
| 232 | |
|---|
| 233 | def assetInstance = Asset.get( params.id ) |
|---|
| 234 | |
|---|
| 235 | if(!assetInstance) { |
|---|
| 236 | flash.message = "Asset not found with id ${params.id}" |
|---|
| 237 | redirect(action:search) |
|---|
| 238 | return |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | return [ assetInstance : assetInstance, |
|---|
| 242 | parentPMs: taskService.getParentPMs(assetInstance)] |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * Delete action. |
|---|
| 247 | */ |
|---|
| 248 | def delete = { |
|---|
| 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 |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
|---|
| 258 | |
|---|
| 259 | if(result.error.code == "default.not.found") { |
|---|
| 260 | redirect(action:search) |
|---|
| 261 | return |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | redirect(action:show, id: params.id) |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | /** |
|---|
| 268 | * Edit action. |
|---|
| 269 | */ |
|---|
| 270 | def edit = { |
|---|
| 271 | |
|---|
| 272 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
|---|
| 273 | if(params._action_Edit) |
|---|
| 274 | params.action='edit' |
|---|
| 275 | |
|---|
| 276 | def assetInstance = Asset.get( params.id ) |
|---|
| 277 | |
|---|
| 278 | if(!assetInstance) { |
|---|
| 279 | flash.message = "Asset not found with id ${params.id}" |
|---|
| 280 | redirect(action:search) |
|---|
| 281 | return |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | return [ assetInstance : assetInstance, |
|---|
| 285 | possibleAssetSubItems: assetService.possibleAssetSubItems(), |
|---|
| 286 | parentPMs: taskService.getParentPMs(assetInstance) ] |
|---|
| 287 | |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | /** |
|---|
| 291 | * Update action. |
|---|
| 292 | */ |
|---|
| 293 | def update = { |
|---|
| 294 | def assetInstance = Asset.get( params.id ) |
|---|
| 295 | |
|---|
| 296 | if(!assetInstance) { |
|---|
| 297 | flash.message = "Asset not found with id ${params.id}" |
|---|
| 298 | redirect(action:search) |
|---|
| 299 | return |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | if(params.version) { |
|---|
| 303 | def version = params.version.toLong() |
|---|
| 304 | if(assetInstance.version > version) { |
|---|
| 305 | |
|---|
| 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 |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | assetInstance.properties = params |
|---|
| 315 | |
|---|
| 316 | use(WordUtils) { |
|---|
| 317 | assetInstance.name = assetInstance.name.capitalize() |
|---|
| 318 | assetInstance.description = assetInstance.description.capitalize() |
|---|
| 319 | } |
|---|
| 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 | } |
|---|
| 327 | else { |
|---|
| 328 | render(view:'edit',model:[assetInstance:assetInstance, |
|---|
| 329 | possibleAssetSubItems: assetService.possibleAssetSubItems(), |
|---|
| 330 | parentPMs: taskService.getParentPMs(assetInstance)] ) |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | /** |
|---|
| 336 | * Create action. |
|---|
| 337 | */ |
|---|
| 338 | def create = { |
|---|
| 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) |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | /** |
|---|
| 349 | * Copy action. |
|---|
| 350 | */ |
|---|
| 351 | def copy = { |
|---|
| 352 | def result = assetService.copy(params) |
|---|
| 353 | |
|---|
| 354 | if(!result.error) |
|---|
| 355 | return [assetInstance: result.assetInstance, assetToCopy: result.assetToCopy] |
|---|
| 356 | |
|---|
| 357 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
|---|
| 358 | redirect(action: search) |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | /** |
|---|
| 362 | * Save action. |
|---|
| 363 | */ |
|---|
| 364 | def save = { |
|---|
| 365 | def result = assetService.save(params) |
|---|
| 366 | |
|---|
| 367 | if(!result.error) { |
|---|
| 368 | flash.message = g.message(code: "default.create.success", args: ["Asset", "'${result.assetInstance.name}'"]) |
|---|
| 369 | redirect(action:show, id: result.assetInstance.id) |
|---|
| 370 | return |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | render(view:'create', model:[assetInstance: result.assetInstance]) |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | /** |
|---|
| 377 | * Copy save action. |
|---|
| 378 | */ |
|---|
| 379 | def saveCopy = { |
|---|
| 380 | def result = assetService.saveCopy(params) |
|---|
| 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]) |
|---|
| 390 | redirect(action: search) |
|---|
| 391 | return |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | flash.errorMessage = g.message(code: result.error.code, args: ["Asset"]) |
|---|
| 395 | |
|---|
| 396 | render(view:'copy', model:[assetInstance: result.assetInstance, assetToCopy: result.assetToCopy]) |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | } // end class |
|---|