diff --git a/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/AppointmentBlockResource1_9.java b/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/AppointmentBlockResource1_9.java index be2ba394..616aaf2a 100644 --- a/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/AppointmentBlockResource1_9.java +++ b/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/AppointmentBlockResource1_9.java @@ -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; @@ -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.*") @@ -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 types = null; - String[] appointmentTypes = context.getRequest().getParameterValues("appointmentType"); - if (appointmentTypes != null && appointmentTypes.length > 0) { - types = new ArrayList(); - for (String appointmentType : appointmentTypes) { - types.add(Context.getService(AppointmentService.class).getAppointmentTypeByUuid(appointmentType)); - } - } + List types = AppointmentRestUtils.getAppointmentTypes(context); Provider provider = context.getParameter("provider") != null ? Context.getProviderService().getProviderByUuid( context.getParameter("provider")) : null; diff --git a/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/ScheduledAppointmentBlockResource1_9.java b/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/ScheduledAppointmentBlockResource1_9.java index c2e4ad22..bbe3ad74 100644 --- a/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/ScheduledAppointmentBlockResource1_9.java +++ b/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/ScheduledAppointmentBlockResource1_9.java @@ -1,5 +1,10 @@ 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; @@ -7,6 +12,7 @@ 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; @@ -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 { @@ -32,14 +33,14 @@ public SimpleObject search(RequestContext context) throws ResponseException { Date date = getDate(context); Location location = getLocation(context); - AppointmentType appointmentType = getAppointmentType(context); + List appointmentTypes = AppointmentRestUtils.getAppointmentTypes(context); if (date == null || location == null) { throw new ValidationException("appointmentscheduling.AppointmentBlock.error.scheduledAppointmentBlocks"); } List dailyAppointmentBlocks = getScheduledAppointmentBlocks(date, location, - appointmentType); + appointmentTypes); SimpleObject result = new SimpleObject(); result.add("results", convertToSimpleObjectList(dailyAppointmentBlocks)); @@ -48,8 +49,8 @@ public SimpleObject search(RequestContext context) throws ResponseException { } private List getScheduledAppointmentBlocks(Date startDate, Location location, - AppointmentType appointmentType) { - return Context.getService(AppointmentService.class).getDailyAppointmentBlocks(location, startDate, appointmentType); + List appointmentTypes) { + return Context.getService(AppointmentService.class).getDailyAppointmentBlocks(location, startDate, appointmentTypes); } private Location getLocation(RequestContext context) { @@ -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 convertToSimpleObjectList(List dailyAppointmentBlocks) { List simpleObjects = new ArrayList(); diff --git a/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/util/AppointmentRestUtils.java b/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/util/AppointmentRestUtils.java new file mode 100644 index 00000000..2e6b335c --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/util/AppointmentRestUtils.java @@ -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 getAppointmentTypes(RequestContext context) { + + List types = null; + String[] appointmentTypes = context.getRequest().getParameterValues("appointmentType"); + if (appointmentTypes != null && appointmentTypes.length > 0) { + types = new ArrayList(); + for (String appointmentType : appointmentTypes) { + types.add(Context.getService(AppointmentService.class).getAppointmentTypeByUuid(appointmentType)); + } + } + + return types; + } + +} diff --git a/omod/src/test/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/ScheduledAppointmentBlockResource1_9Test.java b/omod/src/test/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/ScheduledAppointmentBlockResource1_9Test.java index 67f04bca..a65bd6d1 100644 --- a/omod/src/test/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/ScheduledAppointmentBlockResource1_9Test.java +++ b/omod/src/test/java/org/openmrs/module/appointmentscheduling/rest/resource/openmrs1_9/ScheduledAppointmentBlockResource1_9Test.java @@ -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; @@ -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; @@ -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); @@ -44,6 +49,7 @@ public void shouldSearchForScheduledAppointmentBlock() { List appointments = (List) firstResult.get("appointments"); assertAppointmentsResult(appointments); + } private void assertAppointmentsResult(List appointments) { @@ -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); } @@ -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); }