From 1c8d0a3700cd4fb48fdbf1f0142d16416548ceb5 Mon Sep 17 00:00:00 2001 From: mogoodrich Date: Tue, 1 Apr 2014 16:01:29 -0400 Subject: [PATCH] AM-155: Add getDailyAppointmentBlocks service method that accepts a List of AppointmentTypes --- .../api/AppointmentService.java | 14 +++++++++ .../api/db/AppointmentDAO.java | 10 +++--- .../HibernateAppointmentBlockDAO.java | 10 ++++-- .../db/hibernate/HibernateAppointmentDAO.java | 8 ++--- .../api/impl/AppointmentServiceImpl.java | 26 ++++++++++------ .../api/AppointmentServiceTest.java | 31 +++++++++++++++---- 6 files changed, 72 insertions(+), 27 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/AppointmentService.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/AppointmentService.java index 4b78f876..c8edc1f2 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/AppointmentService.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/AppointmentService.java @@ -717,6 +717,20 @@ public Map getAverageHistoryDurationByConditionsPerProvider(Da @Transactional(readOnly = true) List getDailyAppointmentBlocks(Location location, Date date, AppointmentType appointmentType); + /** + * Gets all scheduled appointment blocks for a certain day at a certain location. Ignores any + * appointments that are voided or in one of the "cancelled" state + * + * @param location + * @param date + * @param appointmentTypes + * @return + */ + + @Transactional(readOnly = true) + List getDailyAppointmentBlocks(Location location, Date date, + List appointmentTypes); + /** * Books a new appointment * diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/AppointmentDAO.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/AppointmentDAO.java index d935d7f4..2683a63c 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/AppointmentDAO.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/AppointmentDAO.java @@ -13,6 +13,9 @@ */ package org.openmrs.module.appointmentscheduling.api.db; +import java.util.Date; +import java.util.List; + import org.openmrs.Patient; import org.openmrs.Provider; import org.openmrs.Visit; @@ -24,9 +27,6 @@ import org.openmrs.module.appointmentscheduling.TimeSlot; import org.openmrs.module.appointmentscheduling.api.AppointmentService; -import java.util.Date; -import java.util.List; - /** * Database methods for {@link AppointmentService}. */ @@ -47,8 +47,8 @@ List getAppointmentsByConstraints(Date fromDate, Date toDate, Provi List getScheduledAppointmentsForPatient(Patient patient); - List getAppointmentsByAppointmentBlockAndAppointmentType(AppointmentBlock appointmentBlock, - AppointmentType appointmentType); + List getAppointmentsByAppointmentBlockAndAppointmentTypes(AppointmentBlock appointmentBlock, + List appointmentTypes); /** * Retrieve all appointments in a given time slot. diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentBlockDAO.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentBlockDAO.java index 16961bd9..35e142c3 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentBlockDAO.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentBlockDAO.java @@ -13,6 +13,12 @@ */ package org.openmrs.module.appointmentscheduling.api.db.hibernate; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import org.hibernate.Criteria; import org.hibernate.criterion.Conjunction; import org.hibernate.criterion.Disjunction; @@ -26,8 +32,6 @@ import org.openmrs.module.appointmentscheduling.api.db.AppointmentBlockDAO; import org.springframework.transaction.annotation.Transactional; -import java.util.*; - public class HibernateAppointmentBlockDAO extends HibernateSingleClassDAO implements AppointmentBlockDAO { public HibernateAppointmentBlockDAO() { @@ -51,7 +55,7 @@ public List getAppointmentBlocks(Date fromDate, Date toDate, S List filteredAppointmentBlocks = null; Criteria criteria = sessionFactory.getCurrentSession().createCriteria(AppointmentBlock.class); - criteria.add(Restrictions.eq("voided", false)); // we only want voided appointment blocks + criteria.add(Restrictions.eq("voided", false)); // we only want non-voided appointment blocks if (locations != null && !locations.isEmpty()) { String[] locationsAsArray = locations.split(","); diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentDAO.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentDAO.java index 7ab57a2c..c000a49b 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentDAO.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateAppointmentDAO.java @@ -156,14 +156,14 @@ public List getScheduledAppointmentsForPatient(Patient patient) { } @Override - public List getAppointmentsByAppointmentBlockAndAppointmentType(AppointmentBlock appointmentBlock, - AppointmentType appointmentType) { + public List getAppointmentsByAppointmentBlockAndAppointmentTypes(AppointmentBlock appointmentBlock, + List appointmentTypes) { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(mappedClass); criteria.createAlias("timeSlot", "time_slot"); criteria.add(Restrictions.eq("time_slot.appointmentBlock", appointmentBlock)); - if (appointmentType != null) - criteria.add(Restrictions.eq("appointmentType", appointmentType)); + if (appointmentTypes != null) + criteria.add(Restrictions.in("appointmentType", appointmentTypes)); // skip cancelled and missed appointment blocks criteria.add(Restrictions.and(Restrictions.ne("status", CANCELLED), Restrictions.ne("status", MISSED))); criteria.add(Restrictions.eq("voided", false)); diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/impl/AppointmentServiceImpl.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/impl/AppointmentServiceImpl.java index c7e0a4e9..9f7187a6 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/impl/AppointmentServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/impl/AppointmentServiceImpl.java @@ -950,14 +950,21 @@ public Map getAppointmentTypeDistribution(Date fromDat @Transactional(readOnly = true) public List getDailyAppointmentBlocks(Location location, Date date, AppointmentType appointmentType) { - AppointmentDAO appointmentDao = getAppointmentDAO(); + return getDailyAppointmentBlocks(location, date, + appointmentType != null ? Collections.singletonList(appointmentType) : null); + } + + @Override + public List getDailyAppointmentBlocks(Location location, Date date, + List appointmentTypes) { + List scheduledAppointmentBlockList = new ArrayList(); - for (AppointmentBlock appointmentBlock : getAppointmentBlockList(location, date, appointmentType)) { + for (AppointmentBlock appointmentBlock : getAppointmentBlockList(location, date, appointmentTypes)) { ScheduledAppointmentBlock scheduledAppointmentBlock = createScheduledAppointmentBlock(appointmentBlock, - appointmentType); + appointmentTypes); if (!scheduledAppointmentBlock.getAppointments().isEmpty()) { scheduledAppointmentBlockList.add(scheduledAppointmentBlock); @@ -968,9 +975,9 @@ public List getDailyAppointmentBlocks(Location locati } private ScheduledAppointmentBlock createScheduledAppointmentBlock(AppointmentBlock appointmentBlock, - AppointmentType appointmentType) { - List appointmentList = getAppointmentDAO().getAppointmentsByAppointmentBlockAndAppointmentType( - appointmentBlock, appointmentType); + List appointmentTypes) { + List appointmentList = getAppointmentDAO().getAppointmentsByAppointmentBlockAndAppointmentTypes( + appointmentBlock, appointmentTypes); return new ScheduledAppointmentBlock(appointmentList, appointmentBlock); } @@ -996,9 +1003,10 @@ public Appointment bookAppointment(Appointment appointment, Boolean allowOverboo return Context.getService(AppointmentService.class).saveAppointment(appointment); } - private List getAppointmentBlockList(Location location, Date date, AppointmentType appointmentType) { - return getAppointmentBlocks(setDateToStartOfDay(date), setDateToEndOfDay(date), location.getId().toString(), null, - appointmentType); + private List getAppointmentBlockList(Location location, Date date, + List appointmentTypes) { + return getAppointmentBlocksByTypes(setDateToStartOfDay(date), setDateToEndOfDay(date), location.getId().toString(), + null, appointmentTypes); } private Date setDateToEndOfDay(Date date) { diff --git a/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java b/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java index 8dcc5d08..5a69f5f4 100644 --- a/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java +++ b/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java @@ -13,6 +13,13 @@ */ package org.openmrs.module.appointmentscheduling.api; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + import junit.framework.Assert; import org.junit.Before; import org.junit.Test; @@ -32,12 +39,6 @@ import org.openmrs.test.BaseModuleContextSensitiveTest; import org.openmrs.test.Verifies; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; - import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; @@ -493,6 +494,24 @@ public void shouldReturnDailyAppointmentsWithoutProviderAssigned() throws Except List appointmentList = scheduledAppointmentBlockList.get(0).getAppointments(); assertEquals(1, appointmentList.size()); } + + @Test + public void shouldReturnDailyAppointmentsWhenMultipleAppointmentTypesChosen() throws Exception { + + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date date = format.parse("2014-01-02 00:00:00.0"); + + Location location = Context.getLocationService().getLocation(3); + + AppointmentType appointmentType2 = service.getAppointmentType(3); + AppointmentType appointmentType3 = service.getAppointmentType(1); + + List scheduledAppointmentBlockList = service.getDailyAppointmentBlocks(location, date, + Arrays.asList(appointmentType2, appointmentType3)); + + assertEquals(1, scheduledAppointmentBlockList.size()); + assertEquals(new Integer(5), scheduledAppointmentBlockList.get(0).getId()); + } @Test @Verifies(value = "retrieve all appointments scheduled in a given time slot", method = "getAppointmentsInTimeSlot(TimeSlot)")