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