Changeset 1010
- Timestamp:
- Oct 28, 2010, 4:39:45 PM (13 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/RestController.groovy
r1001 r1010 21 21 import grails.converters.* 22 22 import nl.metabolomicscentre.dsp.http.BasicAuthentication 23 import dbnp.rest.common.CommunicationManager 23 24 24 25 … … 32 33 def beforeInterceptor = [action:this.&auth,except:["isUser"]] 33 34 def credentials 34 def requestUser // = SecUser.findByName( "user" )35 def requestUser //= SecUser.findByUsername( "user" ) 35 36 36 37 /** … … 139 140 * If the user is not allowed to read the study contents, a 401 error is given 140 141 * 141 * Example call of the getAssays REST resource:142 * http://localhost:8080/gscf/rest/getAssays?studyToken=PPSH&moduleURL=http://localhost:8182/sam143 *144 142 * @param studyToken String The external study id (code) of the target GSCF Study object 145 143 * @param moduleURL String The base URL of the calling dbNP module 146 144 * @param consumer consumer name of the calling module 147 * @param token token for the authenticated user (e.g. session_id)148 145 * @return list of assays in the study as JSON object list, filtered to only contain assays 149 146 * 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. 150 175 */ 151 176 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 153 187 if( params.studyToken ) { 188 154 189 def id = params.studyToken 155 190 def study = Study.find( "from Study as s where s.code=?", [id] ) … … 162 197 } 163 198 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 -> 165 219 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 } 168 233 } 169 234 } 170 } 235 } 236 171 237 } 172 render assays as JSON 173 } 238 239 render returnList as JSON 240 } 241 242 243 244 245 246 174 247 175 248 /** … … 310 383 311 384 312 /**313 * REST resource for dbNP modules.314 *315 * @param assayToken String, the external identifier of the study316 * @param consumer consumer name of the calling module317 * @param token token for the authenticated user (e.g. session_id)318 * @return List of all fields of this assay319 *320 * Example REST call (without authentication):321 * http://localhost:8080/gscf/rest/getAssay/assay?assayToken=PPS3_SAM322 *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.name334 def value = assay.getFieldValue( name )335 items[name] = value336 }337 items["parentStudyToken"] = assay.parent.getToken()338 }339 }340 render items as JSON341 }342 343 344 385 345 386 /** -
trunk/src/groovy/dbnp/rest/common/CommunicationManager.groovy
r934 r1010 4 4 import java.net.URLEncoder 5 5 import org.codehaus.groovy.grails.web.json.* 6 6 7 7 8 /** CommunicationManager … … 264 265 265 266 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 266 284 267 285 }
Note: See TracChangeset
for help on using the changeset viewer.