Changeset 1010


Ignore:
Timestamp:
Oct 28, 2010, 4:39:45 PM (13 years ago)
Author:
j.saito@…
Message:

Ticket #57 - Unified getAssays and getAssay.

Also, added convenience method for checking the params object in RestControllers? in CommunicationManager?.

Location:
trunk
Files:
2 edited

Legend:

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

    r1001 r1010  
    2121import grails.converters.*
    2222import nl.metabolomicscentre.dsp.http.BasicAuthentication
     23import dbnp.rest.common.CommunicationManager
    2324
    2425
     
    3233        def beforeInterceptor = [action:this.&auth,except:["isUser"]]
    3334        def credentials
    34         def requestUser // = SecUser.findByName( "user" )
     35        def requestUser //= SecUser.findByUsername( "user" )
    3536
    3637        /**
     
    139140         * If the user is not allowed to read the study contents, a 401 error is given
    140141         *
    141          * Example call of the getAssays REST resource:
    142          * http://localhost:8080/gscf/rest/getAssays?studyToken=PPSH&moduleURL=http://localhost:8182/sam
    143          *
    144142         * @param       studyToken      String The external study id (code) of the target GSCF Study object
    145143         * @param       moduleURL       String The base URL of the calling dbNP module
    146144         * @param       consumer        consumer name of the calling module
    147          * @param       token           token for the authenticated user (e.g. session_id)
    148145         * @return list of assays in the study as JSON object list, filtered to only contain assays
    149146         *         for the specified module, with 'assayToken' and 'name' for each assay
     147         *
     148         *
     149         * Example 1. REST call without assayToken
     150         *            http://localhost:8080/gscf/rest/getAssays/aas?studyToken=PPSH
     151         *                              &moduleURL=http://localhost:8182/sam
     152         *
     153         * Result: [{"name":"Glucose assay after",
     154         *                      "module":{"class":"dbnp.studycapturing.AssayModule","id":1,"name":"SAM module for clinical data",
     155         *                              "platform":"clinical measurements","url":"http://localhost:8182/sam"},
     156         *                      "externalAssayID":"PPSH-Glu-A", "Description":null,"parentStudyToken":"PPSH"},
     157         *                      {"name":"Glucose assay before",
     158         *                              "module":{"class":"dbnp.studycapturing.AssayModule","id":1,"name":"SAM module for clinical data",
     159         *                              "platform":"clinical measurements","url":"http://localhost:8182/sam"},
     160         *                              "externalAssayID":"PPSH-Glu-B","Description":null,"parentStudyToken":"PPSH"}]
     161         *
     162         *
     163         * Example 2. REST call with one assayToken
     164         *                        http://localhost:8080/gscf/rest/getAssays/queryOneTokenz?studyToken=PPSH
     165         *                              &moduleURL=http://localhost:8182/sam&assayToken=PPSH-Glu-A
     166         *
     167         * Result: [{"name":"Glucose assay after","module":{"class":"dbnp.studycapturing.AssayModule","id":1,
     168         *                      "name":"SAM module for clinical data","platform":"clinical measurements","url":"http://localhost:8182/sam"},
     169         *                      "externalAssayID":"PPSH-Glu-A","Description":null,"parentStudyToken":"PPSH"}]
     170         *
     171         *
     172         * Example 3. REST call with two assayTokens.
     173         *
     174         * Result: Same as result in Example 1.
    150175         */
    151176        def getAssays = {
    152                 List assays = []
     177
     178                List returnList = []    // return list of hashes each containing fields and values belonging to an assay
     179
     180                // Check if required parameters are present
     181                def validCall = CommunicationManager.hasValidParams( params, "moduleURL", "studyToken" )
     182                if( !validCall ) {
     183                        render "Error. Wrong or insufficient parameters." as JSON
     184                        return
     185                }
     186
    153187                if( params.studyToken ) {
     188
    154189                        def id = params.studyToken
    155190                        def study = Study.find( "from Study as s where s.code=?", [id] )
     
    162197                                }
    163198
    164                                 study.assays.each{ assay ->
     199                                def assays = []
     200                                if(params.assayToken==null) {
     201                                        assays = study.assays
     202                                }
     203                                else if( params.assayToken instanceof String ) {
     204                                        def assay = study.assays.find{ it.externalAssayID==params.assayToken }
     205                                        if( assay ) {
     206                                                 assays.push assay
     207                                        }
     208                                }
     209                                else {                                                                                                  // there are multiple assayTokens instances
     210                                        params.assayToken.each { assayToken ->
     211                                                def assay = study.assays.find{ it.externalAssayID==assayToken }
     212                                                if(assay) {
     213                                                        assays.push assay
     214                                                }
     215                                        }
     216                                }
     217
     218                                assays.each{ assay ->
    165219                                        if (assay.module.url.equals(params.moduleURL)) {
    166                                                 def map = ['name':assay.name, 'assayToken':assay.getToken()]
    167                                                 assays.push( map )
     220                                                if(assay) {
     221                                                        def map = [:]
     222                                                        assay.giveFields().each { field ->
     223                                                                def name = field.name
     224                                                                def value = assay.getFieldValue( name )
     225                                                                if(field.name=='externalAssayID') {
     226                                                                        name = 'assayToken'
     227                                                                }
     228                                                                map[name] = value
     229                                                        }
     230                                                        map["parentStudyToken"] = assay.parent.getToken()
     231                                                        returnList.push( map )
     232                                                }
    168233                                        }
    169234                                }
    170                         }
     235                }
     236
    171237                }
    172                 render assays as JSON
    173         }
     238
     239                render returnList as JSON
     240        }
     241
     242
     243
     244
     245
     246
    174247
    175248        /**
     
    310383
    311384
    312         /**
    313          * REST resource for dbNP modules.
    314          *
    315          * @param       assayToken String, the external identifier of the study
    316          * @param       consumer        consumer name of the calling module
    317          * @param       token           token for the authenticated user (e.g. session_id)
    318          * @return List of all fields of this assay
    319          *
    320          * Example REST call (without authentication):
    321      * http://localhost:8080/gscf/rest/getAssay/assay?assayToken=PPS3_SAM
    322      *
    323          * Returns the JSON object: {"name":"Lipid profiling","module":{"class":"dbnp.studycapturing.AssayModule","id":1,
    324          * "name":"SAM module for clinical data","platform":"clinical measurements","url":"http://sam.nmcdsp.org"},
    325          * "assayToken":"PPS3_SAM","parentStudyToken":"PPS","Description":null}
    326          */
    327         def getAssay = {
    328                 def items = [:]
    329                 if( params.assayToken ) {
    330                         def assay = Assay.find( "from Assay as a where externalAssayID=?",[params.assayToken])
    331                         if(assay) {
    332                                 assay.giveFields().each { field ->
    333                                         def name = field.name
    334                                         def value = assay.getFieldValue( name )
    335                                         items[name] = value
    336                                 }
    337                                 items["parentStudyToken"] = assay.parent.getToken()
    338                         }
    339         }
    340                 render items as JSON
    341         }
    342 
    343 
    344385
    345386        /**
  • trunk/src/groovy/dbnp/rest/common/CommunicationManager.groovy

    r934 r1010  
    44import java.net.URLEncoder
    55import org.codehaus.groovy.grails.web.json.*
     6
    67
    78/**  CommunicationManager
     
    264265
    265266
     267    /**
     268     * Give list of missing parameters for a parameter call in a RestController.
     269     * 
     270     * @params params Map params        The parameter list required by this view.
     271     * @params requiredParamers                 List of parameter names that must be provided
     272     * @return true, if params has all required parameters, false otherwise
     273     */ 
     274        static String hasValidParams( params, Object [] requiredParams ) {
     275                def list = []
     276                requiredParams.each { p ->
     277                        if( !params[p] ) list.push p
     278                }
     279                if(list.size()>0) { return true }
     280                return false
     281        }
     282
     283
    266284
    267285}
Note: See TracChangeset for help on using the changeset viewer.