Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: openmrs/openmrs-core
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 39c783d63e7a
Choose a base ref
...
head repository: openmrs/openmrs-core
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 05b88a9b399d
Choose a head ref
  • 3 commits
  • 3 files changed
  • 1 contributor

Commits on Jan 31, 2014

  1. Copy the full SHA
    0d8d387 View commit details
  2. Updated OrderService.getOrderHistoryByConcept to require concept and …

    …added units tests - TRUNK-4147
    wluyima committed Jan 31, 2014
    Copy the full SHA
    f6a7565 View commit details
  3. Copy the full SHA
    05b88a9 View commit details
23 changes: 14 additions & 9 deletions api/src/main/java/org/openmrs/api/OrderService.java
Original file line number Diff line number Diff line change
@@ -173,6 +173,8 @@ public <Ord extends Order> List<Ord> getOrders(Class<Ord> orderClassType, List<P
* @return the list of orders.
* @should return orders with the given concept
* @should return empty list for concept without orders
* @should reject a null patient
* @should reject a null concept
*/
public List<Order> getOrderHistoryByConcept(Patient patient, Concept concept);

@@ -223,13 +225,14 @@ public <Ord extends Order> List<Ord> getOrders(Class<Ord> orderClassType, List<P
* @should return all active test orders for the specified patient
* @should fail if patient is null
* @should return active orders as of the specified date
* @should default to Order class if no orderClass is specified
*/
public <Ord extends Order> List<Ord> getActiveOrders(Patient patient, Class<Ord> orderClass, CareSetting careSetting,
Date asOfDate);

/**
* Retrieve care setting
*
*
* @param careSettingId
* @return the care setting
* @since 1.10
@@ -247,32 +250,34 @@ public <Ord extends Order> List<Ord> getActiveOrders(Patient patient, Class<Ord>
public OrderFrequency getOrderFrequency(Integer orderFrequencyId);

/**
* Discontinues an order.
* Creates a new order that discontinues the orderToDiscontinue
*
* Discontinues an order. Creates a new order that discontinues the orderToDiscontinue
*
* @param orderToDiscontinue
* @param reasonCoded
* @param discontinueDate
* @return the new order that discontinued orderToDiscontinue
* @throws APIException if the <code>action</code> of orderToDiscontinue is <code>Order.Action.DISCONTINUE</code>
* @throws APIException if the <code>action</code> of orderToDiscontinue is
* <code>Order.Action.DISCONTINUE</code>
* @since 1.10
* @should populate correct attributes on the discontinue and discontinued orders
* @should fail for a discontinue order
* @should reject a future discontinueDate
*/
public Order discontinueOrder(Order orderToDiscontinue, Concept reasonCoded, Date discontinueDate);

/**
* Discontinues an order.
* Creates a new order that discontinues the orderToDiscontinue.
*
* Discontinues an order. Creates a new order that discontinues the orderToDiscontinue.
*
* @param orderToDiscontinue
* @param reasonNonCoded
* @param discontinueDate
* @return the new order that discontinued orderToDiscontinue
* @throws APIException if the <code>action</code> of orderToDiscontinue is <code>Order.Action.DISCONTINUE</code>
* @throws APIException if the <code>action</code> of orderToDiscontinue is
* <code>Order.Action.DISCONTINUE</code>
* @since 1.10
* @should populate correct attributes on the discontinue and discontinued orders
* @should fail for a discontinue order
* @should fail if discontinueDate is in the future
*/
public Order discontinueOrder(Order orderToDiscontinue, String reasonNonCoded, Date discontinueDate);
}
31 changes: 20 additions & 11 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
import org.apache.commons.logging.LogFactory;
import org.openmrs.CareSetting;
import org.openmrs.Concept;
import org.openmrs.DrugOrder;
import org.openmrs.Encounter;
import org.openmrs.Order;
import org.openmrs.OrderFrequency;
@@ -86,11 +87,11 @@ public Order saveOrder(Order order) throws APIException {
}

/**
* If this is a discontinue order, ensure that the previous order is discontinued.
* If a previousOrder is present, then ensure this is discontinued.
* If no previousOrder is present, then try to find a previousOrder and discontinue it.
* If cannot find a previousOrder, throw exception
*
* If this is a discontinue order, ensure that the previous order is discontinued. If a
* previousOrder is present, then ensure this is discontinued. If no previousOrder is present,
* then try to find a previousOrder and discontinue it. If cannot find a previousOrder, throw
* exception
*
* @param order
*/
private void discontinueExistingOrdersIfRequired(Order order) {
@@ -244,9 +245,9 @@ public Order getOrderByOrderNumber(String orderNumber) {
@Override
@Transactional(readOnly = true)
public List<Order> getOrderHistoryByConcept(Patient patient, Concept concept) {
if (patient == null)
throw new IllegalArgumentException("patient is required");

if (patient == null || concept == null) {
throw new IllegalArgumentException("patient and concept are required");
}
List<Concept> concepts = new Vector<Concept>();
concepts.add(concept);

@@ -294,6 +295,9 @@ public <Ord extends Order> List<Ord> getActiveOrders(Patient patient, Class<Ord>
if (asOfDate == null) {
asOfDate = new Date();
}
if (orderClass == null) {
orderClass = (Class<Ord>) Order.class;
}
return dao.getActiveOrders(patient, orderClass, careSetting, asOfDate);
}

@@ -314,7 +318,8 @@ public OrderFrequency getOrderFrequency(Integer orderFrequencyId) {
}

/**
* @see org.openmrs.api.OrderService#discontinueOrder(org.openmrs.Order, org.openmrs.Concept, java.util.Date)
* @see org.openmrs.api.OrderService#discontinueOrder(org.openmrs.Order, org.openmrs.Concept,
* java.util.Date)
*/
@Override
public Order discontinueOrder(Order orderToDiscontinue, Concept reasonCoded, Date discontinueDate) {
@@ -340,12 +345,16 @@ public Order discontinueOrder(Order orderToDiscontinue, String reasonNonCoded, D
}

/**
* Make necessary checks, set necessary fields for discontinuing <code>orderToDiscontinue</code> and save.
*
* Make necessary checks, set necessary fields for discontinuing <code>orderToDiscontinue</code>
* and save.
*
* @param orderToDiscontinue
* @param discontinueDate
*/
private void discontinue(Order orderToDiscontinue, Date discontinueDate) {
if (discontinueDate != null && discontinueDate.after(new Date())) {
throw new IllegalArgumentException("Future discontinue date is not allowed");
}
if (orderToDiscontinue.getAction().equals(Order.Action.DISCONTINUE)) {
throw new APIException("An order with action " + Order.Action.DISCONTINUE + " cannot be discontinued. ");
}
67 changes: 61 additions & 6 deletions api/src/test/java/org/openmrs/api/OrderServiceTest.java
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
@@ -250,6 +251,24 @@ public void getOrderHistoryByConcept_shouldReturnEmptyListForConceptWithoutOrder
Assert.assertEquals(0, orders.size());
}

/**
* @verifies reject a null concept
* @see OrderService#getOrderHistoryByConcept(org.openmrs.Patient, org.openmrs.Concept)
*/
@Test(expected = IllegalArgumentException.class)
public void getOrderHistoryByConcept_shouldRejectANullConcept() throws Exception {
orderService.getOrderHistoryByConcept(new Patient(), null);
}

/**
* @verifies reject a null patient
* @see OrderService#getOrderHistoryByConcept(org.openmrs.Patient, org.openmrs.Concept)
*/
@Test(expected = IllegalArgumentException.class)
public void getOrderHistoryByConcept_shouldRejectANullPatient() throws Exception {
orderService.getOrderHistoryByConcept(null, new Concept());
}

/**
* @see {@link OrderService#getOrderHistoryByOrderNumber(String)}
*/
@@ -409,15 +428,27 @@ public void getActiveOrders_shouldReturnActiveOrdersAsOfTheSpecifiedDate() throw

asOfDate = Context.getDateFormat().parse("04/12/2008");
orders = orderService.getActiveOrders(patient, Order.class, null, asOfDate);
for (Order o : orders) {
System.out.println(o.getOrderId());
}
assertEquals(5, orders.size());
Order[] expectedOrders5 = { orderService.getOrder(222), orderService.getOrder(3), orderService.getOrder(444),
orderService.getOrder(5), orderService.getOrder(7) };
assertThat(orders, hasItems(expectedOrders5));
}

/**
* @verifies default to Order class if no orderClass is specified
* @see OrderService#getActiveOrders(org.openmrs.Patient, Class, org.openmrs.CareSetting,
* java.util.Date)
*/
@Test
public void getActiveOrders_shouldDefaultToOrderClassIfNoOrderClassIsSpecified() throws Exception {
Patient patient = Context.getPatientService().getPatient(2);
List<Order> orders = orderService.getActiveOrders(patient, null, null, null);
assertEquals(5, orders.size());
Order[] expectedOrders = { orderService.getOrder(222), orderService.getOrder(3), orderService.getOrder(444),
orderService.getOrder(5), orderService.getOrder(7) };
assertThat(orders, hasItems(expectedOrders));
}

/**
* @see {@link OrderService#discontinueOrder(org.openmrs.Order, String, java.util.Date)}
*/
@@ -568,8 +599,32 @@ public void saveOrder_shouldFailIfConceptInPreviousOrderDoesNotMatchThisConcept(
orderService.saveOrder(order);
}

private boolean isOrderActive(Order order) {
return order.getDateStopped() == null && order.getAutoExpireDate() == null
&& order.getAction() != Action.DISCONTINUE;
/**
* @verifies reject a future discontinueDate
* @see OrderService#discontinueOrder(org.openmrs.Order, org.openmrs.Concept, java.util.Date)
*/
@Test(expected = IllegalArgumentException.class)
public void discontinueOrder_shouldRejectAFutureDiscontinueDate() throws Exception {
executeDataSet("org/openmrs/api/include/OrderServiceTest-globalProperties.xml");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 1);
Patient patient = Context.getPatientService().getPatient(2);
CareSetting careSetting = orderService.getCareSetting(1);
Order orderToDiscontinue = orderService.getActiveOrders(patient, Order.class, careSetting, null).get(0);
orderService.discontinueOrder(orderToDiscontinue, new Concept(), cal.getTime());
}

/**
* @verifies fail if discontinueDate is in the future
* @see OrderService#discontinueOrder(org.openmrs.Order, String, java.util.Date)
*/
@Test(expected = IllegalArgumentException.class)
public void discontinueOrder_shouldFailIfDiscontinueDateIsInTheFuture() throws Exception {
executeDataSet("org/openmrs/api/include/OrderServiceTest-globalProperties.xml");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 1);
Order orderToDiscontinue = orderService.getActiveOrders(Context.getPatientService().getPatient(2), Order.class,
orderService.getCareSetting(1), null).get(0);
orderService.discontinueOrder(orderToDiscontinue, "Testing", cal.getTime());
}
}