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