- Timestamp:
- Jan 21, 2010, 9:54:06 PM (15 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/i18n/messages.properties
r285 r287 1 1 asset.tree.import.success=Asset tree imported. 2 2 asset.tree.import.failure=Could not create asset tree from supplied file. 3 asset.tree.import.file.over.max.size=Supplied file is over max size.3 asset.tree.import.file.over.max.size=Supplied file is greater than max size of {0} Mbytes. 4 4 asset.tree.import.file.not.supplied=No file supplied. 5 5 asset.tree.import.no.header=The supplied file does not have the correct headers, please see the template file. -
trunk/grails-app/services/CsvService.groovy
r271 r287 11 11 12 12 /** 13 * Import an asset tree for a single asset. 14 * @param file A csv file containing the asset tree for a single asset. 13 * Import an asset tree creating items as required. 14 * @param request The http request to run getFile against. 15 * Get file should return a csv format file containing the asset tree as per template. 15 16 */ 16 17 def importAssetTree(request) { … … 20 21 def fileMaxSize = 10 * megaByteMultiplier //Mb 21 22 23 def multiPartFile = request.getFile('file') 24 25 InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream) 26 CSVReader reader = new CSVReader(sr) 27 22 28 def fail = { Map m -> 23 29 //status.setRollbackOnly() 24 result.error = [ code: m.code, args: ["Import Asset Tree"] ] 30 reader.close() 31 result.error = [ code: m.code, args: m.args ] 25 32 return result 26 33 } 27 28 def multiPartFile = request.getFile('file')29 34 30 35 if(!multiPartFile || multiPartFile.isEmpty()) … … 32 37 33 38 if (multiPartFile.getSize() > fileMaxSize) 34 return fail(code: "asset.tree.import.file.over.max.size" )39 return fail(code: "asset.tree.import.file.over.max.size", args: [fileMaxSize/megaByteMultiplier]) 35 40 36 InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream) 37 CSVReader reader = new CSVReader(sr) 38 39 def nextLine = reader.readNext() 41 def line = reader.readNext() 40 42 def lineNumber = 1 41 43 42 44 def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"] 43 if(nextLine != header) 45 46 if(line != header) 44 47 return fail(code: "asset.tree.import.no.header") 45 48 46 nextLine = reader.readNext() 49 log.info "Import checks passed, start processing asset file." 50 51 // Prepare the first body line. 52 line = reader.readNext() 47 53 lineNumber ++ 48 54 49 while(nextLine) { 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 64 line = reader.readNext() 50 65 lineNumber ++ 51 52 println lineNumber + " : " + nextLine[0] 53 54 // if(nextLine[0]) { 55 // if( !Site.findByName(nextLine[0].toString()) ) 56 // println nextLine[0] 57 // } 58 59 nextLine = reader.readNext() 60 } //while(nextLine) 66 } //while(line) 61 67 62 68 // Success. 69 reader.close() 63 70 return result 64 71 … … 77 84 //Header 78 85 def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"] 86 def blankLine = [] 87 def noteLine = ["Note: the header is required, start by replacing this line."] 88 79 89 writer.writeNext(header as String[]) 80 81 //Rows 82 def row 83 84 row = [] 85 writer.writeNext(row as String[]) //blank row between assets. 90 writer.writeNext(blankLine as String[]) 91 writer.writeNext(noteLine as String[]) 86 92 87 93 writer.close() -
trunk/grails-app/views/assetDetailed/importAssetTree.gsp
r271 r287 12 12 <div class="body"> 13 13 <g:render template="/shared/messages" /> 14 <g:uploadForm action="importAssetTreeSave" enctype="multipart/form-data"onsubmit="return Lightbox.loading();">14 <g:uploadForm action="importAssetTreeSave" onsubmit="return Lightbox.loading();"> 15 15 <div class="dialog"> 16 16 <table> … … 20 20 <label for="file">File:</label> 21 21 </td> 22 <td valign="top" class="value ${hasErrors(bean: picture, field: 'file', 'errors')}">22 <td valign="top" class="value"> 23 23 <input type="file" id="file" name="file" size="40"/> 24 24 </td>
Note: See TracChangeset
for help on using the changeset viewer.