Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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.
Sorry, something went wrong.
wluyima
Member
|
||
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) { | ||
|
@@ -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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
wluyima
Member
|
||
} | ||
|
||
/** | ||
* @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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.openmrs.api.db.hibernate; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
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.
Sorry, something went wrong.
wluyima
Member
|
||
OrderType orderType = dao.getOrderTypeByConceptClass(Context.getConceptService().getConceptClass(1)); | ||
|
||
Assert.assertNotNull(orderType); | ||
Assert.assertTrue(orderType.getOrderTypeId() == 1); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
} |
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