Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaluku committed Oct 28, 2024
2 parents fe0e9c9 + eae6ae8 commit 5d463b5
Show file tree
Hide file tree
Showing 11 changed files with 578 additions and 169 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package org.openmrs.module.ugandaemrreports.api;

import org.openmrs.*;
import org.openmrs.Concept;
import org.openmrs.api.APIException;
import org.openmrs.api.OpenmrsService;
import org.openmrs.Cohort;
import org.openmrs.module.ugandaemrreports.model.Dashboard;
import org.openmrs.module.ugandaemrreports.model.DashboardReportObject;
import org.openmrs.reporting.AbstractReportObject;
import org.openmrs.reporting.PatientSearch;
import org.openmrs.reporting.PatientSearchReportObject;
import org.openmrs.reporting.ReportObjectWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Set;

/**
* This service exposes module's core functionality. It is a Spring managed bean which is configured in
Expand Down Expand Up @@ -105,4 +107,18 @@ public interface UgandaEMRReportsService extends OpenmrsService {
PatientSearch getPatientSearchByUuid(String uuid);

Cohort getPatientCurrentlyInProgram(String programUuid);

Map<Integer, String> getPatientsConditionsStatus(org.openmrs.cohort.Cohort patients, Concept codedCondition);

Set<Concept> getConditionsConcepts();

Map<Integer,Object> getLatestPatientAppointmentsScheduled(org.openmrs.cohort.Cohort patients, int limit);

List<Integer> getObsConceptsFromEncounters(EncounterType encounterType);

List<Object> getNonCodedOrderReasons(OrderType orderType);

List<Concept> getCodedOrderReasons(OrderType orderType);

Map<Integer, Map<String, Object>> getDrugOrderByIndicator(org.openmrs.cohort.Cohort patients,String drugIndication,OrderType orderType);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.openmrs.module.ugandaemrreports.api.db;

import org.openmrs.Program;
import org.openmrs.Cohort;
import org.openmrs.*;
import org.openmrs.Concept;
import org.openmrs.logic.op.In;
import org.openmrs.module.ugandaemrreports.api.UgandaEMRReportsService;
import org.openmrs.reporting.PatientSearch;
import org.openmrs.reporting.ReportObjectWrapper;


import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Set;

/**
* Database methods for {@link UgandaEMRReportsService}.
Expand All @@ -20,4 +24,16 @@ public interface UgandaEMRReportsDAO {

Cohort getPatientCurrentlyInPrograms(String uuid);

List<Integer> getObsConceptsFromEncounters(EncounterType encounterType);
Map<Integer, String> getPatientsConditionsStatus(org.openmrs.cohort.Cohort patients, Concept codedCondition);

Set<Concept> getAllConditions();

Map<Integer,Object> getLatestPatientAppointmentsScheduled(org.openmrs.cohort.Cohort patients, int limit);

List<Object> getNonCodedOrderReasons(OrderType orderType);

List<Concept> getCodedOrderReasons(OrderType orderType);

Map<Integer, Map<String, Object>> getDrugOrderByIndication(org.openmrs.cohort.Cohort patients,String drugIndication,OrderType orderType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;
import org.openmrs.*;
import org.openmrs.Concept;
import org.openmrs.api.context.Context;
import org.openmrs.api.db.hibernate.DbSession;
import org.openmrs.api.db.hibernate.DbSessionFactory;
import org.openmrs.Cohort;
import org.openmrs.module.ugandaemrreports.api.db.UgandaEMRReportsDAO;
import org.openmrs.module.ugandaemrreports.model.Dashboard;
import org.openmrs.module.ugandaemrreports.model.DashboardReportObject;
Expand All @@ -19,7 +23,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.List;
import java.util.Set;

/**
*/
Expand Down Expand Up @@ -142,4 +148,196 @@ public Cohort getPatientCurrentlyInPrograms(String programUuid) {
Query query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());
return new Cohort(query.list());
}

@Override
public Map<Integer, String> getPatientsConditionsStatus(org.openmrs.cohort.Cohort patients, Concept codedCondition) {
Map<Integer, String> ret = new HashMap<Integer, String>();


Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Condition.class);
criteria.setCacheMode(CacheMode.IGNORE);


// only restrict on patient ids if some were passed in
if (patients != null)
criteria.add(Restrictions.in("patient.personId", patients.getMemberIds()));


criteria.add(Expression.eq("condition.coded", codedCondition));
criteria.add(Expression.eq("voided", false));

criteria.addOrder(org.hibernate.criterion.Order.desc("onsetDate"));
long start = System.currentTimeMillis();
List<Condition> conditions = criteria.list();


log.debug("Took: " + (System.currentTimeMillis() - start) + " ms to run the patient/obs query");

// set up the return map
for (Condition c : conditions) {
int ptId = c.getPatient().getPatientId();

String status = c.getClinicalStatus().toString();
ret.put(ptId,status);
}


return ret;
}

@Override
public Set<Concept> getAllConditions() {
Set<Concept> ret = new HashSet<>();
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Condition.class);
criteria.setCacheMode(CacheMode.IGNORE);
criteria.add(Expression.eq("voided", false));


long start = System.currentTimeMillis();
List<Condition> conditions = criteria.list();
log.debug("Took: " + (System.currentTimeMillis() - start) + " ms to run the patient/obs query");

// set up the return map
for (Condition c : conditions) {
Concept concept = c.getCondition().getCoded();

if(concept !=null){
ret.add(concept);
}

}
return ret;
}


@Override
public Map<Integer,Object> getLatestPatientAppointmentsScheduled(org.openmrs.cohort.Cohort patients, int limit){
Map<Integer, Object> ret = new HashMap<Integer, Object>();
Query query = sessionFactory.getCurrentSession().createSQLQuery(
"select patient_id, start_date_time from patient_appointment where voided = false and patient_id in (:patientIds) order by start_date_time DESC ");

if (!patients.getMemberIds().isEmpty())
query.setParameterList("patientIds", patients.getMemberIds());
query.setCacheMode(CacheMode.IGNORE);

List<Object[]> temp = query.list();

long now = System.currentTimeMillis();
for (Object[] results : temp) {
Integer ptId = (Integer) results[0];
Object apptDate = results[1];

if (!ret.containsKey(ptId))
ret.put(ptId, apptDate);
}
return ret;
}

@Override
public List<Object> getNonCodedOrderReasons(OrderType orderType) {

String sb =String.format("SELECT DISTINCT o.order_reason_non_coded\n" +
"FROM orders o\n" +
" INNER JOIN order_type ot ON o.order_type_id = ot.order_type_id\n" +
"WHERE ot.uuid = '%s' and o.order_reason_non_coded is not null ;",orderType.getUuid());
log.debug("query: " + sb);
Query query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());

query.setCacheMode(CacheMode.IGNORE);
List<Object> ret = query.list();

return ret;
}

@Override
public List<Concept> getCodedOrderReasons(OrderType orderType) {
List<Concept> ret = new ArrayList<>();
String sb =String.format("SELECT DISTINCT o.order_reason\n" +
"FROM orders o\n" +
" INNER JOIN order_type ot ON o.order_type_id = ot.order_type_id\n" +
"WHERE ot.uuid = '%s' and o.order_reason is not null;\n",orderType.getUuid());
log.debug("query: " + sb);
Query query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());

query.setCacheMode(CacheMode.IGNORE);
List<Object[]> temp = query.list();

for (Object[] results : temp) {
int conceptId = (int) results[0];
Concept concept = Context.getConceptService().getConcept(conceptId);
ret.add(concept);
}
return ret;
}

@Override
public Map<Integer, Map<String, Object>> getDrugOrderByIndication(org.openmrs.cohort.Cohort patients, String drugIndication, OrderType orderType) {
String hql =String.format("SELECT patient_id, cn.name as drug,DATE(date_activated),dose, quantity,cn1.name as quantity_unit , duration,cn2.name as duration_units\n" +
"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" +
" INNER JOIN concept c on o.concept_id = c.concept_id\n" +
" LEFT JOIN concept_name cn on c.concept_id = cn.concept_id\n" +
" LEFT JOIN concept c1 on d_o.quantity_units = c1.concept_id\n" +
" LEFT JOIN concept_name cn1 on c1.concept_id = cn1.concept_id and cn1.locale='en' and cn1.concept_name_type='FULLY_SPECIFIED'\n" +
" LEFT JOIN concept c2 on d_o.duration_units = c2.concept_id\n" +
" LEFT JOIN concept_name cn2 on c2.concept_id = cn2.concept_id and cn2.locale='en' and cn2.concept_name_type='FULLY_SPECIFIED'\n" +
"where cn.locale='en' and cn.concept_name_type='FULLY_SPECIFIED' and ot.uuid='%s' and o.order_reason_non_coded='%s' and patient_id in (:patientIds)",orderType.getUuid(),drugIndication);

Query query = sessionFactory.getCurrentSession().createSQLQuery(hql.toString());
query.setParameter("patientIds", patients.getMemberIds());
Map<Integer, Map<String, Object>> ret = new HashMap<>();
List<Object[]> rows = query.list();

for (Object[] rowArray : rows) {
LinkedHashMap<String, Object> row = new LinkedHashMap<>();
Integer ptId = (Integer) rowArray[0];

Object drug = rowArray[1];
row.put("drug", drug);
Object encounter_date = rowArray[2];
row.put("drug_date_ordered",encounter_date);
Object dose = rowArray[3];
row.put("dose",dose);
Object quantity = rowArray[4];
row.put("quantity", quantity);
Object quantity_unit = rowArray[5];
row.put("quantity_unit",quantity_unit);
Object duration = rowArray[6];
row.put("duration",duration);
Object duration_unit = rowArray[7];
row.put("duration_unit",duration_unit);

ret.put(ptId, row);
}

Set<Integer> patientWithNoRecords = new HashSet<>(patients.getMemberIds());
patientWithNoRecords.removeAll(ret.keySet());

for (Integer i : patientWithNoRecords){
LinkedHashMap<String, Object> row = new LinkedHashMap<>();
row.put("drug", "");
row.put("drug_date_ordered", "");
row.put("dose", "");
row.put("quantity", "");
row.put("quantity_unit", "");
row.put("duration", "");
row.put("duration_unit", "");
ret.put(i, row);
}

return ret;
}
@Override
public List<Integer> getObsConceptsFromEncounters(EncounterType encounterType) {
String hql = "SELECT DISTINCT o.concept.id " +
"FROM Obs o " +
"INNER JOIN o.encounter e " +
"INNER JOIN e.encounterType et " +
"WHERE et.uuid = :uuid";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setParameter("uuid", encounterType.getUuid());
List<Integer> conceptIds = query.list();
return conceptIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Cohort;
import org.openmrs.*;
import org.openmrs.Concept;
import org.openmrs.api.APIException;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.logic.op.In;
import org.openmrs.module.ugandaemrreports.api.UgandaEMRReportsService;
import org.openmrs.module.ugandaemrreports.api.db.hibernate.HibernateUgandaEMRReportsDAO;
import org.openmrs.module.ugandaemrreports.model.Dashboard;
Expand All @@ -13,8 +15,8 @@
import org.openmrs.reporting.ReportObjectWrapper;


import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.Set;

/**
* It is a default implementation of {@link UgandaEMRReportsService}.
Expand Down Expand Up @@ -102,4 +104,39 @@ public Cohort getPatientCurrentlyInProgram(String programUuid) {
return dao.getPatientCurrentlyInPrograms(programUuid);
}

@Override
public Map<Integer, String> getPatientsConditionsStatus(org.openmrs.cohort.Cohort patients, Concept codedCondition) {
return dao.getPatientsConditionsStatus(patients, codedCondition);
}

@Override
public Set<Concept> getConditionsConcepts() {
return dao.getAllConditions();
}

@Override
public Map<Integer,Object> getLatestPatientAppointmentsScheduled(org.openmrs.cohort.Cohort patients, int limit){
return dao.getLatestPatientAppointmentsScheduled(patients,limit);
}

@Override
public List<Integer> getObsConceptsFromEncounters(EncounterType encounterType) {
return dao.getObsConceptsFromEncounters(encounterType);
}

@Override
public List<Object> getNonCodedOrderReasons(OrderType orderType) {
return dao.getNonCodedOrderReasons(orderType);
}

@Override
public List<Concept> getCodedOrderReasons(OrderType orderType) {
return dao.getCodedOrderReasons(orderType);
}

@Override
public Map<Integer, Map<String, Object>> getDrugOrderByIndicator(org.openmrs.cohort.Cohort patients, String drugIndication,OrderType orderType) {
return dao.getDrugOrderByIndication(patients,drugIndication,orderType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
*/
package org.openmrs.module.ugandaemrreports.definition.dataset.evaluator;

import liquibase.util.StringUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.openmrs.annotation.Handler;
import org.openmrs.api.context.Context;
import org.openmrs.module.reporting.common.ObjectUtil;
import org.openmrs.module.reporting.dataset.DataSet;
import org.openmrs.module.reporting.dataset.DataSetColumn;
import org.openmrs.module.reporting.dataset.DataSetRow;
Expand All @@ -30,10 +27,6 @@
import org.openmrs.module.reporting.evaluation.EvaluationContext;
import org.openmrs.module.reporting.evaluation.EvaluationException;
import org.openmrs.module.ugandaemrreports.definition.dataset.definition.EMRVersionDatasetDefinition;
import org.openmrs.module.ugandaemrreports.definition.dataset.definition.NameOfHealthUnitDatasetDefinition;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Handler(supports = EMRVersionDatasetDefinition.class)
public class EMRVersionDatasetEvaluator implements DataSetEvaluator{
Expand Down
Loading

0 comments on commit 5d463b5

Please sign in to comment.