Search in sources :

Example 16 with Immunization

use of org.hl7.fhir.r4.model.Immunization in project openmrs-module-fhir2 by openmrs.

the class ImmunizationFhirResourceProviderTest method updateImmunization_shouldUpdateRequestedImmunization.

@Test
public void updateImmunization_shouldUpdateRequestedImmunization() {
    when(immunizationService.update(IMMUNIZATION_UUID, immunization)).thenReturn(immunization);
    MethodOutcome result = resourceProvider.updateImmunization(new IdType().setValue(IMMUNIZATION_UUID), immunization);
    assertThat(result, notNullValue());
    assertThat(result.getResource(), equalTo(immunization));
}
Also used : MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome) IdType(org.hl7.fhir.r4.model.IdType) BaseFhirProvenanceResourceTest(org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest) Test(org.junit.Test)

Example 17 with Immunization

use of org.hl7.fhir.r4.model.Immunization in project openmrs-module-fhir2 by openmrs.

the class ImmunizationFhirResourceProviderTest method getImmunizationHistory_shouldReturnListOfResource.

@Test
public void getImmunizationHistory_shouldReturnListOfResource() {
    IdType id = new IdType();
    id.setValue(IMMUNIZATION_UUID);
    when(immunizationService.get(IMMUNIZATION_UUID)).thenReturn(immunization);
    List<Resource> resources = resourceProvider.getImmunizationHistoryById(id);
    assertThat(resources, notNullValue());
    assertThat(resources, not(empty()));
    assertThat(resources.size(), equalTo(2));
}
Also used : IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) IdType(org.hl7.fhir.r4.model.IdType) BaseFhirProvenanceResourceTest(org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest) Test(org.junit.Test)

Example 18 with Immunization

use of org.hl7.fhir.r4.model.Immunization in project openmrs-module-fhir2 by openmrs.

the class ImmunizationFhirResourceProviderTest method initImmunization.

@Before
public void initImmunization() {
    patient = new Patient();
    patient.setId(PATIENT_UUID);
    immunization = new Immunization();
    immunization.setId(IMMUNIZATION_UUID);
    immunization.setPatientTarget(patient);
    setProvenanceResources(immunization);
}
Also used : Immunization(org.hl7.fhir.r4.model.Immunization) Patient(org.hl7.fhir.r4.model.Patient) Before(org.junit.Before)

Example 19 with Immunization

use of org.hl7.fhir.r4.model.Immunization in project openmrs-module-fhir2 by openmrs.

the class ImmunizationTranslatorImpl method toOpenmrsType.

@Override
public Obs toOpenmrsType(@Nonnull Obs openmrsImmunization, @Nonnull Immunization fhirImmunization) {
    if (openmrsImmunization == null) {
        return null;
    }
    if (fhirImmunization == null) {
        return openmrsImmunization;
    }
    Patient patient = patientReferenceTranslator.toOpenmrsType(fhirImmunization.getPatient());
    if (patient == null) {
        final String errMsg;
        if (fhirImmunization.getPatient().hasReference()) {
            errMsg = "Could not find patient matching " + fhirImmunization.getPatient().getReference();
        } else {
            errMsg = "No patient was specified for this request";
        }
        throw createImmunizationRequestValidationError(errMsg);
    }
    List<ImmunizationPerformerComponent> performers = fhirImmunization.getPerformer();
    Provider provider = null;
    if (performers.size() > 1) {
        throw createImmunizationRequestValidationError("More than one performer was specified. Only a single performer is currently supported for each immunization.");
    } else if (performers.size() == 1) {
        ImmunizationPerformerComponent performer = performers.get(0);
        if (performer != null && performer.hasActor()) {
            provider = practitionerReferenceTranslator.toOpenmrsType(performer.getActor());
        }
    }
    final Visit visit;
    if (fhirImmunization.hasEncounter()) {
        visit = visitReferenceTranslator.toOpenmrsType(fhirImmunization.getEncounter());
    } else {
        visit = null;
    }
    if (visit == null) {
        final String errMsg;
        if (fhirImmunization.getEncounter().hasReference()) {
            errMsg = "Could not find visit matching " + fhirImmunization.getEncounter().getReference();
        } else {
            errMsg = "No encounter was specified for this request";
        }
        throw createImmunizationRequestValidationError(errMsg);
    }
    Location location = visit.getLocation();
    if (fhirImmunization.hasLocation()) {
        Location recordedLocation = locationReferenceTranslator.toOpenmrsType(fhirImmunization.getLocation());
        if (recordedLocation != null) {
            location = recordedLocation;
        }
    }
    if (!patient.equals(visit.getPatient())) {
        throw createImmunizationRequestValidationError("The visit '" + visit.getUuid() + "' does not belong to patient '" + patient.getUuid() + "'.");
    }
    EncounterType encounterType = helper.getImmunizationsEncounterType();
    // taking the visit's most recent immunization encounter
    Optional<Encounter> existingEncounter = visit.getEncounters().stream().filter(e -> encounterType.equals(e.getEncounterType())).max(Comparator.comparing(Encounter::getEncounterDatetime));
    final Provider encounterProvider = provider;
    final Location finalLocation = location;
    Encounter encounter = existingEncounter.orElseGet(() -> {
        final EncounterRole encounterRole = helper.getAdministeringEncounterRole();
        final Encounter newEncounter = new Encounter();
        newEncounter.setVisit(visit);
        newEncounter.setLocation(finalLocation);
        newEncounter.setEncounterType(encounterType);
        newEncounter.setPatient(patient);
        if (encounterProvider != null) {
            newEncounter.setProvider(encounterRole, encounterProvider);
        }
        if (visit.getStopDatetime() != null) {
            newEncounter.setEncounterDatetime(visit.getStopDatetime());
        } else {
            newEncounter.setEncounterDatetime(openmrsImmunization.getObsDatetime());
        }
        return newEncounter;
    });
    openmrsImmunization.setPerson(patient);
    openmrsImmunization.setLocation(location);
    openmrsImmunization.setEncounter(encounter);
    openmrsImmunization.getGroupMembers().forEach(obs -> {
        obs.setPerson(patient);
        obs.setLocation(finalLocation);
        obs.setEncounter(encounter);
    });
    Map<String, Obs> members = helper.getObsMembersMap(openmrsImmunization);
    Coding coding = fhirImmunization.getVaccineCode().getCoding().stream().filter(code -> StringUtils.isEmpty(code.getSystem())).reduce((code1, code2) -> {
        throw createImmunizationRequestValidationError("Multiple system-less coding found for the immunization's vaccine: " + code1.getCode() + " and " + code2.getCode() + ". No unique system concept could be identified as the coded answer.");
    }).orElseThrow(() -> createImmunizationRequestValidationError("Could not find a valid coding could be identified for this immunization."));
    {
        Obs obs = members.get(CIEL_984);
        if (obs == null) {
            obs = helper.addNewObs(openmrsImmunization, CIEL_984);
            members.put(CIEL_984, obs);
            obs.setValueCoded(conceptService.getConceptByUuid(coding.getCode()));
        } else if (obs.getId() == null) {
            obs.setValueCoded(conceptService.getConceptByUuid(coding.getCode()));
        } else {
            Concept newValue = conceptService.getConceptByUuid(coding.getCode());
            Concept prevValue = obs.getValueCoded();
            if (!newValue.equals(prevValue)) {
                obs = helper.replaceObs(openmrsImmunization, obs);
                obs.setValueCoded(newValue);
            }
        }
    }
    if (!fhirImmunization.hasOccurrenceDateTimeType() || !fhirImmunization.getOccurrenceDateTimeType().hasValue()) {
        throw createImmunizationRequestValidationError("An Immunization must have a valid occurrenceDateTime value");
    }
    {
        Obs obs = members.get(CIEL_1410);
        if (obs == null) {
            obs = helper.addNewObs(openmrsImmunization, CIEL_1410);
            members.put(CIEL_1410, obs);
            obs.setValueDatetime(fhirImmunization.getOccurrenceDateTimeType().getValue());
        } else if (obs.getId() == null) {
            obs.setValueDatetime(fhirImmunization.getOccurrenceDateTimeType().getValue());
        } else {
            Date newValue = fhirImmunization.getOccurrenceDateTimeType().getValue();
            Date prevValue = obs.getValueDatetime();
            if (!newValue.equals(prevValue)) {
                obs = helper.replaceObs(openmrsImmunization, obs);
                obs.setValueDatetime(newValue);
            }
        }
    }
    if (fhirImmunization.hasProtocolApplied()) {
        if (fhirImmunization.getProtocolApplied().size() != 1) {
            throw createImmunizationRequestValidationError("Either no protocol applied was found or multiple protocols applied were found. " + "Only one protocol is currently supported for each immunization.");
        }
        ImmunizationProtocolAppliedComponent protocolApplied = fhirImmunization.getProtocolApplied().get(0);
        if (protocolApplied.hasDoseNumber()) {
            {
                Obs obs = members.get(CIEL_1418);
                if (obs == null) {
                    obs = helper.addNewObs(openmrsImmunization, CIEL_1418);
                    members.put(CIEL_1418, obs);
                    obs.setValueNumeric(protocolApplied.getDoseNumberPositiveIntType().getValue().doubleValue());
                } else if (obs.getId() == null) {
                    obs.setValueNumeric(protocolApplied.getDoseNumberPositiveIntType().getValue().doubleValue());
                } else {
                    double newValue = protocolApplied.getDoseNumberPositiveIntType().getValue().doubleValue();
                    Double updatedValue = obs.getValueNumeric();
                    if (updatedValue != null && newValue != updatedValue) {
                        obs = helper.replaceObs(openmrsImmunization, obs);
                        obs.setValueNumeric(newValue);
                    }
                }
            }
        }
    } else {
        openmrsImmunization.removeGroupMember(members.get(CIEL_1418));
    }
    if (fhirImmunization.hasManufacturer() && fhirImmunization.getManufacturer().hasDisplay()) {
        {
            Obs obs = members.get(CIEL_1419);
            if (obs == null) {
                obs = helper.addNewObs(openmrsImmunization, CIEL_1419);
                members.put(CIEL_1419, obs);
                obs.setValueText(fhirImmunization.getManufacturer().getDisplay());
            } else if (obs.getId() == null) {
                obs.setValueText(fhirImmunization.getManufacturer().getDisplay());
            } else {
                String newValue = fhirImmunization.getManufacturer().getDisplay();
                String prevValue = obs.getValueText();
                if (!newValue.equals(prevValue)) {
                    obs = helper.replaceObs(openmrsImmunization, obs);
                    obs.setValueText(newValue);
                }
            }
        }
    } else {
        openmrsImmunization.removeGroupMember(members.get(CIEL_1419));
    }
    if (fhirImmunization.hasLotNumber()) {
        {
            Obs obs = members.get(CIEL_1420);
            if (obs == null) {
                obs = helper.addNewObs(openmrsImmunization, CIEL_1420);
                members.put(CIEL_1420, obs);
                obs.setValueText(fhirImmunization.getLotNumber());
            } else if (obs.getId() == null) {
                obs.setValueText(fhirImmunization.getLotNumber());
            } else {
                String newValue = fhirImmunization.getLotNumber();
                String prevValue = obs.getValueText();
                if (!newValue.equals(prevValue)) {
                    obs = helper.replaceObs(openmrsImmunization, obs);
                    obs.setValueText(newValue);
                }
            }
        }
    } else {
        openmrsImmunization.removeGroupMember(members.get(CIEL_1420));
    }
    if (fhirImmunization.hasExpirationDate()) {
        {
            Obs obs = members.get(CIEL_165907);
            if (obs == null) {
                obs = helper.addNewObs(openmrsImmunization, CIEL_165907);
                members.put(CIEL_165907, obs);
                obs.setValueDate(fhirImmunization.getExpirationDate());
            } else if (obs.getId() == null) {
                obs.setValueDate(fhirImmunization.getExpirationDate());
            } else {
                Date newValue = fhirImmunization.getExpirationDate();
                Date prevValue = obs.getValueDate();
                if (!newValue.equals(prevValue)) {
                    obs = helper.replaceObs(openmrsImmunization, obs);
                    obs.setValueDate(newValue);
                }
            }
        }
    } else {
        openmrsImmunization.removeGroupMember(members.get(CIEL_165907));
    }
    return openmrsImmunization;
}
Also used : Setter(lombok.Setter) ImmunizationObsGroupHelper.createImmunizationRequestValidationError(org.openmrs.module.fhir2.api.util.ImmunizationObsGroupHelper.createImmunizationRequestValidationError) Date(java.util.Date) Visit(org.openmrs.Visit) Autowired(org.springframework.beans.factory.annotation.Autowired) ImmunizationPerformerComponent(org.hl7.fhir.r4.model.Immunization.ImmunizationPerformerComponent) Reference(org.hl7.fhir.r4.model.Reference) StringUtils(org.apache.commons.lang3.StringUtils) AccessLevel(lombok.AccessLevel) ImmunizationProtocolAppliedComponent(org.hl7.fhir.r4.model.Immunization.ImmunizationProtocolAppliedComponent) Location(org.openmrs.Location) ImmunizationObsGroupHelper(org.openmrs.module.fhir2.api.util.ImmunizationObsGroupHelper) ImmunizationStatus(org.hl7.fhir.r4.model.Immunization.ImmunizationStatus) Map(java.util.Map) ConceptService(org.openmrs.api.ConceptService) Obs(org.openmrs.Obs) Nonnull(javax.annotation.Nonnull) PractitionerReferenceTranslator(org.openmrs.module.fhir2.api.translators.PractitionerReferenceTranslator) ImmunizationTranslator(org.openmrs.module.fhir2.api.translators.ImmunizationTranslator) ImmutableSet(com.google.common.collect.ImmutableSet) Encounter(org.openmrs.Encounter) Iterator(java.util.Iterator) EncounterReferenceTranslator(org.openmrs.module.fhir2.api.translators.EncounterReferenceTranslator) ConceptTranslator(org.openmrs.module.fhir2.api.translators.ConceptTranslator) Set(java.util.Set) LocationReferenceTranslator(org.openmrs.module.fhir2.api.translators.LocationReferenceTranslator) List(java.util.List) Immunization(org.hl7.fhir.r4.model.Immunization) Component(org.springframework.stereotype.Component) Concept(org.openmrs.Concept) Provider(org.openmrs.Provider) EncounterType(org.openmrs.EncounterType) Coding(org.hl7.fhir.r4.model.Coding) PatientReferenceTranslator(org.openmrs.module.fhir2.api.translators.PatientReferenceTranslator) Optional(java.util.Optional) ObservationValueTranslator(org.openmrs.module.fhir2.api.translators.ObservationValueTranslator) PositiveIntType(org.hl7.fhir.r4.model.PositiveIntType) Comparator(java.util.Comparator) Collections(java.util.Collections) Patient(org.openmrs.Patient) EncounterRole(org.openmrs.EncounterRole) Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) ImmunizationPerformerComponent(org.hl7.fhir.r4.model.Immunization.ImmunizationPerformerComponent) Visit(org.openmrs.Visit) Patient(org.openmrs.Patient) Date(java.util.Date) Provider(org.openmrs.Provider) Coding(org.hl7.fhir.r4.model.Coding) Encounter(org.openmrs.Encounter) EncounterRole(org.openmrs.EncounterRole) ImmunizationProtocolAppliedComponent(org.hl7.fhir.r4.model.Immunization.ImmunizationProtocolAppliedComponent) EncounterType(org.openmrs.EncounterType) Location(org.openmrs.Location)

Example 20 with Immunization

use of org.hl7.fhir.r4.model.Immunization in project openmrs-module-fhir2 by openmrs.

the class ImmunizationFhirResourceProviderIntegrationTest method shouldCreateNewImmunizationWithoutSomeOptionalMembersAsXML.

@Test
public void shouldCreateNewImmunizationWithoutSomeOptionalMembersAsXML() throws Exception {
    // read XML record
    String xmlImmunization;
    try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(XML_CREATE_PARTIAL_IMMUNIZATION_DOCUMENT)) {
        Objects.requireNonNull(is);
        xmlImmunization = IOUtils.toString(is, StandardCharsets.UTF_8);
    }
    // create IMMUNIZATION
    MockHttpServletResponse response = post("/Immunization").accept(FhirMediaTypes.XML).xmlContent(xmlImmunization).go();
    // verify created correctly
    assertThat(response, isCreated());
    assertThat(response.getContentType(), is(FhirMediaTypes.XML.toString()));
    assertThat(response.getContentAsString(), notNullValue());
    Immunization immunization = readResponse(response);
    assertThat(immunization, notNullValue());
    assertThat(immunization.getResourceType().toString(), equalTo("Immunization"));
    assertThat(immunization.getStatus().toCode(), equalTo("completed"));
    assertThat(immunization.getVaccineCode().getCodingFirstRep().getCode(), equalTo("15f83cd6-64e9-4e06-a5f9-364d3b14a43d"));
    assertThat(immunization.getPatient().getReferenceElement().getIdPart(), equalTo("8d703ff2-c3e2-4070-9737-73e713d5a50d"));
    assertThat(immunization.getOccurrenceDateTimeType().getValueAsCalendar().getTime(), sameInstant(Date.from(ZonedDateTime.parse("2020-07-08T18:30:00.000Z").toInstant())));
    assertThat(immunization.hasManufacturer(), is(true));
    assertThat(immunization.getManufacturer().getDisplay(), equalTo("Pfizer"));
    assertThat(immunization.getLotNumber(), equalTo("22"));
    assertThat(immunization.getExpirationDate(), sameDay(LocalDate.parse("2021-07-28")));
    assertThat(immunization.getPerformer().get(0).getActor().getReferenceElement().getIdPart(), equalTo("6f763a67-2bd1-451c-93b9-95caeb36cc24"));
    assertThat(immunization, validResource());
    // try to get new immunization
    response = get("/Immunization/" + immunization.getIdElement().getIdPart()).accept(FhirMediaTypes.XML).go();
    assertThat(response, isOk());
    Immunization newImmunization = readResponse(response);
    assertThat(newImmunization.getId(), equalTo(immunization.getId()));
}
Also used : Immunization(org.hl7.fhir.r4.model.Immunization) InputStream(java.io.InputStream) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

Immunization (org.hl7.fhir.r4.model.Immunization)47 Test (org.junit.Test)27 Test (org.junit.jupiter.api.Test)22 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)18 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)15 Resource (org.hl7.fhir.r4.model.Resource)13 OperationOutcome (org.hl7.fhir.r4.model.OperationOutcome)8 Bundle (org.hl7.fhir.r4.model.Bundle)7 Organization (org.hl7.fhir.r4.model.Organization)7 BaseFhirProvenanceResourceTest (org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest)6 Date (java.util.Date)5 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)5 IdType (org.hl7.fhir.r4.model.IdType)5 Observation (org.hl7.fhir.r4.model.Observation)5 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)5 InputStream (java.io.InputStream)4 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)4 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)4 Obs (org.openmrs.Obs)4 FhirContext (ca.uhn.fhir.context.FhirContext)3