Skip to content

Commit

Permalink
AM-155: Add getDailyAppointmentBlocks service method that accepts a L…
Browse files Browse the repository at this point in the history
…ist of AppointmentTypes
  • Loading branch information
mogoodrich committed Apr 1, 2014
1 parent 21655f7 commit 1c8d0a3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,20 @@ public Map<Provider, Double> getAverageHistoryDurationByConditionsPerProvider(Da
@Transactional(readOnly = true)
List<ScheduledAppointmentBlock> 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<ScheduledAppointmentBlock> getDailyAppointmentBlocks(Location location, Date date,
List<AppointmentType> appointmentTypes);

/**
* Books a new appointment
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}.
*/
Expand All @@ -47,8 +47,8 @@ List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate, Provi

List<Appointment> getScheduledAppointmentsForPatient(Patient patient);

List<Appointment> getAppointmentsByAppointmentBlockAndAppointmentType(AppointmentBlock appointmentBlock,
AppointmentType appointmentType);
List<Appointment> getAppointmentsByAppointmentBlockAndAppointmentTypes(AppointmentBlock appointmentBlock,
List<AppointmentType> appointmentTypes);

/**
* Retrieve all appointments in a given time slot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -51,7 +55,7 @@ public List<AppointmentBlock> getAppointmentBlocks(Date fromDate, Date toDate, S
List<AppointmentBlock> 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(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ public List<Appointment> getScheduledAppointmentsForPatient(Patient patient) {
}

@Override
public List<Appointment> getAppointmentsByAppointmentBlockAndAppointmentType(AppointmentBlock appointmentBlock,
AppointmentType appointmentType) {
public List<Appointment> getAppointmentsByAppointmentBlockAndAppointmentTypes(AppointmentBlock appointmentBlock,
List<AppointmentType> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -950,14 +950,21 @@ public Map<AppointmentType, Integer> getAppointmentTypeDistribution(Date fromDat
@Transactional(readOnly = true)
public List<ScheduledAppointmentBlock> getDailyAppointmentBlocks(Location location, Date date,
AppointmentType appointmentType) {
AppointmentDAO appointmentDao = getAppointmentDAO();

return getDailyAppointmentBlocks(location, date,
appointmentType != null ? Collections.singletonList(appointmentType) : null);
}

@Override
public List<ScheduledAppointmentBlock> getDailyAppointmentBlocks(Location location, Date date,
List<AppointmentType> appointmentTypes) {

List<ScheduledAppointmentBlock> scheduledAppointmentBlockList = new ArrayList<ScheduledAppointmentBlock>();

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);
Expand All @@ -968,9 +975,9 @@ public List<ScheduledAppointmentBlock> getDailyAppointmentBlocks(Location locati
}

private ScheduledAppointmentBlock createScheduledAppointmentBlock(AppointmentBlock appointmentBlock,
AppointmentType appointmentType) {
List<Appointment> appointmentList = getAppointmentDAO().getAppointmentsByAppointmentBlockAndAppointmentType(
appointmentBlock, appointmentType);
List<AppointmentType> appointmentTypes) {
List<Appointment> appointmentList = getAppointmentDAO().getAppointmentsByAppointmentBlockAndAppointmentTypes(
appointmentBlock, appointmentTypes);
return new ScheduledAppointmentBlock(appointmentList, appointmentBlock);
}

Expand All @@ -996,9 +1003,10 @@ public Appointment bookAppointment(Appointment appointment, Boolean allowOverboo
return Context.getService(AppointmentService.class).saveAppointment(appointment);
}

private List<AppointmentBlock> getAppointmentBlockList(Location location, Date date, AppointmentType appointmentType) {
return getAppointmentBlocks(setDateToStartOfDay(date), setDateToEndOfDay(date), location.getId().toString(), null,
appointmentType);
private List<AppointmentBlock> getAppointmentBlockList(Location location, Date date,
List<AppointmentType> appointmentTypes) {
return getAppointmentBlocksByTypes(setDateToStartOfDay(date), setDateToEndOfDay(date), location.getId().toString(),
null, appointmentTypes);
}

private Date setDateToEndOfDay(Date date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -493,6 +494,24 @@ public void shouldReturnDailyAppointmentsWithoutProviderAssigned() throws Except
List<Appointment> 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<ScheduledAppointmentBlock> 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)")
Expand Down

0 comments on commit 1c8d0a3

Please sign in to comment.