Changeset 1830

Show
Ignore:
Timestamp:
10-05-11 15:44:15 (3 years ago)
Author:
s.h.sikkema@…
Message:

Clarified error messages related to unreachable module; added functionality to export numbers using comma as decimal separator and made this the default for european csv style export

Location:
trunk
Files:
3 modified

Legend:

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

    r1828 r1830  
    217217 
    218218        // process requested output file type 
    219         def outputDelimiter, outputFileExtension 
     219        def outputDelimiter, outputFileExtension, locale = java.util.Locale.US 
    220220 
    221221        switch(session.exportFileType) { 
     
    227227                outputDelimiter = ';' 
    228228                outputFileExtension = 'csv' 
     229                locale = java.util.Locale.GERMAN // force use of comma as decimal separator 
    229230                break 
    230231            default: // Tab delimited with .txt extension 
     
    238239                try { 
    239240 
    240                         // assayService.exportRowWiseDataToExcelFile(session.rowData, response.outputStream) 
    241                         assayService.exportRowWiseDataToCSVFile(session.rowData, response.outputStream, outputDelimiter) 
     241                        assayService.exportRowWiseDataToCSVFile(session.rowData, response.outputStream, outputDelimiter, locale) 
    242242 
    243243                        // clear the data from the session 
  • trunk/grails-app/services/dbnp/studycapturing/AssayService.groovy

    r1828 r1830  
    1818import org.codehaus.groovy.grails.web.json.JSONObject 
    1919import org.dbnp.gdt.RelTime 
    20 import org.dbnp.gdt.TemplateFieldType; 
     20import org.dbnp.gdt.TemplateFieldType 
     21import java.text.DecimalFormat 
     22import java.text.NumberFormat 
    2123 
    2224class AssayService { 
     
    244246                def moduleUrl = assay.module.url 
    245247 
    246                 def path = moduleUrl + "/rest/getMeasurements/query?assayToken=$assay.assayUUID" 
    247  
    248                 def jsonArray = moduleCommunicationService.callModuleRestMethodJSON(moduleUrl, path) 
     248                def path = moduleUrl + "/rest/getMeasurements/query" 
     249        def query = "assayToken=$assay.assayUUID" 
     250        def jsonArray 
     251 
     252        try { 
     253            jsonArray = moduleCommunicationService.callModuleMethod(moduleUrl, path, query) 
     254        } catch (e) { 
     255            throw new Exception("An error occured while trying to get the measurement tokens from the $assay.module.name. \ 
     256             This means the module containing the measurement data is not available right now. Please try again \ 
     257             later or notify the system administrator if the problem persists. URL: $path?$query.") 
     258        } 
    249259 
    250260                jsonArray.collect { 
     
    275285                } 
    276286 
    277                 def path = moduleUrl + "/rest/getMeasurementData/query?assayToken=$assay.assayUUID" + tokenString 
    278  
    279                 def (sampleTokens, measurementTokens, moduleData) = moduleCommunicationService.callModuleRestMethodJSON(moduleUrl, path) 
     287                def path = moduleUrl + "/rest/getMeasurementData/query" 
     288 
     289        def query = "assayToken=$assay.assayUUID$tokenString" 
     290 
     291                def sampleTokens = [], measurementTokens = [], moduleData = [] 
     292 
     293        try { 
     294            (sampleTokens, measurementTokens, moduleData) = moduleCommunicationService.callModuleMethod(moduleUrl, path, query) 
     295        } catch (e) { 
     296            throw new Exception("An error occured while trying to get the measurement data from the $assay.module.name. \ 
     297             This means the module containing the measurement data is not available right now. Please try again \ 
     298             later or notify the system administrator if the problem persists. URL: $path?$query.") 
     299        } 
    280300 
    281301                if (!sampleTokens?.size()) return [] 
     
    307327                                                measurements << null 
    308328                                        }  else { 
    309                                                 measurements << ( moduleData[ valueIndex ] == JSONObject.NULL ? "" : moduleData[ valueIndex ].toString() ); 
     329                                                measurements << ( moduleData[ valueIndex ] == JSONObject.NULL ? "" : moduleData[ valueIndex ].toDouble() ); 
    310330                                        } 
    311331                                } else { 
     
    520540         * @return 
    521541         */ 
    522         def exportRowWiseDataToCSVFile(rowData, outputStream, outputDelimiter = '\t') { 
     542        def exportRowWiseDataToCSVFile(rowData, outputStream, outputDelimiter = '\t', locale = java.util.Locale.US) { 
     543 
     544        def formatter = NumberFormat.getNumberInstance(locale) 
     545        formatter.setGroupingUsed false // we don't want grouping (thousands) separators 
    523546 
    524547        outputStream << rowData.collect { row -> 
    525548          row.collect{ 
    526549 
    527               // omit quotes in case of numeric values 
    528               if (it instanceof Number) return it 
     550              // omit quotes in case of numeric values and format using chosen locale 
     551              if (it instanceof Number) return formatter.format(it) 
    529552 
    530553              def s = it?.toString() ?: '' 
  • trunk/test/unit/dbnp/studycapturing/AssayServiceTests.groovy

    r1828 r1830  
    271271        // - is the custom delimiter (e.g. tab, comma, semicolon) correctly handled 
    272272        // - null values are exported as empty strings 
     273        // - possibility to use comma's as decimal separators 
     274        // - 2.0 -> 2 and 2.1 -> 2.1 
     275        // - no thousand separators 
    273276 
    274277        def rowData = [["""a 
    275 b""","a,b","a\"b", "abc"],[1,2.0,"3,1"],[null,2]] 
     278b""","a,b","a\"b", "abc"],[1,2.1,"3,1"],[null,2, 2.0, 2000]] 
    276279 
    277280        def baos = new ByteArrayOutputStream() 
    278281 
    279282        service.exportRowWiseDataToCSVFile rowData, baos, '\t' 
    280         assertEquals 'CSV Output', '"a\nb"\t"a,b"\t"a""b"\tabc\n1\t2.0\t"3,1"\n\t2', baos.toString() 
     283        assertEquals 'CSV Output', '"a\nb"\t"a,b"\t"a""b"\tabc\n1\t2.1\t"3,1"\n\t2\t2\t2000', baos.toString() 
    281284 
    282285        baos.reset() 
    283286 
    284287        service.exportRowWiseDataToCSVFile rowData, baos, ',' 
    285         assertEquals 'CSV Output', '"a\nb","a,b","a""b",abc\n1,2.0,"3,1"\n,2', baos.toString() 
     288        assertEquals 'CSV Output', '"a\nb","a,b","a""b",abc\n1,2.1,"3,1"\n,2,2,2000', baos.toString() 
    286289 
    287290        baos.reset() 
    288291 
    289         service.exportRowWiseDataToCSVFile rowData, baos, ';' 
    290         assertEquals 'CSV Output', '"a\nb";"a,b";"a""b";abc\n1;2.0;"3,1"\n;2', baos.toString() 
     292        service.exportRowWiseDataToCSVFile rowData, baos, ';', java.util.Locale.GERMAN 
     293        assertEquals 'CSV Output', '"a\nb";"a,b";"a""b";abc\n1;2,1;"3,1"\n;2;2;2000', baos.toString() 
    291294 
    292295