Skip to content

Commit

Permalink
TRUNK-5963: Order Service does not allow saving Order Groups with an …
Browse files Browse the repository at this point in the history
…OrderContext (#4389)

* TRUNK-5963: Order Service does not allow saving Order Groups with an OrderContext

* TRUNK-5963: Order Service does not allow saving Order Groups with an OrderContext
  • Loading branch information
IamMujuziMoses committed Sep 19, 2023
1 parent 4ba6da6 commit 0b0ae76
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
13 changes: 13 additions & 0 deletions api/src/main/java/org/openmrs/api/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,19 @@ public Order discontinueOrder(Order orderToDiscontinue, String reasonNonCoded, D
@Authorized({ PrivilegeConstants.EDIT_ORDERS, PrivilegeConstants.ADD_ORDERS })
public OrderGroup saveOrderGroup(OrderGroup orderGroup) throws APIException;

/**
* Saves an order group with a specific order context
*
* @param orderGroup the order group to be saved
* @param orderContext the order context data transfer object containing care setting and
* the order type to save with the order group
* @return the order group that was saved with the specified order context data
* @since 2.7.0
* @throws APIException
*/
@Authorized({ PrivilegeConstants.EDIT_ORDERS, PrivilegeConstants.ADD_ORDERS })
public OrderGroup saveOrderGroup(OrderGroup orderGroup, OrderContext orderContext) throws APIException;

/**
* Fetches all order groups for the specified patient
*
Expand Down
12 changes: 10 additions & 2 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public synchronized Order saveOrder(Order order, OrderContext orderContext) thro
*/
@Override
public OrderGroup saveOrderGroup(OrderGroup orderGroup) throws APIException {
return saveOrderGroup(orderGroup, null);
}

/**
* @see org.openmrs.api.OrderService#saveOrderGroup(org.openmrs.OrderGroup, org.openmrs.api.OrderContext)
*/
@Override
public OrderGroup saveOrderGroup(OrderGroup orderGroup, OrderContext orderContext) throws APIException {
if (orderGroup.getId() == null) {
// an OrderGroup requires an encounter, which has a patient, so it
// is odd that OrderGroup has a patient field. There is no obvious
Expand All @@ -126,13 +134,13 @@ public OrderGroup saveOrderGroup(OrderGroup orderGroup) throws APIException {
for (Order order : orders) {
if (order.getId() == null) {
order.setEncounter(orderGroup.getEncounter());
Context.getOrderService().saveOrder(order, null);
Context.getOrderService().saveOrder(order, orderContext);
}
}
Set<OrderGroup> nestedGroups = orderGroup.getNestedOrderGroups();
if (nestedGroups != null) {
for (OrderGroup nestedGroup : nestedGroups) {
Context.getOrderService().saveOrderGroup(nestedGroup);
Context.getOrderService().saveOrderGroup(nestedGroup, orderContext);
}
}
return orderGroup;
Expand Down
37 changes: 37 additions & 0 deletions api/src/test/java/org/openmrs/api/OrderServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3963,6 +3963,43 @@ public void saveOrder_shouldAllowARetrospectiveOrderToCloseAnOrderThatExpiredInT
encounterService.saveEncounter(e2);
assertThat(new SimpleDateFormat("yyyy-MM-dd").format(o1.getDateStopped()), is("2008-08-14"));
}

/**
* @see OrderService#saveOrderGroup(org.openmrs.OrderGroup, OrderContext)
*/
@Test
public void saveOrderGroup_shouldSaveOrderGroupWithOrderContext() {
executeDataSet(ORDER_SET);
Encounter encounter = encounterService.getEncounter(3);
OrderSet orderSet = Context.getOrderSetService().getOrderSet(1);
OrderGroup orderGroup = new OrderGroup();
orderGroup.setOrderSet(orderSet);
orderGroup.setPatient(encounter.getPatient());
orderGroup.setEncounter(encounter);

Order firstOrder = new OrderBuilder().withAction(Order.Action.NEW).withPatient(1).withConcept(10).withOrderer(1)
.withEncounter(3).withDateActivated(new Date()).withOrderType(17)
.withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).build();

Order secondOrder = new OrderBuilder().withAction(Order.Action.NEW).withPatient(7).withConcept(10).withOrderer(1)
.withEncounter(3).withDateActivated(new Date()).withOrderType(17)
.withUrgency(Order.Urgency.ON_SCHEDULED_DATE).withScheduledDate(new Date()).build();

orderGroup.addOrder(firstOrder);
orderGroup.addOrder(secondOrder);

OrderType orderType = orderService.getOrderType(17);
CareSetting careSetting = orderService.getCareSetting(1);

OrderContext orderContext = new OrderContext();
orderContext.setCareSetting(careSetting);
orderContext.setOrderType(orderType);
OrderGroup result = orderService.saveOrderGroup(orderGroup, orderContext);

assertEquals(2, result.getOrders().size());
assertEquals(orderType, result.getOrders().get(0).getOrderType());
assertEquals(careSetting, result.getOrders().get(1).getCareSetting());
}

/**
* @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
Expand Down

0 comments on commit 0b0ae76

Please sign in to comment.