Changeset 287


Ignore:
Timestamp:
Jan 21, 2010, 9:54:06 PM (10 years ago)
Author:
gav
Message:

First asset tree import working and created a site.

Location:
trunk/grails-app
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/i18n/messages.properties

    r285 r287  
    11asset.tree.import.success=Asset tree imported.
    22asset.tree.import.failure=Could not create asset tree from supplied file.
    3 asset.tree.import.file.over.max.size=Supplied file is over max size.
     3asset.tree.import.file.over.max.size=Supplied file is greater than max size of {0} Mbytes.
    44asset.tree.import.file.not.supplied=No file supplied.
    55asset.tree.import.no.header=The supplied file does not have the correct headers, please see the template file.
  • trunk/grails-app/services/CsvService.groovy

    r271 r287  
    1111
    1212    /**
    13     * Import an asset tree for a single asset.
    14     * @param file A csv file containing the asset tree for a single asset.
     13    * Import an asset tree creating items as required.
     14    * @param request The http request to run getFile against.
     15    * Get file should return a csv format file containing the asset tree as per template.
    1516    */
    1617    def importAssetTree(request) {
     
    2021        def fileMaxSize = 10 * megaByteMultiplier //Mb
    2122
     23        def multiPartFile = request.getFile('file')
     24
     25        InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream)
     26        CSVReader reader = new CSVReader(sr)
     27
    2228        def fail = { Map m ->
    2329            //status.setRollbackOnly()
    24             result.error = [ code: m.code, args: ["Import Asset Tree"] ]
     30            reader.close()
     31            result.error = [ code: m.code, args: m.args ]
    2532            return result
    2633        }
    27 
    28         def multiPartFile = request.getFile('file')
    2934
    3035        if(!multiPartFile || multiPartFile.isEmpty())
     
    3237
    3338        if (multiPartFile.getSize() > fileMaxSize)
    34             return fail(code: "asset.tree.import.file.over.max.size")
     39            return fail(code: "asset.tree.import.file.over.max.size", args: [fileMaxSize/megaByteMultiplier])
    3540
    36         InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream)
    37         CSVReader reader = new CSVReader(sr)
    38 
    39         def nextLine = reader.readNext()
     41        def line = reader.readNext()
    4042        def lineNumber = 1
    4143
    4244        def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"]
    43         if(nextLine != header)
     45
     46        if(line != header)
    4447            return fail(code: "asset.tree.import.no.header")
    4548
    46         nextLine = reader.readNext()
     49        log.info "Import checks passed, start processing asset file."
     50
     51        // Prepare the first body line.
     52        line = reader.readNext()
    4753        lineNumber ++
    4854
    49         while(nextLine) {
     55        while(line) {
     56            def lineSize = line.size()
     57//             log.info lineNumber+ "(" + lineSize + ")" + " : " + line
     58
     59            if(line[0]) {
     60                    if( !Site.findByName(line[0]) )
     61                        new Site(name: line[0]).save()
     62            }
     63
     64            line = reader.readNext()
    5065            lineNumber ++
    51 
    52             println lineNumber + " : " + nextLine[0]
    53 
    54 //             if(nextLine[0]) {
    55 //                 if( !Site.findByName(nextLine[0].toString()) )
    56 //                     println nextLine[0]
    57 //             }
    58 
    59             nextLine = reader.readNext()
    60         } //while(nextLine)
     66        } //while(line)
    6167
    6268        // Success.
     69        reader.close()
    6370        return result
    6471
     
    7784        //Header
    7885        def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"]
     86        def blankLine = []
     87        def noteLine = ["Note: the header is required, start by replacing this line."]
     88
    7989        writer.writeNext(header as String[])
    80 
    81         //Rows
    82         def row
    83 
    84         row = []
    85         writer.writeNext(row as String[]) //blank row between assets.
     90        writer.writeNext(blankLine as String[])
     91        writer.writeNext(noteLine as String[])
    8692
    8793        writer.close()
  • trunk/grails-app/views/assetDetailed/importAssetTree.gsp

    r271 r287  
    1212        <div class="body">
    1313            <g:render template="/shared/messages" />
    14             <g:uploadForm action="importAssetTreeSave" enctype="multipart/form-data" onsubmit="return Lightbox.loading();">
     14            <g:uploadForm action="importAssetTreeSave" onsubmit="return Lightbox.loading();">
    1515                <div class="dialog">
    1616                    <table>
     
    2020                                    <label for="file">File:</label>
    2121                                </td>
    22                                 <td valign="top" class="value ${hasErrors(bean: picture, field: 'file', 'errors')}">
     22                                <td valign="top" class="value">
    2323                                    <input type="file" id="file" name="file" size="40"/>
    2424                                </td>
Note: See TracChangeset for help on using the changeset viewer.