Changeset 290 for trunk/grails-app/services
- Timestamp:
- Jan 22, 2010, 5:29:24 PM (15 years ago)
- Location:
- trunk/grails-app/services
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/AssetSubItemService.groovy
r285 r290 22 22 23 23 def save(params) { 24 Asset .withTransaction { status ->24 AssetSubItem.withTransaction { status -> 25 25 def result = [:] 26 26 -
trunk/grails-app/services/CreateDataService.groovy
r276 r290 156 156 saveAndTest(authInstance) 157 157 158 // #3 158 159 authInstance = new Authority(description:"Application User, all application users need this base role to allow login.", 159 160 authority:"ROLE_AppUser") -
trunk/grails-app/services/CsvService.groovy
r287 r290 16 16 */ 17 17 def importAssetTree(request) { 18 def result = [:] 19 20 def megaByteMultiplier = 1000 * 1000 21 def fileMaxSize = 10 * megaByteMultiplier //Mb 22 23 def multiPartFile = request.getFile('file') 24 25 InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream) 26 CSVReader reader = new CSVReader(sr) 27 28 def fail = { Map m -> 29 //status.setRollbackOnly() 30 reader.close() 31 result.error = [ code: m.code, args: m.args ] 32 return result 33 } 34 35 if(!multiPartFile || multiPartFile.isEmpty()) 36 return fail(code: "asset.tree.import.file.not.supplied") 37 38 if (multiPartFile.getSize() > fileMaxSize) 39 return fail(code: "asset.tree.import.file.over.max.size", args: [fileMaxSize/megaByteMultiplier]) 40 41 def line = reader.readNext() 42 def lineNumber = 1 43 44 def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"] 45 46 if(line != header) 47 return fail(code: "asset.tree.import.no.header") 48 49 log.info "Import checks passed, start processing asset file." 50 51 // Prepare the first body line. 52 line = reader.readNext() 53 lineNumber ++ 54 55 while(line) { 56 def lineSize = line.size() 57 // log.info lineNumber+ "(" + lineSize + ")" + " : " + line 58 59 if(line[0]) { 60 if( !Site.findByName(line[0]) ) 61 new Site(name: line[0]).save() 62 } 63 18 Asset.withTransaction { status -> 19 def result = [:] 20 21 def megaByteMultiplier = 1000 * 1000 22 def fileMaxSize = 10 * megaByteMultiplier //Mb 23 24 def multiPartFile = request.getFile('file') 25 26 InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream) 27 CSVReader reader = new CSVReader(sr) 28 29 def fail = { Map m -> 30 status.setRollbackOnly() 31 reader.close() 32 result.error = [ code: m.code, args: m.args ] 33 return result 34 } 35 36 if(!multiPartFile || multiPartFile.isEmpty()) 37 return fail(code: "asset.tree.import.file.not.supplied") 38 39 if (multiPartFile.getSize() > fileMaxSize) 40 return fail(code: "asset.tree.import.file.over.max.size", args: [fileMaxSize/megaByteMultiplier]) 41 42 def line = reader.readNext() 43 def lineNumber = 1 44 45 def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"] 46 47 if(line != header) 48 return fail(code: "asset.tree.import.no.header") 49 50 log.info "Import checks passed, start processing asset file." 51 52 // Prepare the first body line. 64 53 line = reader.readNext() 65 54 lineNumber ++ 66 } //while(line) 67 68 // Success. 69 reader.close() 70 return result 71 55 56 def siteInstance 57 def sectionInstance 58 def assetInstance 59 60 while(line) { 61 def lineSize = line.size() 62 // log.info lineNumber+ "(" + lineSize + ")" + " : " + line 63 64 if(line[0]) { 65 if( !Site.findByName(line[0]) ) 66 siteInstance = new Site(name: line[0]) 67 if(!siteInstance.save()) 68 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 69 } 70 else continue 71 72 if(line[1]) { 73 if( !Section.findByName(line[1]) ) 74 sectionInstance = new Section(name: line[1], 75 site: siteInstance) 76 if(!sectionInstance.save()) 77 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 78 } 79 else continue 80 81 if(line[2]) { 82 if( !Asset.findByName(line[2]) ) 83 assetInstance = new Asset(name: line[2], 84 section: sectionInstance) 85 if(!sectionInstance.save()) 86 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 87 } 88 else continue 89 90 line = reader.readNext() 91 lineNumber ++ 92 } //while(line) 93 94 // Success. 95 reader.close() 96 return result 97 98 } //end withTransaction 72 99 } // end importAssetTree() 73 100
Note: See TracChangeset
for help on using the changeset viewer.