-
Notifications
You must be signed in to change notification settings - Fork 12
Add serializer for DataType retrieval #36
base: gb-dev
Are you sure you want to change the base?
Changes from 3 commits
8b60750
be77f41
4dbaae6
5dd040b
4a4784e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ package org.transmartproject.rest | |
import grails.validation.Validateable | ||
import groovy.json.JsonException | ||
import groovy.json.JsonSlurper | ||
import org.apache.commons.lang.NullArgumentException | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.transmartproject.core.exceptions.InvalidArgumentsException | ||
import org.transmartproject.core.exceptions.NoSuchResourceException | ||
import org.transmartproject.db.RestExportService | ||
|
||
class ExportController { | ||
|
@@ -29,29 +29,27 @@ class ExportController { | |
* Returns datatypes and patient number of given concepts. | ||
* | ||
*/ | ||
def datatypes(){ | ||
def jsonSlurper = new JsonSlurper() | ||
def datatypes() throws NoSuchResourceException { | ||
if (!(params.containsKey('concepts'))){ | ||
throw new NoSuchElementException( | ||
"No parameter named concepts." | ||
) | ||
throw new NoSuchResourceException("No parameter named concepts was given.") | ||
} | ||
if (params.get('concepts') == "") { | ||
throw new InvalidArgumentsException("Parameter concepts has no value.") | ||
} | ||
def test = params.get('concepts').decodeURL() | ||
def jsonSlurper = new JsonSlurper() | ||
def conceptParameters = params.get('concepts').decodeURL() | ||
try { | ||
def concept_arguments = jsonSlurper.parseText(test) | ||
if (concept_arguments==null){ | ||
throw new NullArgumentException( | ||
"Parameter concepts has no value." | ||
) | ||
} | ||
List datatypes = [] | ||
concept_arguments.each { it -> | ||
def conceptArguments = jsonSlurper.parseText(conceptParameters) | ||
List dataTypes = [] | ||
int cohortNumber = 1 | ||
conceptArguments.each { it -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is quite some logics in this call, preferably you would put as much transformation and business logic in a Service. Leaving Http handling and top level control flow to the Controler |
||
List conceptKeysList = it.conceptKeys | ||
datatypes += restExportService.getDataTypes(conceptKeysList) | ||
dataTypes = restExportService.getDataTypes(conceptKeysList, dataTypes, cohortNumber) | ||
cohortNumber += 1 | ||
} | ||
respond(restExportService.formatDataTypes(datatypes)) | ||
respond(dataTypes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stylistic remark: in this call you use brackets - in the other one you use groovy bracketless call (index call). This is inconsistent, would look better to use just one way everywhere. (but this is not a big deal) |
||
} catch(JsonException e){ | ||
"Given value was non valid JSON." | ||
throw new InvalidArgumentsException("Given parameter was non valid JSON.") | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,10 @@ class PatientSetController { | |
* GET /patient_sets | ||
*/ | ||
def index() { | ||
respond queriesResource.getQueryResultsSummaryByUsername(currentUser.getUsername() ) | ||
String noPatients = "There are no saved patient sets by $currentUser.username" | ||
def patientSets = queriesResource.getQueryResultsSummaryByUsername(currentUser.getUsername() ) | ||
patientSets = patientSets == null ? noPatients : patientSets | ||
respond patientSets | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it wrap the noPatients string in a JSON object here? |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.transmartproject.rest.marshallers | ||
|
||
import org.transmartproject.export.DataTypeRetrieved | ||
|
||
class DataTypeSerializationHelper extends AbstractHalOrJsonSerializationHelper<DataTypeRetrieved>{ | ||
|
||
final Class targetType = DataTypeRetrieved | ||
final String collectionName = 'dataTypeRetrieved' | ||
|
||
@Override | ||
Map<String, Object> convertToMap(DataTypeRetrieved dataTypeRetrieved) { | ||
def cohortInfoList = [] | ||
def cohortsMap = [:] | ||
dataTypeRetrieved.OntologyTermsMap.each { ID, terms -> | ||
terms.collect { term -> | ||
if (ID in cohortsMap.keySet()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you describe the desired control flow here? I am not sure if I get it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The frontend wants to know which concepts are in which cohort. Now a cohort can only contain one study, so the most logical thing to do would be to differentiate between studies. However in the near future cross studies will be added, which would result in this serialiser not functioning properly. So there needed to be a way to easily differentiate between the cohorts. This way it also supports multiple cohorts, which is also planned for the future. So based on (cohort)ID I create the structure for cohorts. |
||
cohortsMap[ID].add([subjects: term.patients.collect({ it.id }), conceptPath: term.fullName]) | ||
} else { | ||
cohortsMap[ID] = [[subjects: term.patients.collect({ it.id }), conceptPath: term.fullName]] | ||
} | ||
} | ||
} | ||
cohortsMap.each{ key, value -> | ||
cohortInfoList.add([concepts:value]) | ||
} | ||
def datatypeMap = [dataType:dataTypeRetrieved.dataType, | ||
dataTypeCode: dataTypeRetrieved.dataTypeCode, | ||
cohorts:cohortInfoList] | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when it is null? You can also use StringUtils.isEmpty() from apache.commons.lang to test for both in one go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it with "/v1/export/datatypes?concepts=" and "/v1/export/datatypes?concepts". Both return "Parameter concepts has no value."