Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRUNK-4133 - Implement discontinueOrder API in OrderService.
TRUNK-4133 - Modify OrderService.saveOrder(Order order) to correctly handle discontinue orders Minor documentation change TRUNK-4133 - Ensure discontinue and discontinued orders have the same concept TRUNK-4133 - Changes based on review comments. Add annotations for junit tests. Some extra documentation. Remove unnecessary executeDataset calls in OrderServiceTest Make concept for discontinuation a normal concept, not a numeric Additional assert statements
- Loading branch information
Showing
7 changed files
with
359 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ public void setOrderDAO(OrderDAO dao) { | |
*/ | ||
public Order saveOrder(Order order) throws APIException { | ||
if (order.getOrderId() == null) { | ||
discontinueExistingOrdersIfRequired(order); | ||
//TODO call module registered order number generators | ||
//and if there is none, use the default below | ||
try { | ||
|
@@ -83,6 +84,46 @@ public Order saveOrder(Order order) throws APIException { | |
|
||
return dao.saveOrder(order); | ||
} | ||
|
||
/** | ||
* 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) { | ||
//Ignore and return if this is not an order to discontinue | ||
if (!Order.Action.DISCONTINUE.equals(order.getAction())) | ||
return; | ||
|
||
//Discontinue previousOrder if it is not already | ||
Order previousOrder = order.getPreviousOrder(); | ||
if (previousOrder != null) { | ||
if (!previousOrder.getConcept().equals(order.getConcept())) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
throw new APIException("Concept of previous order and this order should be the same"); | ||
} | ||
|
||
if (previousOrder.getDateStopped() == null) { | ||
discontinue(previousOrder, order.getStartDate()); | ||
} | ||
return; | ||
} | ||
|
||
//Discontinue the first found order corresponding to this DC order. | ||
List<? extends Order> orders = getActiveOrders(order.getPatient(), order.getClass(), order.getCareSetting(), null); | ||
for (Order activeOrder : orders) { | ||
if (activeOrder.getConcept().equals(order.getConcept())) { | ||
This comment has been minimized.
Sorry, something went wrong.
wluyima
Member
|
||
order.setPreviousOrder(activeOrder); | ||
discontinue(activeOrder, order.getStartDate()); | ||
return; | ||
} | ||
} | ||
|
||
throw new APIException("Could not find an active order with the concept " + order.getConcept() | ||
+ " to discontinue. "); | ||
} | ||
|
||
/** | ||
* @see org.openmrs.api.OrderService#purgeOrder(org.openmrs.Order) | ||
|
@@ -272,4 +313,45 @@ public CareSetting getCareSetting(Integer careSettingId) { | |
public OrderFrequency getOrderFrequency(Integer orderFrequencyId) { | ||
return dao.getOrderFrequency(orderFrequencyId); | ||
} | ||
|
||
/** | ||
* @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) { | ||
discontinue(orderToDiscontinue, discontinueDate); | ||
|
||
Order newOrder = orderToDiscontinue.cloneForDiscontinuing(); | ||
newOrder.setDiscontinuedReason(reasonCoded); | ||
|
||
return saveOrder(newOrder); | ||
} | ||
|
||
/** | ||
* @see org.openmrs.api.OrderService#discontinueOrder(org.openmrs.Order, String, java.util.Date) | ||
*/ | ||
@Override | ||
public Order discontinueOrder(Order orderToDiscontinue, String reasonNonCoded, Date discontinueDate) { | ||
discontinue(orderToDiscontinue, discontinueDate); | ||
|
||
Order newOrder = orderToDiscontinue.cloneForDiscontinuing(); | ||
newOrder.setDiscontinuedReasonNonCoded(reasonNonCoded); | ||
|
||
return saveOrder(newOrder); | ||
} | ||
|
||
/** | ||
* 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 (orderToDiscontinue.getAction().equals(Order.Action.DISCONTINUE)) { | ||
throw new APIException("An order with action " + Order.Action.DISCONTINUE + " cannot be discontinued. "); | ||
} | ||
|
||
orderToDiscontinue.setDateStopped(discontinueDate); | ||
saveOrder(orderToDiscontinue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Am starting to think we need to improve this if this is a drug order, we need to check for the order with the same drug and not just the generic concept unless drug is null