/**
* Retrieve an term by NCIt Code from NCI thesaurus via LEXEVS 6.1 API. This
* is used for retrieving intervention term as this API returns the type of
* the alternate names which is required for interventions in CTRP
*
* @param ncitCode
* @return {@link NCItTerm}, null
if term was not found
* @throws LEXEVSLookupException
* if there were errors invoking the web service
*/
private NCItTerm retrieveNCItTermViaLexEVS(String ncitCode) throws LEXEVSLookupException {
Element termEl = invokeWebService(lexAPIURL.replace("{CODE}", ncitCode));
if (termEl == null) {
return null;
}
NCItTerm term = new NCItTerm();
term.ncitCode = ncitCode;
try {
// term not found
if (getChildElementsByName(termEl, "queryResponse").size() == 0) {
return null;
}
List classList = getChildElementsByName(getChildElementsByName(termEl, "queryResponse").get(0),
ELEMENT_NAME_CLASS);
Element entity = null;
for (int i = 0; i < classList.size(); i++) {
Element tempEntity = (Element) classList.get(i);
if (tempEntity.getAttribute(ATTR_NAME_NAME).equals("org.LexGrid.concepts.Entity")) {
entity = (Element) classList.get(i);
}
}
// Parse presentations
List fieldList = getChildElementsByName(entity, ELEMENT_NAME_FIELD);
List presentations = null;
for (int i = 0; i < fieldList.size(); i++) {
if (((Element) fieldList.get(i)).getAttribute(ATTR_NAME_NAME).equals("_presentationList")) {
presentations = getChildElementsByName(fieldList.get(i), ELEMENT_NAME_CLASS);
break;
}
}
for (int j = 0; j < presentations.size(); j++) {
Element presentation = (Element) presentations.get(j);
boolean isPrefrred = false;
List fields = getChildElementsByName(presentation, ELEMENT_NAME_FIELD);
String value = null, type = null, source = null;
for (int k = 0; k < fields.size(); k++) {
if (((Element) fields.get(k)).getAttribute(ATTR_NAME_NAME).equals("_isPreferred")) {
if (fields.get(k).getChildNodes().getLength() > 0
&& fields.get(k).getChildNodes().item(0).getNodeValue() != null
&& fields.get(k).getChildNodes().item(0).getNodeValue().equals("true")) {
isPrefrred = true;
}
} else if (((Element) fields.get(k)).getAttribute(ATTR_NAME_NAME).equals("_sourceList")) {
List elementList = getChildElementsByName(fields.get(k), ELEMENT_NAME_CLASS);
if (elementList != null && elementList.size() > 0) {
List values = getChildElementsByName(
elementList.get(0), ELEMENT_NAME_FIELD);
for (int l = 0; l < values.size(); l++) {
if (((Element) values.get(l)).getAttribute(ATTR_NAME_NAME).equals("_content")
&& values.get(l).getChildNodes().getLength() > 0
&& values.get(l).getChildNodes().item(0).getNodeValue() != null) {
source = values.get(l).getChildNodes().item(0).getNodeValue().trim();
}
}
}
} else if (((Element) fields.get(k)).getAttribute(ATTR_NAME_NAME).equals("_value")) {
List values = getChildElementsByName(
getChildElementsByName(fields.get(k), ELEMENT_NAME_CLASS).get(0), ELEMENT_NAME_FIELD);
for (int l = 0; l < values.size(); l++) {
if (((Element) values.get(l)).getAttribute(ATTR_NAME_NAME).equals("_content")
&& values.get(l).getChildNodes().getLength() > 0
&& values.get(l).getChildNodes().item(0).getNodeValue() != null) {
value = values.get(l).getChildNodes().item(0).getNodeValue().trim();
}
}
} else if (((Element) fields.get(k)).getAttribute(ATTR_NAME_NAME).equals("_representationalForm")
&& fields.get(k).getChildNodes().getLength() > 0
&& fields.get(k).getChildNodes().item(0).getNodeValue() != null) {
type = fields.get(k).getChildNodes().item(0).getNodeValue().trim();
}
}
if (isPrefrred) {
term.preferredName = value;
} else if (NCItTermAlterName.ALTNAMECODEMAP.containsKey(type) || type == null) {
//if code is PT and source is not NCI then only we need to add this as synonym
if (type != null && type.equals("PT")) {
if (source != null && !source.equals("NCI")) {
term.alterNames.add(new NCItTermAlterName(value, type));
}
} else {
term.alterNames.add(new NCItTermAlterName(value, type));
}
}
}
// Parse properties
List properties = null;
fieldList = getChildElementsByName(entity, ELEMENT_NAME_FIELD);
for (int i = 0; i < fieldList.size(); i++) {
if (((Element) fieldList.get(i)).getAttribute(ATTR_NAME_NAME).equals("_propertyList")) {
properties = getChildElementsByName(fieldList.get(i), ELEMENT_NAME_CLASS);
}
}
if (properties != null) {
for (int j = 0; j < properties.size(); j++) {
Element prop = (Element) properties.get(j);
String value = null, type = null;
NodeList fields = prop.getElementsByTagName(ELEMENT_NAME_FIELD);
for (int k = 0; k < fields.getLength(); k++) {
if (((Element) fields.item(k)).getAttribute(ATTR_NAME_NAME).equals("_value")) {
NodeList values = ((Element) ((Element) fields.item(k)).
getElementsByTagName(ELEMENT_NAME_CLASS).item(0))
.getElementsByTagName(ELEMENT_NAME_FIELD);
for (int l = 0; l < values.getLength(); l++) {
if (((Element) values.item(l)).getAttribute(ATTR_NAME_NAME).equals("_content")
&& values.item(l).getChildNodes().getLength() > 0
&& values.item(l).getChildNodes().item(0).getNodeValue() != null) {
value = values.item(l).getChildNodes().item(0).getNodeValue().trim();
}
}
} else if (((Element) fields.item(k)).getAttribute(ATTR_NAME_NAME).equals("_propertyName")
&& fields.item(k).getChildNodes().getLength() > 0
&& fields.item(k).getChildNodes().item(0).getNodeValue() != null) {
type = fields.item(k).getChildNodes().item(0).getNodeValue().trim();
}
}
if (NCItTermAlterName.ALTNAMECODEMAP.containsKey(type)) {
term.alterNames.add(new NCItTermAlterName(value, type));
}
}
}
} catch (Exception e) {
throw new LEXEVSLookupException("Exception while parsing LEX EVS service response", e);
}
return term;
}
/**
* Retrieve a children terms of the given disease term
*
* @param ncitCode
* @return List of children terms
* @throws LEXEVSLookupException
* if there were errors invoking the web service
*/
private List retrieveDiseaseChildren(String ncitCode) throws LEXEVSLookupException {
List children = new ArrayList();
Element termEl = invokeWebService(lexEVSURL + ncitCode + "/children");
if (termEl == null) {
return children;
}
try {
List entries = getChildElementsByName(termEl, "entry");
// Get preferred Name and synonyms
for (Iterator iterator = entries.iterator(); iterator.hasNext();) {
Element entry = iterator.next();
String childCode = getChildElementsByName(getChildElementsByName(entry, CORE_NAME).get(0), CORE_NAME)
.get(0).getChildNodes().item(0).getNodeValue();
if (childCode.charAt(0) == 'C') {
String childName = getChildElementsByName(
getChildElementsByName(entry, "core:knownEntityDescription").get(0), "core:designation").get(0)
.getChildNodes().item(0).getNodeValue();
NCItTerm t = new NCItTerm();
t.ncitCode = childCode;
t.preferredName = childName;
children.add(t);
}
}
} catch (Exception e) {
throw new LEXEVSLookupException("Exception while parsing LEX EVS service response", e);
}
return children;
}
/**
* Retrieve a NCI term via LEX EVS CTS RESTful webservice
*
* @param ncitCode
* @return {@link NCItTerm}, null
if term was not found
* @throws LEXEVSLookupException
* if there were errors invoking the web service
*/
private NCItTerm retrieveNCItDiseaseTermViaLexEVSCTS(String ncitCode, boolean getParent)
throws LEXEVSLookupException {
Element termEl = invokeWebService(lexEVSURL + ncitCode);
if (termEl == null) { // Term not found
return null;
}
NCItTerm term = new NCItTerm();
term.ncitCode = ncitCode;
try {
// Term not found
if (getChildElementsByName(termEl, ENTITY).size() == 0) {
return null;
}
List designations = getChildElementsByName(
getChildElementsByName(getChildElementsByName(termEl, ENTITY).get(0), "namedEntity")
.get(0), "designation");
// Get preferred Name and synonyms
for (Iterator iterator = designations.iterator(); iterator.hasNext();) {
Element designation = iterator.next();
if ("PREFERRED".equals(designation.getAttribute("designationRole"))) {
term.preferredName = getChildElementsByName(designation, CORE_VALUE).get(0).getChildNodes().item(0)
.getNodeValue();
} else if ("ALTERNATIVE".equals(designation.getAttribute("designationRole"))) {
term.alterNames.add(new NCItTermAlterName(getChildElementsByName(designation, CORE_VALUE).get(0)
.getChildNodes().item(0).getNodeValue(), "Synonym"));
}
}
// get displayName
List displayProperty = getChildElementsByName(
getChildElementsByName(getChildElementsByName(termEl, ENTITY).get(0), "namedEntity")
.get(0), "property");
for (Iterator iterator = displayProperty.iterator(); iterator.hasNext();) {
Element property = iterator.next();
List predicate = getChildElementsByName(property, "core:predicate");
for (Iterator iterator1 = predicate.iterator(); iterator1.hasNext();) {
Element entry = iterator1.next();
NodeList optionList = entry.getElementsByTagName(CORE_NAME);
for (int j = 0; j < optionList.getLength(); ++j) {
Element option = (Element) optionList.item(j);
String optionText = option.getFirstChild().getNodeValue();
if ("Display_Name".equalsIgnoreCase(optionText)) {
List neededValues = getChildElementsByName(property, CORE_VALUE);
for (Iterator iterator2 = neededValues.iterator(); iterator2.hasNext();) {
Element entry1 = iterator2.next();
List finalOptionList = getChildElementsByName(entry1, "core:literal");
for (Iterator iterator3 = finalOptionList.iterator(); iterator3.hasNext();) {
term.displayName = getChildElementsByName(iterator3.next(), CORE_VALUE)
.get(0).getChildNodes().item(0)
.getNodeValue();
}
}
}
}
}
}
// Get parent terms
if (getParent) {
List parents = getChildElementsByName(
getChildElementsByName(getChildElementsByName(termEl, ENTITY).get(0), "namedEntity")
.get(0), "parent");
for (Iterator iterator = parents.iterator(); iterator.hasNext();) {
String parentCode = getChildElementsByName(iterator.next(), CORE_NAME).get(0).getChildNodes().item(0)
.getNodeValue();
if (parentCode.charAt(0) == 'C') {
NCItTerm parentTerm = retrieveNCItDiseaseTermViaLexEVSCTS(parentCode, false);
if (parentTerm != null) {
term.parentTerms.add(parentTerm);
}
}
}
}
} catch (Exception e) {
throw new LEXEVSLookupException("Exception while parsing LEX EVS service response", e);
}
return term;
}