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