- Timestamp:
- Jan 28, 2011, 12:30:39 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/nl/tno/metagenomics/integration/RestController.groovy
r4 r9 38 38 /****************************************************************/ 39 39 private getMeasurementTypes() { 40 return [ "# sequences" , "average quality"]40 return [ "# sequences" ] 41 41 } 42 42 … … 274 274 def notifyStudyChange = { 275 275 def studyToken = params.studyToken 276 276 277 277 if( !studyToken ) { 278 278 response.sendError(400, "No studyToken given" ) 279 279 return 280 280 } 281 281 282 282 // Search for the changed study 283 283 def study = Study.findByStudyToken( studyToken ); 284 284 285 285 // If the study is not found, it is added in GSCF. Add a dummy (dirty) study, in order to 286 286 // update it immediately when asked for … … 288 288 log.info( "METAGENOMICS: GSCF notification for new study " + studyToken ); 289 289 study = new Study( 290 name: "",291 studyToken: studyToken,292 isDirty: true293 )290 name: "", 291 studyToken: studyToken, 292 isDirty: true 293 ) 294 294 } else { 295 295 log.info( "METAGENOMICS: GSCF notification for existing study " + studyToken ); … … 297 297 } 298 298 study.save(flush:true); 299 299 300 300 def jsonData = [ 'studyToken': studyToken, message: "Notify succesful" ]; 301 301 302 302 render jsonData as JSON 303 303 } 304 304 305 305 /** 306 306 * Return URL to view an assay. … … 322 322 // If the assay is not found, try synchronizing 323 323 synchronizationService.sessionToken = session.sessionToken 324 324 325 325 if( !assay ) { 326 326 synchronizationService.synchronizeStudies() 327 327 assay = Assay.findByAssayToken( assayToken ); 328 328 329 329 if( !assay ) { 330 330 response.sendError(404, "Not Found" ) … … 338 338 return 339 339 } 340 340 341 341 def url = [ 'url' : ConfigurationHolder.config.grails.serverURL + '/assay/show/' + assay.id.toString() ] 342 342 … … 344 344 } 345 345 } 346 346 347 347 /***************************************************/ 348 348 /* REST resources related to the querying in GSCF */ 349 349 /***************************************************/ 350 351 /** 352 * Retrieves a list of fields that could be queried when searching for a specific entity. 353 * 354 * The module is allowed to return different fields when the user searches for different entities 355 * 356 * Example call: [moduleurl]/rest/getQueryableFields?entity=Study&entity=Sample 357 * Example response: { "Study": [ "# sequences" ], "Sample": [ "# sequences", "# bacteria" ] } 358 * 359 * @param params.entity Entity that is searched for. Might be more than one. If no entity is given, 360 * a list of searchable fields for all entities is given 361 * @return JSON List with the names of the fields 362 */ 363 def getQueryableFields = { 364 // We don't really care about the entity. The only thing is that this module 365 // is only aware of studies, assays and samples, but doesn't know anything about 366 // subjects or events. If the user searches for those entities (maybe in the future) 367 // this module doesn't have anything to search for. 368 369 def entities = params.entity ?: [] 370 371 if( entities instanceof String ) 372 entities = [entities] 373 else 374 entities = entities.toList() 375 376 if( !entities ) 377 entities = [ "Study", "Assay", "Sample" ] 378 379 380 def fields = [:]; 381 entities.unique().each { entity -> 382 switch( entity ) { 383 case "Study": 384 case "Assay": 385 case "Sample": 386 fields[ entity ] = [ "# sequences" ] 387 break; 388 default: 389 // Do nothing 390 break; 391 } 392 } 393 394 render fields as JSON 395 } 396 397 /** 398 * Returns data for the given field and entities. 399 * 400 * Example call: [moduleurl]/rest/getQueryableFieldData?entity=Study&tokens=abc1&tokens=abc2&fields=# sequences&fields=# bacteria 401 * Example response: { "abc1": { "# sequences": 141, "# bacteria": 0 }, "abc2": { "#sequences": 412 } } 402 * 403 * @param params.entity Entity that is searched for 404 * @param params.tokens One or more tokens of the entities that the data should be returned for 405 * @param params.fields One or more field names of the data to be returned. 406 * @return JSON Map with keys being the entity tokens and the values being maps with entries [field] = [value]. Not all 407 * fields and tokens that are asked for have to be returned by the module (e.g. when a specific entity can 408 * not be found, or a value is not present for an entity) 409 */ 410 def getQueryableFieldData = { 411 def entity = params.entity; 412 def tokens = params.tokens ?: [] 413 def fields = params.fields ?: [] 414 415 if( tokens instanceof String ) 416 tokens = [tokens] 417 else 418 tokens = tokens.toList(); 419 420 if( fields instanceof String ) 421 fields = [fields] 422 else 423 fields = fields.toList(); 424 425 // Only search for unique tokens and fields 426 tokens = tokens.unique() 427 fields = fields.unique() 428 429 // Without tokens or fields we can only return an empty list 430 def map = [:] 431 if( tokens.size() == 0 || fields.size() == 0 ) { 432 log.trace "Return empty string for getQueryableFieldData: #tokens: " + tokens.size() + " #fields: " + fields.size() 433 render map as JSON 434 return; 435 } 436 437 tokens.each { token -> 438 def object = getQueryableObject( entity, token ); 439 if( object ) { 440 map[ token ] = [:] 441 fields.each { field -> 442 def v = getQueryableFieldValue( entity, object, field ); 443 if( v != null ) 444 map[ token ][ field ] = v 445 } 446 } else { 447 log.trace "No " + entity + " with token " + token + " found." 448 } 449 } 450 451 render map as JSON 452 } 453 454 /** 455 * Searches for a specific entity 456 * 457 * @param entity Entity to search in 458 * @param token Token of the entity to search in 459 * @return 460 */ 461 protected def getQueryableObject( def entity, def token ) { 462 switch( entity ) { 463 case "Study": 464 return Study.findByStudyToken( token ); 465 case "Assay": 466 return Assay.findByAssayToken( token ); 467 case "Sample": 468 return Sample.findBySampleToken( token ); 469 default: 470 // Other entities can't be handled 471 return null; 472 } 473 } 474 475 /** 476 * Searches for the value of a specific field in a specific entity 477 * 478 * @param entity Entity of the given object 479 * @param object Object to search in 480 * @param field Field value to retrieve 481 * @return 482 */ 483 protected def getQueryableFieldValue( def entity, def object, def field ) { 484 if( !entity || !object || !field ) 485 return null; 486 487 switch( entity ) { 488 case "Study": 489 switch( field ) { 490 // Returns the total number of sequences in this study 491 case "# sequences": 492 def assaySamples = object.assays*.assaySamples; 493 if( assaySamples ) { 494 assaySamples = assaySamples.flatten() 495 return assaySamples.collect { it.numSequences() }.sum(); 496 } else { 497 return null; 498 } 499 // Other fields are not handled 500 default: 501 return null; 502 } 503 case "Assay": 504 switch( field ) { 505 // Returns the total number of sequences in this study 506 case "# sequences": 507 def assaySamples = assay.assaySamples; 508 if( assaySamples ) { 509 return assaySamples.collect { it.numSequences() }.sum(); 510 } else { 511 return null; 512 } 513 // Other fields are not handled 514 default: 515 return null; 516 } 517 case "Sample": 518 switch( field ) { 519 // Returns the total number of sequences in this study 520 case "# sequences": 521 def assaySamples = sample.assaySamples; 522 if( assaySamples ) { 523 return assaySamples.collect { it.numSequences() }.sum(); 524 } else { 525 return null; 526 } 527 // Other fields are not handled 528 default: 529 return null; 530 } 531 default: 532 // Other entities can't be handled 533 return null; 534 } 535 } 350 536 351 537 /**
Note: See TracChangeset
for help on using the changeset viewer.