Skip to content

Commit

Permalink
chore: Add MFL Code as location identifier in LabCreationListener
Browse files Browse the repository at this point in the history
  • Loading branch information
moshonk committed Sep 7, 2024
1 parent 1a1e117 commit 1c1518e
Showing 1 changed file with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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<LocationAttribute> 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())
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down

0 comments on commit 1c1518e

Please sign in to comment.