Changeset 2184 for trunk/grails-app/controllers
- Timestamp:
- Mar 28, 2012, 12:55:20 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/api/ApiController.groovy
r2181 r2184 1 1 /** 2 * Api ControllerControler2 * Api Controler 3 3 * 4 * Description of my controller 4 * API for third party applications to interact 5 * with GSCF 5 6 * 6 7 * @author your email (+name?) 7 * @since 2010mmdd 8 * @package ??? 8 * @since 20120328ma 9 9 * 10 10 * Revision information: … … 19 19 import dbnp.studycapturing.Study 20 20 import dbnp.authentication.SecUser 21 import org.dbnp.gdt.TemplateFieldType 21 22 22 23 class ApiController { … … 71 72 @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) 72 73 def getStudies = { 74 println "api::getStudies: ${params}" 75 73 76 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 74 77 String validation = (params.containsKey('validation')) ? params.validation : '' … … 86 89 // get result data 87 90 studies[ studies.size() ] = [ 91 'token' : study.getToken(), 88 92 'title' : study.title, 89 93 'description' : study.description, … … 118 122 } 119 123 } 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 } 120 228 }
Note: See TracChangeset
for help on using the changeset viewer.