Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

U4X-738: VL Program Data improvisions #178

Merged
merged 5 commits into from
Oct 16, 2024
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 @@ -483,5 +483,7 @@ public Encounter addVLToEncounter(String vlQualitative, String vlQuantitative, S
public List<SyncFhirResource> getSyncFHIRResourceBySyncFhirProfile(SyncFhirProfile syncFhirProfile, String synceDateFrom, String synceDateTo);

public List<SyncTask> searchSyncTask(SyncTaskType syncTaskType,Integer statusCode,Date fromDate, Date toDate);

public void deleteSyncTask(String syncTask, SyncTaskType syncTaskType);
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
package org.openmrs.module.ugandaemrsync.api.dao;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StringType;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.api.db.hibernate.DbSession;
import org.openmrs.api.db.hibernate.DbSessionFactory;
import org.openmrs.module.ugandaemrsync.model.SyncTask;
Expand Down Expand Up @@ -471,4 +474,8 @@ public List<SyncTask> searchSyncTask(SyncTaskType syncTaskType, Integer statusCo
}
return criteria.list();
}

public void deleteUnSuccessfulSyncTasks(String syncTask, SyncTaskType syncTaskType) {
Context.getAdministrationService().executeSQL(String.format("delete from sync_task where status_code != %s and sync_task = '%s' and sync_task_type= %s",200,syncTask,syncTaskType.getSyncTaskTypeId()),false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1172,5 +1172,10 @@ public List<SyncTask> getSyncTasksByType(SyncTaskType syncTaskType) {
public List<SyncTask> searchSyncTask(SyncTaskType syncTaskType,Integer statusCode,Date fromDate, Date toDate) {
return dao.searchSyncTask( syncTaskType, statusCode, fromDate, toDate);
}

@Override
public void deleteSyncTask(String syncTask, SyncTaskType syncTaskType) {
dao.deleteUnSuccessfulSyncTasks(syncTask,syncTaskType);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -503,5 +503,10 @@ public class SyncConstant {
public static List<String> VL_SYNC_TASK_COLUMNS = Arrays.asList("sync_task_id", "sync_task_type", "sync_task", "status", "status_code", "sent_to_url", "require_action", "action_completed", "date_sent", "creator", "date_created", "changed_by", "date_changed", "voided", "date_voided", "voided_by ", "void_reason", "uuid");

public static final String Latest_obs_of_Person = "SELECT %s from obs where person_id=%s and concept_id=%s and obs_datetime <= '%s' and voided= 0 ORDER BY obs_datetime DESC LIMIT 1";
public static final String Latest_drug_order_of_person = "SELECT o.concept_id FROM orders o\n" +
" INNER JOIN order_type ot ON o.order_type_id = ot.order_type_id\n" +
" INNER JOIN drug_order d_o ON o.order_id = d_o.order_id\n" +
"where ot.uuid='131168f4-15f5-102d-96e4-000c29c2a5d7' and o.voided=0 and patient_id=%s and DATE(date_activated) <='%s'" +
" and o.concept_id in (SELECT answer_concept from concept_answer where concept_id=90315) ORDER BY date_activated DESC LIMIT 1\n";
public static final String ALIS_SYNC_TASK_TYPE_UUID = "d4a3ebbb-e793-4e56-867c-0cf998e51f56";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,12 @@

import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

import static org.openmrs.module.ugandaemrsync.server.SyncConstant.VIRAL_LOAD_SYNC_TYPE_UUID;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.VL_PROGRAM_DATA_SYNC_TYPE_UUID;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.VL_SEND_PROGRAM_DATA_FHIR_JSON_STRING;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.PATIENT_IDENTIFIER_TYPE;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.OPENMRS_IDENTIFIER_TYPE_UUID;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.ANC_IDENTIFIER_TYPE_UUID;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.NATIONAL_ID_IDENTIFIER_TYPE_UUID;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.PNC_IDENTIFIER_TYPE_UUID;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.Latest_obs_of_Person;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.firstLineBody;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.secondLineBody;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.thirdLineBody;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.VIRAL_LOAD_ORDERS_QUERY;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.REGIMEN_LINE_QUERY;
import static org.openmrs.module.ugandaemrsync.server.SyncConstant.*;

/**
* Posts Viral load PROGRAM data to the central server
Expand Down Expand Up @@ -79,6 +68,7 @@ public void execute() {
String json = dataOutput.get("json");
String empty_fields = dataOutput.get("empty_fields");
String patientARTno = dataOutput.get("patient");
ugandaEMRSyncService.deleteSyncTask(order.getAccessionNumber(),syncTaskType);
Map map = ugandaEMRHttpURLConnection.sendPostBy(syncTaskType.getUrl(), syncTaskType.getUrlUserName(), syncTaskType.getUrlPassword(), "", json, false);
if (map != null) {
SyncTask newSyncTask = new SyncTask();
Expand Down Expand Up @@ -109,6 +99,7 @@ public Map<String, String> generateVLProgramDataFHIRBody(TestOrder testOrder, St
Map<String, String> jsonMap = new HashMap<>();
UgandaEMRSyncService ugandaEMRSyncService = new UgandaEMRSyncServiceImpl();
String filledJsonFile = "";
String empty_fields="";
if (testOrder != null) {
AdministrationService administrationService = Context.getAdministrationService();

Expand All @@ -125,17 +116,22 @@ public Map<String, String> generateVLProgramDataFHIRBody(TestOrder testOrder, St
String sampleID = testOrder.getAccessionNumber();
String gender = patient.getGender();
List current_regimenList = administrationService.executeSQL(String.format(Latest_obs_of_Person,"value_coded", patientId,90315,date_activated),true);
List current_regimen_orders_List = administrationService.executeSQL(String.format(Latest_drug_order_of_person, patientId,date_activated),true);

String empty_fields="";
String current_regimen="";
int regimen_code=0;
if(current_regimenList.size() > 0) {
ArrayList regimenList = (ArrayList) current_regimenList.get(0);
regimen_code = Integer.parseInt(regimenList.get(0).toString());
current_regimen = Context.getConceptService().getConcept(regimen_code).getName().getName();
}else if (current_regimen_orders_List.size()>0){
ArrayList regimenList = (ArrayList) current_regimen_orders_List.get(0);
regimen_code = Integer.parseInt(regimenList.get(0).toString());

current_regimen = Context.getConceptService().getConcept(regimen_code).getName().getName();
}
if(current_regimen.isEmpty())
empty_fields += empty_fields+", current regimen";
empty_fields += ", current regimen";

List obs_dsdmList = administrationService.executeSQL(String.format(Latest_obs_of_Person,"value_coded", patientId,165143,date_activated),true);

Expand All @@ -146,29 +142,29 @@ public Map<String, String> generateVLProgramDataFHIRBody(TestOrder testOrder, St
int dsdm_code = Integer.parseInt(myList.get(0).toString());


if(dsdm_code==165138){
if(dsdm_code==165138){
dsdm = "FBIM";
dsdm_hie_code = "734163000_01";
}
else if (dsdm_code==165140) {
}
else if (dsdm_code==165140) {
dsdm = "FBG";
dsdm_hie_code = "734163000_02";
}
else if(dsdm_code==165139){
else if(dsdm_code==165139){
dsdm = "FTDR";
dsdm_hie_code = "734163000_03";
}
else if (dsdm_code==165142) {
else if (dsdm_code==165142) {
dsdm = "CDDP";
dsdm_hie_code = "734163000_04";
}
else if(dsdm_code==165141){
else if(dsdm_code==165141){
dsdm = "CCLAD";
dsdm_hie_code = "734163000_05";
}
}
if(dsdm_hie_code.isEmpty())
empty_fields += empty_fields+", dsdm";
dsdm_hie_code="734163000_01";

List obs_adherenceList = administrationService.executeSQL(String.format(Latest_obs_of_Person,"value_coded", patientId,90221,date_activated),true);
String adherence="";
Expand All @@ -189,9 +185,12 @@ else if(dsdm_code==165141){
}
}
if(adherence_hie_code.isEmpty())
empty_fields += empty_fields+", adherence";
empty_fields += ", adherence";

List current_regimen_start_date = administrationService.executeSQL(String.format("SELECT TIMESTAMPDIFF(MONTH, obs_datetime,'%s') from obs where person_id=%s and concept_id=90315 and voided=0 and value_coded = %s ORDER BY obs_datetime ASC LIMIT 1",date_activated.toString(),patientId,regimen_code),true);
List current_regimen_start_date_by_orders = administrationService.executeSQL(String.format("SELECT TIMESTAMPDIFF(MONTH, date_activated,'%s') from orders o INNER JOIN order_type ot ON o.order_type_id = ot.order_type_id\n" +
"\t INNER JOIN drug_order d_o ON o.order_id = d_o.order_id\n" +
"\t where ot.uuid='131168f4-15f5-102d-96e4-000c29c2a5d7' and patient_id= %s and o.voided=0 and o.concept_id = %s ORDER BY date_activated ASC LIMIT 1",date_activated.toString(),patientId,regimen_code),true);

String duration_string="";
String duration_string_hie_code="";
Expand All @@ -218,9 +217,32 @@ else if( duration < 6) {
duration_string="< 6months";
duration_string_hie_code = "261773006_01";
}
} else if (current_regimen_start_date_by_orders.size()>0) {
ArrayList myList = (ArrayList) current_regimen_start_date_by_orders.get(0);
int duration= Integer.parseInt(myList.get(0).toString());
if(duration >=60) {
duration_string=">5yrs";
duration_string_hie_code = "261773006_05";
}
else if(duration >=24 && duration < 60 ) {
duration_string="2 -< 5yrs";
duration_string_hie_code = "261773006_04";
}
else if(duration >=12 && duration < 24 ) {
duration_string="1 - 2yrs";
duration_string_hie_code = "261773006_03";
}
else if(duration >=6 && duration < 12) {
duration_string="6 months - < 1yr";
duration_string_hie_code = "261773006_02";
}
else if( duration < 6) {
duration_string="< 6months";
duration_string_hie_code = "261773006_01";
}
}
if(duration_string_hie_code.isEmpty())
empty_fields += empty_fields+", duration on art";
if (duration_string_hie_code.isEmpty())
empty_fields += ", duration on art";

List obs_pregnantList = administrationService.executeSQL(String.format(Latest_obs_of_Person,"value_coded", patientId,90041,date_activated),true);

Expand Down Expand Up @@ -264,39 +286,46 @@ else if( duration < 6) {
}

if(artStartDate==null)
empty_fields += empty_fields+", ART Start Date";
empty_fields += ", ART Start Date";

List obs_indication_for_VL = administrationService.executeSQL(String.format(Latest_obs_of_Person,"value_coded", patientId,168689,date_activated),true);

String vl_indication_hie_code ="";
int vl_indicator_code=0;
if(obs_indication_for_VL.size()>0) {
ArrayList myList = (ArrayList) obs_indication_for_VL.get(0);
int vl_indicator_code = Integer.parseInt(myList.get(0).toString());

if(vl_indicator_code==168683){ // 6 months after ART initiation
vl_indication_hie_code = "315124004_01";
}
else if (vl_indicator_code==168684) { //12 months after ART initiation
vl_indication_hie_code = "315124004_02";
}
else if(vl_indicator_code==168688){ //Routine
vl_indication_hie_code = "315124004_03";
}
else if (vl_indicator_code==168687) { //Repeat (after IAC)
vl_indication_hie_code = "315124004_04";
}
else if(vl_indicator_code==168685){ //Suspected treatment failure
vl_indication_hie_code = "315124004_05";
}
else if (vl_indicator_code==166508) { //1st ANC visit
vl_indication_hie_code = "315124004_06";
vl_indicator_code = Integer.parseInt(myList.get(0).toString());

vl_indication_hie_code = getVl_indication_hie_code(vl_indicator_code);
}
if(vl_indication_hie_code.isEmpty()){
// do manual fill of vl indication warning : this is a hack
List last_vl = administrationService.executeSQL(String.format(Latest_obs_of_Person,"DATE(value_datetime)", patientId,163023,date_activated),true);
if(pregnant){
vl_indicator_code= 166508;
}else if(artStartDate!=null && isSixToSevenMonthsAgo(artStartDate,date_activated)){
vl_indicator_code=168683;
}else if( artStartDate!=null &&isTwelveToThirteenMonthsAgo(artStartDate,date_activated)){
vl_indicator_code = 168684;
}else if(last_vl.size()>0){
ArrayList myList = (ArrayList) last_vl.get(0);
Date last_vl_date = Context.getService(UgandaEMRSyncService.class).convertStringToDate(myList.get(0).toString(),"","yyyy-MM-dd");

if(patient.getAge() <= 15 && last_vl_date!=null && isSixToSevenMonthsAgo(last_vl_date,date_activated)){
vl_indicator_code=168688 ;
}else if(patient.getAge() >15 && last_vl_date!=null && isTwelveToThirteenMonthsAgo(last_vl_date,date_activated)){
vl_indicator_code=168688 ;
}else{
vl_indicator_code=168688 ;
}
}else{
vl_indicator_code=168688 ;
}
else if(vl_indicator_code==168686){ //Special Considerations
vl_indication_hie_code = "315124004_07";
vl_indication_hie_code = getVl_indication_hie_code(vl_indicator_code);
if(vl_indication_hie_code.isEmpty()){
empty_fields += " Indication for viral load";
}
}
if(vl_indication_hie_code.isEmpty())
empty_fields += empty_fields+", indication for vl";

List obs_WHOList = administrationService.executeSQL(String.format(Latest_obs_of_Person,"value_coded", patientId,90203,date_activated),true);

Expand Down Expand Up @@ -354,7 +383,7 @@ else if(vl_indicator_code==168686){ //Special Considerations
}
}
if(who_hie_code.isEmpty())
empty_fields += empty_fields+", who code";
empty_fields += ", who code";

String regimenline = getRegimenLineOfPatient(patient);
String coded_regimen_line ="";
Expand All @@ -366,7 +395,7 @@ else if(vl_indicator_code==168686){ //Special Considerations
coded_regimen_line=thirdLineBody;
}
if(coded_regimen_line.isEmpty())
empty_fields += empty_fields+", regimen_line";
coded_regimen_line= firstLineBody;

jsonMap.put("empty_fields",empty_fields);
jsonMap.put("patient",patientARTNO);
Expand All @@ -377,6 +406,34 @@ else if(vl_indicator_code==168686){ //Special Considerations
return jsonMap;
}

private static String getVl_indication_hie_code(int vl_indicator_code) {

String vl_indication_hie_code ="";

if(vl_indicator_code ==168683){ // 6 months after ART initiation
vl_indication_hie_code = "315124004_01";
}
else if (vl_indicator_code ==168684) { //12 months after ART initiation
vl_indication_hie_code = "315124004_02";
}
else if(vl_indicator_code ==168688){ //Routine
vl_indication_hie_code = "315124004_03";
}
else if (vl_indicator_code ==168687) { //Repeat (after IAC)
vl_indication_hie_code = "315124004_04";
}
else if(vl_indicator_code ==168685){ //Suspected treatment failure
vl_indication_hie_code = "315124004_05";
}
else if (vl_indicator_code ==166508) { //1st ANC visit
vl_indication_hie_code = "315124004_06";
}
else if(vl_indicator_code ==168686){ //Special Considerations
vl_indication_hie_code = "315124004_07";
}
return vl_indication_hie_code;
}


public List<Order> getOrders() throws IOException, ParseException {
OrderService orderService = Context.getOrderService();
Expand Down Expand Up @@ -419,4 +476,39 @@ private String getRegimenLineOfPatient(Patient patient){
}


public boolean isSixToSevenMonthsAgo(Date date,Date dateActivated) {
// Convert Date to LocalDate
LocalDate inputDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

// Get current date
LocalDate activeDate = dateActivated.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

// Calculate the date 6 and 7 months ago
LocalDate sixMonthsAgo = activeDate.minusMonths(6);
LocalDate sevenMonthsAgo = activeDate.minusMonths(7);

// Check if the input date is between 6 and 7 months ago
return (inputDate.isBefore(sevenMonthsAgo) || inputDate.isEqual(sevenMonthsAgo)) &&
(inputDate.isAfter(sixMonthsAgo) || inputDate.isEqual(sixMonthsAgo));
}

public boolean isTwelveToThirteenMonthsAgo(Date date,Date dateActivated) {
LocalDate inputDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate activeDate = dateActivated.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

LocalDate twelveMonthsAgo = activeDate.minusMonths(11);
LocalDate thirteenMonthsAgo = activeDate.minusMonths(13);

return (inputDate.isBefore(thirteenMonthsAgo) || inputDate.isEqual(thirteenMonthsAgo)) &&
(inputDate.isAfter(twelveMonthsAgo) || inputDate.isEqual(twelveMonthsAgo));
}

public boolean is12MonthsAfter(Date date,Date dateActivated) {
LocalDate inputDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate activeDate = dateActivated.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

LocalDate thirteenMonthsAgo = activeDate.minusMonths(13);

return (inputDate.isAfter(thirteenMonthsAgo) );
}
}
Loading