-
Notifications
You must be signed in to change notification settings - Fork 68
BED-17: Queries for BedPatientAssignment should account for the voided field
#90
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
Changes from 48 commits
6c2fb73
e683740
dded9cb
8d5dd34
6f37ff3
d08fd7b
30761d2
dc3e4f6
004ec4d
a88772b
5a50714
3507c3e
4f00ac5
6bd7366
70d8c9e
f67a3ea
5ef4f73
e4d74bd
4442096
2993ebe
83ea660
08a982c
cae1697
af3b9a3
c27cb95
9dddb07
a76f280
28cdceb
1c9f492
934f011
65fb6d0
1b2a2a7
11ba938
d5b3f8f
ed7822c
ffa2279
d41c0f3
999567f
4915bb9
0fd4758
027b83e
0826974
bcc9b7b
d1d70dd
ad199ce
4863cb8
589b7f9
a5f69ca
63026d6
37a3a38
5d41799
a16c521
11d4ba4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,14 +13,20 @@ | |
| */ | ||
| package org.openmrs.module.bedmanagement.dao.impl; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.commons.collections.CollectionUtils; | ||
| import org.apache.commons.lang.BooleanUtils; | ||
| import org.hibernate.Criteria; | ||
| import org.hibernate.FlushMode; | ||
| import org.hibernate.query.Query; | ||
| import org.hibernate.Session; | ||
| import org.hibernate.SessionFactory; | ||
| import org.hibernate.criterion.Restrictions; | ||
| import org.hibernate.query.Query; | ||
| import org.hibernate.transform.Transformers; | ||
| import org.openmrs.Location; | ||
| import org.openmrs.Patient; | ||
|
|
@@ -35,12 +41,6 @@ | |
| import org.openmrs.module.bedmanagement.entity.BedTagMap; | ||
| import org.openmrs.module.bedmanagement.entity.BedType; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class BedManagementDaoImpl implements BedManagementDao { | ||
|
|
||
| SessionFactory sessionFactory; | ||
|
|
@@ -67,8 +67,8 @@ public Bed getBedByUuid(String uuid) { | |
| public Bed getBedByPatient(Patient patient) { | ||
| Session session = sessionFactory.getCurrentSession(); | ||
| Bed bed = (Bed) session | ||
| .createQuery("select bpa.bed.bedNumber as bedNumber,bpa.bed.id as id from BedPatientAssignment bpa " | ||
| + "where bpa.patient = :patient and bpa.endDatetime is null") | ||
| .createQuery("select bpa.bed.bedNumber as bedNumber, bpa.bed.id as id from BedPatientAssignment bpa " | ||
| + "where bpa.patient = :patient and bpa.endDatetime is null and bpa.voided = false") | ||
| .setParameter("patient", patient).setResultTransformer(Transformers.aliasToBean(Bed.class)).uniqueResult(); | ||
| return bed; | ||
| } | ||
|
|
@@ -89,7 +89,7 @@ public Location getWardForBed(Bed bed) { | |
| @Override | ||
| public BedPatientAssignment getBedPatientAssignmentByUuid(String uuid) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have the |
||
| Session session = sessionFactory.getCurrentSession(); | ||
| return (BedPatientAssignment) session.createQuery("from BedPatientAssignment bpa " + "where bpa.uuid = :uuid") | ||
| return (BedPatientAssignment) session.createQuery("from BedPatientAssignment bpa where bpa.uuid = :uuid") | ||
| .setParameter("uuid", uuid).uniqueResult(); | ||
| } | ||
|
|
||
|
|
@@ -125,9 +125,10 @@ public List<BedPatientAssignment> getBedPatientAssignmentByEncounter(String enco | |
| return bpaList; | ||
| } | ||
|
|
||
| @Override | ||
| public List<BedPatientAssignment> getBedPatientAssignmentByVisit(String visitUuid, boolean includeEnded) { | ||
| Session session = sessionFactory.getCurrentSession(); | ||
| List<BedPatientAssignment> bpaList = (List<BedPatientAssignment>) session | ||
| List<BedPatientAssignment> bpaList = session | ||
| .createQuery( | ||
| "select bpa from BedPatientAssignment bpa " + "inner join bpa.encounter enc " + "inner join enc.visit v " | ||
| + "where v.uuid = :visitUuid AND " + "(bpa.endDatetime IS NULL OR :includeEnded IS TRUE) AND " | ||
|
|
@@ -141,19 +142,21 @@ public List<BedPatientAssignment> getBedPatientAssignmentByVisit(String visitUui | |
| public List<BedPatientAssignment> getCurrentAssignmentsByBed(Bed bed) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have the |
||
| Session session = sessionFactory.getCurrentSession(); | ||
| List<BedPatientAssignment> assignments = session | ||
| .createQuery("from BedPatientAssignment where bed=:bed and endDatetime is null").setParameter("bed", bed) | ||
| .list(); | ||
| .createQuery( | ||
| "from BedPatientAssignment bpa where bpa.bed = :bed and bpa.endDatetime is null and bpa.voided = false") | ||
| .setParameter("bed", bed).list(); | ||
| return assignments; | ||
| } | ||
|
|
||
| @Override | ||
| public Bed getLatestBedByVisit(String visitUuid) { | ||
| Session session = sessionFactory.getCurrentSession(); | ||
| Bed bed = (Bed) session | ||
| .createQuery("select bpa.bed from BedPatientAssignment bpa " + "inner join bpa.encounter enc " | ||
| + "inner join enc.visit v where v.uuid = :visitUuid order by bpa.startDatetime DESC") | ||
| .setParameter("visitUuid", visitUuid).setMaxResults(1).uniqueResult(); | ||
| return bed; | ||
|
|
||
| String hql = "select bpa.bed " + "from BedPatientAssignment bpa " + "inner join bpa.encounter enc " | ||
| + "inner join enc.visit v " + "where v.uuid = :visitUuid " + "and bpa.voided = false " | ||
| + "order by bpa.startDatetime DESC"; | ||
|
|
||
| return (Bed) session.createQuery(hql).setParameter("visitUuid", visitUuid).setMaxResults(1).uniqueResult(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -287,23 +290,28 @@ private Criteria createGetBedsCriteria(Location location, BedType bedType, BedSt | |
| criteria.createAlias("blm.location", "location"); | ||
| criteria.add(Restrictions.eq("location", location)); | ||
| criteria.add(Restrictions.eq("bed.voided", false)); | ||
| if (bedStatus != null) | ||
| if (bedStatus != null) { | ||
| criteria.add(Restrictions.eq("bed.status", bedStatus.toString())); | ||
| if (bedType != null) | ||
| } | ||
| if (bedType != null) { | ||
| criteria.add(Restrictions.eq("bed.bedType", bedType)); | ||
| } | ||
| } else { | ||
| criteria = session.createCriteria(Bed.class, "bed"); | ||
| criteria.add(Restrictions.eq("voided", false)); | ||
| if (bedStatus != null) | ||
| if (bedStatus != null) { | ||
| criteria.add(Restrictions.eq("status", bedStatus.toString())); | ||
| if (bedType != null) | ||
| } | ||
| if (bedType != null) { | ||
| criteria.add(Restrictions.eq("bedType", bedType)); | ||
| } | ||
| } | ||
|
|
||
| if (limit != null) { | ||
| criteria.setMaxResults(limit); | ||
| if (offset != null) | ||
| if (offset != null) { | ||
| criteria.setFirstResult(offset); | ||
| } | ||
| } | ||
|
|
||
| return criteria; | ||
|
|
@@ -350,13 +358,15 @@ public BedTag getBedTagByUuid(String uuid) { | |
| public List<BedTag> getBedTags(String name, Integer limit, Integer offset) { | ||
| Criteria criteria = sessionFactory.getCurrentSession().createCriteria(BedTag.class); | ||
| criteria.add(Restrictions.eq("voided", false)); | ||
| if (name != null) | ||
| if (name != null) { | ||
| criteria.add(Restrictions.eq("name", name)); | ||
| } | ||
|
|
||
| if (limit != null) { | ||
| criteria.setMaxResults(limit); | ||
| if (offset != null) | ||
| if (offset != null) { | ||
| criteria.setFirstResult(offset); | ||
| } | ||
| } | ||
| return criteria.list(); | ||
| } | ||
|
|
@@ -393,12 +403,14 @@ public BedType getBedTypeByUuid(String uuid) { | |
| public List<BedType> getBedTypes(String name, Integer limit, Integer offset) { | ||
| Criteria criteria = sessionFactory.getCurrentSession().createCriteria(BedType.class); | ||
| criteria.add(Restrictions.eq("retired", false)); | ||
| if (name != null) | ||
| if (name != null) { | ||
| criteria.add(Restrictions.eq("name", name)); | ||
| } | ||
| if (limit != null) { | ||
| criteria.setMaxResults(limit); | ||
| if (offset != null) | ||
| if (offset != null) { | ||
| criteria.setFirstResult(offset); | ||
| } | ||
| } | ||
| return criteria.list(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| package org.openmrs.module.bedmanagement; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.openmrs.Encounter; | ||
| import org.openmrs.EncounterType; | ||
| import org.openmrs.Patient; | ||
| import org.openmrs.Visit; | ||
| import org.openmrs.api.context.Context; | ||
| import org.openmrs.module.bedmanagement.dao.BedManagementDao; | ||
| import org.openmrs.module.bedmanagement.entity.Bed; | ||
| import org.openmrs.module.bedmanagement.entity.BedPatientAssignment; | ||
| import org.openmrs.module.bedmanagement.entity.BedType; | ||
| import org.openmrs.test.BaseModuleContextSensitiveTest; | ||
|
|
||
| import java.util.Date; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
|
|
||
| public class BedManagementDaoImplTest extends BaseModuleContextSensitiveTest { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the sub-module/location of the BedManagementDaoImpl class?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @wikumChamith. I now understand why the duplicate ehcache jars/configs on classpath causing the Spring fails to create the sessionFactory. I have deleted the file. |
||
|
|
||
| private BedManagementDao bedManagementDao; | ||
|
|
||
| private EncounterType defaultEncounterType; | ||
|
|
||
| @Before | ||
| public void setup() throws Exception { | ||
| executeDataSet("org/openmrs/include/standardTestDataset.xml"); | ||
|
|
||
| bedManagementDao = Context.getRegisteredComponent("bedManagementDao", BedManagementDao.class); | ||
|
|
||
| defaultEncounterType = Context.getEncounterService().getEncounterType("Scheduled"); | ||
| } | ||
|
|
||
| private Bed createBed(String bedNumber) { | ||
| BedType bedType = new BedType(); | ||
| bedType.setName("Test BedType"); | ||
|
||
| bedType.setDisplayName("TB"); | ||
| bedType.setDescription("Test Bed Type"); | ||
| bedType = bedManagementDao.saveBedType(bedType); | ||
|
|
||
| Bed bed = new Bed(); | ||
| bed.setBedNumber(bedNumber); | ||
| bed.setStatus("AVAILABLE"); | ||
| bed.setBedType(bedType); | ||
|
|
||
| return bedManagementDao.saveBed(bed); | ||
| } | ||
|
|
||
| private Visit getTestVisit(Patient patient) { | ||
| List<Visit> visits = Context.getVisitService().getVisitsByPatient(patient); | ||
|
||
| assertNotNull(visits); | ||
| assertEquals(true, !visits.isEmpty()); | ||
| return visits.get(0); | ||
| } | ||
|
|
||
| private Encounter getTestEncounter(Patient patient) { | ||
| List<Encounter> encounters = Context.getEncounterService().getEncountersByPatient(patient); | ||
|
||
| assertNotNull(encounters); | ||
| assertEquals(true, !encounters.isEmpty()); | ||
| return encounters.get(0); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnBedByPatient() { | ||
| Patient patient = Context.getPatientService().getPatient(2); | ||
| Bed bed = createBed("B101"); | ||
|
|
||
| Encounter encounter = getTestEncounter(patient); | ||
|
|
||
| BedPatientAssignment assignment = new BedPatientAssignment(); | ||
| assignment.setBed(bed); | ||
| assignment.setPatient(patient); | ||
|
||
| assignment.setEncounter(encounter); | ||
| assignment.setStartDatetime(new Date()); | ||
|
|
||
| bedManagementDao.saveBedPatientAssignment(assignment); | ||
|
|
||
| Bed result = bedManagementDao.getBedByPatient(patient); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals("B101", result.getBedNumber()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnBedPatientAssignmentByUuid() { | ||
| Patient patient = Context.getPatientService().getPatient(2); | ||
| Bed bed = createBed("B102"); | ||
|
|
||
| Encounter encounter = getTestEncounter(patient); | ||
|
|
||
| BedPatientAssignment assignment = new BedPatientAssignment(); | ||
| assignment.setBed(bed); | ||
| assignment.setPatient(patient); | ||
| assignment.setEncounter(encounter); | ||
|
||
| assignment.setStartDatetime(new Date()); | ||
|
|
||
| BedPatientAssignment saved = bedManagementDao.saveBedPatientAssignment(assignment); | ||
|
|
||
| BedPatientAssignment result = bedManagementDao.getBedPatientAssignmentByUuid(saved.getUuid()); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(patient.getUuid(), result.getPatient().getUuid()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnCurrentAssignmentsByBed() { | ||
| Bed bed = createBed("B103"); | ||
|
|
||
| Patient patient1 = Context.getPatientService().getPatient(2); | ||
| Patient patient2 = Context.getPatientService().getPatient(7); | ||
|
|
||
| Encounter encounter1 = getTestEncounter(patient1); | ||
| Encounter encounter2 = getTestEncounter(patient2); | ||
|
|
||
| BedPatientAssignment a1 = new BedPatientAssignment(); | ||
| a1.setBed(bed); | ||
| a1.setPatient(patient1); | ||
| a1.setEncounter(encounter1); | ||
| a1.setStartDatetime(new Date()); | ||
| bedManagementDao.saveBedPatientAssignment(a1); | ||
|
|
||
| BedPatientAssignment a2 = new BedPatientAssignment(); | ||
| a2.setBed(bed); | ||
| a2.setPatient(patient2); | ||
| a2.setEncounter(encounter2); | ||
| a2.setStartDatetime(new Date()); | ||
| bedManagementDao.saveBedPatientAssignment(a2); | ||
|
|
||
| List<BedPatientAssignment> results = bedManagementDao.getCurrentAssignmentsByBed(bed); | ||
|
|
||
| assertNotNull(results); | ||
| assertEquals(2, results.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnLatestBedByVisit() { | ||
| Patient patient = Context.getPatientService().getPatient(2); | ||
| Visit visit = getTestVisit(patient); | ||
|
|
||
| Encounter encounter1 = new Encounter(); | ||
| encounter1.setPatient(patient); | ||
| encounter1.setVisit(visit); | ||
| encounter1.setEncounterType(defaultEncounterType); | ||
| encounter1.setEncounterDatetime(new Date(System.currentTimeMillis() - 100000L)); | ||
| encounter1 = Context.getEncounterService().saveEncounter(encounter1); | ||
|
|
||
| Encounter encounter2 = new Encounter(); | ||
| encounter2.setPatient(patient); | ||
| encounter2.setVisit(visit); | ||
| encounter2.setEncounterType(defaultEncounterType); | ||
| encounter2.setEncounterDatetime(new Date()); | ||
| encounter2 = Context.getEncounterService().saveEncounter(encounter2); | ||
|
|
||
| Bed bed1 = createBed("B104"); | ||
| Bed bed2 = createBed("B105"); | ||
|
|
||
| BedPatientAssignment assignment1 = new BedPatientAssignment(); | ||
| assignment1.setBed(bed1); | ||
| assignment1.setPatient(patient); | ||
| assignment1.setEncounter(encounter1); | ||
| assignment1.setStartDatetime(new Date(System.currentTimeMillis() - 100000L)); | ||
| assignment1.setEndDatetime(new Date(System.currentTimeMillis() - 50000L)); | ||
| bedManagementDao.saveBedPatientAssignment(assignment1); | ||
|
|
||
| BedPatientAssignment assignment2 = new BedPatientAssignment(); | ||
| assignment2.setBed(bed2); | ||
| assignment2.setPatient(patient); | ||
| assignment2.setEncounter(encounter2); | ||
| assignment2.setStartDatetime(new Date()); | ||
| bedManagementDao.saveBedPatientAssignment(assignment2); | ||
|
|
||
| Bed latest = bedManagementDao.getLatestBedByVisit(visit.getUuid()); | ||
|
|
||
| assertNotNull(latest); | ||
| assertEquals("B105", latest.getBedNumber()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have the
getBedByPatientmethod called/not mocked, in a test?