Skip to content

Commit

Permalink
Delete other order stuff before deleting current order - TRUNK-3289
Browse files Browse the repository at this point in the history
TRUNK-3289a

TRUNK-3289a -- change OrderServiceTest

TRUNK-3289 new method and test - deleteObsThatReference

TRUNK-3289 Remove comments

Removed debug code, added javadoc

TRUNK-3289 cleanup code

TRUNK-3289 remove unneccessary code

Cleaned up the query that  deletes related obs - TRUNK-3289
  • Loading branch information
andreapat authored and wluyima committed Jun 10, 2013
1 parent 94b6841 commit aab254a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
18 changes: 18 additions & 0 deletions api/src/main/java/org/openmrs/api/OrderService.java
Expand Up @@ -80,6 +80,24 @@ public interface OrderService extends OpenmrsService {
@Authorized(PrivilegeConstants.PURGE_ORDERS)
public void purgeOrder(Order order) throws APIException;

/**
* Completely delete an order from the database. This should not typically be used unless
* desperately needed. Most orders should just be voided. See {@link #voidOrder(Order, String)}
* This method is different from purgeOrder(Order order) above:
* If param cascade is false will completely delete an order from the database period
* If param cascade is true will completely delete an order from the database and delete any
* Obs that references the Order.
*
* @param order The Order to remove from the system
* @param boolean cascade
* @throws APIException
* @should delete order
* @should delete order when cascade is false
* @should delete order when cascade is true and also delete any Obs that references it
*/
@Authorized(PrivilegeConstants.PURGE_ORDERS)
public void purgeOrder(Order order, boolean cascade) throws APIException;

/**
* Mark an order as voided. This functionally removes the Order from the system while keeping a
* semblance
Expand Down
5 changes: 5 additions & 0 deletions api/src/main/java/org/openmrs/api/db/OrderDAO.java
Expand Up @@ -97,5 +97,10 @@ public <Ord extends Order> List<Ord> getOrders(Class<Ord> orderClassType, List<P
* @see org.openmrs.api.OrderService#getOrderHistoryByOrderNumber(java.lang.String)
*/
public List<Order> getOrderHistoryByOrderNumber(String orderNumber);

/**
* Delete Obs that references an order
*/
public void deleteObsThatReference(Order order);

}
Expand Up @@ -22,16 +22,22 @@
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.openmrs.Concept;
import org.openmrs.ConceptClass;
import org.openmrs.ConceptWord;
import org.openmrs.DrugOrder;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.Order;
import org.openmrs.Order.OrderAction;
import org.openmrs.Patient;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.api.db.DAOException;
import org.openmrs.api.db.OrderDAO;

Expand Down Expand Up @@ -202,6 +208,17 @@ public List<DrugOrder> getDrugOrdersByPatientAndIngredient(Patient patient, Conc

return (List<DrugOrder>) searchDrugOrderCriteria.list();
}

/**
* Delete Obs that references (deleted) Order
*/
public void deleteObsThatReference(Order order) {
if (order != null) {
sessionFactory.getCurrentSession().createQuery("delete Obs where order = :order").setParameter("order", order)
.executeUpdate();

}
}

/* (non-Javadoc)
* @see org.openmrs.api.db.OrderDAO#getOrderHistoryByOrderNumber(java.lang.String)
Expand Down
5 changes: 1 addition & 4 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Expand Up @@ -110,11 +110,8 @@ public void purgeOrder(Order order) throws APIException {
*/
public void purgeOrder(Order order, boolean cascade) throws APIException {
if (cascade) {
// TODO delete other order stuff before deleting this order
// (like DrugOrder?)
throw new APIException("Cascade purging of Orders is not written yet");
dao.deleteObsThatReference(order);
}

dao.deleteOrder(order);
}

Expand Down
38 changes: 37 additions & 1 deletion api/src/test/java/org/openmrs/api/OrderServiceTest.java
Expand Up @@ -27,10 +27,12 @@
import org.junit.Before;
import org.junit.Test;
import org.openmrs.Concept;
import org.openmrs.ConceptMapType;
import org.openmrs.Drug;
import org.openmrs.DrugOrder;
import org.openmrs.Encounter;
import org.openmrs.GenericDrug;
import org.openmrs.Obs;
import org.openmrs.Order;
import org.openmrs.Order.OrderAction;
import org.openmrs.Orderable;
Expand All @@ -52,6 +54,8 @@ public class OrderServiceTest extends BaseContextSensitiveTest {

protected static final String ORDERS_DATASET_XML = "org/openmrs/api/include/OrderServiceTest-ordersList.xml";

protected static final String OBS_THAT_REFERENCE_DATASET_XML = "org/openmrs/api/include/OrderServiceTest-deleteObsThatReference.xml";

private OrderService service;

@Before
Expand Down Expand Up @@ -579,7 +583,7 @@ public void saveOrder_shouldAssignOrderNumberForNewOrder() throws Exception {
Assert.assertNotNull(order.getOrderId());
Assert.assertNotNull(order.getOrderNumber());
}

/**
* @throws Exception
* @see OrderService#getOrderHistoryByOrderNumber(String)
Expand Down Expand Up @@ -607,4 +611,36 @@ public void getOrderHistoryByOrderNumber_shouldReturnTheListOfOrdersInAHistory()
Assert.assertEquals(expectedHistory, history);

}

@Test
public void purgeOrder_shouldDeleteObsThatReference() throws Exception {
executeDataSet(OBS_THAT_REFERENCE_DATASET_XML);
final String ordUuid = "0c96f25c-4949-4f72-9931-d808fbcdb612";
final String obsUuid = "be3a4d7a-f9ab-47bb-aaad-bc0b452fcda4";
ObsService os = Context.getObsService();
OrderService service = Context.getOrderService();

Obs obs = os.getObsByUuid(obsUuid);
Assert.assertNotNull(obs);

Order order = service.getOrderByUuid(ordUuid);
Assert.assertNotNull(order);

//sanity check to ensure that the obs and order are actually related
Assert.assertEquals(order, obs.getOrder());

//Ensure that passing false does not delete the related obs
service.purgeOrder(order, false);
Assert.assertNotNull(os.getObsByUuid(obsUuid));

service.purgeOrder(order, true);

//Ensure that actually the order got purged
Assert.assertNull(service.getOrderByUuid(ordUuid));

//Ensure that the related obs got deleted
Assert.assertNull(os.getObsByUuid(obsUuid));

}

}
@@ -0,0 +1,11 @@
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<orders order_id="61" order_number="ORD-61" order_action="ORDER" urgency="ROUTINE" start_date="2012-08-19 00:00:00.0" creator="1" date_created="2012-01-19 12:24:10.0" voided="false" uuid="0c96f25c-4949-4f72-9931-d808fbcdb612"/>
<orders order_id="71" order_number="ORD-71" order_action="ORDER" urgency="ROUTINE" start_date="2012-09-19 00:00:00.0" creator="1" date_created="2012-01-19 12:24:10.0" voided="false" uuid="1c96f25c-4949-4f72-9931-d808fbcdb613"/>
<orders order_id="72" order_number="ORD-72" order_action="ORDER" urgency="ROUTINE" start_date="2012-08-19 00:00:00.0" creator="1" date_created="2012-01-19 12:24:10.0" voided="true" uuid="1c96f25c-4949-4f72-9931-d808fbcdb615"/>

<obs obs_id="1" person_id="2" concept_id="88" order_id="61" obs_datetime="2006-02-09 00:00:00.0" creator="1" date_created="2006-02-10 15:57:35.0" voided="true" uuid="be3a4d7a-f9ab-47bb-aaad-bc0b452fcda4"/>
<obs obs_id="2" person_id="2" concept_id="792" order_id="61" obs_datetime="2006-02-09 00:00:00.0" creator="1" date_created="2006-02-10 15:57:35.0" voided="true" uuid="b5499df2-b17c-4b39-88a6-44591c165569"/>
<obs obs_id="3" person_id="6" concept_id="88" order_id="71" obs_datetime="2006-02-09 00:00:00.0" creator="1" date_created="2006-02-10 15:57:35.0" voided="true" uuid="efbba621-51ca-428b-8671-4f4eede10b4b"/>
<obs obs_id="4" person_id="2" concept_id="792" order_id="72" obs_datetime="2006-02-09 00:00:00.0" creator="1" date_created="2006-02-10 15:57:35.0" voided="true" uuid="144cb55d-69ab-4009-8256-f4750e8770ee"/>
</dataset>

0 comments on commit aab254a

Please sign in to comment.