use of org.hl7.fhir.r4.model.Location in project openmrs-module-fhir2 by openmrs.
the class LocationFhirResourceProviderTest method findLocationsByState_shouldReturnMatchingBundleOfLocations.
@Test
public void findLocationsByState_shouldReturnMatchingBundleOfLocations() {
StringAndListParam stateParam = new StringAndListParam().addAnd(new StringOrListParam().add(new StringParam(STATE)));
when(locationService.searchForLocations(isNull(), isNull(), isNull(), isNull(), argThat(Matchers.is(stateParam)), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull())).thenReturn(new MockIBundleProvider<>(Collections.singletonList(location), PREFERRED_PAGE_SIZE, COUNT));
IBundleProvider results = resourceProvider.searchLocations(null, null, null, null, stateParam, null, null, null, null, null, null, null);
assertThat(results, notNullValue());
List<Location> resultList = get(results);
assertThat(resultList, hasSize(greaterThanOrEqualTo(1)));
assertThat(resultList.get(0).fhirType(), is(FhirConstants.LOCATION));
assertThat(resultList.get(0).getAddress().getState(), equalTo(STATE));
}
use of org.hl7.fhir.r4.model.Location in project openmrs-module-fhir2 by openmrs.
the class VisitTranslatorImplTest method toFhirResource_shouldTranslateToEncounterClassFhirType.
@Test
public void toFhirResource_shouldTranslateToEncounterClassFhirType() {
when(encounterClassMap.getFhirClass(LOCATION_UUID)).thenReturn(TEST_FHIR_CLASS);
Location location = new Location();
location.setUuid(LOCATION_UUID);
Visit visit = new Visit();
visit.setUuid(VISIT_UUID);
visit.setLocation(location);
Encounter result = visitTranslator.toFhirResource(visit);
assertThat(result, notNullValue());
assertThat(result.getClass_(), notNullValue());
assertThat(result.getClass_().getSystem(), CoreMatchers.is(FhirConstants.ENCOUNTER_CLASS_VALUE_SET_URI));
assertThat(result.getClass_().getCode(), CoreMatchers.is(TEST_FHIR_CLASS));
}
use of org.hl7.fhir.r4.model.Location 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;
}
use of org.hl7.fhir.r4.model.Location in project openmrs-module-fhir2 by openmrs.
the class LocationAddressTranslatorImpl method toFhirResource.
@Override
public Address toFhirResource(@Nonnull Location omrsLocation) {
Address address = null;
if (omrsLocation != null) {
address = new Address();
address.setId(null);
address.setCity(omrsLocation.getCityVillage());
address.setState(omrsLocation.getStateProvince());
address.setCountry(omrsLocation.getCountry());
address.setPostalCode(omrsLocation.getPostalCode());
addAddressExtensions(address, omrsLocation);
}
return address;
}
use of org.hl7.fhir.r4.model.Location in project openmrs-module-fhir2 by openmrs.
the class BaseEncounterTranslator method mapLocationToClass.
protected Coding mapLocationToClass(Location location) {
Coding coding = new Coding();
coding.setSystem(FhirConstants.ENCOUNTER_CLASS_VALUE_SET_URI);
// The default code for anything that cannot be matched with FHIR codes.
coding.setCode("AMB");
if (location == null) {
return coding;
}
String classCode = encounterClassMap.getFhirClass(location.getUuid());
if (classCode != null) {
coding.setCode(classCode);
}
return coding;
}
Aggregations