Skip to content

Commit

Permalink
Merge pull request #328 from PatrickWaweru/eid2024fix
Browse files Browse the repository at this point in the history
Fixing EID lab manifest
  • Loading branch information
makombe authored Dec 11, 2024
2 parents c90a917 + e3f37bc commit 7f914e1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.kenyaemrorderentry.api.db.hibernate.HibernateKenyaemrOrdersDAO;
import org.openmrs.module.kenyaemrorderentry.api.service.KenyaemrOrdersService;
import org.openmrs.module.kenyaemrorderentry.labDataExchange.LabOrderDataExchange;
import org.openmrs.module.kenyaemrorderentry.manifest.LabManifest;
import org.openmrs.module.kenyaemrorderentry.manifest.LabManifestOrder;
import org.openmrs.module.kenyaemrorderentry.queue.LimsQueue;
Expand All @@ -20,6 +21,9 @@
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.openmrs.module.kenyaemrorderentry.labDataExchange.LabOrderDataExchange;
import org.openmrs.module.kenyaemrorderentry.ModuleConstants;


@Transactional
public class KenyaemrOrdersServiceImpl extends BaseOpenmrsService implements KenyaemrOrdersService {
Expand Down Expand Up @@ -85,6 +89,30 @@ public LabManifest saveLabOrderManifest(LabManifest labManifest) {
}
} catch(Exception ex) {}

try {
Integer manifestId = labManifest.getId();
if(manifestId == null) {
System.out.println("Manifest ID fix: Saving a new manifest");
LabOrderDataExchange labOrderDataExchange = new LabOrderDataExchange();
if(LabOrderDataExchange.getSystemType() == ModuleConstants.LABWARE_SYSTEM) {
String mType = "E";
Integer manifestType = labManifest.getManifestType();
if(manifestType == LabManifest.EID_TYPE) {
mType = "E";
} else if(manifestType == LabManifest.VL_TYPE) {
mType = "V";
} else if(manifestType == LabManifest.FLU_TYPE) {
mType = "F";
}
String newManifestIdentifier = labOrderDataExchange.generateUniqueManifestID(mType);
System.out.println("Manifest ID fix: New labware manifest identifier set as: " + newManifestIdentifier);
labManifest.setIdentifier(newManifestIdentifier);
}
} else {
System.out.println("Manifest ID fix: manifest in edit mode. No need for a new identifier");
}
} catch(Exception ex) {}

// Save or update manifest
return dao.saveLabOrderManifest(labManifest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openmrs.api.AdministrationService;
import org.openmrs.api.ConceptService;
import org.openmrs.api.EncounterService;
import org.openmrs.api.ObsService;
import org.openmrs.api.OrderService;
import org.openmrs.api.context.Context;
import org.openmrs.module.kenyacore.RegimenMappingUtils;
Expand All @@ -45,6 +46,7 @@ public class LabOrderDataExchange {
ConceptService conceptService = Context.getConceptService();
EncounterService encounterService = Context.getEncounterService();
OrderService orderService = Context.getOrderService();
ObsService obsService = Context.getObsService();
KenyaemrOrdersService kenyaemrOrdersService = Context.getService(KenyaemrOrdersService.class);


Expand Down Expand Up @@ -880,9 +882,11 @@ public String processIncomingViralLoadLabResults(String resultPayload) {
*/
private void updateOrder(Integer orderId, String result, String specimenStatus, String specimenRejectedReason, Date dateSampleReceived, Date dateSampleTested) {
Order od = orderService.getOrder(orderId);
System.out.println("Lab Results Get Results: Getting order for order id: " + orderId + " : Order: " + od);
LabManifestOrder manifestOrder = kenyaemrOrdersService.getLabManifestOrderByOrderId(od);

System.out.println("Order ID: " + od.getOrderId() + ", manifest order: " + manifestOrder.getId() + ", manifest id: " + manifestOrder.getLabManifest().getId() + ", isActive: " + od.isActive());
System.out.println("Lab Results Get Results: Manifest order status is : " + specimenStatus);

Date orderDiscontinuationDate = null;
if (dateSampleTested != null) {
Expand All @@ -896,6 +900,7 @@ private void updateOrder(Integer orderId, String result, String specimenStatus,
if (od != null && od.isActive()) {
if ((StringUtils.isNotBlank(specimenStatus) && specimenStatus.equals("Rejected")) || (StringUtils.isNotBlank(result) && result.equals("Collect New Sample"))) {

System.out.println("Lab Results Get Results: Setting Manifest order status to rejected or collect new sample");
String discontinuationReason = "";
if (StringUtils.isNotBlank(specimenRejectedReason)) {
discontinuationReason = specimenRejectedReason;
Expand Down Expand Up @@ -942,13 +947,17 @@ private void updateOrder(Integer orderId, String result, String specimenStatus,
kenyaemrOrdersService.saveLabManifestOrder(manifestOrder);
} else if (StringUtils.isNotBlank(specimenStatus) && specimenStatus.equalsIgnoreCase("Complete") && StringUtils.isNotBlank(result)) {

System.out.println("Lab Results Get Results: Setting Manifest order status to complete");
Encounter enc = new Encounter();
enc.setEncounterType(labEncounterType);
enc.setEncounterDatetime(orderDiscontinuationDate);
enc.setPatient(od.getPatient());
enc.setCreator(Context.getUserService().getUser(1));
encounterService.saveEncounter(enc);

// For EID manifest type
if (manifestType == LabManifest.EID_TYPE) {
// For EID manifest type
Obs o = new Obs();
String eidNegative = "Negative";
String eidPositive = "Positive";
Expand All @@ -957,10 +966,13 @@ private void updateOrder(Integer orderId, String result, String specimenStatus,
Concept eidPositiveConcept = conceptService.getConcept(703);
/*Concept eidIndeterminateConcept = conceptService.getConcept(1138);
Concept eidPoorSampleConcept = conceptService.getConcept(1304);*/
System.out.println("Negative concept: " + eidNegativeConcept + " and positive concept: " + eidPositiveConcept);

if (result.equalsIgnoreCase(eidNegative)) {
System.out.println("Got Negative result");
o.setValueCoded(eidNegativeConcept);
} else if (result.equalsIgnoreCase(eidPositive)) {
System.out.println("Got Positive result");
o.setValueCoded(eidPositiveConcept);
}

Expand All @@ -969,9 +981,15 @@ private void updateOrder(Integer orderId, String result, String specimenStatus,
o.setObsDatetime(od.getDateActivated());
o.setPerson(od.getPatient());
o.setOrder(od);
o.setConcept(od.getConcept());
o.setLocation(Utils.getDefaultLocation());

Obs obs = new Obs();
o.setEncounter(enc);
obs = obsService.saveObs(o, null);

try {
enc.addObs(o);
enc.addObs(obs);
encounterService.saveEncounter(enc);

orderService.discontinueOrder(od, "Results received", orderDiscontinuationDate, od.getOrderer(), od.getEncounter());
Expand All @@ -991,7 +1009,9 @@ private void updateOrder(Integer orderId, String result, String specimenStatus,
System.out.println("Lab Results Get Results: An error was encountered while updating orders for EID");
e.printStackTrace();
}
// End of EID manifest type
} else if (manifestType == LabManifest.VL_TYPE) {
// For VL Type Manifests
Concept viralLoadConcept = null;
String lDLResult = "< LDL copies/ml";
String labwarelDLResult = "<LDL";
Expand Down Expand Up @@ -1197,10 +1217,26 @@ private void updateOrder(Integer orderId, String result, String specimenStatus,
kenyaemrOrdersService.saveLabManifestOrder(manifestOrder);
}
}
// End of VL Manifest Type
}

} else if (StringUtils.isNotBlank(specimenStatus) && specimenStatus.equalsIgnoreCase("Incomplete")) {
// indicate the incomplete status
System.out.println("Lab Results Get Results: Setting Manifest order status to incomplete");
manifestOrder.setStatus("Incomplete");
manifestOrder.setResult("");
manifestOrder.setLastStatusCheckDate(new Date());
kenyaemrOrdersService.saveLabManifestOrder(manifestOrder);
} else if (specimenStatus == null || StringUtils.isBlank(specimenStatus)) {
// indicate the incomplete status
System.out.println("Lab Results Get Results: Setting Manifest order status to incomplete");
manifestOrder.setStatus("Incomplete");
manifestOrder.setResult("");
manifestOrder.setLastStatusCheckDate(new Date());
kenyaemrOrdersService.saveLabManifestOrder(manifestOrder);
} else {
// indicate the incomplete status
System.out.println("Lab Results Get Results: Setting Manifest order status to incomplete");
manifestOrder.setStatus("Incomplete");
manifestOrder.setResult("");
manifestOrder.setLastStatusCheckDate(new Date());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public ObjectNode baselinePostRequestPayload(Order o, Date dateSampleCollected,
test.put("datecollected", Utils.getSimpleDateFormat("yyyy-MM-dd").format(dateSampleCollected));
test.put("regimen", "1");
test.put("feeding", "yes");
test.put("sample_type", "DBS");
if(isKDoD.trim().equalsIgnoreCase("true")) {
test.put("hei_id", kdod != null ? kdod : "");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,16 @@ public void pullResult(List<Integer> orderIds, List<Integer> manifestOrderIds, L
String serverUrl = "";
String API_KEY = "";

System.out.println("Get Labware Lab Results: Manifest ID is: " + manifestToUpdateResults.getId());
System.out.println("Get Labware Lab Results: Manifest type is: " + manifestToUpdateResults.getManifestType());
if(manifestToUpdateResults.getManifestType() == LabManifest.EID_TYPE) {
System.out.println("Get Labware Lab Results: Getting EID global properties");
GlobalProperty gpEIDServerPullUrl = Context.getAdministrationService().getGlobalPropertyObject(ModuleConstants.GP_LABWARE_EID_LAB_SERVER_RESULT_URL);
GlobalProperty gpEIDApiToken = Context.getAdministrationService().getGlobalPropertyObject(ModuleConstants.GP_LABWARE_EID_LAB_SERVER_API_TOKEN);
serverUrl = gpEIDServerPullUrl.getPropertyValue().trim();
API_KEY = gpEIDApiToken.getPropertyValue().trim();
} else if(manifestToUpdateResults.getManifestType() == LabManifest.VL_TYPE) {
System.out.println("Get Labware Lab Results: Getting VL global properties");
GlobalProperty gpVLServerPullUrl = Context.getAdministrationService().getGlobalPropertyObject(ModuleConstants.GP_LABWARE_VL_LAB_SERVER_RESULT_URL);
GlobalProperty gpVLApiToken = Context.getAdministrationService().getGlobalPropertyObject(ModuleConstants.GP_LABWARE_VL_LAB_SERVER_API_TOKEN);
serverUrl = gpVLServerPullUrl.getPropertyValue().trim();
Expand All @@ -242,7 +246,6 @@ public void pullResult(List<Integer> orderIds, List<Integer> manifestOrderIds, L
GlobalProperty gpLastProcessedManifest = Context.getAdministrationService().getGlobalPropertyObject(ModuleConstants.GP_MANIFEST_LAST_PROCESSED);
GlobalProperty gpLastProcessedManifestUpdatetime = Context.getAdministrationService().getGlobalPropertyObject(ModuleConstants.GP_MANIFEST_LAST_UPDATETIME);


//Using SSL
SSLConnectionSocketFactory sslsf = null;
GlobalProperty gpSslVerification = Context.getAdministrationService().getGlobalPropertyObject(ModuleConstants.GP_SSL_VERIFICATION_ENABLED);
Expand All @@ -263,6 +266,7 @@ public void pullResult(List<Integer> orderIds, List<Integer> manifestOrderIds, L
try {
String facilityCode = Utils.getDefaultLocationMflCode(Utils.getDefaultLocation());
String orderNumbers = StringUtils.join(orderIds, ",");
System.out.println("Get Labware Lab Results: Server URL is: " + serverUrl);
URIBuilder builder = new URIBuilder(serverUrl);
builder.addParameter("mfl_code", facilityCode);
builder.addParameter("order_no", orderNumbers);
Expand Down Expand Up @@ -290,7 +294,7 @@ public void pullResult(List<Integer> orderIds, List<Integer> manifestOrderIds, L

try {
jsonString = rd.lines().collect(Collectors.joining()).toString();
System.out.println("Labware Lab Results Get: Request JSON -> " + jsonString);
System.out.println("Labware Lab Results Get: We got a response JSON : " + jsonString);
} finally {
rd.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void execute() {
return;
}

System.out.println("Lab Results Get: Polling results for " + ordersWithPendingResults.size() + " samples in currently processing manifest with id :" + manifestToUpdateResults.getId());
System.out.println("Lab Results Get: Polling results for " + ordersWithPendingResults.size() + " samples in currently processing manifest with id : " + manifestToUpdateResults.getId());
}

if (ordersWithPendingResults.size() < 1) { // exit there are no pending samples
Expand Down

0 comments on commit 7f914e1

Please sign in to comment.