use of org.hl7.fhir.r4b.formats.IParser in project syndesis by syndesisio.
the class FhirTransactionCustomizer method beforeProducer.
public void beforeProducer(Exchange exchange) {
final Message in = exchange.getIn();
String body = in.getBody(String.class);
if (body != null) {
List<IBaseResource> resources = new ArrayList<>();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(body.getBytes("UTF-8")));
Node transactionElement = doc.getFirstChild();
NodeList childNodes = transactionElement.getChildNodes();
IParser parser = fhirContext.newXmlParser();
for (int i = 0; i < childNodes.getLength(); i++) {
Node resourceNode = childNodes.item(i);
Document resourceDocument = toDocument(resourceNode, dbf);
String resourceXml = toXml(resourceDocument);
IBaseResource resource = parser.parseResource(resourceXml);
resources.add(resource);
}
} catch (SAXException | IOException | ParserConfigurationException | TransformerException e) {
throw new RuntimeExchangeException("Cannot convert Transaction to a list of resources", exchange, e);
}
in.setHeader("CamelFhir.resources", resources);
}
}
use of org.hl7.fhir.r4b.formats.IParser in project gpconnect-demonstrator by nhsconnect.
the class ValueSetValidator method findValueSet.
private ValueSet findValueSet(String systemUrl) {
int valueSetNamePos = systemUrl.lastIndexOf("/") + 1;
String valueSetFilename = String.format("%s.xml", systemUrl.substring(valueSetNamePos));
ValueSet valSet = null;
String xmlContent = null;
if (fhirValueSetsCheckWebFirst == true) {
xmlContent = readValueSetFromWeb(valueSetFilename);
}
if (xmlContent == null) {
xmlContent = readValueSetFromDisk(valueSetFilename);
}
if (fhirValueSetsCheckWebFirst == false && xmlContent == null) {
xmlContent = readValueSetFromWeb(valueSetFilename);
}
if (xmlContent != null) {
try {
FhirContext fhirCtx = FhirContext.forDstu3();
IParser parser = fhirCtx.newXmlParser();
valSet = parser.parseResource(ValueSet.class, xmlContent);
} catch (DataFormatException ex) {
LOG.error(String.format("Error parsing valueSetFilename: %s", valueSetFilename));
}
}
if (valSet == null) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(String.format("Could not find or parse Value Set [SystemUrl: %s] at: %s. See system log for details.", systemUrl, valueSetFilename)), SystemCode.REFERENCE_NOT_FOUND, IssueType.NOTFOUND);
}
return valSet;
}
use of org.hl7.fhir.r4b.formats.IParser in project clinical_quality_language by cqframework.
the class DataRequirementsProcessorTest method TestDataRequirementsFHIRReferences.
@Test
public void TestDataRequirementsFHIRReferences() {
CqlTranslatorOptions cqlTranslatorOptions = new CqlTranslatorOptions();
cqlTranslatorOptions.getFormats().add(CqlTranslator.Format.JSON);
try {
CqlTranslator translator = createTranslator("FHIRReferencesRevisited.cql", cqlTranslatorOptions);
translator.toELM();
assertTrue(translator.getErrors().isEmpty());
libraryManager.cacheLibrary(translator.getTranslatedLibrary());
DataRequirementsProcessor dqReqTrans = new DataRequirementsProcessor();
org.hl7.fhir.r5.model.Library moduleDefinitionLibrary = dqReqTrans.gatherDataRequirements(libraryManager, translator.getTranslatedLibrary(), cqlTranslatorOptions, null, false);
FhirContext context = getFhirContext();
IParser parser = context.newJsonParser();
String moduleDefString = parser.setPrettyPrint(true).encodeResourceToString(moduleDefinitionLibrary);
logger.debug(moduleDefString);
// TODO: Validate consolidation of requirements
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
use of org.hl7.fhir.r4b.formats.IParser in project clinical_quality_language by cqframework.
the class DataRequirementsProcessorTest method TestDataRequirementsProcessorWithExpressions.
@Test
public void TestDataRequirementsProcessorWithExpressions() {
CqlTranslatorOptions cqlTranslatorOptions = new CqlTranslatorOptions();
cqlTranslatorOptions.getFormats().add(CqlTranslator.Format.JSON);
try {
Set<String> expressions = new HashSet<>();
// TODO - add expressions to expressions
// Active Ambulatory Opioid Rx");
expressions.add("Conditions Indicating End of Life or With Limited Life Expectancy");
CqlTranslator translator = createTranslator("OpioidCDS/cql/OpioidCDSCommon.cql", cqlTranslatorOptions);
translator.toELM();
assertTrue(translator.getErrors().isEmpty());
libraryManager.cacheLibrary(translator.getTranslatedLibrary());
DataRequirementsProcessor dqReqTrans = new DataRequirementsProcessor();
org.hl7.fhir.r5.model.Library moduleDefinitionLibrary = dqReqTrans.gatherDataRequirements(libraryManager, translator.getTranslatedLibrary(), cqlTranslatorOptions, expressions, false);
assertTrue(moduleDefinitionLibrary.getType().getCode("http://terminology.hl7.org/CodeSystem/library-type").equalsIgnoreCase("module-definition"));
List<Extension> directReferenceCodes = moduleDefinitionLibrary.getExtensionsByUrl("http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-directReferenceCode");
assertTrue(directReferenceCodes.size() == 4);
Extension directReferenceCode = directReferenceCodes.get(0);
Coding coding = directReferenceCode.getValueCoding();
assertEquals("http://hl7.org/fhir/condition-category", coding.getSystem());
assertEquals("encounter-diagnosis", coding.getCode());
assertEquals("Encounter Diagnosis", coding.getDisplay());
assertTrue(moduleDefinitionLibrary.getRelatedArtifact().size() == 6);
RelatedArtifact conditionCategoryCodes = null;
for (RelatedArtifact relatedArtifact : moduleDefinitionLibrary.getRelatedArtifact()) {
if (relatedArtifact.getType() == RelatedArtifact.RelatedArtifactType.DEPENDSON && relatedArtifact.getResource() != null && relatedArtifact.getResource().equals("http://hl7.org/fhir/condition-category")) {
conditionCategoryCodes = relatedArtifact;
break;
}
}
assertTrue(conditionCategoryCodes != null);
assertTrue(moduleDefinitionLibrary.getParameter().size() == 1);
ParameterDefinition conditionsIndicatingEndOfLife = null;
for (ParameterDefinition parameter : moduleDefinitionLibrary.getParameter()) {
if (parameter.getName().equals("Conditions Indicating End of Life or With Limited Life Expectancy")) {
conditionsIndicatingEndOfLife = parameter;
break;
}
}
assertTrue(conditionsIndicatingEndOfLife != null);
assertTrue(moduleDefinitionLibrary.getDataRequirement().size() == 3);
DataRequirement diagnosisRequirement = null;
for (DataRequirement requirement : moduleDefinitionLibrary.getDataRequirement()) {
if (requirement.getType() == Enumerations.FHIRAllTypes.CONDITION && requirement.getCodeFilter().size() == 1) {
DataRequirement.DataRequirementCodeFilterComponent cfc = requirement.getCodeFilterFirstRep();
if (cfc.hasPath() && cfc.getPath().equals("category") && cfc.getCode().size() == 1 && cfc.getCodeFirstRep().hasCode() && cfc.getCodeFirstRep().getCode().equals("encounter-diagnosis")) {
diagnosisRequirement = requirement;
break;
}
}
}
assertTrue(diagnosisRequirement != null);
FhirContext context = getFhirContext();
IParser parser = context.newJsonParser();
String moduleDefString = parser.setPrettyPrint(true).encodeResourceToString(moduleDefinitionLibrary);
logger.debug(moduleDefString);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
use of org.hl7.fhir.r4b.formats.IParser in project clinical_quality_language by cqframework.
the class DataRequirementsProcessorTest method TestLibraryDataRequirements.
@Test
public void TestLibraryDataRequirements() {
CqlTranslatorOptions cqlTranslatorOptions = new CqlTranslatorOptions();
cqlTranslatorOptions.getFormats().add(CqlTranslator.Format.JSON);
try {
// CqlTranslator translator = createTranslator("/ecqm/resources/library-EXM506-2.2.000.json", cqlTranslatorOptions);
CqlTranslator translator = createTranslator("CompositeMeasures/cql/BCSComponent.cql", cqlTranslatorOptions);
translator.toELM();
assertTrue(translator.getErrors().isEmpty());
libraryManager.cacheLibrary(translator.getTranslatedLibrary());
DataRequirementsProcessor dqReqTrans = new DataRequirementsProcessor();
org.hl7.fhir.r5.model.Library moduleDefinitionLibrary = dqReqTrans.gatherDataRequirements(libraryManager, translator.getTranslatedLibrary(), cqlTranslatorOptions, null, false);
assertTrue(moduleDefinitionLibrary.getType().getCode("http://terminology.hl7.org/CodeSystem/library-type").equalsIgnoreCase("module-definition"));
List<Extension> directReferenceCodes = moduleDefinitionLibrary.getExtensionsByUrl("http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-directReferenceCode");
assertTrue(directReferenceCodes.size() == 5);
Extension directReferenceCode = directReferenceCodes.get(0);
Coding coding = directReferenceCode.getValueCoding();
assertEquals("http://loinc.org", coding.getSystem());
assertEquals("21112-8", coding.getCode());
assertEquals("Birth date", coding.getDisplay());
assertTrue(moduleDefinitionLibrary.getRelatedArtifact().size() >= 45);
RelatedArtifact loincCodeSystem = null;
for (RelatedArtifact relatedArtifact : moduleDefinitionLibrary.getRelatedArtifact()) {
if (relatedArtifact.getType() == RelatedArtifact.RelatedArtifactType.DEPENDSON && relatedArtifact.getResource() != null && relatedArtifact.getResource().equals("http://loinc.org")) {
loincCodeSystem = relatedArtifact;
break;
}
}
assertTrue(loincCodeSystem != null);
assertTrue(moduleDefinitionLibrary.getParameter().size() >= 16);
ParameterDefinition measurementPeriod = null;
for (ParameterDefinition parameter : moduleDefinitionLibrary.getParameter()) {
if (parameter.getName().equals("Measurement Period")) {
measurementPeriod = parameter;
break;
}
}
assertTrue(measurementPeriod != null);
assertTrue(moduleDefinitionLibrary.getDataRequirement().size() >= 15);
DataRequirement diagnosisRequirement = null;
for (DataRequirement requirement : moduleDefinitionLibrary.getDataRequirement()) {
if (requirement.getType() == Enumerations.FHIRAllTypes.CONDITION && requirement.getCodeFilter().size() == 1) {
DataRequirement.DataRequirementCodeFilterComponent cfc = requirement.getCodeFilterFirstRep();
if (cfc.hasPath() && cfc.getPath().equals("code") && cfc.hasValueSet() && cfc.getValueSet().equals("http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.198.12.1071")) {
diagnosisRequirement = requirement;
break;
}
}
}
assertTrue(diagnosisRequirement != null);
FhirContext context = getFhirContext();
IParser parser = context.newJsonParser();
String moduleDefString = parser.setPrettyPrint(true).encodeResourceToString(moduleDefinitionLibrary);
logger.debug(moduleDefString);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
Aggregations