Skip to content

Commit

Permalink
The API should be able to assign OrderTypes - TRUNK-4302
Browse files Browse the repository at this point in the history
  • Loading branch information
dkayiwa committed Mar 17, 2014
1 parent f394446 commit d1abac0
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/org/openmrs/api/OrderService.java
Expand Up @@ -63,6 +63,8 @@ public interface OrderService extends OpenmrsService {
* @should save a revised order
* @should set order number specified in the context if specified
* @should set the order number returned by the configured generator
* @should set order type if null but mapped to the concept class
* @should fail if order type is null and not mapped to the concept class
*/
@Authorized( { PrivilegeConstants.EDIT_ORDERS, PrivilegeConstants.ADD_ORDERS })
public Order saveOrder(Order order, OrderContext orderContext) throws APIException;
Expand Down
9 changes: 9 additions & 0 deletions api/src/main/java/org/openmrs/api/db/OrderDAO.java
Expand Up @@ -19,6 +19,7 @@

import org.openmrs.CareSetting;
import org.openmrs.Concept;
import org.openmrs.ConceptClass;
import org.openmrs.Encounter;
import org.openmrs.Order;
import org.openmrs.OrderFrequency;
Expand Down Expand Up @@ -194,4 +195,12 @@ public List<OrderFrequency> getOrderFrequencies(String searchPhrase, Locale loca
* @See OrderService#getOrderTypes
*/
public List<OrderType> getOrderTypes(boolean includeRetired);

/**
* Gets the order type mapped to a given concept class
*
* @param conceptClass the concept class
* @return the matching order type
*/
public OrderType getOrderTypeByConceptClass(ConceptClass conceptClass);
}
Expand Up @@ -35,6 +35,7 @@
import org.hibernate.transform.DistinctRootEntityResultTransformer;
import org.openmrs.CareSetting;
import org.openmrs.Concept;
import org.openmrs.ConceptClass;
import org.openmrs.Encounter;
import org.openmrs.GlobalProperty;
import org.openmrs.Order;
Expand Down Expand Up @@ -470,4 +471,14 @@ public List<OrderType> getOrderTypes(boolean includeRetired) {
}
return c.list();
}

/**
* @see org.openmrs.api.db.OrderDAO#getOrderTypeByConceptClass(org.openmrs.ConceptClass)
*/
@Override
public OrderType getOrderTypeByConceptClass(ConceptClass conceptClass) {
return (OrderType) sessionFactory.getCurrentSession().createQuery(
"from OrderType where :conceptClass in elements(conceptClasses)").setParameter("conceptClass", conceptClass)

This comment has been minimized.

Copy link
@wluyima

wluyima Mar 18, 2014

Member

I'm not sure how this works, did you write unit tests around this, i actually don't see where you added this method to OrderService, you need to add it and getOrderTypeByClass.
Since order types will certainly be few even for the largest implementation i can imagine i believe it should be okay to fetch all order types from the DB in the serviceImpl by calling getOrderTypes(true) and check if any contains the concept class in its concept classes collection and return it if exactly one is found. This is what getLocationByTag does

.uniqueResult();
}
}
29 changes: 29 additions & 0 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Expand Up @@ -24,6 +24,7 @@
import org.apache.commons.logging.LogFactory;
import org.openmrs.CareSetting;
import org.openmrs.Concept;
import org.openmrs.ConceptClass;
import org.openmrs.DrugOrder;
import org.openmrs.Encounter;
import org.openmrs.GlobalProperty;
Expand Down Expand Up @@ -83,6 +84,14 @@ public Order saveOrder(Order order, OrderContext orderContext) throws APIExcepti
throw new APIException("Cannot edit an existing order, you need to revise it instead");
}

if (order.getOrderType() == null) {

This comment has been minimized.

Copy link
@wluyima

wluyima Mar 18, 2014

Member

I think this should be in OrderSaveHandler so that the validator doesn't fail since this is required

OrderType orderType = getOrderTypeByConcept(order.getConcept());
if (orderType == null) {
throw new APIException("No order type matches the concept class");
}
order.setOrderType(orderType);
}

if (Order.Action.REVISE.equals(order.getAction())) {
Order previousOrder = order.getPreviousOrder();
if (previousOrder == null) {
Expand Down Expand Up @@ -595,4 +604,24 @@ public OrderType getOrderTypeByUuid(String uuid) {
public List<OrderType> getOrderTypes(boolean includeRetired) {
return dao.getOrderTypes(includeRetired);
}

/**
* Gets the order type mapped to a given concept
*
* @param concept the concept
* @return the matching order type
*/
private OrderType getOrderTypeByConcept(Concept concept) {
return getOrderTypeByConceptClass(concept.getConceptClass());
}

/**
* Gets the order type mapped to a given concept class
*
* @param conceptClass the concept class
* @return the matching order type
*/
private OrderType getOrderTypeByConceptClass(ConceptClass conceptClass) {
return dao.getOrderTypeByConceptClass(conceptClass);
}
}
37 changes: 37 additions & 0 deletions api/src/test/java/org/openmrs/api/OrderServiceTest.java
Expand Up @@ -1408,4 +1408,41 @@ public void getAllOrdersByPatient_shouldGetAllTheOrdersForTheSpecifiedPatient()
assertEquals(12, orderService.getAllOrdersByPatient(patientService.getPatient(2)).size());
assertEquals(2, orderService.getAllOrdersByPatient(patientService.getPatient(7)).size());
}

/**
* @verifies set order type if null but mapped to the concept class
* @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
*/
@Test
public void saveOrder_shouldSetOrderTypeIfNullButMappedToTheConceptClass() throws Exception {
executeDataSet(OTHER_ENCOUNTERS_XML);
Order order = new Order();
order.setPatient(patientService.getPatient(2));
order.setConcept(conceptService.getConcept(5497));
order.setOrderer(providerService.getProvider(1));
order.setCareSetting(orderService.getCareSetting(1));
order.setEncounter(encounterService.getEncounter(6));
order.setStartDate(new Date());
order = orderService.saveOrder(order, null);
assertTrue(order.getOrderType().getOrderTypeId() == 1);

This comment has been minimized.

Copy link
@wluyima

wluyima Mar 18, 2014

Member

I personally prefer to use assertEquals(1, order.getOrderType().getOrderTypeId().intValue() ) because then junit gives a better failure message like: expected 1 but was 2 instead of throwing a java.lang.AssertionError exception with no helpful error message

}

/**
* @verifies fail if order type is null and not mapped to the concept class
* @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
*/
@Test
public void saveOrder_shouldFailIfOrderTypeIsNullAndNotMappedToTheConceptClass() throws Exception {
executeDataSet(OTHER_ENCOUNTERS_XML);
Order order = new Order();
order.setPatient(patientService.getPatient(2));
order.setConcept(conceptService.getConcept(3));
order.setOrderer(providerService.getProvider(1));
order.setCareSetting(orderService.getCareSetting(1));
order.setEncounter(encounterService.getEncounter(6));
order.setStartDate(new Date());
expectedException.expect(APIException.class);
expectedException.expectMessage("No order type matches the concept class");
orderService.saveOrder(order, null);
}
}
@@ -0,0 +1,39 @@
package org.openmrs.api.db.hibernate;

This comment has been minimized.

Copy link
@wluyima

wluyima Mar 22, 2014

Member

Missing license


import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.ConceptClass;
import org.openmrs.OrderType;
import org.openmrs.api.context.Context;
import org.openmrs.test.BaseContextSensitiveTest;

public class HibernateOrderDAOTest extends BaseContextSensitiveTest {

private HibernateOrderDAO dao = null;

/**
* Run this before each unit test in this class.
*
* @throws Exception
*/
@Before
public void runBeforeEachTest() throws Exception {

if (dao == null) {
dao = (HibernateOrderDAO) applicationContext.getBean("orderDAO");
}
}

/**
* @see HibernateOrderDAO#getOrderTypeByConceptClass(ConceptClass)
* @verifies return order type mapped to given concept class
*/
@Test
public void getAllPatientIdentifierTypes_shouldReturnOrderTypeMappedToGivenConceptClass() throws Exception {

This comment has been minimized.

Copy link
@wluyima

wluyima Mar 18, 2014

Member

This method name is incorrect, patient identifier types?
And this test should be a service level test once you add the equivalent one to the service layer as said earlier

OrderType orderType = dao.getOrderTypeByConceptClass(Context.getConceptService().getConceptClass(1));

Assert.assertNotNull(orderType);
Assert.assertTrue(orderType.getOrderTypeId() == 1);

This comment has been minimized.

Copy link
@wluyima

wluyima Mar 18, 2014

Member

same here, use assertEquals

}
}

0 comments on commit d1abac0

Please sign in to comment.