Changeset 300 for trunk/grails-app
- Timestamp:
- Jan 26, 2010, 9:44:50 PM (15 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/i18n/messages.properties
r290 r300 1 1 asset.tree.import.success=Asset tree imported. 2 2 asset.tree.import.failure=Could not create asset tree from supplied file, failed on line {0}. 3 asset.tree.import.file.over.max.size=Supplied file is greater than max size of {0} Mbytes.3 asset.tree.import.file.over.max.size=Supplied file is greater than max size of {0} {1}. 4 4 asset.tree.import.file.not.supplied=No file supplied. 5 asset.tree.import.no.header=The supplied file does not have the correct header s, please see the template file.5 asset.tree.import.no.header=The supplied file does not have the correct header lines, please see the template file. 6 6 7 7 asset.copy.method.required=Please select a copy method for sub items. -
trunk/grails-app/services/CsvService.groovy
r290 r300 19 19 def result = [:] 20 20 21 def megaByteMultiplier = 1000 *100022 def fileMaxSize = 10 * megaByteMultiplier //Mb21 def kByteMultiplier = 1000 22 def fileMaxSize = 500 * kByteMultiplier 23 23 24 24 def multiPartFile = request.getFile('file') … … 38 38 39 39 if (multiPartFile.getSize() > fileMaxSize) 40 return fail(code: "asset.tree.import.file.over.max.size", args: [fileMaxSize/megaByteMultiplier]) 41 40 return fail(code: "asset.tree.import.file.over.max.size", args: [fileMaxSize/kByteMultiplier, "kB"]) 41 42 def columnIndex = 0 43 def numberOfColumns = 0 44 def maxNumberOfColumns = 20 45 46 // Get first line. 42 47 def line = reader.readNext() 43 48 def lineNumber = 1 44 49 45 def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"] 46 47 if(line != header) 50 // Check for header line 1. 51 if(line != templateHeaderLine1) { 52 log.error "Failed to find header line 1. " 53 log.error "Required: " + templateHeaderLine1.toString() 54 log.error "Supplied: " + line.toString() 48 55 return fail(code: "asset.tree.import.no.header") 56 } 57 58 // Get second line. 59 line = reader.readNext() 60 lineNumber ++ 61 62 // Check for header line 2. 63 if(line != templateHeaderLine2) { 64 log.error "Failed to find header line 2. " 65 log.error "Required: " + templateHeaderLine2.toString() 66 log.error "Supplied: " + line.toString() 67 return fail(code: "asset.tree.import.no.header") 68 } 49 69 50 70 log.info "Import checks passed, start processing asset file." … … 55 75 56 76 def siteInstance 77 def departmentInstance 57 78 def sectionInstance 58 79 def assetInstance 80 def assetSubItemInstance 81 def parentItem 59 82 60 83 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()) 84 columnIndex = 0 85 numberOfColumns = Math.min( line.size(), maxNumberOfColumns ) 86 87 if( (columnIndex+2) > numberOfColumns ) { 88 line = reader.readNext() 89 lineNumber ++ 90 continue 91 } 92 93 if(line[columnIndex]) { 94 siteInstance = Site.findByName(line[columnIndex]) 95 if(!siteInstance) { 96 log.info "Creating site: " + line[columnIndex] 97 siteInstance = new Site(name: line[columnIndex], 98 description: line[++columnIndex]) 99 if(!siteInstance.save()) { 100 log.error "Failed to create site on line: " + line[--columnIndex] + "(" + lineNumber + ")" 101 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 102 } 103 } 104 else log.info "Existing site: " + siteInstance 105 106 columnIndex++ 107 if( (columnIndex+2) > numberOfColumns ) { 108 line = reader.readNext() 109 lineNumber ++ 110 continue 111 } 112 113 if(line[columnIndex]) { 114 departmentInstance = Department.findByName(line[columnIndex]) 115 if(!departmentInstance) { 116 departmentInstance = new Department(name: line[columnIndex], 117 description: line[++columnIndex], 118 site: siteInstance) 119 if(!departmentInstance.save()) { 120 log.error "Failed to create department on line: " + line[--columnIndex] + "(" + lineNumber + ")" 68 121 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 122 } 123 } 124 125 columnIndex++ 126 if( (columnIndex+2) > numberOfColumns ) { 127 line = reader.readNext() 128 lineNumber ++ 129 continue 130 } 131 132 if(line[columnIndex]) { 133 sectionInstance = Section.findByName(line[columnIndex]) 134 if(!sectionInstance) { 135 sectionInstance = new Section(name: line[columnIndex], 136 description: line[++columnIndex], 137 site: siteInstance, 138 department: departmentInstance) 139 if(!sectionInstance.save()) { 140 log.error "Failed to create section on line: " + line[--columnIndex] + "(" + lineNumber + ")" 141 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 142 } 143 } 144 145 columnIndex++ 146 if( (columnIndex+2) > numberOfColumns ) { 147 line = reader.readNext() 148 lineNumber ++ 149 continue 150 } 151 152 if(line[columnIndex]) { 153 assetInstance = Asset.findByName(line[columnIndex]) 154 if(!assetInstance) { 155 assetInstance = new Asset(name: line[columnIndex], 156 description: line[++columnIndex], 157 section: sectionInstance) 158 if(!assetInstance.save()) { 159 log.error "Failed to create asset on line: " + line[--columnIndex] + "(" + lineNumber + ")" 160 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 161 } 162 } 163 164 columnIndex++ 165 if( (columnIndex+2) > numberOfColumns ) { 166 line = reader.readNext() 167 lineNumber ++ 168 continue 169 } 170 171 if(line[columnIndex]) { 172 assetSubItemInstance = AssetSubItem.findByName(line[columnIndex]) 173 if(!assetSubItemInstance) { 174 assetSubItemInstance = new AssetSubItem(name: line[columnIndex], 175 description: line[++columnIndex]) 176 if(!assetInstance.save()) { 177 log.error "Failed to create assetSubItem on line: " + line[--columnIndex] + "(" + lineNumber + ")" 178 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 179 } 180 } 181 182 assetInstance.addToAssetSubItems(assetSubItemInstance) 183 184 columnIndex++ 185 if( (columnIndex+2) > numberOfColumns ) { 186 line = reader.readNext() 187 lineNumber ++ 188 continue 189 } 190 191 while( (columnIndex+2) < numberOfColumns ) { 192 193 if(line[columnIndex]) { 194 parentItem = assetSubItemInstance 195 assetSubItemInstance = new AssetSubItem(name: line[columnIndex], 196 description: line[++columnIndex], 197 parentItem: parentItem) 198 if(!assetInstance.save()) { 199 log.error "Failed to create assetSubItem on line: " + line[--columnIndex] + "(" + lineNumber + ")" 200 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 201 } 202 } 203 else break 204 205 columnIndex++ 206 } // while() 207 208 } // AssetSubItem L1 209 } // Asset 210 } // Section 211 } // Department 212 } // Site 89 213 90 214 line = reader.readNext() … … 109 233 CSVWriter writer = new CSVWriter(sw) 110 234 111 //Header 112 def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"] 113 def blankLine = [] 114 def noteLine = ["Note: the header is required, start by replacing this line."] 115 116 writer.writeNext(header as String[]) 117 writer.writeNext(blankLine as String[]) 118 writer.writeNext(noteLine as String[]) 235 writer.writeNext(templateHeaderLine1 as String[]) 236 writer.writeNext(templateHeaderLine2 as String[]) 237 writer.writeNext() 238 writer.writeNext("Note: the header lines are required, start by replacing this line.") 119 239 120 240 writer.close() … … 212 332 } // end buildAssetTree 213 333 334 private getTemplateHeaderLine1() { 335 ["Site", "", "Department", "", "Section", "","Asset", "", "Sub Asset", "", "Functional Assembly", "", "Sub Assembly Group", "", "SubItem", ""] 336 } 337 338 private getTemplateHeaderLine2() { 339 (["Name", "Description"])*8 340 } 341 214 342 } // end class
Note: See TracChangeset
for help on using the changeset viewer.