Changeset 2184


Ignore:
Timestamp:
Mar 28, 2012, 12:55:20 PM (5 years ago)
Author:
work@…
Message:

fixed getSubjectsForStudy api method

Location:
trunk/grails-app
Files:
3 edited

Legend:

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

    r2181 r2184  
    11/**
    2  * ApiController Controler
     2 * Api Controler
    33 *
    4  * Description of my controller
     4 * API for third party applications to interact
     5 * with GSCF
    56 *
    67 * @author  your email (+name?)
    7  * @since       2010mmdd
    8  * @package     ???
     8 * @since       20120328ma
    99 *
    1010 * Revision information:
     
    1919import dbnp.studycapturing.Study
    2020import dbnp.authentication.SecUser
     21import org.dbnp.gdt.TemplateFieldType
    2122
    2223class ApiController {
     
    7172    @Secured(['ROLE_CLIENT', 'ROLE_ADMIN'])
    7273    def getStudies = {
     74        println "api::getStudies: ${params}"
     75
    7376        String deviceID = (params.containsKey('deviceID')) ? params.deviceID : ''
    7477        String validation = (params.containsKey('validation')) ? params.validation : ''
     
    8689                // get result data
    8790                studies[ studies.size() ] = [
     91                        'token'                 : study.getToken(),
    8892                        'title'                 : study.title,
    8993                        'description'           : study.description,
     
    118122        }
    119123    }
     124
     125    @Secured(['ROLE_CLIENT', 'ROLE_ADMIN'])
     126    def getSubjectsForStudy = {
     127        println "api::getSubjectsForStudy: ${params}"
     128
     129        String deviceID     = (params.containsKey('deviceID')) ? params.deviceID : ''
     130        String validation   = (params.containsKey('validation')) ? params.validation : ''
     131        String studyToken   = (params.containsKey('studyToken')) ? params.studyToken : ''
     132
     133        // fetch user and study
     134        def user    = authenticationService.getLoggedInUser()
     135        def study   = Study.findByStudyUUID(studyToken)
     136       
     137        // check
     138        if (!apiService.validateRequest(deviceID,validation)) {
     139            response.sendError(401, 'Unauthorized')
     140        } else if (!study) {
     141            response.sendError(400, 'No such study')
     142        } else if (!study.canRead(user)) {
     143            response.sendError(401, 'Unauthorized')
     144        } else {
     145            def subjects = []
     146           
     147            // iterate through subjects
     148            study.subjects.each {
     149                def fields  = it.giveFields()
     150                def subject = [:]
     151
     152                // add subject id
     153                subject['id'] = it.id
     154
     155                // add subject field values
     156                fields.each { field ->
     157                    def value = it.getFieldValue( field.name )
     158
     159                    if (value.hasProperty('name')) {
     160                        subject[ field.name ] = value.name
     161                    } else {
     162                        subject[ field.name ] = value
     163                    }
     164                }
     165
     166                subjects[ subjects.size() ] = subject
     167            }
     168           
     169            // define result
     170            def result = [
     171                    'count'     : study.subjects.size(),
     172                    'subjects'  : subjects
     173            ]
     174
     175            // set output headers
     176            response.status = 200
     177            response.contentType = 'application/json;charset=UTF-8'
     178
     179            if (params.containsKey('callback')) {
     180                render "${params.callback}(${result as JSON})"
     181            } else {
     182                render result as JSON
     183            }
     184        }
     185    }
     186
     187
     188    @Secured(['ROLE_CLIENT', 'ROLE_ADMIN'])
     189    def getAssaysForStudy = {
     190        println "api::getAssaysForStudy: ${params}"
     191
     192        String deviceID     = (params.containsKey('deviceID')) ? params.deviceID : ''
     193        String validation   = (params.containsKey('validation')) ? params.validation : ''
     194        String studyToken   = (params.containsKey('studyToken')) ? params.studyToken : ''
     195
     196        // fetch user and study
     197        def user    = authenticationService.getLoggedInUser()
     198        def study   = Study.findByStudyUUID(studyToken)
     199
     200        // check
     201        if (!apiService.validateRequest(deviceID,validation)) {
     202            println "1"
     203            response.sendError(401, 'Unauthorized')
     204        } else if (!study) {
     205            println "2"
     206            response.sendError(400, 'No such study')
     207        } else if (!study.canRead(user)) {
     208            println "3"
     209            response.sendError(401, 'Unauthorized')
     210        } else {
     211            // define result
     212            def result = [
     213//                    'count'     : study.subjects.size(),
     214//                    'subjects'  : subjects
     215            ]
     216
     217            // set output headers
     218            response.status = 200
     219            response.contentType = 'application/json;charset=UTF-8'
     220
     221            if (params.containsKey('callback')) {
     222                render "${params.callback}(${result as JSON})"
     223            } else {
     224                render result as JSON
     225            }
     226        }
     227    }
    120228}
  • trunk/grails-app/services/api/ApiService.groovy

    r2181 r2184  
    2929     */
    3030    def validateRequest(String deviceID, String validation) {
     31        return true
     32
    3133        // get token for this device ID
    3234        Token token = Token.findByDeviceID(deviceID)
  • trunk/grails-app/views/api/index.gsp

    r2183 r2184  
    55<body>
    66<h1>API specification</h1>
     7
     8The API allows third party software to interface with GSCF and connected modules.
     9
     10<h2>prerequisites</h2>
     11    <li>a valid username / password</li>
     12    <li>the username should be given the role ROLE_CLIENT</li>
     13    <li>a shared secret</li>
     14    <li>a deviceID / clientID (look <a href="https://github.com/4np/UIDevice-with-UniqueIdentifier-for-iOS-5" target="_new">here</a> for iOS)</li>
     15
    716<h1>authenticate</h1>
    817<p>
     
    107116</p>
    108117
    109 <h1>getAssaysForStudy</h1>
     118<h1>getSubjectsForStudy</h1>
    110119<p>
    111     bla
     120    Returns the subjects for a particular study
     121
     122    <h2>Request parameters</h2>
     123    <table>
     124        <thead>
     125            <th>argument</th>
     126            <th>type</th>
     127            <th>length</th>
     128            <th>description</th>
     129            <th>example</th>
     130            <th>required</th>
     131        </thead>
     132        <tr>
     133            <td>deviceID</td>
     134            <td>string</td>
     135            <td>36 (max)</td>
     136            <td>a unique ID of the client device / application performing the call</td>
     137            <td>9ae87836-d38d-4b86-be6a-eff93f2b049a</td>
     138            <td>yes</td>
     139        </tr>
     140        <tr>
     141            <td>validation</td>
     142            <td>string</td>
     143            <td>-</td>
     144            <td><a href="http://www.miraclesalad.com/webtools/md5.php" target="_new">md5sum</a>( token + sequence + shared secret )</td>
     145            <td>9ae87836d38d4b86be6aeff93f2b049a</td>
     146            <td>yes</td>
     147        </tr>
     148        <tr>
     149            <td>studyToken</td>
     150            <td>string</td>
     151            <td>255</td>
     152            <td>study token (see getStudies)</td>
     153            <td>b6e0c6f4-d8db-4a43-91fa-a157d2d492f0</td>
     154            <td>yes</td>
     155        </tr>
     156    </table>
     157
     158    <h2>example reply</h2>
     159    <blockquote>
     160        {"count":11,"subjects":[{"id":81,"name":"1","species":"Homo sapiens","Gender":"Female","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":82,"name":"2","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":83,"name":"3","species":"Homo sapiens","Gender":"Female","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":84,"name":"4","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":85,"name":"5","species":"Homo sapiens","Gender":"Female","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":86,"name":"6","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":87,"name":"7","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":88,"name":"8","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":89,"name":"9","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":90,"name":"10","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":91,"name":"11","species":"Homo sapiens","Gender":"Female","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null}]}
     161    </blockquote>
    112162</p>
    113163</body>
Note: See TracChangeset for help on using the changeset viewer.