From 01c07dba9fc54602baac86866858a69ddf02a04e Mon Sep 17 00:00:00 2001 From: Kipkemoii Date: Fri, 20 Dec 2024 15:38:31 +0300 Subject: [PATCH] fixed defaulter tracing and ovc --- .../models/AMRSDefaulterTracing.java | 18 ++ .../AMRSDefaulterTracingRepository.java | 1 + .../repositories/AMRSOvcRepository.java | 1 + .../service/AMRSDefaulterTracingService.java | 4 + .../amrs_kenyaemr/service/AMRSOvcService.java | 4 + .../amrs_kenyaemr/tasks/MigrateCareData.java | 165 ++++++++---------- .../tasks/payloads/CareOpenMRSPayload.java | 131 +++++++------- 7 files changed, 168 insertions(+), 156 deletions(-) diff --git a/src/main/java/ampath/co/ke/amrs_kenyaemr/models/AMRSDefaulterTracing.java b/src/main/java/ampath/co/ke/amrs_kenyaemr/models/AMRSDefaulterTracing.java index 1d1a526..ea83c85 100644 --- a/src/main/java/ampath/co/ke/amrs_kenyaemr/models/AMRSDefaulterTracing.java +++ b/src/main/java/ampath/co/ke/amrs_kenyaemr/models/AMRSDefaulterTracing.java @@ -23,6 +23,8 @@ public class AMRSDefaulterTracing { private String kenyaEmrPatientUuid; private String kenyaEmrFormUuid; private String responseCode; + private String visitId; + private String obsDatetime; public Integer getId() { return Id; @@ -127,4 +129,20 @@ public String getResponseCode() { public void setResponseCode(String responseCode) { this.responseCode = responseCode; } + + public String getVisitId() { + return visitId; + } + + public void setVisitId(String visitId) { + this.visitId = visitId; + } + + public String getObsDatetime() { + return obsDatetime; + } + + public void setObsDatetime(String obsDatetime) { + this.obsDatetime = obsDatetime; + } } diff --git a/src/main/java/ampath/co/ke/amrs_kenyaemr/repositories/AMRSDefaulterTracingRepository.java b/src/main/java/ampath/co/ke/amrs_kenyaemr/repositories/AMRSDefaulterTracingRepository.java index 167a9d2..60fb09a 100644 --- a/src/main/java/ampath/co/ke/amrs_kenyaemr/repositories/AMRSDefaulterTracingRepository.java +++ b/src/main/java/ampath/co/ke/amrs_kenyaemr/repositories/AMRSDefaulterTracingRepository.java @@ -13,4 +13,5 @@ public interface AMRSDefaulterTracingRepository extends JpaRepository findFirstByOrderByIdDesc(); List findByResponseCodeIsNull(); List findByPatientId(String pid); + List findByVisitId(String vid); } diff --git a/src/main/java/ampath/co/ke/amrs_kenyaemr/repositories/AMRSOvcRepository.java b/src/main/java/ampath/co/ke/amrs_kenyaemr/repositories/AMRSOvcRepository.java index 3325110..5f43c35 100644 --- a/src/main/java/ampath/co/ke/amrs_kenyaemr/repositories/AMRSOvcRepository.java +++ b/src/main/java/ampath/co/ke/amrs_kenyaemr/repositories/AMRSOvcRepository.java @@ -12,4 +12,5 @@ public interface AMRSOvcRepository extends JpaRepository { List findFirstByOrderByIdDesc(); List findByResponseCodeIsNull(); List findByPatientId(String pid); + List findByVisitId(String vid); } diff --git a/src/main/java/ampath/co/ke/amrs_kenyaemr/service/AMRSDefaulterTracingService.java b/src/main/java/ampath/co/ke/amrs_kenyaemr/service/AMRSDefaulterTracingService.java index 832843d..ec3a976 100644 --- a/src/main/java/ampath/co/ke/amrs_kenyaemr/service/AMRSDefaulterTracingService.java +++ b/src/main/java/ampath/co/ke/amrs_kenyaemr/service/AMRSDefaulterTracingService.java @@ -34,6 +34,10 @@ public List findByPatientId(String pid) { return amrsDefaulterTracingRepository.findByPatientId(pid); } + public List findByVisitId(String vid) { + return amrsDefaulterTracingRepository.findByVisitId(vid); + } + public AMRSDefaulterTracing save(AMRSDefaulterTracing dataset) { return amrsDefaulterTracingRepository.save(dataset); } diff --git a/src/main/java/ampath/co/ke/amrs_kenyaemr/service/AMRSOvcService.java b/src/main/java/ampath/co/ke/amrs_kenyaemr/service/AMRSOvcService.java index 545c95b..b87ef07 100644 --- a/src/main/java/ampath/co/ke/amrs_kenyaemr/service/AMRSOvcService.java +++ b/src/main/java/ampath/co/ke/amrs_kenyaemr/service/AMRSOvcService.java @@ -35,6 +35,10 @@ public List findByPatientId(String pid) { return amrsOvcRepository.findByPatientId(pid); } + public List findByVisitId(String vid) { + return amrsOvcRepository.findByVisitId(vid); + } + public AMRSOvc save(AMRSOvc dataset) { return amrsOvcRepository.save(dataset); } diff --git a/src/main/java/ampath/co/ke/amrs_kenyaemr/tasks/MigrateCareData.java b/src/main/java/ampath/co/ke/amrs_kenyaemr/tasks/MigrateCareData.java index c27e542..a8d7c26 100644 --- a/src/main/java/ampath/co/ke/amrs_kenyaemr/tasks/MigrateCareData.java +++ b/src/main/java/ampath/co/ke/amrs_kenyaemr/tasks/MigrateCareData.java @@ -3572,22 +3572,26 @@ public static void artRefill(String server, String username, String password, St public static void defaulterTracing(String server,String username,String password,String locations,String parentUUID,AMRSDefaulterTracingService amrsDefaulterTracingService,AMRSTranslater amrsTranslater,AMRSPatientServices amrsPatientServices,String url,String auth) throws IOException, SQLException, JSONException { - List amrsPatientsList = amrsPatientServices.getAll(); - String pidss = ""; - for (int y = 0; y < amrsPatientsList.size(); y++) { - pidss += amrsPatientsList.get(y).getPersonId() + ","; - } - String pid = pidss.substring(0, pidss.length() - 1); - - System.out.println("Patient Id " + pid); - +// List amrsPatientsList = amrsPatientServices.getAll(); +// String pidss = ""; +// for (int y = 0; y < amrsPatientsList.size(); y++) { +// pidss += amrsPatientsList.get(y).getPersonId() + ","; +// } +// String pid = pidss.substring(0, pidss.length() - 1); +// +// System.out.println("Patient Id " + pid); +// +// +// System.out.println("Patient Id " + pid); - System.out.println("Patient Id " + pid); + String samplePatientList = AMRSSamples.getPersonIdList(); String sql = "SELECT o.person_id as patient_id,\n" + "e.form_id,\n" + "o.concept_id,\n" + + " e.visit_id,\n"+ "o.encounter_id,\n" + + "e.encounter_datetime,\n" + "cn.name question,\n" + "case when o.value_datetime is not null then o.value_datetime\n" + " when o.value_coded is not null then o.value_coded\n" + @@ -3621,45 +3625,39 @@ public static void defaulterTracing(String server,String username,String passwor String encounterId = rs.getString("encounter_id"); String question = rs.getString("question"); String value = rs.getString(("value")); + String visitId = rs.getString(("visit_id")); + String obsDatetime = rs.getString(("encounter_datetime")); - AMRSDefaulterTracing amrsDefaulterTracing = new AMRSDefaulterTracing(); - - - amrsDefaulterTracing.setPatientId(patientId); - amrsDefaulterTracing.setFormId(formId); - amrsDefaulterTracing.setConceptId(conceptId); - amrsDefaulterTracing.setEncounterId(encounterId); - amrsDefaulterTracing.setQuestion(question); - amrsDefaulterTracing.setValue(value); - - List amrsPatients = amrsPatientServices.getByPatientID(patientId); - String kenyaemr_patient_uuid = ""; - if (!amrsPatients.isEmpty()) { - kenyaemr_patient_uuid = amrsPatients.get(0).getKenyaemrpatientUUID(); - amrsDefaulterTracing.setKenyaEmrPatientUuid(kenyaemr_patient_uuid); - } else { - kenyaemr_patient_uuid = "Not Found"; // add logic for missing patientkenyaemr_patient_uuid - } - - - String kenyaEmrEncounterUuid = "1495edf8-2df2-11e9-b210-d663bd873d93"; - amrsDefaulterTracing.setKenyaEmrEncounterUuid(kenyaEmrEncounterUuid); - - - - String kenyaEmrFormUuid = "a1a62d1e-2def-11e9-b210-d663bd873d93"; - amrsDefaulterTracing.setKenyaEmrFormUuid(kenyaEmrFormUuid); - String kenyaEmrConceptUuid = ""; - kenyaEmrConceptUuid = amrsTranslater.translater(conceptId); - amrsDefaulterTracing.setKenyaEmrConceptUuid(kenyaEmrConceptUuid); + List amrsDefaulterTracingList =amrsDefaulterTracingService.findByPatientIdAndEncounterIdAndConceptId(patientId,encounterId,conceptId); + if(amrsDefaulterTracingList.isEmpty()) { + AMRSDefaulterTracing amrsDefaulterTracing = new AMRSDefaulterTracing(); + amrsDefaulterTracing.setPatientId(patientId); + amrsDefaulterTracing.setFormId(formId); + amrsDefaulterTracing.setConceptId(conceptId); + amrsDefaulterTracing.setEncounterId(encounterId); + amrsDefaulterTracing.setQuestion(question); + amrsDefaulterTracing.setValue(value); + amrsDefaulterTracing.setVisitId(visitId); + amrsDefaulterTracing.setObsDatetime(obsDatetime); - String kenyaEmrValueUuid = ""; - kenyaEmrValueUuid = amrsTranslater.translater(value); - amrsDefaulterTracing.setKenyaEmrValueUuid(kenyaEmrValueUuid); + String kenyaemrPatientUuid = ""; + kenyaemrPatientUuid = amrsTranslater.KenyaemrPatientUuid(patientId); + amrsDefaulterTracing.setKenyaEmrPatientUuid(kenyaemrPatientUuid); + String kenyaEmrEncounterUuid = "1495edf8-2df2-11e9-b210-d663bd873d93"; + amrsDefaulterTracing.setKenyaEmrEncounterUuid(kenyaEmrEncounterUuid); + String kenyaEmrFormUuid = "a1a62d1e-2def-11e9-b210-d663bd873d93"; + amrsDefaulterTracing.setKenyaEmrFormUuid(kenyaEmrFormUuid); + String kenyaEmrConceptUuid = ""; + kenyaEmrConceptUuid = amrsTranslater.translater(conceptId); + amrsDefaulterTracing.setKenyaEmrConceptUuid(kenyaEmrConceptUuid); + String kenyaEmrValueUuid = ""; + kenyaEmrValueUuid = amrsTranslater.translater(value); + amrsDefaulterTracing.setKenyaEmrValueUuid(kenyaEmrValueUuid); - amrsDefaulterTracingService.save(amrsDefaulterTracing); + amrsDefaulterTracingService.save(amrsDefaulterTracing); + } // Call method to create and insert the payload } @@ -4083,60 +4081,43 @@ public static void ovc(String server,String username,String password,String loca String encounterDatetime = rs.getString("encounter_datetime"); - - AMRSOvc amrsOvc = new AMRSOvc(); - - - amrsOvc.setPatientId(patientId); - amrsOvc.setFormId(formId); - amrsOvc.setConceptId(conceptId); - amrsOvc.setEncounterId(encounterId); - amrsOvc.setQuestion(question); - amrsOvc.setValue(value); - amrsOvc.setVisitId(visitId); - amrsOvc.setObsDateTime(obsDatetime); - amrsOvc.setKenyaEmrEncounterDateTime(encounterDatetime); - -// List amrsPatients = amrsPatientServices.getByPatientID(patientId); -// String kenyaemr_patient_uuid = ""; -// if (!amrsPatients.isEmpty()) { -// kenyaemr_patient_uuid = amrsPatients.get(0).getKenyaemrpatientUUID(); -// amrsOvc.setKenyaemrPatientUuid(kenyaemr_patient_uuid); -// } else { -// kenyaemr_patient_uuid = "Not Found"; // add logic for missing patientkenyaemr_patient_uuid -// } - String kenyaEmrPatientUuid = ""; - kenyaEmrPatientUuid = amrsTranslater.KenyaemrPatientUuid(patientId); - amrsOvc.setKenyaemrPatientUuid(kenyaEmrPatientUuid); - - - String kenyaEmrEncounterUuid = "5cf00d9e-09da-11ea-8d71-362b9e155667"; - amrsOvc.setKenyaemrEncounterTypeUuid(kenyaEmrEncounterUuid); - - - - String kenyaEmrFormUuid = "5cf013e8-09da-11ea-8d71-362b9e155667"; - amrsOvc.setKenyaemrFormUuid(kenyaEmrFormUuid); - - String kenyaemrVisitUuid = ""; - kenyaemrVisitUuid = amrsTranslater.kenyaemrVisitUuid(visitId); - amrsOvc.setKenyaemrVisitUuid(kenyaemrVisitUuid); - - String kenyaEmrConceptUuid = ""; - kenyaEmrConceptUuid = amrsTranslater.translater(conceptId); - amrsOvc.setKenyaEmrConceptUuid(kenyaEmrConceptUuid); - - String kenyaEmrValueUuid = ""; - kenyaEmrValueUuid = amrsTranslater.translater(value); - amrsOvc.setKenyaEmrValue(kenyaEmrValueUuid); + List amrsOvcList =amrsOvcService.findByPatientIdAndEncounterIdAndConceptId(patientId,encounterId,conceptId); + if(amrsOvcList.isEmpty()) { + AMRSOvc amrsOvc = new AMRSOvc(); + amrsOvc.setPatientId(patientId); + amrsOvc.setFormId(formId); + amrsOvc.setConceptId(conceptId); + amrsOvc.setEncounterId(encounterId); + amrsOvc.setQuestion(question); + amrsOvc.setValue(value); + amrsOvc.setVisitId(visitId); + amrsOvc.setObsDateTime(obsDatetime); + amrsOvc.setKenyaEmrEncounterDateTime(encounterDatetime); + String kenyaEmrPatientUuid = ""; + kenyaEmrPatientUuid = amrsTranslater.KenyaemrPatientUuid(patientId); + amrsOvc.setKenyaemrPatientUuid(kenyaEmrPatientUuid); + String kenyaEmrEncounterUuid = "5cf00d9e-09da-11ea-8d71-362b9e155667"; + amrsOvc.setKenyaemrEncounterTypeUuid(kenyaEmrEncounterUuid); + String kenyaEmrFormUuid = "5cf013e8-09da-11ea-8d71-362b9e155667"; + amrsOvc.setKenyaemrFormUuid(kenyaEmrFormUuid); + String kenyaemrVisitUuid = ""; + kenyaemrVisitUuid = amrsTranslater.kenyaemrVisitUuid(visitId); + amrsOvc.setKenyaemrVisitUuid(kenyaemrVisitUuid); + String kenyaEmrConceptUuid = ""; + kenyaEmrConceptUuid = amrsTranslater.translater(conceptId); + amrsOvc.setKenyaEmrConceptUuid(kenyaEmrConceptUuid); + String kenyaEmrValueUuid = ""; + kenyaEmrValueUuid = amrsTranslater.translater(value); + amrsOvc.setKenyaEmrValue(kenyaEmrValueUuid); - amrsOvcService.save(amrsOvc); + amrsOvcService.save(amrsOvc); + } // Call method to create and insert the payload } - CareOpenMRSPayload.ovc(amrsOvcService, amrsPatientServices, url, auth); + CareOpenMRSPayload.ovc(amrsOvcService, amrsPatientServices, amrsTranslater, url, auth); } } diff --git a/src/main/java/ampath/co/ke/amrs_kenyaemr/tasks/payloads/CareOpenMRSPayload.java b/src/main/java/ampath/co/ke/amrs_kenyaemr/tasks/payloads/CareOpenMRSPayload.java index 9b13ce7..67dd270 100644 --- a/src/main/java/ampath/co/ke/amrs_kenyaemr/tasks/payloads/CareOpenMRSPayload.java +++ b/src/main/java/ampath/co/ke/amrs_kenyaemr/tasks/payloads/CareOpenMRSPayload.java @@ -712,48 +712,52 @@ public static void defaulterTracing(AMRSDefaulterTracingService amrsDefaulterTra if (!amrsDefaulterTracings.isEmpty()) { // Use a Set to store unique patient IDs - Set patientIdSet = new HashSet<>(); - List distinctPatientIds = new ArrayList<>(); + Set visitIdSet = new HashSet<>(); + List distinctVisitIds = new ArrayList<>(); // Collect unique patient IDs for (AMRSDefaulterTracing defaulterTracing : amrsDefaulterTracings) { - if (defaulterTracing.getResponseCode() == null && patientIdSet.add(defaulterTracing.getPatientId())) { - distinctPatientIds.add(defaulterTracing.getPatientId()); + if (defaulterTracing.getResponseCode() == null && visitIdSet.add(defaulterTracing.getVisitId())) { + distinctVisitIds.add(defaulterTracing.getVisitId()); } } - System.out.println("list of distinct clients " + distinctPatientIds.toString()); + System.out.println("list of distinct clients " + distinctVisitIds); - for (String patientId : distinctPatientIds) { - System.out.println("Processing Patient ID: " + patientId); + for (String visitId : distinctVisitIds) { + System.out.println("Processing Visit ID: " + visitId); - String kenyaemrPatientUuid = amrsTranslater.KenyaemrPatientUuid(patientId); - - List amrsDefaulterTracingList = amrsDefaulterTracingService.findByPatientId(patientId); - - // Prepare JSON observations + List amrsDefaulterTracingList = amrsDefaulterTracingService.findByVisitId(visitId); JSONArray jsonObservations = new JSONArray(); - JSONObject jsonObservationD = new JSONObject(); - jsonObservationD.put("person", kenyaemrPatientUuid); - if(!Objects.equals(amrsDefaulterTracingList.get(0).getKenyaEmrConceptUuid(), "") || !amrsDefaulterTracingList.get(0).getKenyaEmrConceptUuid().isEmpty()) { - jsonObservationD.put("concept", amrsDefaulterTracingList.get(0).getKenyaEmrConceptUuid()); - } - if(!Objects.equals(amrsDefaulterTracingList.get(0).getKenyaEmrValueUuid(), "") || !amrsDefaulterTracingList.get(0).getKenyaEmrValueUuid().isEmpty()) { - jsonObservationD.put("value", amrsDefaulterTracingList.get(0).getKenyaEmrValueUuid()); - } + String kenyaemrPatientUuid = ""; + String kenyaemrVisitUuid = ""; + String obsDatetime = ""; - if(!Objects.equals(amrsDefaulterTracingList.get(0).getKenyaEmrConceptUuid(), "") && !Objects.equals(amrsDefaulterTracingList.get(0).getKenyaEmrValueUuid(), "") ) { - - jsonObservations.put(jsonObservationD); + // Prepare JSON observations + for(int x=0; x amrsOvcs = amrsOvcService.findByResponseCodeIsNull(); if (!amrsOvcs.isEmpty()) { // Use a Set to store unique patient IDs - Set patientIdSet = new HashSet<>(); - List distinctPatientIds = new ArrayList<>(); + Set visitIdSet = new HashSet<>(); + List distinctVisitIds = new ArrayList<>(); // Collect unique patient IDs for (AMRSOvc amrsOvc : amrsOvcs) { - if (amrsOvc.getResponseCode() == null && patientIdSet.add(amrsOvc.getPatientId())) { - distinctPatientIds.add(amrsOvc.getPatientId()); + if (amrsOvc.getResponseCode() == null && visitIdSet.add(amrsOvc.getPatientId())) { + distinctVisitIds.add(amrsOvc.getPatientId()); } } - System.out.println("list of distinct clients " + distinctPatientIds); + System.out.println("list of distinct clients " + distinctVisitIds); - for (String patientId : distinctPatientIds) { - System.out.println("Processing Patient ID: " + patientId); + for (String visitId : distinctVisitIds) { + System.out.println("Processing visit ID: " + visitId); - // Fetch patient status and enrollment details - List patientStatusList = amrsPatientServices.getByPatientID(patientId); - if (patientStatusList.isEmpty()) { - System.err.println("No patient status found for Patient ID: " + patientId); - continue; // Skip if no patient status is found - } - String kenyaemrPatientUuid = patientStatusList.get(0).getKenyaemrpatientUUID(); - List amrsOvcList = amrsOvcService.findByPatientId(patientId); + List amrsOvcList = amrsOvcService.findByVisitId(visitId); + JSONArray jsonObservations = new JSONArray(); + String kenyaemrPatientUuid = ""; + String kenyaemrVisitUuid = ""; + String obsDatetime = ""; // Prepare JSON observations - JSONArray jsonObservations = new JSONArray(); - JSONObject jsonObservationD = new JSONObject(); - jsonObservationD.put("person", kenyaemrPatientUuid); - if(!amrsOvcList.get(0).getKenyaEmrConceptUuid().isEmpty() || !Objects.equals(amrsOvcList.get(0).getKenyaEmrConceptUuid(), "") || amrsOvcList.get(0).getKenyaEmrConceptUuid() != null) { + for(int x=0; x