Search in sources :

Example 51 with IParser

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);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Message(org.apache.camel.Message) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) TransformerException(javax.xml.transform.TransformerException) IParser(ca.uhn.fhir.parser.IParser)

Example 52 with IParser

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;
}
Also used : UnprocessableEntityException(ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException) FhirContext(ca.uhn.fhir.context.FhirContext) DataFormatException(ca.uhn.fhir.parser.DataFormatException) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) IParser(ca.uhn.fhir.parser.IParser)

Example 53 with IParser

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();
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) IOException(java.io.IOException) IParser(ca.uhn.fhir.parser.IParser) Test(org.testng.annotations.Test)

Example 54 with IParser

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();
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) IOException(java.io.IOException) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) IParser(ca.uhn.fhir.parser.IParser) Test(org.testng.annotations.Test)

Example 55 with IParser

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();
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) IOException(java.io.IOException) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) IParser(ca.uhn.fhir.parser.IParser) Test(org.testng.annotations.Test)

Aggregations

IParser (ca.uhn.fhir.parser.IParser)89 FhirContext (ca.uhn.fhir.context.FhirContext)43 IOException (java.io.IOException)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)24 InputStream (java.io.InputStream)22 IParser (org.hl7.fhir.r5.formats.IParser)19 JsonParser (org.hl7.fhir.r5.formats.JsonParser)18 Test (org.junit.jupiter.api.Test)18 FHIRException (org.hl7.fhir.exceptions.FHIRException)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 File (java.io.File)16 FileInputStream (java.io.FileInputStream)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bundle (org.hl7.fhir.r4.model.Bundle)14 FileOutputStream (java.io.FileOutputStream)12 Bundle (org.hl7.fhir.dstu3.model.Bundle)12 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)12 XmlParser (org.hl7.fhir.r5.formats.XmlParser)12 ArrayList (java.util.ArrayList)11