Changeset 2200


Ignore:
Timestamp:
Mar 30, 2012, 5:17:57 PM (11 years ago)
Author:
work@…
Message:
  • implemented getAssayData api call
Location:
trunk/grails-app
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/api/ApiController.groovy

    r2199 r2200  
    1919import dbnp.studycapturing.Study
    2020import dbnp.studycapturing.Assay
    21 import dbnp.authentication.SecUser
    22 import org.dbnp.gdt.TemplateFieldType
    2321
    2422class ApiController {
    2523    def authenticationService
    26     def moduleCommunicationService
    27     def ApiService
     24    def apiService
    2825
    2926        /**
     
    3936
    4037        // see if we already have a token on file for this device id
    41         def token = Token.findByDeviceID(params?.deviceID)
     38        String deviceID = (params.containsKey('deviceID')) ? params.deviceID : ''
     39        def token = Token.findByDeviceID(deviceID)
    4240       
    4341        // generate a new token if we don't have a token on file
     
    281279        }
    282280    }
     281
     282    def getDataForAssay = {
     283        println "api:getDataForAssay: ${params}"
     284
     285        String deviceID     = (params.containsKey('deviceID')) ? params.deviceID : ''
     286        String validation   = (params.containsKey('validation')) ? params.validation : ''
     287        String assayToken   = (params.containsKey('assayToken')) ? params.assayToken : ''
     288
     289        // fetch user and study
     290        def user    = Token.findByDeviceID(deviceID)?.user
     291        def assay   = Assay.findByAssayUUID(assayToken)
     292
     293        // check
     294        if (!apiService.validateRequest(deviceID,validation)) {
     295            response.sendError(401, 'Unauthorized')
     296        } else if (!assay) {
     297            response.sendError(400, 'No such assay')
     298        } else if (!assay.parent.canRead(user)) {
     299            response.sendError(401, 'Unauthorized')
     300        } else {
     301            // fetch data from model
     302//            def measurements =
     303            // define sample measurement data matrix
     304
     305            def matrix = [:]
     306            def measurements    = apiService.getMeasurements(assay, user)
     307            def measurementData = apiService.getMeasurementData(assay, user)
     308
     309            // iterate through measurementData and build data matrix
     310            measurementData.each { data ->
     311                if (!matrix.containsKey(data.sampleToken)) matrix[ data.sampleToken ] = [:]
     312                matrix[ data.sampleToken ][ data.measurementToken ] = data.value
     313            }
     314
     315            // define result
     316            def result = [
     317                    'measurements'  : apiService.getMeasurements(assay, user),
     318                    'matrix'        : matrix
     319            ]
     320
     321            // set output headers
     322            response.status = 200
     323            response.contentType = 'application/json;charset=UTF-8'
     324
     325            if (params.containsKey('callback')) {
     326                render "${params.callback}(${result as JSON})"
     327            } else {
     328                render result as JSON
     329            }
     330        }
     331    }
    283332}
  • trunk/grails-app/domain/dbnp/studycapturing/Assay.groovy

    r1873 r2200  
    11package dbnp.studycapturing
     2
    23import org.dbnp.gdt.*
    34
     
    89 */
    910class Assay extends TemplateEntity {
    10         // The name of the assay, which should indicate the measurements represented in this assay to the user.
    11         String name
     11    // The name of the assay, which should indicate the measurements represented in this assay to the user.
     12    String name
    1213
    13         // The dbNP module in which the assay omics data can be found. */
    14         AssayModule module
     14    // The dbNP module in which the assay omics data can be found. */
     15    AssayModule module
    1516
    16         /**
    17         * UUID of this assay
    18         */
     17    /**
     18     * UUID of this assay
     19     */
    1920    String assayUUID
    2021
    21         /**
    22          * return the domain fields for this domain class
    23          * @return List
    24          */
    25         static List<TemplateField> giveDomainFields() { return Assay.domainFields }
    26         static List<TemplateField> domainFields = [
    27                 new TemplateField(
    28                         name: 'name',
    29                         type: TemplateFieldType.STRING,
    30                         preferredIdentifier: true,
    31                         comment: 'The name you give here is used to discern this assay within the study (e.g. \'liver transcriptomics\', \'blood lipidomics\')',
    32                         required: true
    33                 ),
    34                 new TemplateField(
    35                         name: 'module',
    36                         type: TemplateFieldType.MODULE,
    37                         comment: 'Select the dbNP module where the actual assay measurement data is stored',
    38                         required: true
    39                 )
    40         ]
     22    /**
     23     * return the domain fields for this domain class
     24     * @return List
     25     */
     26    static List<TemplateField> giveDomainFields() { return Assay.domainFields }
    4127
    42         // An Assay always belongs to one study.
    43         static belongsTo = [parent: Study]
     28    static List<TemplateField> domainFields = [
     29            new TemplateField(
     30                    name: 'name',
     31                    type: TemplateFieldType.STRING,
     32                    preferredIdentifier: true,
     33                    comment: 'The name you give here is used to discern this assay within the study (e.g. \'liver transcriptomics\', \'blood lipidomics\')',
     34                    required: true
     35            ),
     36            new TemplateField(
     37                    name: 'module',
     38                    type: TemplateFieldType.MODULE,
     39                    comment: 'Select the dbNP module where the actual assay measurement data is stored',
     40                    required: true
     41            )
     42    ]
    4443
    45         // An Assay can have many samples on which it is performed, but all samples should be within the 'parent' Study.
    46         static hasMany = [samples: Sample]
     44    // An Assay always belongs to one study.
     45    static belongsTo = [parent: Study]
    4746
    48         static constraints = {
    49                 assayUUID(nullable:true, unique: true)
    50         }
     47    // An Assay can have many samples on which it is performed, but all samples should be within the 'parent' Study.
     48    static hasMany = [samples: Sample]
     49
     50    static constraints = {
     51        assayUUID(nullable: true, unique: true)
     52    }
    5153
    5254    static mapping = {
     
    5456
    5557        // Workaround for bug http://jira.codehaus.org/browse/GRAILS-6754
    56         templateTextFields type: 'text'
     58        templateTextFields type: 'text'
    5759    }
    5860
    59         def String toString() {
    60                 return name;
    61         }
     61    def String toString() {
     62        return name;
     63    }
    6264
    6365    def getToken() {
    64                 return giveUUID()
     66        return giveUUID()
    6567    }
    66        
    67         /**
    68          * Basic equals method to check whether objects are equals, by comparing the ids
    69          * @param o             Object to compare with
    70          * @return              True iff the id of the given Study is equal to the id of this Study
    71          */
    72         public boolean equals( Object o ) {
    73                 if( o == null )
    74                         return false;
    7568
    76                 if( !( o instanceof Assay ) )
    77                         return false
     69    /**
     70     * Basic equals method to check whether objects are equals, by comparing the ids
     71     * @param o Object to compare with
     72     * @return True iff the id of the given Study is equal to the id of this Study
     73     */
     74    public boolean equals(Object o) {
     75        if (o == null)
     76            return false;
    7877
    79                 Assay s = (Assay) o;
     78        if (!(o instanceof Assay))
     79            return false
    8080
    81                 return this.id == s.id
    82         }
    83        
    84         /**
    85          * Returns the UUID of this sample and generates one if needed
    86          */
    87         public String giveUUID() {
    88                 if( !this.assayUUID ) {
    89                         this.assayUUID = UUID.randomUUID().toString();
    90                         if( !this.save(flush:true) ) {
    91                                 log.error "Couldn't save assay UUID: " + this.getErrors();
    92                         }
    93                 }
    94                
    95                 return this.assayUUID;
    96         }
     81        Assay s = (Assay) o;
     82
     83        return this.id == s.id
     84    }
     85
     86    /**
     87     * Returns the UUID of this sample and generates one if needed
     88     */
     89    public String giveUUID() {
     90        if (!this.assayUUID) {
     91            this.assayUUID = UUID.randomUUID().toString();
     92            if (!this.save(flush: true)) {
     93                log.error "Couldn't save assay UUID: " + this.getErrors();
     94            }
     95        }
     96
     97        return this.assayUUID;
     98    }
    9799}
  • trunk/grails-app/services/api/ApiService.groovy

    r2199 r2200  
    2323    static final String API_SECRET = "th!s_sH0uld^Pr0bab7y_m0v3_t%_th3_uSeR_d0Ma!n_ins7ead!"
    2424    static transactional = false
     25
    2526    def moduleCommunicationService
    2627
     
    3233     */
    3334    def validateRequest(String deviceID, String validation) {
     35        return true
     36
    3437        // disable validation check on development and ci
    3538        if (['development', 'ci'].contains(grails.util.GrailsUtil.environment)) {
     
    7780                item['token'] = it.getToken()
    7881            } else if (it.respondsTo('giveUUID')) {
    79                 // ...while other implement giveUUID
     82                // ...while others implement giveUUID
    8083                item['token'] = it.giveUUID()
    8184            } else {
    82                 // and others don't at all... :S
     85                // and others don't at all, so far
     86                // the consistency...
    8387                item['id'] = it.id
    8488            }
Note: See TracChangeset for help on using the changeset viewer.