Changeset 1827


Ignore:
Timestamp:
May 9, 2011, 4:39:18 PM (6 years ago)
Author:
s.h.sikkema@…
Message:

added different assay export possibilities (tab delimited, csv (comma and semicolon);

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/dbnp/studycapturing/AssayController.groovy

    r1799 r1827  
    171171                                }
    172172
     173                // collect the assay data according to user selecting
    173174                                def assayData           = assayService.collectAssayData(flow.assay, fieldMapSelection, measurementTokens)
    174175                                flow.rowData            = assayService.convertColumnToRowStructure(assayData)
    175176
     177                // prepare the assay data preview
    176178                                def previewRows         = Math.min(flow.rowData.size()    as int, 5) - 1
    177179                                def previewCols         = Math.min(flow.rowData[0].size() as int, 5) - 1
     
    179181                                flow.assayDataPreview   = flow.rowData[0..previewRows].collect{ it[0..previewCols] as ArrayList }
    180182
     183                // store the selected file type in the flow
     184                flow.exportFileType = params.exportFileType
     185
    181186                        }.to "compileExportData"
    182187
     
    185190
    186191                compileExportData {
    187                         on ("ok"){session.rowData = flow.rowData}.to "export"
     192                        on ("ok"){
     193                session.rowData = flow.rowData
     194                session.exportFileType = flow.exportFileType
     195            }.to "export"
    188196                        on ("cancel").to "selectAssay"
    189197                }
     
    204212        def doExport = {
    205213
    206                 def filename = 'export.csv'
     214        // make sure we're coming from the export flow, otherwise redirect there
     215        if (!(session.rowData && session.exportFileType))
     216            redirect(action: 'assayExportFlow')
     217
     218        // process requested output file type
     219        def outputDelimiter, outputFileExtension
     220
     221        switch(session.exportFileType) {
     222            case 2: // Comma delimited csv
     223                outputDelimiter = ','
     224                outputFileExtension = '.csv'
     225                break
     226            case 3: // Semicolon delimited csv
     227                outputDelimiter = ';'
     228                outputFileExtension = '.csv'
     229                break
     230            default: // Tab delimited with .txt extension
     231                outputDelimiter = '\t'
     232                outputFileExtension = '.txt'
     233        }
     234
     235                def filename = "export.$outputFileExtension"
    207236                response.setHeader("Content-disposition", "attachment;filename=\"${filename}\"")
    208237                response.setContentType("application/octet-stream")
    209238                try {
    210239
    211                         //                      assayService.exportRowWiseDataToExcelFile(session.rowData, response.outputStream)
    212                         assayService.exportRowWiseDataToCSVFile(session.rowData, response.outputStream)
     240                        // assayService.exportRowWiseDataToExcelFile(session.rowData, response.outputStream)
     241                        assayService.exportRowWiseDataToCSVFile(session.rowData, response.outputStream, outputDelimiter)
    213242
    214243                        // clear the data from the session
    215244                        session.removeAttribute('rowData')
     245                        session.removeAttribute('exportFileType')
    216246
    217247                } catch (Exception e) {
  • trunk/grails-app/services/dbnp/studycapturing/AssayService.groovy

    r1803 r1827  
    520520         * @return
    521521         */
    522         def exportRowWiseDataToCSVFile(rowData, outputStream) {
     522        def exportRowWiseDataToCSVFile(rowData, outputStream, outputDelimiter = '\t') {
    523523
    524524        outputStream << rowData.collect { row ->
     
    544544              addQuotes ? "\"$s\"" : s
    545545
    546           }.join(',')
     546          }.join(outputDelimiter)
    547547        }.join('\n')
    548548
  • trunk/grails-app/views/assay/assayExport/selectFields.gsp

    r1790 r1827  
    5858      <g:select name="measurementToken" id="measurementToken" from="${measurementTokens}" value="${measurementTokens}" class="field" multiple="true" />
    5959      <br /><br />
     60
     61      <h1>Select type of resulting file</h1>
     62      <g:radioGroup name="exportFileType" labels="['Tab delimited (.txt)', 'Comma Separated: USA/UK (.csv)', 'Semicolon Separated: European (.csv)']" values="[1,2,3]" value="1" >
     63        <p>${it.radio} ${it.label}</p>
     64      </g:radioGroup>
    6065      <g:submitButton name="submit" value="Submit"/>
    6166
  • trunk/test/unit/dbnp/studycapturing/AssayServiceTests.groovy

    r1803 r1827  
    269269        // - double quotes should be escaped by double quotes ( " -> "" )
    270270        // - other strings and numbers should remain 'quoteless'
     271        // - is the custom delimiter (e.g. tab, comma, semicolon) correctly handled
    271272
    272273        def rowData = [["""a
     
    275276        def baos = new ByteArrayOutputStream()
    276277
    277         service.exportRowWiseDataToCSVFile rowData, baos
    278 
     278        service.exportRowWiseDataToCSVFile rowData, baos, '\t'
     279        assertEquals 'CSV Output', '"a\nb"\t"a,b"\t"a""b"\tabc\n1\t2.0\t"3,1"', baos.toString()
     280
     281        baos.reset()
     282
     283        service.exportRowWiseDataToCSVFile rowData, baos, ','
    279284        assertEquals 'CSV Output', '"a\nb","a,b","a""b",abc\n1,2.0,"3,1"', baos.toString()
    280285
     286        baos.reset()
     287
     288        service.exportRowWiseDataToCSVFile rowData, baos, ';'
     289        assertEquals 'CSV Output', '"a\nb";"a,b";"a""b";abc\n1;2.0;"3,1"', baos.toString()
     290
     291
    281292    }
    282293}
Note: See TracChangeset for help on using the changeset viewer.