Changeset 315 for trunk/grails-app


Ignore:
Timestamp:
Feb 4, 2010, 4:00:12 PM (15 years ago)
Author:
gav
Message:

Improvements to CsvService.
More comments to code and asset tree template.
Add description column to asset tree export and make import more robust with trimming and capitalising.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/services/CsvService.groovy

    r302 r315  
    11import au.com.bytecode.opencsv.CSVWriter
    22import au.com.bytecode.opencsv.CSVReader
     3import org.apache.commons.lang.WordUtils
    34
    45/**
     
    9394                    log.info "No more columns on line: " + lineNumber
    9495                    return false
    95 
    9696                }
    9797                if(!line[columnIndex]) {
     
    9999                    return false
    100100                }
    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                }
    103106                columnIndex++
    104107                // Success.
     
    106109            }
    107110
     111            // Primary loop.
    108112            while(line) {
    109113                numberOfColumns = Math.min( line.size(), maxNumberOfColumns )
     
    116120
    117121                // Ignore comment lines.
    118                 if( column.name.startsWith("Comment") ) {
     122                if(line.toString().toLowerCase().contains("comment")) {
    119123                    log.info "Comment line found."
    120124                    nextLine()
     
    123127
    124128                // Ignore example lines.
    125                 if( column.name.startsWith("Example") ) {
     129                if(line.toString().toLowerCase().contains("example")) {
    126130                    log.info "Example line found."
    127131                    nextLine()
     
    129133                }
    130134
     135                // Site.
    131136                siteInstance = Site.findByName(column.name)
    132137                if(!siteInstance) {
     
    146151                }
    147152
     153                // Department.
    148154                departmentInstance = Department.findByName(column.name)
    149155                if(!departmentInstance) {
     
    164170                }
    165171
     172                // Section.
    166173                sectionInstance = Section.findByName(column.name)
    167174                if(!sectionInstance) {
     
    183190                }
    184191
     192                // Asset.
    185193                assetInstance = Asset.findByName(column.name)
    186194                if(!assetInstance) {
     
    201209                }
    202210
     211                // AssetSubItem Level 1.
    203212                assetSubItemInstance = AssetSubItem.findByName(column.name)
    204213                if(!assetSubItemInstance) {
     
    215224                assetInstance.addToAssetSubItems(assetSubItemInstance)
    216225
     226                // Remaining AssetSubItems.
    217227                while( nextColumn() ) {
    218228
     
    229239                        }
    230240                    }
    231                 else log.info "Existing asset sub item: " + assetSubItemInstance
     241                    else log.info "Existing asset sub item: " + assetSubItemInstance
    232242
    233243                } // while( nextColumn() )
     
    268278        writer.writeNext(["Example: Site1", "", "Department 1", "", "Section 1", "", "Asset 2", ""] as String[])
    269279        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.")
    277289        writer.writeNext()
    278290    }
     
    324336        CSVWriter writer = new CSVWriter(sw)
    325337
    326         //Header
    327         writer.writeNext(templateHeaderLine1 as String[])
    328         writer.writeNext(templateHeaderLine2 as String[])
     338        writeTemplateLines(writer)
    329339
    330340        //Rows
     
    332342
    333343        def writeAssetSubItem4 = { assetSubItem ->
    334             row.add(assetSubItem)
     344            row.add(assetSubItem.name)
     345            row.add(assetSubItem.description)
    335346            writer.writeNext(row as String[])
    336347        }
    337348
    338349        def writeAssetSubItem3 = { assetSubItem ->
    339             row.add(assetSubItem)
     350            row.add(assetSubItem.name)
     351            row.add(assetSubItem.description)
    340352
    341353            if(assetSubItem.subItems.size() > 0) {
     
    343355                    writeAssetSubItem4(assetSubItem4)
    344356                    row.remove(row.last())
     357                    row.remove(row.last())
    345358                }
    346359            }
     
    352365
    353366        def writeAssetSubItem2 = { assetSubItem ->
    354             row.add(assetSubItem)
     367            row.add(assetSubItem.name)
     368            row.add(assetSubItem.description)
    355369
    356370            if(assetSubItem.subItems.size() > 0) {
     
    358372                    writeAssetSubItem3(assetSubItem3)
    359373                    row.remove(row.last())
     374                    row.remove(row.last())
    360375                }
    361376            }
     
    367382
    368383        def writeAssetSubItem1 = { assetSubItem ->
    369             row.add(assetSubItem)
     384            row.add(assetSubItem.name)
     385            row.add(assetSubItem.description)
    370386
    371387            if(assetSubItem.subItems.size() > 0) {
     
    373389                    writeAssetSubItem2(assetSubItem2)
    374390                    row.remove(row.last())
     391                    row.remove(row.last())
    375392                }
    376393            }
     
    384401            row = []
    385402            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)
    388409            row.add(asset.name)
     410            row.add(asset.description)
    389411
    390412            if(asset.assetSubItems.size() > 0) {
     
    392414                    writeAssetSubItem1(assetSubItem1)
    393415                    row.remove(row.last())
     416                    row.remove(row.last())
    394417                }
    395418            }
Note: See TracChangeset for help on using the changeset viewer.