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