Ignore:
Timestamp:
Feb 24, 2011, 5:14:08 PM (6 years ago)
Author:
s.h.sikkema@…
Message:

Assay export functionality

File:
1 edited

Legend:

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

    r1455 r1559  
    133133
    134134    /**
     135     * Shows a page where individual fields for the different categories (ie.
     136     * subject data, sampling events... etc.) can be selected for export
     137     *
     138     * @param params.id Assay id
     139     */
     140    def selectFields = {
     141        // receives an assay id
     142        def assay = Assay.get(params.assayId)
     143
     144        // obtain fields for each category
     145        try {
     146
     147            def fieldMap = assayService.collectAssayTemplateFields(assay)
     148           
     149        } catch (Exception e) {
     150
     151            flash.errorMessage = e.message
     152            redirect action: 'selectAssay'
     153
     154        }
     155        def measurementTokens = fieldMap.remove('Module Measurement Data')
     156
     157        flash.fieldMap = fieldMap
     158        flash.measurementTokens = measurementTokens
     159        flash.assayId = params.assayId
     160
     161        [fieldMap: fieldMap, measurementTokens: measurementTokens*.name]
     162    }
     163
     164    /**
    135165     * Exports all assay information as an Excel file.
    136166     *
    137167     * @param params.id Assay id
    138168     */
    139     def exportAssayAsExcel = {
    140 
    141         Assay assay = Assay.get(params.assayId)
     169    def compileExportData = {
     170
     171        def fieldMap = flash.fieldMap
     172        def measurementTokens = flash.measurementTokens
     173
     174        def fieldMapSelection = [:]
     175
     176        fieldMap.eachWithIndex { cat, cat_i ->
     177
     178            if (params."cat_$cat_i" == 'on') {
     179                fieldMapSelection[cat.key] = []
     180
     181                cat.value.eachWithIndex { field, field_i ->
     182
     183                    if (params."cat_${cat_i}_${field_i}" == 'on') {
     184
     185                        fieldMapSelection[cat.key] += field
     186
     187                    }
     188
     189                }
     190
     191                if (fieldMapSelection[cat.key] == []) fieldMapSelection.remove(cat.key)
     192
     193            }
     194
     195        }
     196
     197        def measurementTokensSelection = []
     198
     199        if (params."cat_4" == 'on') {
     200
     201            measurementTokensSelection = params.measurementToken == 'null' ? measurementTokens : [ name: params.measurementToken]
     202
     203        }
     204
     205        Assay assay = Assay.get(flash.assayId)
    142206
    143207        // check if assay exists
    144208        if (!assay) {
    145             flash.errorMessage = "No assay found with id: $params.assayId."
     209
     210            flash.errorMessage = flash.assayId ? "No assay found with id: ${flash.assayId}" : 'Assay has no value (null).'
    146211            redirect action: 'selectAssay'
    147212            return
     213
    148214        }
    149215
    150216        try {
    151217
    152             def assayData = assayService.collectAssayData(assay, grailsApplication.config.modules.metabolomics.url)
    153 
    154             def filename = 'export.xlsx'
    155             response.setHeader("Content-disposition", "attachment;filename=\"${filename}\"")
    156             response.setContentType("application/octet-stream")
    157 
    158             assayService.exportColumnWiseDataToExcelFile(assayData, response.outputStream)
     218            def assayData = assayService.collectAssayData(assay, fieldMapSelection, measurementTokensSelection)
     219
     220            def rowData = assayService.convertColumnToRowStructure(assayData)
     221
     222            flash.rowData = rowData
     223
     224            def assayDataPreview = rowData[0..4].collect{it[0..4]}
     225
     226
     227            [assayDataPreview: assayDataPreview]
    159228
    160229        } catch (Exception e) {
     
    165234        }
    166235    }
     236
     237    def doExport = {
     238
     239        def filename = 'export.xlsx'
     240        response.setHeader("Content-disposition", "attachment;filename=\"${filename}\"")
     241        response.setContentType("application/octet-stream")
     242        try {
     243           
     244            assayService.exportRowWiseDataToExcelFile(flash.rowData, response.outputStream)
     245
     246        } catch (Exception e) {
     247
     248            flash.errorMessage = e.message
     249            redirect action: 'selectAssay'
     250
     251        }
     252
     253
     254    }
    167255}
Note: See TracChangeset for help on using the changeset viewer.