Changeset 1813
- Timestamp:
- May 5, 2011, 1:01:04 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/RestController.groovy
r1781 r1813 26 26 class RestController { 27 27 28 29 30 28 /**************************************************/ 29 /** Rest resources for Simple Assay Module (SAM) **/ 30 /**************************************************/ 31 31 32 32 def authenticationService … … 92 92 93 93 /** 94 95 96 94 * REST resource for data modules. 95 * Consumer and token should be supplied via URL parameters. 96 * Provide a list of all studies owned by the supplied user. 97 97 * 98 98 * @param studyToken optional parameter. If no studyToken is given, all studies available to user are returned. … … 137 137 def getStudies = { 138 138 139 List returnStudies = [] 140 List studies = [] 139 List returnStudies = [] 140 List studies = [] 141 141 142 142 if( !params.studyToken ) { … … 144 144 } 145 145 else if( params.studyToken instanceof String ) { 146 def study = Study.findByStudyUUID( params.studyToken ) 146 def study = Study.findByStudyUUID( params.studyToken ) 147 147 if( study ) { 148 148 if( !study.canRead(authenticationService.getRemotelyLoggedInUser( params.consumer, params.token )) ) { … … 150 150 return false 151 151 } 152 153 studies.push study 152 153 studies.push study 154 154 } else { 155 155 response.sendError(404) 156 156 return false 157 157 } 158 159 } 160 else { 158 159 } 160 else { 161 161 params.studyToken.each{ studyToken -> 162 162 def study = Study.findByStudyUUID( studyToken ); 163 163 if( study ) 164 studies.push study 165 } 166 } 167 164 studies.push study 165 } 166 } 167 168 168 169 169 studies.each { study -> … … 173 173 if( study.canRead(authenticationService.getRemotelyLoggedInUser( params.consumer, params.token ))) { 174 174 175 176 177 178 179 180 181 182 // Add study version number 175 def items = [studyToken:study.giveUUID()] 176 study.giveFields().each { field -> 177 def name = field.name 178 def value = study.getFieldValue( name ) 179 items[name] = value 180 } 181 182 // Add study version number 183 183 items['version'] = study.version; 184 185 186 184 185 returnStudies.push items 186 } 187 187 } 188 188 } … … 217 217 def versionInfo = [:]; 218 218 def study 219 219 220 220 if( !params.studyToken || !(params.studyToken instanceof String)) { 221 221 response.sendError(400) … … 256 256 */ 257 257 def getSubjects = { 258 List subjects = [] 258 List subjects = [] 259 259 if( params.studyToken ) { 260 260 def study = Study.findByStudyUUID( params.studyToken) 261 261 262 262 if(study) { … … 292 292 * @return list of assays in the study as JSON object list, filtered to only contain assays 293 293 * for the specified module, with 'assayToken' and 'name' for each assay 294 295 296 297 294 * 295 * 296 * Example 1. REST call without assayToken 297 * http://localhost:8080/gscf/rest/getAssays/aas?studyToken=PPSH 298 298 * &consumer=http://localhost:8182/sam 299 300 299 * 300 * Result: [{"name":"Glucose assay after", 301 301 * "module":{"class":"dbnp.studycapturing.AssayModule","id":1,"name":"SAM module for clinical data", 302 302 * "platform":"clinical measurements","url":"http://localhost:8182/sam"}, … … 306 306 * "platform":"clinical measurements","url":"http://localhost:8182/sam"}, 307 307 * "externalAssayID":"PPSH-Glu-B","Description":null,"parentStudyToken":"PPSH"}] 308 309 310 311 308 * 309 * 310 * Example 2. REST call with one assayToken 311 * http://localhost:8080/gscf/rest/getAssays/queryOneTokenz?studyToken=PPSH 312 312 * &consumer=http://localhost:8182/sam&assayToken=PPSH-Glu-A 313 313 * 314 314 * Result: [{"name":"Glucose assay after","module":{"class":"dbnp.studycapturing.AssayModule","id":1, 315 315 * "name":"SAM module for clinical data","platform":"clinical measurements","url":"http://localhost:8182/sam"}, 316 316 * "externalAssayID":"PPSH-Glu-A","Description":null,"parentStudyToken":"PPSH"}] 317 317 * 318 319 320 * 321 318 * 319 * Example 3. REST call with two assayTokens. 320 * 321 * Result: Same as result in Example 1. 322 322 */ 323 323 def getAssays = { … … 325 325 response.contentType = 'application/json' 326 326 327 List returnList = [] // return list of hashes each containing fields and values belonging to an assay 328 329 // Check if required parameters are present 330 def validCall = CommunicationManager.hasValidParams( params, "consumer" , "studyToken")331 if( !validCall ) { 327 List returnList = [] // return list of hashes each containing fields and values belonging to an assay 328 329 // Check if required parameters are present 330 def validCall = CommunicationManager.hasValidParams( params, "consumer" ) 331 if( !validCall ) { 332 332 render "Error. Wrong or insufficient parameters." as JSON 333 333 return 334 334 } 335 335 336 def assays = [] 337 336 338 if( params.studyToken ) { 337 339 338 340 def study = Study.findByStudyUUID(params.studyToken) 339 341 340 342 if(study) { … … 345 347 } 346 348 347 def assays = []348 349 if(params.assayToken==null) { 349 350 assays = study.assays 350 351 } 351 else if( params.assayToken instanceof String ) { 352 else if( params.assayToken instanceof String ) { 352 353 def assay = study.assays.find{ it.giveUUID() == params.assayToken } 353 354 if( assay ) { 354 355 assays.push assay 355 356 } 356 357 } … … 364 365 } 365 366 366 assays.each{ assay -> 367 if (assay.module?.url && assay.module.url.equals(params.moduleURL)) { 368 if(assay) { 369 def map = [assayToken : assay.giveUUID()] 370 assay.giveFields().each { field -> 371 def name = field.name 372 def value = assay.getFieldValue( name ) 373 map[name] = value 374 } 375 map["parentStudyToken"] = assay.parent.giveUUID() 376 returnList.push( map ) 377 } 378 } 379 } 380 } else { 367 } else { 381 368 response.sendError(404) 382 369 return false 383 370 } 384 371 385 } 372 } else { 373 // Return all assays for the given module 374 assays = Assay.list().findAll{ it.parent.canRead(authenticationService.getRemotelyLoggedInUser( params.consumer, params.token ) ) } 375 } 376 377 // Create data for all assays 378 assays.each{ assay -> 379 if (assay.module?.url && assay.module.url.equals(params.moduleURL)) { 380 if(assay) { 381 def map = [assayToken : assay.giveUUID()] 382 assay.giveFields().each { field -> 383 def name = field.name 384 def value = assay.getFieldValue( name ) 385 map[name] = value 386 } 387 map["parentStudyToken"] = assay.parent.giveUUID() 388 returnList.push( map ) 389 } 390 } 391 } 392 386 393 render returnList as JSON 387 394 } … … 405 412 * @return 'startTime' (the time the sample was taken relative to the start of the study, as a string) 406 413 * @return additional template fields are returned 407 408 409 410 414 * 415 * 416 * 417 * Example 1: no sampleTokens given. 411 418 * Query: 412 413 419 * http://localhost:8080/gscf/rest/getSamples/query?assayToken=PPSH-Glu-A 420 * 414 421 * Result: 415 422 * [{"sampleToken":"5_A","material":"blood plasma","subject":"5","event":"Blood extraction","startTime":"4 days, 6 hours"}, 416 423 * {"sampleToken":"6_A","material":"blood plasma","subject":"6","event":"Blood extraction","startTime":"4 days, 6 hours"}, 417 424 * {"sampleToken":"10_A","material":"blood plasma","subject":"10","event":"Blood extraction","startTime":"4 days, 6 hours"}, … … 424 431 * {"sampleToken":"7_A","material":"blood plasma","subject":"7","event":"Blood extraction","startTime":"4 days, 6 hours"}, 425 432 * {"sampleToken":"3_A","material":"blood plasma","subject":"3","event":"Blood extraction","startTime":"4 days, 6 hours"}] 426 427 428 429 433 * 434 * 435 * 436 * Example 2: one sampleToken given. 430 437 * Query: 431 438 * http://localhost:8080/gscf/rest/getSamples/query?assayToken=PPSH-Glu-A&sampleToken=5_A 432 433 439 * 440 * Result: 434 441 * [{"sampleToken":"5_A","material":"blood plasma","subject":"5","event":"Blood extraction","startTime":"4 days, 6 hours"}] 435 436 437 438 442 * 443 * 444 * 445 * Example 3: two sampleTokens given. 439 446 * Query: 440 447 * http://localhost:8080/gscf/rest/getSamples/query?assayToken=PPSH-Glu-A&sampleToken=5_A&sampleToken=6_A 441 442 448 * 449 * Result: 443 450 * [{"sampleToken":"5_A","material":"blood plasma","subject":"5","event":"Blood extraction","startTime":"4 days, 6 hours"}, 444 451 * {"sampleToken":"6_A","material":"blood plasma","subject":"6","event":"Blood extraction","startTime":"4 days, 6 hours"}] 445 452 * 446 453 * 447 454 * Example 4: no assaytoken given 448 455 * Query: 449 456 * http://localhost:8080/gscf/rest/getSamples/query?sampleToken=5_A&sampleToken=6_A 450 451 457 * 458 * Result: 452 459 * [{"sampleToken":"5_A","material":"blood plasma","subject":"5","event":"Blood extraction","startTime":"4 days, 6 hours"}, 453 460 * {"sampleToken":"6_A","material":"blood plasma","subject":"6","event":"Blood extraction","startTime":"4 days, 6 hours"}] … … 458 465 def samples 459 466 if( params.assayToken ) { 460 467 def assay = Assay.findByAssayUUID( params.assayToken ); 461 468 462 469 if( assay ) { … … 466 473 return false 467 474 } 468 475 469 476 samples = assay.getSamples() // on all samples 470 477 } else { … … 478 485 samples = studies*.getSamples().flatten(); 479 486 } 480 487 481 488 // Check whether only a subset of samples should be returned 482 489 if( params.sampleToken ) { 483 490 def sampleTokens = params.list( "sampleToken" ); 484 samples = samples.findAll { sampleTokens.contains( it.giveUUID() ) } 491 samples = samples.findAll { sampleTokens.contains( it.giveUUID() ) } 485 492 } 486 493 487 494 samples.each { sample -> 488 495 489 def item = [ 490 'sampleToken' : sample.giveUUID(),491 'material' : sample.material?.name,492 'subject' : sample.parentSubject?.name,493 'event' : sample.parentEvent?.template?.name,494 'startTime' : sample.parentEvent?.getStartTimeString()495 ]496 def item = [ 497 'sampleToken' : sample.giveUUID(), 498 'material' : sample.material?.name, 499 'subject' : sample.parentSubject?.name, 500 'event' : sample.parentEvent?.template?.name, 501 'startTime' : sample.parentEvent?.getStartTimeString() 502 ] 496 503 497 504 sample.giveFields().each { field -> … … 514 521 } 515 522 } 516 item['eventObject'] = eventHash 523 item['eventObject'] = eventHash 517 524 } 518 525 … … 527 534 } 528 535 } 529 item['subjectObject'] = subject 530 } 531 532 items.push item 536 item['subjectObject'] = subject 537 } 538 539 items.push item 533 540 } 534 541 … … 552 559 def getAuthorizationLevel = { 553 560 if( params.studyToken ) { 554 561 def study = Study.findByStudyUUID(params.studyToken) 555 562 556 563 if( !study ) { … … 571 578 return false 572 579 } 573 580 } 574 581 }
Note: See TracChangeset
for help on using the changeset viewer.