Changeset 301
- Timestamp:
- Jan 27, 2010, 2:38:48 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/CsvService.groovy
r300 r301 81 81 def parentItem 82 82 83 while(line) { 84 columnIndex = 0 85 numberOfColumns = Math.min( line.size(), maxNumberOfColumns ) 86 87 if( (columnIndex+2) > numberOfColumns ) { 83 def column = [:] 84 85 def nextLine = { 88 86 line = reader.readNext() 89 87 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 + ")" 88 log.info "Processing line: " + lineNumber 89 } 90 91 def nextColumn = { 92 if( (columnIndex+2) > numberOfColumns ) { 93 log.info "No more columns on line: " + lineNumber 94 return false 95 96 } 97 if(!line[columnIndex]) { 98 log.info "No name at " + "line: " + lineNumber + " col: " + columnIndex 99 return false 100 } 101 column.name = line[columnIndex] 102 column.description = line[++columnIndex] 103 columnIndex++ 104 // Success. 105 return column 106 } 107 108 while(line) { 109 numberOfColumns = Math.min( line.size(), maxNumberOfColumns ) 110 columnIndex = 0 111 112 if(!nextColumn()) { 113 nextLine() 114 continue 115 } 116 117 siteInstance = Site.findByName(column.name) 118 if(!siteInstance) { 119 log.info "Creating site: " + column.name 120 siteInstance = new Site(name: column.name, 121 description: column.description) 122 if(!siteInstance.save()) { 123 log.error "Failed to create site on line: " + column.name + "(" + lineNumber + ")" 124 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 125 } 126 } 127 else log.info "Existing site: " + siteInstance 128 129 if(!nextColumn()) { 130 nextLine() 131 continue 132 } 133 134 departmentInstance = Department.findByName(column.name) 135 if(!departmentInstance) { 136 log.info "Creating department: " + column.name 137 departmentInstance = new Department(name: column.name, 138 description: column.description, 139 site: siteInstance) 140 if(!departmentInstance.save()) { 141 log.error "Failed to create department on line: " + column.name + "(" + lineNumber + ")" 142 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 143 } 144 } 145 else log.info "Existing department: " + departmentInstance 146 147 if(!nextColumn()) { 148 nextLine() 149 continue 150 } 151 152 sectionInstance = Section.findByName(column.name) 153 if(!sectionInstance) { 154 log.info "Creating section: " + column.name 155 sectionInstance = new Section(name: column.name, 156 description: column.description, 157 site: siteInstance, 158 department: departmentInstance) 159 if(!sectionInstance.save()) { 160 log.error "Failed to create section on line: " + column.name + "(" + lineNumber + ")" 161 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 162 } 163 } 164 else log.info "Existing section: " + sectionInstance 165 166 if(!nextColumn()) { 167 nextLine() 168 continue 169 } 170 171 assetInstance = Asset.findByName(column.name) 172 if(!assetInstance) { 173 log.info "Creating asset: " + column.name 174 assetInstance = new Asset(name: column.name, 175 description: column.description, 176 section: sectionInstance) 177 if(!assetInstance.save()) { 178 log.error "Failed to create asset on line: " + column.name + "(" + lineNumber + ")" 179 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 180 } 181 } 182 else log.info "Existing asset: " + assetInstance 183 184 if(!nextColumn()) { 185 nextLine() 186 continue 187 } 188 189 assetSubItemInstance = AssetSubItem.findByName(column.name) 190 if(!assetSubItemInstance) { 191 log.info "Creating asset sub item: " + column.name 192 assetSubItemInstance = new AssetSubItem(name: column.name, 193 description: column.description) 194 if(!assetInstance.save()) { 195 log.error "Failed to create assetSubItem on line: " + column.name + "(" + lineNumber + ")" 196 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 197 } 198 } 199 else log.info "Existing asset sub item: " + assetSubItemInstance 200 201 assetInstance.addToAssetSubItems(assetSubItemInstance) 202 203 while( nextColumn() ) { 204 205 parentItem = assetSubItemInstance 206 assetSubItemInstance = AssetSubItem.findByName(column.name) 207 if(!assetSubItemInstance) { 208 log.info "Creating asset sub item: " + column.name 209 assetSubItemInstance = new AssetSubItem(name: column.name, 210 description: column.description, 211 parentItem: parentItem) 212 if(!assetSubItemInstance.save()) { 213 log.error "Failed to create assetSubItem on line: " + column.name + "(" + lineNumber + ")" 101 214 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 102 215 } 103 216 } 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 + ")" 121 return fail(code: "asset.tree.import.failure", args: [lineNumber]) 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 213 214 line = reader.readNext() 215 lineNumber ++ 217 else log.info "Existing asset sub item: " + assetSubItemInstance 218 219 } // while( nextColumn() ) 220 221 nextLine() 216 222 } //while(line) 217 223
Note: See TracChangeset
for help on using the changeset viewer.