diff --git a/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/DiagnosisServiceImpl2_2.java b/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/DiagnosisServiceImpl2_2.java index e66e1c874..ed7f6d927 100644 --- a/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/DiagnosisServiceImpl2_2.java +++ b/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/DiagnosisServiceImpl2_2.java @@ -1,7 +1,5 @@ package org.openmrs.module.emrapi.diagnosis; -import org.openmrs.CodedOrFreeText; -import org.openmrs.ConditionVerificationStatus; import org.openmrs.Encounter; import org.openmrs.Obs; import org.openmrs.Patient; @@ -9,7 +7,6 @@ import org.openmrs.api.context.Context; import org.openmrs.module.emrapi.EmrApiConstants; -import java.util.ArrayList; import java.util.Date; import java.util.List; /** @@ -23,29 +20,13 @@ public void setAdminService(AdministrationService adminService) { this.adminService = adminService; } - /** - * Method to convert the core diagnosis object into a list of diagnoses compatible with the diagnosis object in the emrapi module - * @return diagnoses - * */ - private List convert(List coreDiagnoses) { - List diagnoses = new ArrayList(); - for (org.openmrs.Diagnosis coreDiagnosis : coreDiagnoses) { - Diagnosis diagnosis = new Diagnosis(); - CodedOrFreeText coded = coreDiagnosis.getDiagnosis(); - diagnosis.setDiagnosis(new CodedOrFreeTextAnswer(coded.getCoded(), coded.getSpecificName(), coded.getNonCoded())); - diagnosis.setCertainty(coreDiagnosis.getCertainty() == ConditionVerificationStatus.CONFIRMED ? Diagnosis.Certainty.CONFIRMED : Diagnosis.Certainty.PRESUMED); - diagnosis.setOrder(coreDiagnosis.getRank() == 1 ? Diagnosis.Order.PRIMARY : Diagnosis.Order.SECONDARY); - diagnoses.add(diagnosis); - } - return diagnoses; - } public List getDiagnoses(Patient patient, Date fromDate) { if (adminService.getGlobalProperty(EmrApiConstants.GP_USE_LEGACY_DIAGNOSIS_SERVICE,"false").equalsIgnoreCase("true")) { return super.getDiagnoses(patient,fromDate); } else { - return convert(Context.getDiagnosisService().getDiagnoses(patient, fromDate)); + return DiagnosisUtils.convert(Context.getDiagnosisService().getDiagnoses(patient, fromDate)); } } @@ -55,7 +36,7 @@ public List getUniqueDiagnoses(Patient patient, Date fromDate){ return super.getUniqueDiagnoses(patient, fromDate); } else { - return convert(Context.getDiagnosisService().getUniqueDiagnoses(patient, fromDate)); + return DiagnosisUtils.convert(Context.getDiagnosisService().getUniqueDiagnoses(patient, fromDate)); } } @@ -65,7 +46,7 @@ public List getPrimaryDiagnoses(Encounter encounter){ return super.getPrimaryDiagnoses(encounter); } else { - return convert(Context.getDiagnosisService().getPrimaryDiagnoses(encounter)); + return DiagnosisUtils.convert(Context.getDiagnosisService().getPrimaryDiagnoses(encounter)); } } diff --git a/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/DiagnosisUtils.java b/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/DiagnosisUtils.java new file mode 100644 index 000000000..5e48f8504 --- /dev/null +++ b/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/DiagnosisUtils.java @@ -0,0 +1,28 @@ +package org.openmrs.module.emrapi.diagnosis; + +import java.util.ArrayList; +import java.util.List; + +import org.openmrs.CodedOrFreeText; +import org.openmrs.ConditionVerificationStatus; + +public class DiagnosisUtils { + + /** + * Method to convert the core diagnosis object into a list of diagnoses compatible with the diagnosis object in the emrapi module + * @return diagnoses + * */ + public static List convert(List coreDiagnoses) { + List diagnoses = new ArrayList(); + for (Object coreDiagnosis2 : coreDiagnoses) { + org.openmrs.Diagnosis coreDiagnosis = (org.openmrs.Diagnosis)coreDiagnosis2; + Diagnosis diagnosis = new Diagnosis(); + CodedOrFreeText coded = coreDiagnosis.getDiagnosis(); + diagnosis.setDiagnosis(new CodedOrFreeTextAnswer(coded.getCoded(), coded.getSpecificName(), coded.getNonCoded())); + diagnosis.setCertainty(coreDiagnosis.getCertainty() == ConditionVerificationStatus.CONFIRMED ? Diagnosis.Certainty.CONFIRMED : Diagnosis.Certainty.PRESUMED); + diagnosis.setOrder(coreDiagnosis.getRank() == 1 ? Diagnosis.Order.PRIMARY : Diagnosis.Order.SECONDARY); + diagnoses.add(diagnosis); + } + return diagnoses; + } +} diff --git a/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/EmrDiagnosisDAO.java b/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/EmrDiagnosisDAO.java new file mode 100644 index 000000000..6bdfef4dc --- /dev/null +++ b/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/EmrDiagnosisDAO.java @@ -0,0 +1,10 @@ +package org.openmrs.module.emrapi.diagnosis; + +import org.openmrs.Visit; + +import java.util.List; + +public interface EmrDiagnosisDAO { + + List getDiagnoses(Visit visit, boolean primaryOnly, boolean confirmedOnly); +} diff --git a/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/EmrDiagnosisDAOImpl2_2.java b/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/EmrDiagnosisDAOImpl2_2.java new file mode 100644 index 000000000..a522b17c6 --- /dev/null +++ b/api-2.2/src/main/java/org/openmrs/module/emrapi/diagnosis/EmrDiagnosisDAOImpl2_2.java @@ -0,0 +1,56 @@ +package org.openmrs.module.emrapi.diagnosis; + +import org.hibernate.Query; +import org.openmrs.Visit; +import org.openmrs.api.db.hibernate.DbSessionFactory; + +import java.util.List; + +/** + * Hibernate implementation of the EmrDiagnosisDAO + */ +public class EmrDiagnosisDAOImpl2_2 implements EmrDiagnosisDAO { + + // TODO: Fetching diagnosis should be delegated to core Diagnosis service. + // https://issues.openmrs.org/browse/TRUNK-5999 + + private static final Integer PRIMARY_RANK = 1; + + private static final String CONFIRMED_CERTAINTY = "CONFIRMED"; + + private DbSessionFactory sessionFactory; + + public void setSessionFactory(DbSessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + } + + /** + * Gets the diagnosis for a given visit + * + * @param visit visit to get the diagnoses from + * @param primaryOnly whether to fetch primary diagnosis only or all diagnosis regardless of rank + * @param confirmedOnly whether to fetch only confirmed diagnosis or both confirmed and provisional + * @return list of diagnoses for a visit + */ + public List getDiagnoses(Visit visit, boolean primaryOnly, boolean confirmedOnly) { + String queryString = "from Diagnosis d where d.encounter.visit.visitId = :visitId and d.voided = false"; + if (primaryOnly == true) { + queryString += " and d.rank = :rankId"; + } + if (confirmedOnly == true) { + queryString += " and d.certainty = :certainty"; + } + queryString += " order by d.dateCreated desc"; + + Query query = sessionFactory.getCurrentSession().createQuery(queryString); + query.setInteger("visitId", visit.getId()); + if (primaryOnly == true) { + query.setInteger("rankId", PRIMARY_RANK); + } + if (confirmedOnly == true) { + query.setString("certainty", CONFIRMED_CERTAINTY); + } + + return (List) query.list(); + } +} diff --git a/api-2.2/src/main/java/org/openmrs/module/emrapi/visit/EmrVisitServiceImpl2_2.java b/api-2.2/src/main/java/org/openmrs/module/emrapi/visit/EmrVisitServiceImpl2_2.java new file mode 100644 index 000000000..2d104821e --- /dev/null +++ b/api-2.2/src/main/java/org/openmrs/module/emrapi/visit/EmrVisitServiceImpl2_2.java @@ -0,0 +1,46 @@ +package org.openmrs.module.emrapi.visit; + +import org.openmrs.Obs; +import org.openmrs.Visit; +import org.openmrs.api.AdministrationService; +import org.openmrs.api.VisitService; +import org.openmrs.module.emrapi.EmrApiConstants; +import org.openmrs.module.emrapi.diagnosis.Diagnosis; +import org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata; +import org.openmrs.module.emrapi.diagnosis.DiagnosisUtils; +import org.openmrs.module.emrapi.diagnosis.EmrDiagnosisDAO; +import java.util.ArrayList; +import java.util.List; + +public class EmrVisitServiceImpl2_2 extends EmrVisitServiceImpl implements EmrVisitService { + + private AdministrationService adminService; + + private EmrDiagnosisDAO emrDiagnosisDAO; + + public void setAdminService(AdministrationService adminService) { + this.adminService = adminService; + } + + public void setEmrDiagnosisDAO(EmrDiagnosisDAO emrDiagnosisDAO) { + this.emrDiagnosisDAO = emrDiagnosisDAO; + } + + public EmrVisitServiceImpl2_2(VisitService visitService, VisitResponseMapper visitResponseMapper) { + super(visitService, visitResponseMapper); + } + + @Override + public List getDiagnoses(Visit visit, DiagnosisMetadata diagnosisMetadata, Boolean primaryOnly, Boolean confirmedOnly) { + if (adminService.getGlobalProperty(EmrApiConstants.GP_USE_LEGACY_DIAGNOSIS_SERVICE, "false").equalsIgnoreCase("true")) { + return super.getDiagnoses(visit, diagnosisMetadata, primaryOnly, confirmedOnly); + } else { + List diagnoses = emrDiagnosisDAO.getDiagnoses(visit, primaryOnly, confirmedOnly); + List diagnosisList = new ArrayList(); + for (Diagnosis diagnosis : DiagnosisUtils.convert(diagnoses)) { + diagnosisList.add(diagnosisMetadata.buildDiagnosisObsGroup(diagnosis)); + } + return diagnosisList; + } + } +} diff --git a/api-2.2/src/main/resources/moduleApplicationContext.xml b/api-2.2/src/main/resources/moduleApplicationContext.xml index 56a69da22..0bedd7de9 100644 --- a/api-2.2/src/main/resources/moduleApplicationContext.xml +++ b/api-2.2/src/main/resources/moduleApplicationContext.xml @@ -33,4 +33,19 @@ + + + + + + + + + + + + + + + diff --git a/api-2.2/src/test/java/org/openmrs/emrapi/diagnosis/EmrDiagnosisDAOTest.java b/api-2.2/src/test/java/org/openmrs/emrapi/diagnosis/EmrDiagnosisDAOTest.java new file mode 100644 index 000000000..7c7863e2d --- /dev/null +++ b/api-2.2/src/test/java/org/openmrs/emrapi/diagnosis/EmrDiagnosisDAOTest.java @@ -0,0 +1,65 @@ +package org.openmrs.emrapi.diagnosis; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openmrs.ConditionVerificationStatus; +import org.openmrs.Visit; +import org.openmrs.module.emrapi.diagnosis.EmrDiagnosisDAO; +import org.openmrs.test.BaseModuleContextSensitiveTest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RunWith(SpringJUnit4ClassRunner.class) +public class EmrDiagnosisDAOTest extends BaseModuleContextSensitiveTest { + + private static final String DIAGNOSIS_DATASET = "DiagnosisDataset.xml"; + + @Autowired + private EmrDiagnosisDAO emrDiagnosisDAO; + + private Visit visit = mock(Visit.class); + + @Before + public void setUp() throws Exception { + executeDataSet(DIAGNOSIS_DATASET); + when(visit.getId()).thenReturn(1010); + } + + @Test + public void shouldReturnAllNonVoidedDiagnosesFromVisit() { + List diagnoses = emrDiagnosisDAO.getDiagnoses(visit, false, false); + assertEquals(4, diagnoses.size()); + assertEquals(Boolean.FALSE, diagnoses.get(0).getVoided()); + assertEquals(Boolean.FALSE, diagnoses.get(1).getVoided()); + assertEquals(Boolean.FALSE, diagnoses.get(2).getVoided()); + assertEquals(Boolean.FALSE, diagnoses.get(3).getVoided()); + } + + @Test + public void shouldReturnAllPrimaryConfirmedDiagnosesFromVisit() { + List diagnoses = emrDiagnosisDAO.getDiagnoses(visit, true, true); + assertEquals(1, diagnoses.size()); + assertEquals(ConditionVerificationStatus.CONFIRMED, diagnoses.get(0).getCertainty()); + } + + @Test + public void shouldReturnAllPrimaryDiagnosesFromVisit() { + List diagnoses = emrDiagnosisDAO.getDiagnoses(visit, true, false); + assertEquals(2, diagnoses.size()); + assertEquals(new Integer(1), diagnoses.get(0).getRank()); + assertEquals(new Integer(1), diagnoses.get(1).getRank()); + } + + @Test + public void shouldReturnAllConfirmedDiagnosesFromVisit() { + List diagnoses = emrDiagnosisDAO.getDiagnoses(visit, false, true); + assertEquals(2, diagnoses.size()); + assertEquals(ConditionVerificationStatus.CONFIRMED, diagnoses.get(0).getCertainty()); + assertEquals(ConditionVerificationStatus.CONFIRMED, diagnoses.get(1).getCertainty()); + } +} diff --git a/api-2.2/src/test/resources/DiagnosisDataset.xml b/api-2.2/src/test/resources/DiagnosisDataset.xml index 2cc0eec94..53c4c78a4 100644 --- a/api-2.2/src/test/resources/DiagnosisDataset.xml +++ b/api-2.2/src/test/resources/DiagnosisDataset.xml @@ -26,4 +26,12 @@ + + + + + + + + diff --git a/api-pre2.2/src/main/resources/moduleApplicationContext.xml b/api-pre2.2/src/main/resources/moduleApplicationContext.xml index cd2513a88..ccfb79f75 100644 --- a/api-pre2.2/src/main/resources/moduleApplicationContext.xml +++ b/api-pre2.2/src/main/resources/moduleApplicationContext.xml @@ -32,4 +32,12 @@ + + + + + + + + diff --git a/api/src/main/resources/hql/patients_diagnoses.hql b/api/src/main/resources/hql/patients_diagnoses.hql index 63d0fb56c..dc4ce6125 100644 --- a/api/src/main/resources/hql/patients_diagnoses.hql +++ b/api/src/main/resources/hql/patients_diagnoses.hql @@ -3,7 +3,7 @@ select from Patient p where - p.voided = 0 + p.voided = 'false' and p.patientId in(select distinct personId from Obs o where o.concept.conceptId = :diagnosisSetConceptId diff --git a/api/src/main/resources/hql/visit_primary_diagnoses.hql b/api/src/main/resources/hql/visit_primary_diagnoses.hql index e95293456..6fbcf9866 100644 --- a/api/src/main/resources/hql/visit_primary_diagnoses.hql +++ b/api/src/main/resources/hql/visit_primary_diagnoses.hql @@ -1,7 +1,5 @@ -select - o.obsGroup -from - Obs o +select o1 from Obs o +inner join o.obsGroup o1 where o.voided = 'false' and (o.encounter.visit = :visitId) diff --git a/api/src/main/resources/moduleApplicationContext.xml b/api/src/main/resources/moduleApplicationContext.xml index 3c0efa90e..cb15f18ec 100644 --- a/api/src/main/resources/moduleApplicationContext.xml +++ b/api/src/main/resources/moduleApplicationContext.xml @@ -239,20 +239,12 @@ - - - - - - - - - +