Index: /trunk/grails-app/services/CsvService.groovy
===================================================================
--- /trunk/grails-app/services/CsvService.groovy	(revision 300)
+++ /trunk/grails-app/services/CsvService.groovy	(revision 301)
@@ -81,137 +81,143 @@
             def parentItem
 
-            while(line) {
-                columnIndex = 0
-                numberOfColumns = Math.min( line.size(), maxNumberOfColumns )
-
-                if( (columnIndex+2) > numberOfColumns ) {
+            def column = [:]
+
+            def nextLine = {
                     line = reader.readNext()
                     lineNumber ++
-                    continue
-                }
-
-                if(line[columnIndex]) {
-                    siteInstance = Site.findByName(line[columnIndex])
-                    if(!siteInstance) {
-                        log.info "Creating site: " + line[columnIndex]
-                        siteInstance = new Site(name: line[columnIndex],
-                                                                    description: line[++columnIndex])
-                        if(!siteInstance.save()) {
-                            log.error "Failed to create site on line: " + line[--columnIndex] + "(" + lineNumber + ")"
+                    log.info "Processing line: " + lineNumber
+            }
+
+            def nextColumn = {
+                if( (columnIndex+2) > numberOfColumns ) {
+                    log.info "No more columns on line: " + lineNumber
+                    return false
+
+                }
+                if(!line[columnIndex]) {
+                    log.info "No name at " + "line: " + lineNumber + " col: " + columnIndex
+                    return false
+                }
+                column.name = line[columnIndex]
+                column.description = line[++columnIndex]
+                columnIndex++
+                // Success.
+                return column
+            }
+
+            while(line) {
+                numberOfColumns = Math.min( line.size(), maxNumberOfColumns )
+                columnIndex = 0
+
+                if(!nextColumn()) {
+                    nextLine()
+                    continue
+                }
+
+                siteInstance = Site.findByName(column.name)
+                if(!siteInstance) {
+                    log.info "Creating site: " + column.name
+                    siteInstance = new Site(name: column.name,
+                                                                description: column.description)
+                    if(!siteInstance.save()) {
+                        log.error "Failed to create site on line: " + column.name + "(" + lineNumber + ")"
+                        return fail(code: "asset.tree.import.failure", args: [lineNumber])
+                    }
+                }
+                else log.info "Existing site: " + siteInstance
+
+                if(!nextColumn()) {
+                    nextLine()
+                    continue
+                }
+
+                departmentInstance = Department.findByName(column.name)
+                if(!departmentInstance) {
+                    log.info "Creating department: " + column.name
+                    departmentInstance = new Department(name: column.name,
+                                                                                            description: column.description,
+                                                                                            site: siteInstance)
+                    if(!departmentInstance.save()) {
+                        log.error "Failed to create department on line: " + column.name + "(" + lineNumber + ")"
+                        return fail(code: "asset.tree.import.failure", args: [lineNumber])
+                    }
+                }
+                else log.info "Existing department: " + departmentInstance
+
+                if(!nextColumn()) {
+                    nextLine()
+                    continue
+                }
+
+                sectionInstance = Section.findByName(column.name)
+                if(!sectionInstance) {
+                    log.info "Creating section: " + column.name
+                    sectionInstance =  new Section(name: column.name,
+                                                                            description: column.description,
+                                                                            site: siteInstance,
+                                                                            department: departmentInstance)
+                    if(!sectionInstance.save()) {
+                        log.error "Failed to create section on line: " + column.name + "(" + lineNumber + ")"
+                        return fail(code: "asset.tree.import.failure", args: [lineNumber])
+                    }
+                }
+                else log.info "Existing section: " + sectionInstance
+
+                if(!nextColumn()) {
+                    nextLine()
+                    continue
+                }
+
+                assetInstance = Asset.findByName(column.name)
+                if(!assetInstance) {
+                    log.info "Creating asset: " + column.name
+                    assetInstance = new Asset(name: column.name,
+                                                                    description: column.description,
+                                                                    section: sectionInstance)
+                    if(!assetInstance.save()) {
+                        log.error "Failed to create asset on line: " + column.name + "(" + lineNumber + ")"
+                        return fail(code: "asset.tree.import.failure", args: [lineNumber])
+                    }
+                }
+                else log.info "Existing asset: " + assetInstance
+
+                if(!nextColumn()) {
+                    nextLine()
+                    continue
+                }
+
+                assetSubItemInstance = AssetSubItem.findByName(column.name)
+                if(!assetSubItemInstance) {
+                    log.info "Creating asset sub item: " + column.name
+                    assetSubItemInstance = new AssetSubItem(name: column.name,
+                                                                                                description: column.description)
+                    if(!assetInstance.save()) {
+                        log.error "Failed to create assetSubItem on line: " + column.name + "(" + lineNumber + ")"
+                        return fail(code: "asset.tree.import.failure", args: [lineNumber])
+                    }
+                }
+                else log.info "Existing asset sub item: " + assetSubItemInstance
+
+                assetInstance.addToAssetSubItems(assetSubItemInstance)
+
+                while( nextColumn() ) {
+
+                    parentItem = assetSubItemInstance
+                    assetSubItemInstance = AssetSubItem.findByName(column.name)
+                    if(!assetSubItemInstance) {
+                        log.info "Creating asset sub item: " + column.name
+                        assetSubItemInstance = new AssetSubItem(name: column.name,
+                                                                                                    description: column.description,
+                                                                                                    parentItem: parentItem)
+                        if(!assetSubItemInstance.save()) {
+                            log.error "Failed to create assetSubItem on line: " + column.name + "(" + lineNumber + ")"
                             return fail(code: "asset.tree.import.failure", args: [lineNumber])
                         }
                     }
-                    else log.info "Existing site: " + siteInstance
-
-                    columnIndex++
-                    if( (columnIndex+2) > numberOfColumns ) {
-                        line = reader.readNext()
-                        lineNumber ++
-                        continue
-                    }
-
-                    if(line[columnIndex]) {
-                        departmentInstance = Department.findByName(line[columnIndex])
-                        if(!departmentInstance) {
-                            departmentInstance = new Department(name: line[columnIndex],
-                                                                                                    description: line[++columnIndex],
-                                                                                                    site: siteInstance)
-                            if(!departmentInstance.save()) {
-                                log.error "Failed to create department on line: " + line[--columnIndex] + "(" + lineNumber + ")"
-                                return fail(code: "asset.tree.import.failure", args: [lineNumber])
-                            }
-                        }
-
-                        columnIndex++
-                        if( (columnIndex+2) > numberOfColumns ) {
-                            line = reader.readNext()
-                            lineNumber ++
-                            continue
-                        }
-
-                        if(line[columnIndex]) {
-                            sectionInstance = Section.findByName(line[columnIndex])
-                            if(!sectionInstance) {
-                                sectionInstance =  new Section(name: line[columnIndex],
-                                                                                        description: line[++columnIndex],
-                                                                                        site: siteInstance,
-                                                                                        department: departmentInstance)
-                                if(!sectionInstance.save()) {
-                                    log.error "Failed to create section on line: " + line[--columnIndex] + "(" + lineNumber + ")"
-                                    return fail(code: "asset.tree.import.failure", args: [lineNumber])
-                                }
-                            }
-
-                            columnIndex++
-                            if( (columnIndex+2) > numberOfColumns ) {
-                                line = reader.readNext()
-                                lineNumber ++
-                                continue
-                            }
-
-                            if(line[columnIndex]) {
-                                assetInstance = Asset.findByName(line[columnIndex])
-                                if(!assetInstance) {
-                                    assetInstance = new Asset(name: line[columnIndex],
-                                                                                    description: line[++columnIndex],
-                                                                                    section: sectionInstance)
-                                    if(!assetInstance.save()) {
-                                        log.error "Failed to create asset on line: " + line[--columnIndex] + "(" + lineNumber + ")"
-                                        return fail(code: "asset.tree.import.failure", args: [lineNumber])
-                                    }
-                                }
-
-                                columnIndex++
-                                if( (columnIndex+2) > numberOfColumns ) {
-                                    line = reader.readNext()
-                                    lineNumber ++
-                                    continue
-                                }
-
-                                if(line[columnIndex]) {
-                                    assetSubItemInstance = AssetSubItem.findByName(line[columnIndex])
-                                    if(!assetSubItemInstance) {
-                                        assetSubItemInstance = new AssetSubItem(name: line[columnIndex],
-                                                                                                                    description: line[++columnIndex])
-                                        if(!assetInstance.save()) {
-                                            log.error "Failed to create assetSubItem on line: " + line[--columnIndex] + "(" + lineNumber + ")"
-                                            return fail(code: "asset.tree.import.failure", args: [lineNumber])
-                                        }
-                                    }
-
-                                    assetInstance.addToAssetSubItems(assetSubItemInstance)
-
-                                    columnIndex++
-                                    if( (columnIndex+2) > numberOfColumns ) {
-                                        line = reader.readNext()
-                                        lineNumber ++
-                                        continue
-                                    }
-
-                                    while( (columnIndex+2) < numberOfColumns ) {
-
-                                        if(line[columnIndex]) {
-                                            parentItem = assetSubItemInstance
-                                            assetSubItemInstance = new AssetSubItem(name: line[columnIndex],
-                                                                                                                        description: line[++columnIndex],
-                                                                                                                        parentItem: parentItem)
-                                            if(!assetInstance.save()) {
-                                                log.error "Failed to create assetSubItem on line: " + line[--columnIndex] + "(" + lineNumber + ")"
-                                                return fail(code: "asset.tree.import.failure", args: [lineNumber])
-                                            }
-                                        }
-                                        else break
-
-                                        columnIndex++
-                                    } // while()
-
-                                } // AssetSubItem L1
-                            } // Asset
-                        } // Section
-                    } // Department
-                } // Site
-
-                line = reader.readNext()
-                lineNumber ++
+                else log.info "Existing asset sub item: " + assetSubItemInstance
+
+                } // while( nextColumn() )
+
+                nextLine()
             } //while(line)
 
