Search in sources :

Example 21 with UNKNOWN

use of org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender.UNKNOWN in project health-patterns by LinuxForHealth.

the class TerminologyService method translateResource.

/**
 * Translates the given single FHIR resource represented as a {@link JsonNode}.
 *
 * @param resource the FHIR resource to translate
 * @returns true if there was something to translate, false otherwise
 * @throws DeIdentifierException if there is an error in the de-identification REST API or parsing the JSON
 * @throws IllegalArgumentException if the given JSON does not have a 'resource' object
 */
private boolean translateResource(JsonNode resource) {
    boolean translatedSomething = false;
    String resourceType = getResourceType(resource);
    boolean isTranslatable = StringUtils.equalsAny(resourceType, TRANSLATABLE_FHIR_TYPES);
    if (!isTranslatable) {
        return translatedSomething;
    }
    ArrayNode extensions = (ArrayNode) resource.get(EXTENSION_OBJECT);
    if (extensions == null) {
        return translatedSomething;
    }
    for (int i = 0; i < extensions.size(); i++) {
        JsonNode extension = extensions.get(i);
        JsonNode urlJson = extension.get(URL_OBJECT);
        JsonNode valueCodeJson = extension.get(VALUE_CODE_OBJECT);
        if (urlJson == null || valueCodeJson == null) {
            // In order to do a translation we need both the url and the valueCode
            continue;
        }
        // The resource's extension URL is the URL for the StructureDefinition, so we resolve a ValueSet if known
        String structureDefinitionURL = urlJson.asText();
        String valueSetURL = valueSetForStructureDefinition.get(structureDefinitionURL);
        // and if known we check the FHIR Server's known ConceptMaps to see if there is a corresponding one
        // http://4603f72b-us-south.lb.appdomain.cloud/fhir-server/api/v4/ConceptMap?_format=json&source-uri=http://hl7.org/fhir/us/core/ValueSet/birthsex
        Bundle bundle = fhirClient.search().forResource(ConceptMap.class).where(ConceptMap.SOURCE_URI.hasId(valueSetURL)).returnBundle(Bundle.class).execute();
        String conceptMapId;
        if (!bundle.getEntry().isEmpty()) {
            Resource conceptMap = bundle.getEntry().get(0).getResource();
            if (bundle.getEntry().size() > 1) {
                System.err.println("Found multiple ConceptMaps that will map " + valueSetURL + " for this StructureDefinition, will use the first one " + conceptMap.getId());
            } else {
                System.out.println("Found ConceptMap for " + valueSetURL + ": " + conceptMap.getId() + " !!");
            }
            conceptMapId = conceptMap.getIdElement().getIdPart();
        } else {
            System.out.println("Did not find ConceptMap for " + valueSetURL + "!!");
            continue;
        }
        // "POST ${FHIR_URL}/${conceptMapID}/$translate?code=${code}&system=${valueSet}&_format=json
        String valueCode = valueCodeJson.asText();
        String url = String.format("%s/ConceptMap/%s/$translate?code=%s&system=%s&_format=json", fhirClient.getServerBase(), conceptMapId, valueCode, valueSetURL);
        Parameters translationResponse = fhirClient.fetchResourceFromUrl(Parameters.class, url);
        // This is what comes back from the server
        // {
        // "resourceType": "Parameters",
        // "parameter": [
        // {
        // "name": "result",
        // "valueBoolean": true
        // },
        // {
        // "name": "match",
        // "part": [
        // {
        // "name": "equivalence",
        // "valueCode": "equivalent"
        // },
        // {
        // "name": "concept",
        // "valueCoding": {
        // "system": "http://ibm.com/fhir/cdm/ValueSet/sex-assigned-at-birth",
        // "code": "male",
        // "display": "Male"
        // }
        // }
        // ]
        // }
        // ]
        // }
        Coding translatedCode = null;
        List<ParametersParameterComponent> parameters = translationResponse.getParameter();
        for (ParametersParameterComponent parameter : parameters) {
            if (parameter.getName().equals(MATCH_VALUE)) {
                List<ParametersParameterComponent> parts = parameter.getPart();
                for (ParametersParameterComponent part : parts) {
                    if (part.getName().equals(CONCEPT_VALUE)) {
                        try {
                            translatedCode = (Coding) part.getValue();
                        } catch (ClassCastException e) {
                            String jsonResponse = fhirClient.getFhirContext().newJsonParser().encodeResourceToString(translationResponse);
                            System.err.println("Found a ConceptMap that will map " + valueSetURL + " for this StructureDefinition, but the FHIR server returned an unknown $translate response (expected a 'valueCoding' part): " + jsonResponse);
                        }
                    }
                }
            }
        }
        if (translatedCode == null) {
            String jsonResponse = fhirClient.getFhirContext().newJsonParser().encodeResourceToString(translationResponse);
            System.err.println("Found a ConceptMap that will map " + valueSetURL + " for this StructureDefinition, but the FHIR server returned an unknown $translate response: " + jsonResponse);
            continue;
        }
        System.out.printf("Found ConceptMap %s which translates (valueCode, system) = (%s, %s) for StructureDefinition %s to (valueCode, system) = (%s, %s) %n", conceptMapId, valueCode, valueSetURL, structureDefinitionURL, translatedCode.getCode(), translatedCode.getSystem());
        String translatedStructuredData = valueSetForStructureDefinition.get(translatedCode.getSystem());
        if (translatedStructuredData == null) {
            System.err.printf("Cannot find the mapping from ValueSet '%s' to its corresponding StructureData for this translation, make sure the corresponding mappings configuration file has it.%n", translatedCode.getSystem());
            continue;
        }
        ((ObjectNode) extension).set(URL_OBJECT, JsonNodeFactory.instance.textNode(translatedStructuredData));
        ((ObjectNode) extension).set(VALUE_CODE_OBJECT, JsonNodeFactory.instance.textNode(translatedCode.getCode()));
        translatedSomething = true;
    }
    return translatedSomething;
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Bundle(org.hl7.fhir.r4.model.Bundle) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) JsonNode(com.fasterxml.jackson.databind.JsonNode) Coding(org.hl7.fhir.r4.model.Coding) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ConceptMap(org.hl7.fhir.r4.model.ConceptMap) ParametersParameterComponent(org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent)

Example 22 with UNKNOWN

use of org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender.UNKNOWN in project cqf-ruler by DBCG.

the class ElmCacheResourceChangeListener method invalidateCacheById.

private void invalidateCacheById(IIdType theId) {
    if (!theId.getResourceType().equals("Library")) {
        return;
    }
    try {
        IBaseResource library = this.myLibraryDao.read(theId);
        String name = this.myNameFunction.apply(library);
        String version = this.myVersionFunction.apply(library);
        this.myGlobalLibraryCache.remove(new VersionedIdentifier().withId(name).withVersion(version));
    }// name and version.
     catch (Exception e) {
        // TODO: This needs to be smarter... the issue is that ELM is cached with
        // library name and version as the key since
        // that's the access path the CQL engine uses, but change notifications occur
        // with the resource Id, which is not
        // necessarily tied to the resource name. In any event, if a unknown resource is
        // deleted, clear all libraries as a workaround.
        // One option is to maintain a cache with multiple indices.
        ourLog.debug("Failed to locate resource {} to look up name and version. Clearing all libraries from cache.", theId.getValueAsString());
        this.myGlobalLibraryCache.clear();
    }
}
Also used : VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 23 with UNKNOWN

use of org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender.UNKNOWN in project BridgeServer2 by Sage-Bionetworks.

the class CRCControllerTest method createEmptyPatient.

@Test
public void createEmptyPatient() {
    Patient patient = controller.createPatient(account);
    assertEquals(patient.getGender().name(), "UNKNOWN");
    // I'm defaulting this because I don't see the client submitting it in the UI, so
    // I'm anticipating it won't be there, but eventually we'll have to collect state.
    assertEquals(patient.getAddress().get(0).getState(), "NY");
    assertTrue(patient.getActive());
}
Also used : Patient(org.hl7.fhir.dstu3.model.Patient) Test(org.testng.annotations.Test)

Example 24 with UNKNOWN

use of org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender.UNKNOWN in project openmrs-module-fhir2 by openmrs.

the class ConceptTranslatorImplTest method shouldNotTranslateUnknownCodeableConceptSourceToConcept.

@Test
public void shouldNotTranslateUnknownCodeableConceptSourceToConcept() {
    CodeableConcept codeableConcept = new CodeableConcept();
    Coding cielCoding = codeableConcept.addCoding();
    cielCoding.setSystem("Unknown");
    cielCoding.setCode("1650");
    when(conceptSourceService.getFhirConceptSourceByUrl("Unknown")).thenReturn(Optional.empty());
    Concept result = conceptTranslator.toOpenmrsType(codeableConcept);
    assertThat(result, nullValue());
}
Also used : CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) Concept(org.openmrs.Concept) Coding(org.hl7.fhir.r4.model.Coding) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) Test(org.junit.Test)

Example 25 with UNKNOWN

use of org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender.UNKNOWN in project openmrs-module-fhir2 by openmrs.

the class EncounterReferenceTranslatorImplTest method toOpenmrsType_shouldThrowExceptionIfReferenceIsNotForEncounter.

@Test(expected = IllegalArgumentException.class)
public void toOpenmrsType_shouldThrowExceptionIfReferenceIsNotForEncounter() {
    Reference reference = new Reference().setReference("Unknown" + "/" + ENCOUNTER_UUID).setType("Unknown");
    encounterReferenceTranslator.toOpenmrsType(reference);
}
Also used : Reference(org.hl7.fhir.r4.model.Reference) Test(org.junit.Test)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)51 ArrayList (java.util.ArrayList)41 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)36 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)27 Test (org.junit.jupiter.api.Test)23 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)20 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)18 Reference (org.hl7.fhir.r4.model.Reference)17 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)17 HashMap (java.util.HashMap)15 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)15 Coding (org.hl7.fhir.r4.model.Coding)14 Identifier (org.hl7.fhir.r4.model.Identifier)14 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)13 Patient (org.hl7.fhir.r4.model.Patient)13 IOException (java.io.IOException)12 Header (org.apache.http.Header)11 Test (org.junit.Test)11 File (java.io.File)10 URISyntaxException (java.net.URISyntaxException)10