Skip to content

Commit

Permalink
UHM-1204 making appointment resource accept multiple statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-areias committed Apr 2, 2014
1 parent daf8bd5 commit 17685a1
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,23 @@
public class Appointment extends BaseOpenmrsData implements Serializable {

private static final long serialVersionUID = 1L;

public enum AppointmentStatusType {
SCHEDULED, ACTIVE, CANCELLED, MISSED, COMPLETED
}

public enum AppointmentStatusType {
SCHEDULED, ACTIVE, CANCELLED, MISSED, COMPLETED
}
// TODO confirm that "WALK-IN" should be considered active and "RESCHEDULED" should be scheduled
@JsonSerialize(using = AppointmentStatusSerializer.class)
public enum AppointmentStatus {

SCHEDULED("Scheduled", AppointmentStatusType.SCHEDULED),
RESCHEDULED("Rescheduled", AppointmentStatusType.SCHEDULED),
WALKIN("Walk-In", AppointmentStatusType.ACTIVE),
WAITING("Waiting", AppointmentStatusType.ACTIVE),
INCONSULTATION("In-Consultation", AppointmentStatusType.ACTIVE),
CANCELLED("Cancelled", AppointmentStatusType.CANCELLED),
CANCELLED_AND_NEEDS_RESCHEDULE("Cancelled and Needs Reschedule", AppointmentStatusType.CANCELLED),
MISSED("Missed", AppointmentStatusType.MISSED),
COMPLETED("Completed", AppointmentStatusType.COMPLETED);


SCHEDULED("Scheduled", AppointmentStatusType.SCHEDULED), RESCHEDULED("Rescheduled", AppointmentStatusType.SCHEDULED), WALKIN(
"Walk-In", AppointmentStatusType.ACTIVE), WAITING("Waiting", AppointmentStatusType.ACTIVE), INCONSULTATION(
"In-Consultation", AppointmentStatusType.ACTIVE), CANCELLED("Cancelled", AppointmentStatusType.CANCELLED), CANCELLED_AND_NEEDS_RESCHEDULE(
"Cancelled and Needs Reschedule", AppointmentStatusType.CANCELLED), MISSED("Missed",
AppointmentStatusType.MISSED), COMPLETED("Completed", AppointmentStatusType.COMPLETED);

private final String name;

private final AppointmentStatusType type;

private AppointmentStatus(final String name, final AppointmentStatusType type) {
Expand All @@ -65,12 +61,12 @@ private AppointmentStatus(final String name, final AppointmentStatusType type) {

public String getName() {
return this.name;
}

public AppointmentStatusType getType() {
return this.type;
}

}
public AppointmentStatusType getType() {
return this.type;
}
@Override
public String toString() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,19 @@ List<ScheduledAppointmentBlock> getDailyAppointmentBlocks(Location location, Dat
*/
Appointment bookAppointment(Appointment appointment, Boolean allowOverbook) throws TimeSlotFullException;

/**
* Retrieves Appointments that satisfy the given constraints
*
* @param fromDate - The appointment start date
* @param toDate - The appointment end date
* @param location - The appointment location
* @param provider - The appointment provider
* @param type - The appointment type
* @param patient - The patient
* @param appointmentStatuses- The appointment status list
* @return a list of appointments that satisfy the given constraints
*/

List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate, Location location, Provider provider,
AppointmentType type, Patient patient, List<AppointmentStatus> appointmentStatuses);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openmrs.module.appointmentscheduling.AppointmentType;
import org.openmrs.module.appointmentscheduling.TimeSlot;
import org.openmrs.module.appointmentscheduling.api.AppointmentService;
import org.springframework.transaction.annotation.Transactional;

/**
* Database methods for {@link AppointmentService}.
Expand All @@ -38,6 +39,10 @@ public interface AppointmentDAO extends SingleClassDAO {

Appointment getLastAppointment(Patient patient);

@Transactional(readOnly = true)
List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate, Provider provider,
AppointmentType appointmentType, List<AppointmentStatus> statuses, Patient patient) throws APIException;

List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate, Provider provider, AppointmentType type,
AppointmentStatus status, Patient patient) throws APIException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package org.openmrs.module.appointmentscheduling.api.db.hibernate;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -80,7 +81,7 @@ public Appointment getLastAppointment(Patient patient) {
@Override
@Transactional(readOnly = true)
public List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate, Provider provider,
AppointmentType appointmentType, AppointmentStatus status, Patient patient) throws APIException {
AppointmentType appointmentType, List<AppointmentStatus> statuses, Patient patient) throws APIException {
if (fromDate != null && toDate != null && !fromDate.before(toDate))
throw new APIException("fromDate can not be later than toDate");

Expand All @@ -93,8 +94,8 @@ public List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate
stringQuery += " AND appointment.timeSlot.endDate <= :endDate";
if (provider != null)
stringQuery += " AND appointment.timeSlot.appointmentBlock.provider = :provider";
if (status != null)
stringQuery += " AND appointment.status=:status";
if (statuses != null)
stringQuery += " AND appointment.status IN (:statuses)";
if (appointmentType != null)
stringQuery += " AND appointment.appointmentType=:appointmentType";
if (patient != null) {
Expand All @@ -109,8 +110,8 @@ public List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate
query.setParameter("endDate", toDate);
if (provider != null)
query.setParameter("provider", provider);
if (status != null)
query.setParameter("status", status);
if (statuses != null)
query.setParameterList("statuses", statuses);
if (appointmentType != null)
query.setParameter("appointmentType", appointmentType);
if (patient != null)
Expand All @@ -120,6 +121,13 @@ public List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate
}
}

@Override
@Transactional(readOnly = true)
public List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate, Provider provider,
AppointmentType appointmentType, AppointmentStatus status, Patient patient) throws APIException {
return getAppointmentsByConstraints(fromDate, toDate, provider, appointmentType, Arrays.asList(status), patient);
}

@Override
@Transactional(readOnly = true)
public List<Appointment> getAppointmentsByStates(List<AppointmentStatus> states) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,11 @@ public List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate
}

@Override
@Transactional(readOnly = true)
public List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate, Location location, Provider provider,
AppointmentType type, AppointmentStatus status, Patient patient) throws APIException {
AppointmentType type, Patient patient, List<AppointmentStatus> statuses) {

List<Appointment> appointments = appointmentDAO.getAppointmentsByConstraints(fromDate, toDate, provider, type,
status, patient);
statuses, patient);

List<Appointment> appointmentsInLocation = new LinkedList<Appointment>();

Expand All @@ -677,6 +676,18 @@ public List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate

}

@Override
@Transactional(readOnly = true)
public List<Appointment> getAppointmentsByConstraints(Date fromDate, Date toDate, Location location, Provider provider,
AppointmentType type, AppointmentStatus status, Patient patient) throws APIException {

if (status == null) {
return getAppointmentsByConstraints(fromDate, toDate, location, provider, type, patient, null);
}

return getAppointmentsByConstraints(fromDate, toDate, location, provider, type, patient, Arrays.asList(status));
}

@Override
@Transactional(readOnly = true)
public Date getAppointmentCurrentStatusStartDate(Appointment appointment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
*/
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 @@ -39,6 +32,13 @@
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.openmrs.test.Verifies;

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 static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
Expand Down Expand Up @@ -357,6 +357,24 @@ public void shouldgetAllUnvoidedAppointmentsByPatient_getAppointmentsByConstrain
assertEquals(patient, appointments.get(1).getPatient());
}

@Test
@Verifies(value = "Should get all unvoided appointments by patient with Scheduled and Rescheduled statuses", method = "getAppointmentsByConstraints(Date, Date, Location, Provider, AppointmentType, List<AppointmentStatus>, Patient)")
public void shouldgetAllUnvoidedAppointmentsByPatientAndStatus_getAppointmentsByConstraints() {

Patient patient = Context.getPatientService().getPatient(1);

List<AppointmentStatus> appointmentStatuses = Arrays.asList(AppointmentStatus.SCHEDULED,
AppointmentStatus.RESCHEDULED);

List<Appointment> appointments = service.getAppointmentsByConstraints(null, null, null, null, null, patient,
appointmentStatuses);
assertEquals(4, appointments.size());
assertEquals(patient, appointments.get(0).getPatient());
assertEquals(patient, appointments.get(1).getPatient());
assertEquals(patient, appointments.get(2).getPatient());
assertEquals(patient, appointments.get(3).getPatient());
}

@Test
@Verifies(value = "Should get correct appointments", method = "getAppointmentsByStatus(List<AppointmentStatus>)")
public void shouldGetCorrectAppointments_getAppointmentsByStatus() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.openmrs.module.appointmentscheduling.rest.resource.openmrs1_9;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.openmrs.Location;
import org.openmrs.Patient;
Expand Down Expand Up @@ -178,14 +180,29 @@ protected PageableResult doSearch(RequestContext context) {
Location location = context.getParameter("location") != null ? Context.getLocationService().getLocationByUuid(
context.getParameter("location")) : null;

Appointment.AppointmentStatus status = context.getParameter("status") != null ? Appointment.AppointmentStatus
.valueOf(context.getParameter("status")) : null;
List<Appointment.AppointmentStatus> statuses = getAppointmentsStatuses(context);

return new NeedsPaging<Appointment>(Context.getService(AppointmentService.class).getAppointmentsByConstraints(
fromDate, toDate, location, provider, appointmentType, status, patient), context);
fromDate, toDate, location, provider, appointmentType, patient, statuses), context);

}

private List<Appointment.AppointmentStatus> getAppointmentsStatuses(RequestContext context) {
String[] statuses = context.getRequest().getParameterValues("status");

if (statuses == null) {
return null;
}

List<Appointment.AppointmentStatus> statusList = new ArrayList<Appointment.AppointmentStatus>();

for (String status : statuses) {
statusList.add(Appointment.AppointmentStatus.valueOf(status));
}

return statusList;
}

public String getDisplayString(Appointment appointment) {
return appointment.getAppointmentType().getName() + " : " + appointment.getStatus();
}
Expand Down

0 comments on commit 17685a1

Please sign in to comment.