Changeset 315 for trunk/grails-app
- Timestamp:
- Feb 4, 2010, 4:00:12 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/CsvService.groovy
r302 r315 1 1 import au.com.bytecode.opencsv.CSVWriter 2 2 import au.com.bytecode.opencsv.CSVReader 3 import org.apache.commons.lang.WordUtils 3 4 4 5 /** … … 93 94 log.info "No more columns on line: " + lineNumber 94 95 return false 95 96 96 } 97 97 if(!line[columnIndex]) { … … 99 99 return false 100 100 } 101 column.name = line[columnIndex] 102 column.description = line[++columnIndex] 101 // Capitalise and assign the name and description columns. 102 use(WordUtils) { 103 column.name = line[columnIndex].trim().capitalize() 104 column.description = line[++columnIndex].trim().capitalize() 105 } 103 106 columnIndex++ 104 107 // Success. … … 106 109 } 107 110 111 // Primary loop. 108 112 while(line) { 109 113 numberOfColumns = Math.min( line.size(), maxNumberOfColumns ) … … 116 120 117 121 // Ignore comment lines. 118 if( column.name.startsWith("Comment")) {122 if(line.toString().toLowerCase().contains("comment")) { 119 123 log.info "Comment line found." 120 124 nextLine() … … 123 127 124 128 // Ignore example lines. 125 if( column.name.startsWith("Example")) {129 if(line.toString().toLowerCase().contains("example")) { 126 130 log.info "Example line found." 127 131 nextLine() … … 129 133 } 130 134 135 // Site. 131 136 siteInstance = Site.findByName(column.name) 132 137 if(!siteInstance) { … … 146 151 } 147 152 153 // Department. 148 154 departmentInstance = Department.findByName(column.name) 149 155 if(!departmentInstance) { … … 164 170 } 165 171 172 // Section. 166 173 sectionInstance = Section.findByName(column.name) 167 174 if(!sectionInstance) { … … 183 190 } 184 191 192 // Asset. 185 193 assetInstance = Asset.findByName(column.name) 186 194 if(!assetInstance) { … … 201 209 } 202 210 211 // AssetSubItem Level 1. 203 212 assetSubItemInstance = AssetSubItem.findByName(column.name) 204 213 if(!assetSubItemInstance) { … … 215 224 assetInstance.addToAssetSubItems(assetSubItemInstance) 216 225 226 // Remaining AssetSubItems. 217 227 while( nextColumn() ) { 218 228 … … 229 239 } 230 240 } 231 else log.info "Existing asset sub item: " + assetSubItemInstance241 else log.info "Existing asset sub item: " + assetSubItemInstance 232 242 233 243 } // while( nextColumn() ) … … 268 278 writer.writeNext(["Example: Site1", "", "Department 1", "", "Section 1", "", "Asset 2", ""] as String[]) 269 279 writer.writeNext() 270 writer.writeNext("Comment: the first two header lines are required.") 271 writer.writeNext("Comment: leave a blank line between assets.") 272 writer.writeNext("Comment: names are required, descriptions are optional.") 273 writer.writeNext("Comment: identical names in each column will be considered as the same item.") 274 writer.writeNext("Comment: any line starting with 'Comment' will be ignored.") 275 writer.writeNext("Comment: any line starting with 'Example' will be ignored.") 276 writer.writeNext("Comment: example and comment lines may be deleted.") 280 writer.writeNext("Comment: The first two header lines are required.") 281 writer.writeNext("Comment: Leave a blank line between assets.") 282 writer.writeNext("Comment: Names are required, descriptions are optional.") 283 writer.writeNext("Comment: Identical and existing names will be considered as the same item.") 284 writer.writeNext("Comment: An asset may share the first level of asset sub items with other assets.") 285 writer.writeNext("Comment: Lower levels of asset sub items are only parented once but may have many children.") 286 writer.writeNext("Comment: Lines containing 'comment' will be ignored.") 287 writer.writeNext("Comment: Lines containing 'example' will be ignored.") 288 writer.writeNext("Comment: This file must be saved as a CSV file before import.") 277 289 writer.writeNext() 278 290 } … … 324 336 CSVWriter writer = new CSVWriter(sw) 325 337 326 //Header 327 writer.writeNext(templateHeaderLine1 as String[]) 328 writer.writeNext(templateHeaderLine2 as String[]) 338 writeTemplateLines(writer) 329 339 330 340 //Rows … … 332 342 333 343 def writeAssetSubItem4 = { assetSubItem -> 334 row.add(assetSubItem) 344 row.add(assetSubItem.name) 345 row.add(assetSubItem.description) 335 346 writer.writeNext(row as String[]) 336 347 } 337 348 338 349 def writeAssetSubItem3 = { assetSubItem -> 339 row.add(assetSubItem) 350 row.add(assetSubItem.name) 351 row.add(assetSubItem.description) 340 352 341 353 if(assetSubItem.subItems.size() > 0) { … … 343 355 writeAssetSubItem4(assetSubItem4) 344 356 row.remove(row.last()) 357 row.remove(row.last()) 345 358 } 346 359 } … … 352 365 353 366 def writeAssetSubItem2 = { assetSubItem -> 354 row.add(assetSubItem) 367 row.add(assetSubItem.name) 368 row.add(assetSubItem.description) 355 369 356 370 if(assetSubItem.subItems.size() > 0) { … … 358 372 writeAssetSubItem3(assetSubItem3) 359 373 row.remove(row.last()) 374 row.remove(row.last()) 360 375 } 361 376 } … … 367 382 368 383 def writeAssetSubItem1 = { assetSubItem -> 369 row.add(assetSubItem) 384 row.add(assetSubItem.name) 385 row.add(assetSubItem.description) 370 386 371 387 if(assetSubItem.subItems.size() > 0) { … … 373 389 writeAssetSubItem2(assetSubItem2) 374 390 row.remove(row.last()) 391 row.remove(row.last()) 375 392 } 376 393 } … … 384 401 row = [] 385 402 writer.writeNext(row as String[]) //blank row between assets. 386 row.add(asset.section.site) 387 row.add(asset.section) 403 row.add(asset.section.site.name) 404 row.add(asset.section.site.description) 405 row.add(asset.section.department.name) 406 row.add(asset.section.department.description) 407 row.add(asset.section.name) 408 row.add(asset.section.description) 388 409 row.add(asset.name) 410 row.add(asset.description) 389 411 390 412 if(asset.assetSubItems.size() > 0) { … … 392 414 writeAssetSubItem1(assetSubItem1) 393 415 row.remove(row.last()) 416 row.remove(row.last()) 394 417 } 395 418 }
Note: See TracChangeset
for help on using the changeset viewer.