[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 | * Search action. |
---|
| 111 | */ |
---|
| 112 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[139] | 113 | def search = { |
---|
[156] | 114 | |
---|
[325] | 115 | if(session.assetSearchParamsMax) |
---|
| 116 | params.max = session.assetSearchParamsMax |
---|
[271] | 117 | |
---|
| 118 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
| 119 | |
---|
| 120 | def assetInstanceList = [] |
---|
| 121 | def assetInstanceTotal |
---|
| 122 | def filterParams = [:] |
---|
| 123 | |
---|
[156] | 124 | // Quick Search: |
---|
| 125 | if(!params.filter) { |
---|
[271] | 126 | assetInstanceList = Asset.list( params ) |
---|
| 127 | assetInstanceTotal = Asset.count() |
---|
| 128 | filterParams.quickSearch = params.quickSearch |
---|
[156] | 129 | } |
---|
[271] | 130 | else { |
---|
[156] | 131 | // filterPane: |
---|
[271] | 132 | assetInstanceList = filterService.filter( params, Asset ) |
---|
| 133 | assetInstanceTotal = filterService.count( params, Asset ) |
---|
| 134 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 135 | } |
---|
[139] | 136 | |
---|
[271] | 137 | // export plugin: |
---|
| 138 | if(params?.format && params.format != "html") { |
---|
| 139 | |
---|
| 140 | def dateFmt = { date -> |
---|
| 141 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | // def fmtAsset = { m -> |
---|
| 145 | // def r = '' |
---|
| 146 | // def assetInstance = Asset.findByName(m) |
---|
| 147 | // |
---|
| 148 | // r += assetInstance |
---|
| 149 | // r += ", " |
---|
| 150 | // |
---|
| 151 | // def lastSubAsset = assetInstance.subAssets.size() - 1 |
---|
| 152 | // assetInstance.subAssets.eachWithIndex() { obj, i -> |
---|
| 153 | // r += "\"" + obj + "\"" |
---|
| 154 | // if( i < lastSubAsset ) |
---|
| 155 | // r += ", " |
---|
| 156 | // } |
---|
| 157 | // return r |
---|
| 158 | // } |
---|
| 159 | |
---|
| 160 | // def fmtSubAsset = { m -> |
---|
| 161 | // def r = '' |
---|
| 162 | // m.each() { |
---|
| 163 | // def machine = Machine.findByName(it) |
---|
| 164 | // def assemblies = machine.assemblies |
---|
| 165 | // r += machine.name |
---|
| 166 | // r += " " |
---|
| 167 | // r += assemblies |
---|
| 168 | // r += " " |
---|
| 169 | // } |
---|
| 170 | // return r |
---|
| 171 | // } |
---|
| 172 | |
---|
| 173 | String title = "Asset List." |
---|
| 174 | |
---|
| 175 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
[326] | 176 | response.setHeader("Content-disposition", "attachment; filename=assets.${params.extension}") |
---|
[271] | 177 | List fields = ["section.site", |
---|
| 178 | "section", |
---|
| 179 | "name", |
---|
| 180 | "description"] |
---|
| 181 | Map labels = ["section.site": "Site", |
---|
| 182 | "section": "Section", |
---|
| 183 | "name": "Asset", |
---|
| 184 | "description": "Description"] |
---|
| 185 | // Map labels |
---|
| 186 | // Map formatters = ["subAsset.name": fmtSubAsset] |
---|
| 187 | Map formatters = [:] |
---|
| 188 | Map parameters = [title: title, separator: ","] |
---|
| 189 | |
---|
| 190 | exportService.export(params.format, |
---|
| 191 | response.outputStream, |
---|
| 192 | assetInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
| 193 | fields, |
---|
| 194 | labels, |
---|
| 195 | formatters, |
---|
| 196 | parameters) |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | // Add some basic params to filterParams. |
---|
| 200 | filterParams.max = params.max |
---|
| 201 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
| 202 | filterParams.sort = params.sort ?: "id" |
---|
| 203 | filterParams.order = params.order ?: "desc" |
---|
| 204 | |
---|
| 205 | return[ assetInstanceList: assetInstanceList, |
---|
| 206 | assetInstanceTotal: assetInstanceTotal, |
---|
| 207 | filterParams: filterParams ] |
---|
| 208 | |
---|
| 209 | } // end search() |
---|
| 210 | |
---|
[298] | 211 | /** |
---|
| 212 | * Show action. |
---|
| 213 | */ |
---|
| 214 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[118] | 215 | def show = { |
---|
[283] | 216 | |
---|
[139] | 217 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 218 | if(params._action_Show) |
---|
| 219 | { params.action='show' } |
---|
[283] | 220 | |
---|
[118] | 221 | def assetInstance = Asset.get( params.id ) |
---|
| 222 | |
---|
| 223 | if(!assetInstance) { |
---|
| 224 | flash.message = "Asset not found with id ${params.id}" |
---|
[139] | 225 | redirect(action:search) |
---|
[118] | 226 | } |
---|
| 227 | else { return [ assetInstance : assetInstance ] } |
---|
| 228 | } |
---|
| 229 | |
---|
[298] | 230 | /** |
---|
| 231 | * Delete action. |
---|
| 232 | */ |
---|
[118] | 233 | def delete = { |
---|
| 234 | def assetInstance = Asset.get( params.id ) |
---|
| 235 | if(assetInstance) { |
---|
| 236 | try { |
---|
[161] | 237 | assetInstance.delete(flush:true) |
---|
[118] | 238 | flash.message = "Asset ${params.id} deleted" |
---|
[139] | 239 | redirect(action:search) |
---|
[118] | 240 | } |
---|
| 241 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
| 242 | flash.message = "Asset ${params.id} could not be deleted" |
---|
| 243 | redirect(action:show,id:params.id) |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | else { |
---|
| 247 | flash.message = "Asset not found with id ${params.id}" |
---|
[139] | 248 | redirect(action:search) |
---|
[118] | 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
[298] | 252 | /** |
---|
| 253 | * Edit action. |
---|
| 254 | */ |
---|
[118] | 255 | def edit = { |
---|
[283] | 256 | |
---|
[139] | 257 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 258 | if(params._action_Edit) |
---|
| 259 | { params.action='edit' } |
---|
[283] | 260 | |
---|
[118] | 261 | def assetInstance = Asset.get( params.id ) |
---|
| 262 | |
---|
| 263 | if(!assetInstance) { |
---|
| 264 | flash.message = "Asset not found with id ${params.id}" |
---|
[139] | 265 | redirect(action:search) |
---|
[118] | 266 | } |
---|
| 267 | else { |
---|
[305] | 268 | return [ assetInstance : assetInstance, possibleAssetSubItems: assetService.possibleAssetSubItems() ] |
---|
[118] | 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
[298] | 272 | /** |
---|
| 273 | * Update action. |
---|
| 274 | */ |
---|
[118] | 275 | def update = { |
---|
| 276 | def assetInstance = Asset.get( params.id ) |
---|
| 277 | if(assetInstance) { |
---|
| 278 | if(params.version) { |
---|
| 279 | def version = params.version.toLong() |
---|
| 280 | if(assetInstance.version > version) { |
---|
[283] | 281 | |
---|
[118] | 282 | assetInstance.errors.rejectValue("version", "asset.optimistic.locking.failure", "Another user has updated this Asset while you were editing.") |
---|
[305] | 283 | render(view:'edit',model:[assetInstance:assetInstance, possibleAssetSubItems: assetService.possibleAssetSubItems()]) |
---|
[118] | 284 | return |
---|
| 285 | } |
---|
| 286 | } |
---|
[286] | 287 | |
---|
[118] | 288 | assetInstance.properties = params |
---|
[286] | 289 | |
---|
| 290 | assetInstance.setAssetSubItemsFromCheckBoxList(params.assetSubItems) |
---|
| 291 | |
---|
[178] | 292 | if(!assetInstance.hasErrors() && assetInstance.save(flush: true)) { |
---|
[118] | 293 | flash.message = "Asset ${params.id} updated" |
---|
| 294 | redirect(action:show,id:assetInstance.id) |
---|
| 295 | } |
---|
| 296 | else { |
---|
[305] | 297 | render(view:'edit',model:[assetInstance:assetInstance, possibleAssetSubItems: assetService.possibleAssetSubItems()]) |
---|
[118] | 298 | } |
---|
| 299 | } |
---|
| 300 | else { |
---|
| 301 | flash.message = "Asset not found with id ${params.id}" |
---|
[334] | 302 | redirect(action:search) |
---|
[118] | 303 | } |
---|
| 304 | } |
---|
| 305 | |
---|
[298] | 306 | /** |
---|
| 307 | * Create action. |
---|
| 308 | */ |
---|
[118] | 309 | def create = { |
---|
[284] | 310 | def result = assetService.create(params) |
---|
| 311 | |
---|
| 312 | if(!result.error) |
---|
| 313 | return [assetInstance: result.assetInstance] |
---|
| 314 | |
---|
| 315 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 316 | redirect(action: search) |
---|
[118] | 317 | } |
---|
| 318 | |
---|
[298] | 319 | /** |
---|
| 320 | * Copy action. |
---|
| 321 | */ |
---|
[271] | 322 | def copy = { |
---|
[284] | 323 | def result = assetService.copy(params) |
---|
[271] | 324 | |
---|
[284] | 325 | if(!result.error) |
---|
| 326 | return [assetInstance: result.assetInstance, assetToCopy: result.assetToCopy] |
---|
[271] | 327 | |
---|
[284] | 328 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 329 | redirect(action: search) |
---|
[271] | 330 | } |
---|
| 331 | |
---|
[298] | 332 | /** |
---|
| 333 | * Save action. |
---|
| 334 | */ |
---|
[118] | 335 | def save = { |
---|
[284] | 336 | def result = assetService.save(params) |
---|
| 337 | |
---|
| 338 | if(!result.error) { |
---|
| 339 | flash.message = g.message(code: "default.create.success", args: ["Asset", result.assetInstance.id]) |
---|
| 340 | redirect(action:show, id: result.assetInstance.id) |
---|
| 341 | return |
---|
[118] | 342 | } |
---|
[284] | 343 | |
---|
| 344 | render(view:'create', model:[assetInstance: result.assetInstance]) |
---|
[118] | 345 | } |
---|
[271] | 346 | |
---|
[298] | 347 | /** |
---|
| 348 | * Copy save action. |
---|
| 349 | */ |
---|
[271] | 350 | def saveCopy = { |
---|
[278] | 351 | def result = assetService.saveCopy(params) |
---|
[271] | 352 | |
---|
| 353 | if(!result.error) { |
---|
| 354 | flash.message = g.message(code: "default.create.success", args: ["Asset", result.assetInstance.id]) |
---|
| 355 | redirect(action:show, id: result.assetInstance.id) |
---|
| 356 | return |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | if(result.error.code == "default.not.found") { |
---|
| 360 | flash.message = g.message(code: result.error.code, args: ["Asset", params.assetToCopy?.id]) |
---|
[334] | 361 | redirect(action: search) |
---|
[271] | 362 | return |
---|
| 363 | } |
---|
| 364 | |
---|
[278] | 365 | if(result.error.code != "default.create.failure") { |
---|
[281] | 366 | flash.errorMessage = g.message(code: result.error.code, args: ["Asset"]) |
---|
[278] | 367 | } |
---|
| 368 | |
---|
[271] | 369 | render(view:'copy', model:[assetInstance: result.assetInstance, assetToCopy: result.assetToCopy]) |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | } // end class |
---|