Changeset 271 for trunk/grails-app/controllers
- Timestamp:
- Jan 15, 2010, 11:37:52 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/AssetDetailedController.groovy
r178 r271 1 1 import org.codehaus.groovy.grails.plugins.springsecurity.Secured 2 import org.codehaus.groovy.grails.commons.ConfigurationHolder 2 3 3 4 class AssetDetailedController extends BaseController { 4 5 6 def csvService 5 7 def filterService 8 def exportService 6 9 7 10 def index = { redirect(action:search,params:params) } 8 11 9 12 // the delete, save and update actions only accept POST requests 10 static allowedMethods = [delete:'POST', save:'POST', update:'POST'] 13 static allowedMethods = [delete:'POST', save:'POST', update:'POST', saveCopy:'POST'] 14 15 def importAssetTree = { 16 } 17 18 def importAssetTreeSave = { 19 def result = csvService.importAssetTree(request) 20 21 if(!result.error) 22 flash.message = g.message(code: "asset.tree.import.success") 23 else 24 flash.warning = g.message(code: result.error.code, args: result.error.args) 25 26 redirect(action: importAssetTree) 27 } 28 29 /** 30 * Export a csv template. 31 * NOTE: IE has a 'validating' bug in dev mode that causes the export to take a long time! 32 * This does not appear to be a problem once deployed to Tomcat. 33 */ 34 def exportAssetTreeTemplate = { 35 response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] 36 response.setHeader("Content-disposition", "attachment; filename=AssetTreeTemplate.csv") 37 def s = csvService.buildAssetTreeTemplate() 38 render s 39 } 40 41 def exportAssetTree = { 42 43 def assetList = Asset.list() 44 45 response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] 46 response.setHeader("Content-disposition", "attachment; filename=AssetTree.csv") 47 def s = csvService.buildAssetTree(assetList) 48 render s 49 } 11 50 12 51 def list = { … … 16 55 17 56 def search = { 18 params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) 57 58 if(session.taskSearchParamsMax) 59 params.max = session.taskSearchParamsMax 60 61 params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) 62 63 def assetInstanceList = [] 64 def assetInstanceTotal 65 def filterParams = [:] 19 66 20 67 // Quick Search: 21 68 if(!params.filter) { 22 return [ assetInstanceList: Asset.list( params ), assetInstanceTotal: Asset.count() ] 23 } 69 assetInstanceList = Asset.list( params ) 70 assetInstanceTotal = Asset.count() 71 filterParams.quickSearch = params.quickSearch 72 } 73 else { 24 74 // filterPane: 25 return[ assetInstanceList: filterService.filter( params, Asset ), 26 assetInstanceTotal: filterService.count( params, Asset ), 27 filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params), 28 params:params ] 29 } 75 assetInstanceList = filterService.filter( params, Asset ) 76 assetInstanceTotal = filterService.count( params, Asset ) 77 filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) 78 } 79 80 // export plugin: 81 if(params?.format && params.format != "html") { 82 83 def dateFmt = { date -> 84 formatDate(format: "EEE, dd-MMM-yyyy", date: date) 85 } 86 87 // def fmtAsset = { m -> 88 // def r = '' 89 // def assetInstance = Asset.findByName(m) 90 // 91 // r += assetInstance 92 // r += ", " 93 // 94 // def lastSubAsset = assetInstance.subAssets.size() - 1 95 // assetInstance.subAssets.eachWithIndex() { obj, i -> 96 // r += "\"" + obj + "\"" 97 // if( i < lastSubAsset ) 98 // r += ", " 99 // } 100 // return r 101 // } 102 103 // def fmtSubAsset = { m -> 104 // def r = '' 105 // m.each() { 106 // def machine = Machine.findByName(it) 107 // def assemblies = machine.assemblies 108 // r += machine.name 109 // r += " " 110 // r += assemblies 111 // r += " " 112 // } 113 // return r 114 // } 115 116 String title = "Asset List." 117 118 response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] 119 response.setHeader("Content-disposition", "attachment; filename=tasks.${params.extension}") 120 List fields = ["section.site", 121 "section", 122 "name", 123 "description"] 124 Map labels = ["section.site": "Site", 125 "section": "Section", 126 "name": "Asset", 127 "description": "Description"] 128 // Map labels 129 // Map formatters = ["subAsset.name": fmtSubAsset] 130 Map formatters = [:] 131 Map parameters = [title: title, separator: ","] 132 133 exportService.export(params.format, 134 response.outputStream, 135 assetInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, 136 fields, 137 labels, 138 formatters, 139 parameters) 140 } 141 142 // Add some basic params to filterParams. 143 filterParams.max = params.max 144 filterParams.offset = params.offset?.toInteger() ?: 0 145 filterParams.sort = params.sort ?: "id" 146 filterParams.order = params.order ?: "desc" 147 148 return[ assetInstanceList: assetInstanceList, 149 assetInstanceTotal: assetInstanceTotal, 150 filterParams: filterParams ] 151 152 } // end search() 30 153 31 154 def show = { … … 113 236 } 114 237 238 def copy = { 239 def assetToCopy = Asset.get(params.asset.id) 240 241 if(!assetToCopy) { 242 flash.message = "Please select an asset to copy." 243 redirect(action: list) 244 return 245 } 246 247 def assetInstance = new Asset(name: assetToCopy.name, section: assetToCopy.section) 248 assetInstance.properties = params 249 return ['assetInstance':assetInstance, assetToCopy: assetToCopy] 250 } 251 115 252 def save = { 116 253 def assetInstance = new Asset(params) … … 123 260 } 124 261 } 125 } 262 263 def saveCopy = { 264 def result = saveCopySrvce(params) 265 266 if(!result.error) { 267 flash.message = g.message(code: "default.create.success", args: ["Asset", result.assetInstance.id]) 268 redirect(action:show, id: result.assetInstance.id) 269 return 270 } 271 272 if(result.error.code == "default.not.found") { 273 flash.message = g.message(code: result.error.code, args: ["Asset", params.assetToCopy?.id]) 274 redirect(action: list) 275 return 276 } 277 278 render(view:'copy', model:[assetInstance: result.assetInstance, assetToCopy: result.assetToCopy]) 279 } 280 281 def saveCopySrvce(params) { 282 Asset.withTransaction { status -> 283 def result = [:] 284 285 def fail = { Map m -> 286 status.setRollbackOnly() 287 if(result.assetInstance && m.field) 288 result.assetInstance.errors.rejectValue(m.field, m.code) 289 result.error = [ code: m.code, args: ["Asset", params.id] ] 290 return result 291 } 292 293 result.assetToCopy = Asset.get(params.assetToCopy?.id) 294 if(!result.assetToCopy) 295 return fail(code:"default.not.found") 296 297 result.assetInstance = new Asset(params) 298 299 if(result.assetInstance.hasErrors() || !result.assetInstance.save()) 300 return fail(code:"default.create.failure") 301 302 def assetSubItemInstance1 303 304 // Copy subItems from level 2 and bellow. 305 def copyAssetSubItem = { assetSubItemToCopy, parentItem -> 306 def assetSubItemInstance = new AssetSubItem(name: assetSubItemToCopy.name, 307 description: assetSubItemToCopy.description, 308 parentItem: parentItem) 309 310 if(assetSubItemInstance.hasErrors() || !assetSubItemInstance.save()) 311 return fail(field:"subItems", code:"default.create.failure") 312 313 def i = 0 314 for(assetSubItem in assetSubItemToCopy.subItems) { 315 call(assetSubItem, assetSubItemInstance) 316 // Protect against endless recurrsion. 317 i++ 318 if(i > 100) 319 fail(code:"default.create.failure") 320 // Stop if an error is flagged. 321 if(result.error) 322 break 323 } 324 } //copyAssetSubItem 325 326 // Copy the 1st level of subItems. 327 def copyAssetSubItem1 = { assetSubItemToCopy -> 328 assetSubItemInstance1 = new AssetSubItem(name: assetSubItemToCopy.name, 329 description: assetSubItemToCopy.description, 330 asset: result.assetInstance) 331 332 if(assetSubItemInstance1.hasErrors() || !assetSubItemInstance1.save()) 333 return fail(field:"assetSubItems", code:"default.create.failure") 334 335 def i = 0 336 for(assetSubItem in assetSubItemToCopy.subItems) { 337 copyAssetSubItem(assetSubItem, assetSubItemInstance1) 338 // Protect against endless recurrsion. 339 i++ 340 if(i > 100) 341 fail(code:"default.create.failure") 342 // Stop if an error is flagged. 343 if(result.error) 344 break 345 } 346 } //copyAssetSubItem1 347 348 def i = 0 349 for(assetSubItem in result.assetToCopy.assetSubItems) { 350 copyAssetSubItem1(assetSubItem) 351 // Protect against endless recurrsion. 352 i++ 353 if(i > 100) 354 fail(code:"default.create.failure") 355 // Stop if an error is flagged. 356 if(result.error) 357 break 358 } 359 360 // Success or not. 361 return result 362 363 } //end withTransaction 364 } // end saveCopySrvce 365 366 } // end class
Note: See TracChangeset
for help on using the changeset viewer.