From 1c1518ec0c854eb8573e725ba8816f8a2f87edc7 Mon Sep 17 00:00:00 2001 From: moshonk Date: Tue, 14 May 2024 14:44:15 +0300 Subject: [PATCH] chore: Add MFL Code as location identifier in LabCreationListener --- .../api/event/LabCreationListener.java | 54 ++++++++++++++++--- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java b/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java index fbcdc9f..fab0b08 100644 --- a/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java +++ b/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java @@ -1,6 +1,8 @@ package org.openmrs.module.labonfhir.api.event; import javax.jms.Message; + +import java.util.Collection; import java.util.HashSet; import java.util.List; @@ -10,10 +12,16 @@ import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.param.TokenParam; +import org.hl7.fhir.r4.model.Location; + import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Resource; import org.hl7.fhir.r4.model.Task; +import org.openmrs.LocationAttribute; +import org.openmrs.LocationAttributeType; +import org.openmrs.api.LocationService; import org.openmrs.api.context.Daemon; import org.openmrs.event.EventListener; import org.openmrs.module.DaemonToken; @@ -31,6 +39,10 @@ public abstract class LabCreationListener implements EventListener { + private static final String MFL_LOCATION_IDENTIFIER_URI = "http://moh.bw.org/ext/location/identifier/mfl"; + + private static final String MFL_LOCATION_ATTRIBUTE_TYPE_UUID = "8a845a89-6aa5-4111-81d3-0af31c45c002"; + private static final Logger log = LoggerFactory.getLogger(EncounterCreationListener.class); private DaemonToken daemonToken; @@ -47,14 +59,17 @@ public abstract class LabCreationListener implements EventListener { private FhirContext ctx; @Autowired - FhirLocationService fhirLocationService ; + FhirLocationService fhirLocationService; @Autowired private FhirTaskService fhirTaskService; @Autowired - private LabOnFhirService labOnFhirService ; + private LabOnFhirService labOnFhirService; + @Autowired + private LocationService locationService; + public DaemonToken getDaemonToken() { return daemonToken; } @@ -70,8 +85,7 @@ public void onMessage(Message message) { Daemon.runInDaemonThread(() -> { try { processMessage(message); - } - catch (Exception e) { + } catch (Exception e) { log.error("Failed to update the user's last viewed patients property", e); } }, daemonToken); @@ -96,8 +110,35 @@ public Bundle createLabBundle(Task task) { if (!task.getLocation().isEmpty()) { labResources.add(fhirLocationService.get(FhirUtils.referenceToId(task.getLocation().getReference()).get())); } + for (IBaseResource r : labResources) { Resource resource = (Resource) r; + + // if resource is location, add MFL Code as location identifier + // TODO: Short term: Make the MFL Location attribute type to be a Global config variable + // TODO: Long term: Make the location translator pick enrich the MFL Code as appropriate + + if (resource instanceof Location) { + Location location = (Location) resource; + if (location != null) { + org.openmrs.Location openmrsLocation = locationService.getLocationByUuid(location.getId()); + if (openmrsLocation != null) { + LocationAttributeType mflLocationAttributeType = locationService.getLocationAttributeTypeByUuid(MFL_LOCATION_ATTRIBUTE_TYPE_UUID); + Collection locationAttributeTypes = openmrsLocation.getActiveAttributes(); + if (!locationAttributeTypes.isEmpty()) { + locationAttributeTypes.stream().filter(locationAttribute -> locationAttribute.getAttributeType().equals(mflLocationAttributeType)).findFirst().ifPresent(locationAttribute -> { + String mflCode = (String) locationAttribute.getValue(); + + Identifier mflIdentifier = new Identifier(); + mflIdentifier.setSystem(MFL_LOCATION_IDENTIFIER_URI); + mflIdentifier.setValue(mflCode); + location.addIdentifier(mflIdentifier); + }); + } + } + } + } + Bundle.BundleEntryComponent component = transactionBundle.addEntry(); component.setResource(resource); component.getRequest().setUrl(resource.fhirType() + "/" + resource.getIdElement().getIdPart()) @@ -113,8 +154,7 @@ protected void sendTask(Task task) { Bundle labBundle = createLabBundle(task); try { client.transaction().withBundle(labBundle).execute(); - } - catch (Exception e) { + } catch (Exception e) { saveFailedTask(task.getIdElement().getIdPart(), e.getMessage()); log.error("Failed to send Task with UUID " + task.getIdElement().getIdPart(), e); } @@ -123,7 +163,7 @@ protected void sendTask(Task task) { } } - private void saveFailedTask(String taskUuid ,String error) { + private void saveFailedTask(String taskUuid, String error) { FailedTask failedTask = new FailedTask(); failedTask.setError(error); failedTask.setIsSent(false);