Skip to content

Commit

Permalink
half way though: Add API methods for retrieving active orders -
Browse files Browse the repository at this point in the history
TRUNK-4147
  • Loading branch information
dkayiwa committed Dec 18, 2013
1 parent f3b4bb7 commit 560b09e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
22 changes: 22 additions & 0 deletions api/src/main/java/org/openmrs/api/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,26 @@ public <Ord extends Order> List<Ord> getOrders(Class<Ord> orderClassType, List<P
@Authorized(PrivilegeConstants.DELETE_ORDERS)
public Order unvoidOrder(Order order) throws APIException;

/**
* Gets the order identified by a given order number
*
* @param orderNumber the order number
* @return the order object
* @should find object given valid order number
* @should return null if no object found with given order number
*/
@Transactional(readOnly = true)
public Order getOrderByOrderNumber(String orderNumber);

/**
* Gets all Order objects that use this Concept for a given patient.

This comment has been minimized.

Copy link
@wluyima

wluyima Dec 20, 2013

Member

I think this needs to say clearly that the orders will be returned in the order in which they occurred i.e sorted by dateCreated starting with the latest

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Dec 23, 2013

Author Member

Done at: b7ec490

*
* @param patient the patient.
* @param concept the concept.
* @return the list of orders.
* @should return orders with the given concept
* @should return empty list for concept without orders
*/
@Transactional(readOnly = true)
public List<Order> getOrderHistoryByConcept(Patient patient, Concept concept);
}
5 changes: 5 additions & 0 deletions api/src/main/java/org/openmrs/api/db/OrderDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ public <Ord extends Order> List<Ord> getOrders(Class<Ord> orderClassType, List<P
* Delete Obs that references an order
*/
public void deleteObsThatReference(Order order);

/**
* @see org.openmrs.api.OrderService#getOrderByOrderNumber(java.lang.String)
*/
public Order getOrderByOrderNumber(String orderNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public <Ord extends Order> Ord getOrder(Integer orderId, Class<Ord> orderClassTy
*/
@SuppressWarnings("unchecked")
public <Ord extends Order> List<Ord> getOrders(Class<Ord> orderClassType, List<Patient> patients,
List<Concept> concepts, List<User> orderers, List<Encounter> encounters) {
List<Concept> concepts, List<User> orderers, List<Encounter> encounters) {

Criteria crit = sessionFactory.getCurrentSession().createCriteria(orderClassType);

Expand Down Expand Up @@ -134,4 +134,13 @@ public void deleteObsThatReference(Order order) {
.executeUpdate();
}
}

/**
* @see org.openmrs.api.db.OrderDAO#getOrderByOrderNumber(java.lang.String)
*/
public Order getOrderByOrderNumber(String orderNumber) {
Criteria searchCriteria = sessionFactory.getCurrentSession().createCriteria(Order.class, "order");
searchCriteria.add(Restrictions.eq("order.orderNumber", orderNumber));
return (Order) searchCriteria.uniqueResult();
}
}
25 changes: 25 additions & 0 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,29 @@ public synchronized String getNewOrderNumber() {

return orderNumber;
}

/**
* @see org.openmrs.api.OrderService#getOrderByOrderNumber(java.lang.String)
*/
@Override
public Order getOrderByOrderNumber(String orderNumber) {
return dao.getOrderByOrderNumber(orderNumber);
}

/**
* @see org.openmrs.api.OrderService#getOrderHistoryByConcept(org.openmrs.Patient, org.openmrs.Concept)
*/
@Override
public List<Order> getOrderHistoryByConcept(Patient patient, Concept concept) {
if (patient == null)
throw new IllegalArgumentException("patient is required");

This comment has been minimized.

Copy link
@wluyima

wluyima Dec 20, 2013

Member

Do the same for concept, i.e. reject null

List<Concept> concepts = new Vector<Concept>();
concepts.add(concept);

List<Patient> patients = new Vector<Patient>();
patients.add(patient);

return getOrders(Order.class, patients, concepts, null, null);

This comment has been minimized.

Copy link
@wluyima

wluyima Dec 20, 2013

Member

This needs to sort the orders by dateCreated, you might need to add Sort columns argument to OrderDAO.getOrders(....), we can chose not to expose this argument on OrderService.getOrders(....)

}
}
56 changes: 56 additions & 0 deletions api/src/test/java/org/openmrs/api/OrderServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

import org.junit.Assert;
import org.junit.Test;
import org.openmrs.Concept;
import org.openmrs.Obs;
import org.openmrs.Order;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.test.BaseContextSensitiveTest;
import org.openmrs.test.Verifies;
Expand Down Expand Up @@ -149,4 +151,58 @@ public void run() {
//since we used a set we should have the size as N indicating that there were no duplicates
Assert.assertEquals(N, uniqueOrderNumbers.size());
}

/**
* @see {@link OrderService#getOrderByOrderNumber(String)}
*/
@Test
@Verifies(value = "should find object given valid order number", method = "getOrderByOrderNumber(String)")
public void getOrderByOrderNumber_shouldFindObjectGivenValidOrderNumber() throws Exception {
Order order = Context.getOrderService().getOrderByOrderNumber("1");
Assert.assertNotNull(order);
Assert.assertEquals(1, (int) order.getOrderId());
}

/**
* @see {@link OrderService#getOrderByOrderNumber(String)}
*/
@Test
@Verifies(value = "should return null if no object found with given order number", method = "getOrderByOrderNumber(String)")
public void getOrderByOrderNumber_shouldReturnNullIfNoObjectFoundWithGivenOrderNumber() throws Exception {
Assert.assertNull(Context.getOrderService().getOrderByOrderNumber("some invalid order number"));
}

/**
* @see {@link OrderService#getOrderHistoryByConcept(Patient,Concept)}
*/
@Test
@Verifies(value = "should return orders with the given concept", method = "getOrderHistoryByConcept(Patient,Concept)")
public void getOrderHistoryByConcept_shouldReturnOrdersWithTheGivenConcept() throws Exception {
//We should have two orders with this concept.
Concept concept = Context.getConceptService().getConcept(88);
Patient patient = Context.getPatientService().getPatient(2);
List<Order> orders = Context.getOrderService().getOrderHistoryByConcept(patient, concept);
Assert.assertEquals(2, orders.size());
for (Order order : orders)
Assert.assertTrue(order.getOrderId() == 4 || order.getOrderId() == 5);

This comment has been minimized.

Copy link
@wluyima

wluyima Dec 20, 2013

Member

Actually you need to check that the orders are at expected indices in the list based on dateCreated


//We should have two different orders with this concept
concept = Context.getConceptService().getConcept(792);
orders = Context.getOrderService().getOrderHistoryByConcept(patient, concept);
Assert.assertEquals(2, orders.size());
for (Order order : orders)
Assert.assertTrue(order.getOrderId() == 2 || order.getOrderId() == 3);
}

/**
* @see {@link OrderService#getOrderHistoryByConcept(PatientConcept)}
*/
@Test
@Verifies(value = "should return empty list for concept without orders", method = "getOrderHistoryByConcept(Patient,Concept)")
public void getOrderHistoryByConcept_shouldReturnEmptyListForConceptWithoutOrders() throws Exception {
Concept concept = Context.getConceptService().getConcept(21);
Patient patient = Context.getPatientService().getPatient(2);
List<Order> orders = Context.getOrderService().getOrderHistoryByConcept(patient, concept);
Assert.assertEquals(0, orders.size());
}
}

0 comments on commit 560b09e

Please sign in to comment.