Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2432,11 +2432,11 @@ public Map sendSingleViralLoadOrder(Order order) {
String payload = processResourceFromOrder(order);

if (payload != null) {
if (!validateTestFHIRBundle(payload,order.getConcept().getUuid())) {
if (!validateTestFHIRBundle(payload, order.getConcept().getUuid())) {
String missingObsInPayload = String.format(
"Order: %s is not valid due to missing %s in the required field",
order.getAccessionNumber(),
getMissingVLFHIRCodesAsString(payload,order.getConcept().getUuid())
getMissingVLFHIRCodesAsString(payload, order.getConcept().getUuid())
);
logTransaction(syncTaskType, 500, missingObsInPayload, order.getAccessionNumber(),
missingObsInPayload,
Expand Down Expand Up @@ -2666,44 +2666,42 @@ private String addResourceToBundle(String resourceString) {


private String processResourceFromOrder(Order order) {

String healthCenterIdentifier = "";
try { healthCenterIdentifier = Context.getAdministrationService().getGlobalProperty(GP_DHIS2); }
catch (Exception e) { log.error("Failed to fetch DHIS2 identifier", e); }

SyncFHIRRecord syncFHIRRecord = new SyncFHIRRecord();
Collection<String> resources = new ArrayList<>();
String finalCaseBundle = null;

List<Order> orderList = new ArrayList<>();
orderList.add(order);
List<Encounter> encounter = new ArrayList<>();
List<Order> orderList = new ArrayList<>(); orderList.add(order);
List<Encounter> encounter = new ArrayList<>(); encounter.add(order.getEncounter());

List<PatientIdentifier> patientArrayList = new ArrayList<>();
patientArrayList.add(order.getPatient().getPatientIdentifier());

List<Person> personList = new ArrayList<>();
personList.add(order.getPatient().getPerson());
List<PatientIdentifier> patientArrayList = new ArrayList<>(); patientArrayList.add(order.getPatient().getPatientIdentifier());
List<Person> personList = new ArrayList<>(); personList.add(order.getPatient().getPerson());

String specimenSource = generateSpecimen(order);

encounter.add(order.getEncounter());

resources.addAll(addSpecimenSource(syncFHIRRecord.groupInCaseBundle("ServiceRequest", syncFHIRRecord.getServiceRequestResourceBundle(orderList), "HIV Clinic No."), order));

resources.addAll(syncFHIRRecord.groupInCaseBundle("Encounter", syncFHIRRecord.getEncounterResourceBundle(encounter), "HIV Clinic No."));

resources.addAll(syncFHIRRecord.groupInCaseBundle("Patient", syncFHIRRecord.getPatientResourceBundle(null, patientArrayList, null), "HIV Clinic No."));

resources.addAll(syncFHIRRecord.groupInCaseBundle("Practitioner", syncFHIRRecord.getPractitionerResourceBundle(null, encounter, orderList), "HIV Clinic No."));

resources.addAll(syncFHIRRecord.groupInCaseBundle("Observation", syncFHIRRecord.getObservationResourceBundle(null, encounter, personList), "HIV Clinic No."));

if (specimenSource != null) {
resources.add(specimenSource);
}
if (specimenSource != null) resources.add(specimenSource);

if (!resources.isEmpty()) {
finalCaseBundle = String.format(FHIR_BUNDLE_CASE_RESOURCE_TRANSACTION, resources.toString());
if (!resources.isEmpty() && healthCenterIdentifier != null && !healthCenterIdentifier.isEmpty() && order.getAccessionNumber() != null) {

String bundleIdentifier = String.format(FHIR_BUNDLE_IDENTIFIER, "ugandaemr", healthCenterIdentifier, order.getAccessionNumber());
finalCaseBundle = String.format(FHIR_BUNDLE_CASE_RESOURCE_TRANSACTION_WITH_IDENTIFIER, bundleIdentifier, "[" + String.join(",", resources) + "]");
}

return finalCaseBundle;
}



private Collection<String> addSpecimenSource(Collection<String> serviceRequests, Order order) {
TestOrder testOrder = (TestOrder) order;
Collection<String> serviceRequestList = new ArrayList<>();
Expand All @@ -2720,6 +2718,22 @@ private Collection<String> addSpecimenSource(Collection<String> serviceRequests,
return serviceRequestList;
}

private Collection<String> addBundleIdentification(Collection<String> serviceRequests, Order order) {
TestOrder testOrder = (TestOrder) order;
Collection<String> serviceRequestList = new ArrayList<>();
for (String serviceRequest : serviceRequests) {
if (testOrder.getAccessionNumber() != null) {
JSONObject jsonObject = new JSONObject(serviceRequest);

jsonObject.getJSONObject("resource").put("specimen", new JSONArray(String.format("[{\"reference\":\"Specimen/%s\"}]", testOrder.getAccessionNumber())));
serviceRequestList.add(jsonObject.toString());
} else {
serviceRequestList.add(serviceRequest);
}
}
return serviceRequestList;
}

/**
* Extracts the patient_id field from the response map's "data" object (if present).
*/
Expand Down Expand Up @@ -2806,7 +2820,7 @@ private JSONArray getProductCatalogFromEAFYA(SyncTaskType syncTaskType) {
return eAFYAProductList;
}

public boolean validateTestFHIRBundle(String bundleJson,String orderConceptUuid) {
public boolean validateTestFHIRBundle(String bundleJson, String orderConceptUuid) {
List<String> targetCodes = getTargetCodes(orderConceptUuid);
Set<String> foundCodes = new HashSet<>();

Expand Down Expand Up @@ -2848,7 +2862,7 @@ private List<String> getTargetCodes(String orderConceptUuid) {
return targetCodes;
}

public String getMissingVLFHIRCodesAsString(String bundleJson,String orderConceptUuid) {
public String getMissingVLFHIRCodesAsString(String bundleJson, String orderConceptUuid) {
List<String> targetCodes = getTargetCodes(orderConceptUuid);

Set<String> foundCodes = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ public class SyncConstant {

public static final String FHIR_BUNDLE_RESOURCE_TRANSACTION ="{\"resourceType\":\"Bundle\",\"type\":\"transaction\",\"entry\":[%s]}";
public static final String FHIR_BUNDLE_CASE_RESOURCE_TRANSACTION ="{\"resourceType\":\"Bundle\",\"type\":\"transaction\",\"entry\":%s}";
public static final String FHIR_BUNDLE_CASE_RESOURCE_TRANSACTION_WITH_IDENTIFIER ="{\"resourceType\":\"Bundle\",\"type\":\"transaction\",\"identifier\":%s,\"entry\":%s}";
public static final String FHIR_BUNDLE_IDENTIFIER = "{\"system\":\"https://www.cphl.go.ug/fhir/identifier/bundle/%s\",\"value\":\"%s-%s\"}";
public static final String FHIR_BUNDLE_RESOURCE_METHOD_POST ="{\"resource\":%s,\"request\":{\"method\":\"POST\"}}";
public static final String FHIR_BUNDLE_RESOURCE_METHOD_PUT ="{\"resource\":%s,\"request\":{\"method\":\"PUT\",\"url\":\"%s\"}}";
public static final String FHIR_CODING_DATATYPE ="{\"system\":\"%s\",\"code\":\"%s\",\"display\":\"%s\"}";
Expand Down