Changeset 1425


Ignore:
Timestamp:
Jan 21, 2011, 4:37:54 PM (6 years ago)
Author:
s.h.sikkema@…
Message:

Rudimentary assay export functionality

Location:
trunk
Files:
3 edited
1 moved

Legend:

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

    r1261 r1425  
    138138     */
    139139    def exportAssayAsExcel = {
    140         Assay assay = Assay.get(params.id)
     140
     141        Assay assay = Assay.get(params.assayId)
    141142
    142143        if (!assay) {
    143             flash.errorMessage = "No assay found with id: $params.id."
     144            flash.errorMessage = "No assay found with id: $params.assayId."
    144145            redirect action: 'selectAssay'
    145146            return
    146147        }
    147148
    148         // TODO: refactor into service
     149        try {
    149150
    150         // Gather sample meta data from GSCF
    151         def samples = assay.samples
     151            def assayData = assayService.collectAssayData(assay, grailsApplication.config.modules.metabolomics.url)
    152152
    153         def collectUsedTemplateFields = { templateEntityList ->
     153            def filename = 'export.xlsx'
     154            response.setHeader("Content-disposition", "attachment;filename=\"${filename}\"")
     155            response.setContentType("application/octet-stream")
    154156
    155             def templateFields = templateEntityList*.giveFields().flatten().unique().findAll{it}
    156             def usedTemplateFields = templateFields.findAll{ tf ->
     157            assayService.exportColumnWiseDataToExcelFile(assayData, response.outputStream)
    157158
    158                 templateEntityList.any { it.fieldExists(tf.name) && it.getFieldValue(tf.name) }
    159             }
     159        } catch (Exception e) {
    160160
    161             def m = [:]
    162             usedTemplateFields.each { tf ->
    163                 m["${tf.name}"] = templateEntityList.collect{ it.fieldExists(tf.name) ? it.getFieldValue(tf.name) : '' }
    164             }
    165             m
    166         }
     161            flash.errorMessage = e.toString()
     162            redirect action: 'selectAssay'
    167163
    168         // get all sample related subjects
    169         def assayData = [
    170                 'Subject Data':         collectUsedTemplateFields(samples*.parentSubject.unique()),
    171                 'Sampling Event Data':  collectUsedTemplateFields(samples*.parentEvent.unique()),
    172                 'Event Data':           collectUsedTemplateFields(samples*.parentEventGroup.events.flatten().unique()),
    173                 'Sample Data':          collectUsedTemplateFields(samples)]
    174 
    175         // Gather sample meta data from the module
    176 
    177         // - sample metadata
    178 
    179         // Gather measurement data from the module
    180 
    181         // - measurements
    182 
    183         // Write to excel
    184 
    185         println assayData
    186 
    187         try {
    188             assayService.exportAssayDataAsExcelFile(assayData)
    189         } catch (Exception e) {
    190             flash.errorMessage = e.message
    191             redirect action: 'selectAssay'
    192164        }
    193165    }
  • trunk/grails-app/services/dbnp/authentication/AuthenticationService.groovy

    r983 r1425  
    5555        def user = getSessionAuthenticatedUser(consumer, token)
    5656       
    57         if( user )
     57        if( user ) {
     58            user.refresh()
    5859            user.delete()
     60        }
    5961       
    6062        return true
  • trunk/grails-app/views/assay/selectAssay.gsp

    r1281 r1425  
    2424  <body>
    2525  <div style="color:red;">
    26     flash.errorMessage
     26    ${flash.errorMessage}
    2727  </div>
    28   <g:form name="assaySelect" action="">
    29     <g:select optionKey="id" optionValue="title" name="study" from="${userStudies}"
     28  <g:form name="assaySelect" action="exportAssayAsExcel">
     29    <g:select optionKey="id" optionValue="title" name="studyId" from="${userStudies}"
    3030            onChange="${remoteFunction(controller:'study',action:'ajaxGetAssays',params:'\'id=\'+escape(this.value)',onComplete: 'updateAssay(XMLHttpRequest.responseText, \'assay\')')}"/>
    31     <g:select name="assay" id="assay" from="${assays}"/>
     31    <g:select optionKey="id" name="assayId" id="assay" from="${assays}"/>
    3232    <g:submitButton name="submit" value="Submit"/>
    3333  </g:form>
  • trunk/test/unit/dbnp/studycapturing/AssayControllerTests.groovy

    r1261 r1425  
    33import grails.test.*
    44
    5 import dbnp.data.Term
    6 
    75/**
    86 * AssayControllerTests Test
    97 *
    10  * Description of my test
    11  *
    128 * @author  your email (+name?)
    13  * @since       2010mmdd
    14  * @package     ???
     9 * @since       20101208
     10 * @package     dbnp.studycapturing
    1511 *
    1612 * Revision information:
     
    2218    protected void setUp() {
    2319        super.setUp()
    24 
    25         mockDomain(Term,          [ new Term(id: 1, name: 'Human')])
    26 
    27         mockDomain(TemplateField, [ new TemplateField(id: 1, name: 'tf1', type: TemplateFieldType.STRING),
    28                                     new TemplateField(id: 2, name: 'tf2', type: TemplateFieldType.STRING),
    29                                     new TemplateField(id: 3, name: 'tf3', type: TemplateFieldType.STRING)])
    30 
    31         mockDomain(Template,      [ new Template(id: 1, fields: [TemplateField.get(1), TemplateField.get(2)]),
    32                                     new Template(id: 2, fields: [TemplateField.get(3)])])
    33 
    34         mockDomain(Subject,       [ new Subject(id: 1, name:'subject1', template: Template.get(1), species: Term.get(1)),
    35                                     new Subject(id: 2, name:'subject2', template: Template.get(2), species: Term.get(1))])
    36 
    37         mockDomain(SamplingEvent, [ new SamplingEvent(id:1, startTime: 2, duration: 5, sampleTemplate: new Template())])
    38 
    39         mockDomain(Event,         [ new Event(id: 1, startTime: 6, endTime: 7)])//, new Event(id: 2, startTime: 8, endTime: 9)])
    40 
    41         mockDomain(EventGroup,    [ new EventGroup(id:1, events: [Event.get(1)]) ])
    42 
    43         mockDomain(Sample,        [ new Sample(id: 1, name:'sample1', parentSubject: Subject.get(1), parentEvent: SamplingEvent.get(1), parentEventGroup: EventGroup.get(1)),
    44                                     new Sample(id: 2, name:'sample2', parentSubject: Subject.get(2), parentEvent: SamplingEvent.get(1))])
    45 
    46         mockDomain(Assay,         [ new Assay(id: 1, samples:[Sample.get(1),Sample.get(2)]),
    47                                     new Assay(id: 2, samples:[])])
    48 
    49         Subject.get(1).setFieldValue('tf1', 'tfv1')
    50         Subject.get(1).setFieldValue('tf2', 'tfv2')
    51         Subject.get(2).setFieldValue('tf3', 'tfv3')
    5220    }
    5321
     
    5725
    5826    void testWrongAssayID() {
    59         mockParams.id = 3
     27        mockParams.assayId = 1
    6028
    6129        controller.exportAssayAsExcel()
     
    6634
    6735    void testExceptionHandling() {
    68         mockParams.id = 1
    69 
    70         controller.assayService = [exportAssayDataAsExcelFile:{throw new Exception('msg')}]
     36        controller.assayService = [
     37                collectAssayData:{a, b -> throw new Exception('msg1')},
     38                exportColumnWiseDataToExcelFile:{a, b -> throw new Exception('msg2')}
     39        ]
    7140        controller.exportAssayAsExcel()
    7241
    7342        assertEquals 'Redirected action should match', [action: 'selectAssay'], redirectArgs
    74         assertEquals 'Error message', 'msg', mockFlash.errorMessage
     43        assertEquals 'Error message', 'java.lang.Exception: msg1', mockFlash.errorMessage
     44
     45        controller.assayService.collectAssayData = {a, b -> true}
     46        controller.exportAssayAsExcel()
     47
     48        assertEquals 'Redirected action should match', [action: 'selectAssay'], redirectArgs
     49        assertEquals 'Error message', 'java.lang.Exception: msg2', mockFlash.errorMessage
     50
    7551    }
    7652
    77     void testEmptySampleList() {
    78         mockParams.id = 2
    79 
    80         def passedAssayData = []
    81 
    82         controller.assayService = [exportAssayDataAsExcelFile:{a -> passedAssayData = a}]
    83         controller.exportAssayAsExcel()
    84 
    85         assertEquals 'Assay data', [], passedAssayData*.value.flatten()
    86     }
    87 
    88     void testTemplateFieldsAreCollected() {
    89 
    90         mockParams.id = 1
    91 
    92         Map passedAssayData
    93 
    94         controller.assayService = [exportAssayDataAsExcelFile:{a -> passedAssayData = a}]
    95         controller.exportAssayAsExcel()
    96 
    97         def sample1index = passedAssayData.'Sample Data'.'name'.findIndexOf{it == 'sample1'}
    98         def sample2index = passedAssayData.'Sample Data'.'name'.findIndexOf{it == 'sample2'}
    99 
    100         assertEquals 'Subject template field', ['tfv1',''], passedAssayData.'Subject Data'.tf1[sample1index, sample2index]
    101         assertEquals 'Subject template field', ['tfv2',''], passedAssayData.'Subject Data'.tf2[sample1index, sample2index]
    102         assertEquals 'Subject template field', ['','tfv3'], passedAssayData.'Subject Data'.tf3[sample1index, sample2index]
    103         assertEquals 'Subject species template field', ['Human', 'Human'], passedAssayData.'Subject Data'.species*.toString()
    104         assertEquals 'Subject name template field', ['subject1','subject2'], passedAssayData.'Subject Data'.name[sample1index, sample2index]
    105 
    106         assertEquals 'Sampling event template fields', [2], passedAssayData.'Sampling Event Data'.startTime
    107         assertEquals 'Sampling event template fields', [5], passedAssayData.'Sampling Event Data'.duration
    108         assertEquals 'Sampling event template fields', '[null]', passedAssayData.'Sampling Event Data'.sampleTemplate.toString()
    109         assertEquals 'Event template fields', [6], passedAssayData.'Event Data'.startTime
    110         assertEquals 'Event template fields', [7], passedAssayData.'Event Data'.endTime
    111         assertEquals 'Sample template fields', ['sample1', 'sample2'], passedAssayData.'Sample Data'.name[sample1index, sample2index]
    112     }
    11353}
Note: See TracChangeset for help on using the changeset viewer.