Skip to content

Commit

Permalink
UHM-1183: Allow filtering by multiple services on the Schedule View
Browse files Browse the repository at this point in the history
Updated Scheduled Appointment Block resource to accept multiple appointment types
  • Loading branch information
mogoodrich committed Apr 2, 2014
1 parent f727734 commit dc32c79
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.openmrs.module.appointmentscheduling.rest.resource.openmrs1_9;

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

import org.openmrs.Provider;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointmentscheduling.AppointmentBlock;
import org.openmrs.module.appointmentscheduling.AppointmentType;
import org.openmrs.module.appointmentscheduling.api.AppointmentService;
import org.openmrs.module.appointmentscheduling.rest.controller.AppointmentRestController;
import org.openmrs.module.appointmentscheduling.rest.resource.openmrs1_9.util.AppointmentRestUtils;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
Expand All @@ -18,12 +22,6 @@
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.springframework.web.util.WebUtils;

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

@Resource(name = RestConstants.VERSION_1 + AppointmentRestController.APPOINTMENT_SCHEDULING_REST_NAMESPACE
+ "/appointmentblock", supportedClass = AppointmentBlock.class, supportedOpenmrsVersions = "1.9.*")
Expand Down Expand Up @@ -133,16 +131,7 @@ protected PageableResult doSearch(RequestContext context) {
Date endDate = context.getParameter("toDate") != null ? (Date) ConversionUtil.convert(
context.getParameter("toDate"), Date.class) : null;

context.getParameter("appointmentType");

List<AppointmentType> types = null;
String[] appointmentTypes = context.getRequest().getParameterValues("appointmentType");
if (appointmentTypes != null && appointmentTypes.length > 0) {
types = new ArrayList<AppointmentType>();
for (String appointmentType : appointmentTypes) {
types.add(Context.getService(AppointmentService.class).getAppointmentTypeByUuid(appointmentType));
}
}
List<AppointmentType> types = AppointmentRestUtils.getAppointmentTypes(context);

Provider provider = context.getParameter("provider") != null ? Context.getProviderService().getProviderByUuid(
context.getParameter("provider")) : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.openmrs.module.appointmentscheduling.rest.resource.openmrs1_9;

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

import org.apache.commons.beanutils.PropertyUtils;
import org.openmrs.Location;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointmentscheduling.AppointmentType;
import org.openmrs.module.appointmentscheduling.ScheduledAppointmentBlock;
import org.openmrs.module.appointmentscheduling.api.AppointmentService;
import org.openmrs.module.appointmentscheduling.rest.controller.AppointmentRestController;
import org.openmrs.module.appointmentscheduling.rest.resource.openmrs1_9.util.AppointmentRestUtils;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
Expand All @@ -18,11 +24,6 @@
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.module.webservices.validation.ValidationException;

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

@Resource(name = RestConstants.VERSION_1 + AppointmentRestController.APPOINTMENT_SCHEDULING_REST_NAMESPACE
+ "/scheduledappointmentblocks", supportedClass = ScheduledAppointmentBlock.class, supportedOpenmrsVersions = "1.9.*")
public class ScheduledAppointmentBlockResource1_9 implements Searchable, Retrievable {
Expand All @@ -32,14 +33,14 @@ public SimpleObject search(RequestContext context) throws ResponseException {

Date date = getDate(context);
Location location = getLocation(context);
AppointmentType appointmentType = getAppointmentType(context);
List<AppointmentType> appointmentTypes = AppointmentRestUtils.getAppointmentTypes(context);

if (date == null || location == null) {
throw new ValidationException("appointmentscheduling.AppointmentBlock.error.scheduledAppointmentBlocks");
}

List<ScheduledAppointmentBlock> dailyAppointmentBlocks = getScheduledAppointmentBlocks(date, location,
appointmentType);
appointmentTypes);

SimpleObject result = new SimpleObject();
result.add("results", convertToSimpleObjectList(dailyAppointmentBlocks));
Expand All @@ -48,8 +49,8 @@ public SimpleObject search(RequestContext context) throws ResponseException {
}

private List<ScheduledAppointmentBlock> getScheduledAppointmentBlocks(Date startDate, Location location,
AppointmentType appointmentType) {
return Context.getService(AppointmentService.class).getDailyAppointmentBlocks(location, startDate, appointmentType);
List<AppointmentType> appointmentTypes) {
return Context.getService(AppointmentService.class).getDailyAppointmentBlocks(location, startDate, appointmentTypes);
}

private Location getLocation(RequestContext context) {
Expand All @@ -62,11 +63,6 @@ private Date getDate(RequestContext context) {
.convert(context.getParameter("date"), Date.class) : null;
}

private AppointmentType getAppointmentType(RequestContext context) {
return context.getParameter("appointmentType") != null ? Context.getService(AppointmentService.class)
.getAppointmentTypeByUuid(context.getParameter("appointmentType")) : null;
}

private List<SimpleObject> convertToSimpleObjectList(List<ScheduledAppointmentBlock> dailyAppointmentBlocks) {
List<SimpleObject> simpleObjects = new ArrayList<SimpleObject>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openmrs.module.appointmentscheduling.rest.resource.openmrs1_9.util;

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

import org.openmrs.api.context.Context;
import org.openmrs.module.appointmentscheduling.AppointmentType;
import org.openmrs.module.appointmentscheduling.api.AppointmentService;
import org.openmrs.module.webservices.rest.web.RequestContext;

public class AppointmentRestUtils {

public static List<AppointmentType> getAppointmentTypes(RequestContext context) {

List<AppointmentType> types = null;
String[] appointmentTypes = context.getRequest().getParameterValues("appointmentType");
if (appointmentTypes != null && appointmentTypes.length > 0) {
types = new ArrayList<AppointmentType>();
for (String appointmentType : appointmentTypes) {
types.add(Context.getService(AppointmentService.class).getAppointmentTypeByUuid(appointmentType));
}
}

return types;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.openmrs.module.appointmentscheduling.rest.resource.openmrs1_9;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
Expand All @@ -8,8 +11,6 @@
import org.openmrs.module.webservices.validation.ValidationException;
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -27,9 +28,13 @@ public void shouldSearchForScheduledAppointmentBlock() {
ScheduledAppointmentBlockResource1_9 scheduledAppointmentBlockResource1_9 = new ScheduledAppointmentBlockResource1_9();

RequestContext context = Mockito.mock(RequestContext.class);
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);

when(context.getParameter("date")).thenReturn("2005-01-03 00:00:00.0");
when(context.getParameter("location")).thenReturn("9356400c-a5a2-4532-8f2b-2361b3446eb7");
when(context.getParameter("appointmentType")).thenReturn("759799ab-c9a5-435e-b671-77773ada74e6");
when(context.getRequest()).thenReturn(request);
when(request.getParameterValues("appointmentType")).thenReturn(
new String[] { "759799ab-c9a5-435e-b671-77773ada74e6" });

SimpleObject search = scheduledAppointmentBlockResource1_9.search(context);
assertSearchResults(search);
Expand All @@ -44,6 +49,7 @@ public void shouldSearchForScheduledAppointmentBlock() {

List appointments = (List) firstResult.get("appointments");
assertAppointmentsResult(appointments);

}

private void assertAppointmentsResult(List appointments) {
Expand Down Expand Up @@ -103,8 +109,12 @@ public void shouldReturnErrorWhenDateParameterIsMissing() {
ScheduledAppointmentBlockResource1_9 scheduledAppointmentBlockResource1_9 = new ScheduledAppointmentBlockResource1_9();

RequestContext context = Mockito.mock(RequestContext.class);
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);

when(context.getParameter("date")).thenReturn(null);
when(context.getParameter("location")).thenReturn("9356400c-a5a2-4532-8f2b-2361b3446eb7");
when(context.getRequest()).thenReturn(request);
when(request.getParameterValues("appointmentType")).thenReturn(null);

scheduledAppointmentBlockResource1_9.search(context);
}
Expand All @@ -114,8 +124,12 @@ public void shouldReturnErrorWhenLocationParameterIsMissing() {
ScheduledAppointmentBlockResource1_9 scheduledAppointmentBlockResource1_9 = new ScheduledAppointmentBlockResource1_9();

RequestContext context = Mockito.mock(RequestContext.class);
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);

when(context.getParameter("date")).thenReturn("2005-01-03 00:00:00.0");
when(context.getParameter("location")).thenReturn(null);
when(context.getRequest()).thenReturn(request);
when(request.getParameterValues("appointmentType")).thenReturn(null);

scheduledAppointmentBlockResource1_9.search(context);
}
Expand Down

0 comments on commit dc32c79

Please sign in to comment.