Skip to content

Commit

Permalink
Follow up to clean up the code that finds an active to discontinue wh…
Browse files Browse the repository at this point in the history
…en a DC order is saved - TRUNK-4133

Follow up to discontinue order - TRUNK-4133

Follow up to clean up the code that finds an active to discontinue when a DC order is saved   - TRUNK-4202
  • Loading branch information
wluyima committed Feb 7, 2014
1 parent 32e2047 commit 0797dc9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
18 changes: 18 additions & 0 deletions api/src/main/java/org/openmrs/DrugOrder.java
Expand Up @@ -412,6 +412,24 @@ public void setDurationUnits(Concept durationUnits) {
this.durationUnits = durationUnits;
}

/**
* @see org.openmrs.Order#cloneForDiscontinuing()
* @should set all the relevant fields
* @since 1.10
*/
@Override
public Order cloneForDiscontinuing() {
DrugOrder newOrder = new DrugOrder();
newOrder.setCareSetting(this.getCareSetting());
newOrder.setConcept(this.getConcept());
newOrder.setAction(Action.DISCONTINUE);
newOrder.setPreviousOrder(this);
newOrder.setPatient(this.getPatient());
newOrder.setDrug(this.getDrug());

return newOrder;
}

public String toString() {
return "DrugOrder(" + getDose() + getDoseUnits() + " of " + (getDrug() != null ? getDrug().getName() : "[no drug]")
+ " from " + getStartDate() + " to " + (isDiscontinuedRightNow() ? getDateStopped() : getAutoExpireDate())
Expand Down
6 changes: 2 additions & 4 deletions api/src/main/java/org/openmrs/Order.java
Expand Up @@ -537,13 +537,11 @@ public void setCareSetting(CareSetting careSetting) {
* note that the discontinuation order needs to be saved for the discontinuation to take effect
*
* @return the newly created order
* @throws IllegalAccessException
* @throws InstantiationException
* @since 1.10
* @should set all the relevant fields
*/
public Order cloneForDiscontinuing() throws IllegalAccessException, InstantiationException {
Order newOrder = this.getClass().newInstance();
public Order cloneForDiscontinuing() {
Order newOrder = new Order();
newOrder.setCareSetting(this.getCareSetting());
newOrder.setConcept(this.getConcept());
newOrder.setAction(Action.DISCONTINUE);
Expand Down
12 changes: 8 additions & 4 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Expand Up @@ -128,15 +128,19 @@ private void discontinueExistingOrdersIfNecessary(Order order) {
List<? extends Order> orders = getActiveOrders(order.getPatient(), order.getClass(), order.getCareSetting(), null);
boolean isDrugOrderAndHasADrug = order.isDrugOrder() && ((DrugOrder) order).getDrug() != null;
for (Order activeOrder : orders) {
//For drug orders, the drug must match if order has a drug
boolean shouldMarkAsDiscontinued = false;
//For drug orders, the drug must match if the order has a drug
if (isDrugOrderAndHasADrug) {
DrugOrder drugOrder1 = (DrugOrder) order;
DrugOrder drugOrder2 = (DrugOrder) activeOrder;
if (!OpenmrsUtil.nullSafeEquals(drugOrder1.getDrug(), drugOrder2.getDrug())) {
continue;
if (OpenmrsUtil.nullSafeEquals(drugOrder1.getDrug(), drugOrder2.getDrug())) {
shouldMarkAsDiscontinued = true;
}
} else if (activeOrder.getConcept().equals(order.getConcept())) {
shouldMarkAsDiscontinued = true;
}
if (isDrugOrderAndHasADrug || activeOrder.getConcept().equals(order.getConcept())) {

if (shouldMarkAsDiscontinued) {
order.setPreviousOrder(activeOrder);
markAsDiscontinued(activeOrder, order.getStartDate());
saveOrderInternal(activeOrder);
Expand Down
51 changes: 51 additions & 0 deletions api/src/test/java/org/openmrs/DrugOrderTest.java
@@ -0,0 +1,51 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

/**
* Contains tests for DrugOrder
*/
public class DrugOrderTest {

/**
* @verifies set all the relevant fields
* @see DrugOrder#cloneForDiscontinuing()
*/
@Test
public void cloneForDiscontinuing_shouldSetAllTheRelevantFields() throws Exception {
DrugOrder order = new DrugOrder();
order.setPatient(new Patient());
order.setCareSetting(new CareSetting());
order.setConcept(new Concept());
order.setDrug(new Drug());

DrugOrder dcOrder = (DrugOrder) order.cloneForDiscontinuing();

assertEquals(order.getDrug(), dcOrder.getDrug());

assertEquals(order.getPatient(), dcOrder.getPatient());

assertEquals(order.getConcept(), dcOrder.getConcept());

assertEquals("should set previous order to anOrder", order, dcOrder.getPreviousOrder());

assertEquals("should set new order action to new", dcOrder.getAction(), Order.Action.DISCONTINUE);

assertEquals(order.getCareSetting(), dcOrder.getCareSetting());
}
}
13 changes: 5 additions & 8 deletions api/src/test/java/org/openmrs/OrderTest.java
Expand Up @@ -13,15 +13,14 @@
*/
package org.openmrs;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
* This class tests all methods that are not getter or setters in the Order java object TODO: finish
Expand Down Expand Up @@ -107,13 +106,11 @@ public void cloneForDiscontinuing_shouldSetAllTheRelevantFields() throws Excepti

Order orderThatCanDiscontinueTheOrder = anOrder.cloneForDiscontinuing();

assertEquals(anOrder.getClass(), orderThatCanDiscontinueTheOrder.getClass());

assertEquals(anOrder.getPatient(), orderThatCanDiscontinueTheOrder.getPatient());

assertEquals(anOrder.getConcept(), orderThatCanDiscontinueTheOrder.getConcept());

assertEquals("should set previous order to anOrder", orderThatCanDiscontinueTheOrder.getPreviousOrder(), anOrder);
assertEquals("should set previous order to anOrder", anOrder, orderThatCanDiscontinueTheOrder.getPreviousOrder());

assertEquals("should set new order action to new", orderThatCanDiscontinueTheOrder.getAction(),
Order.Action.DISCONTINUE);
Expand Down

0 comments on commit 0797dc9

Please sign in to comment.