Skip to content

Commit

Permalink
Trunk-5225:Get list of OrderGroups by patient or encounter is missing…
Browse files Browse the repository at this point in the history
… in openmrs-core (openmrs#3084)
  • Loading branch information
gitcliff authored and dkayiwa committed Dec 22, 2019
1 parent 793ff22 commit 94d6517
Show file tree
Hide file tree
Showing 6 changed files with 131 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 @@ -812,4 +812,26 @@ public Order discontinueOrder(Order orderToDiscontinue, String reasonNonCoded, D
*/
@Authorized({ PrivilegeConstants.EDIT_ORDERS, PrivilegeConstants.ADD_ORDERS })
public OrderGroup saveOrderGroup(OrderGroup orderGroup) throws APIException;

/**
* Fetches all order groups for the specified patient
*
* @param patient the patient to match on
* @return list of matching OrderGroups
* @since 2.4.0
* @throws APIException
*/
@Authorized(PrivilegeConstants.GET_ORDERS)
public List<OrderGroup> getOrderGroupsByPatient(Patient patient) throws APIException;

/**
* Fetches all order groups for the specified encounter
*
* @param encounter the encounter to match on
* @return list of matching OrderGroups
* @since 2.4.0
* @throws APIException
*/
@Authorized(PrivilegeConstants.GET_ORDERS)
public List<OrderGroup> getOrderGroupsByEncounter(Encounter encounter) throws APIException;
}
10 changes: 10 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 @@ -259,4 +259,14 @@ public List<OrderFrequency> getOrderFrequencies(String searchPhrase, Locale loca
* @see org.openmrs.api.OrderService#getOrderGroup(Integer)
*/
public OrderGroup getOrderGroupById(Integer orderGroupId) throws DAOException;

/**
* @see org.openmrs.api.OrderService#getOrderGroupsByPatient(Patient)
*/
public List<OrderGroup> getOrderGroupsByPatient(Patient patient) throws DAOException;

/**
* @see org.openmrs.api.OrderService#getOrderGroupsByEncounter(Encounter)
*/
public List<OrderGroup> getOrderGroupsByEncounter(Encounter encounter) throws DAOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,29 @@ public boolean isOrderTypeInUse(OrderType orderType) {
criteria.add(Restrictions.eq("orderType", orderType));
return !criteria.list().isEmpty();
}
/**
* @see OrderDAO#getOrderGroupsByPatient(Patient)
*/
@Override
public List<OrderGroup> getOrderGroupsByPatient(Patient patient) throws DAOException {
if (patient == null) {
throw new APIException("Patient cannot be null");
}
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(OrderGroup.class);
criteria.add(Restrictions.eq("patient", patient));
return criteria.list();
}

/**
* @see OrderDAO#getOrderGroupsByEncounter(Encounter)
*/
@Override
public List<OrderGroup> getOrderGroupsByEncounter(Encounter encounter) throws DAOException {
if (encounter == null) {
throw new APIException("Encounter cannot be null");
}
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(OrderGroup.class);
criteria.add(Restrictions.eq("encounter", encounter));
return criteria.list();
}
}
9 changes: 9 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 @@ -1050,4 +1050,13 @@ private List<Concept> getSetMembersOfConceptSetFromGP(String globalProperty) {
}
return Collections.emptyList();
}
@Override
public List<OrderGroup> getOrderGroupsByPatient(Patient patient) throws APIException {
return dao.getOrderGroupsByPatient(patient);
}

@Override
public List<OrderGroup> getOrderGroupsByEncounter(Encounter encounter) throws APIException {
return dao.getOrderGroupsByEncounter(encounter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
*/
package org.openmrs.api.db.hibernate;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.openmrs.Encounter;
import org.openmrs.Order;
import org.openmrs.OrderGroup;
import org.openmrs.Patient;
import org.openmrs.api.APIException;
import org.openmrs.api.builder.OrderBuilder;
import org.openmrs.api.context.Context;
import org.openmrs.test.BaseContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -33,9 +39,12 @@ public class HibernateOrderDAOTest extends BaseContextSensitiveTest {

private static final String ORDER_SET = "org/openmrs/api/include/OrderSetServiceTest-general.xml";

private static final String ORDER_GROUP = "org/openmrs/api/include/OrderServiceTest-createOrderGroup.xml";

@Before
public void setUp() {
executeDataSet(ORDER_SET);
executeDataSet(ORDER_GROUP);
}

/**
Expand Down Expand Up @@ -63,6 +72,41 @@ public void saveOrderGroup_shouldSaveOrderGroup() {
for (Order savedOrder : savedOrderGroup.getOrders()) {
assertNull("Order is not saved as a part of Order Group", savedOrder.getOrderId());
}

}
/**
* @see {@link HibernateOrderDAO#getOrderGroupsByEncounter(Encounter)}
* @throws Exception
*/
@Test(expected = APIException.class)
public void getOrderGroupsByEncounter_shouldFailGivenNullEncounter() {
dao.getOrderGroupsByEncounter(null);
}
/**
* @see {@link HibernateOrderDAO#getOrderGroupsByPatient(Patient)}
* @throws Exception
*/
@Test(expected = APIException.class)
public void getOrderGroupsByPatient_shouldFailGivenNullPatient() {
dao.getOrderGroupsByPatient(null);
}
/**
* @see {@link HibernateOrderDAO#getOrderGroupsByEncounter(Encounter)}
* @throws Exception
*/
@Test
public void getOrderGroupsByEncounter_shouldGetOrderGroupsFromAnEncounter() {
Encounter existingEncounter = Context.getEncounterService().getEncounter(4);
List<OrderGroup> ordergroups = Context.getOrderService().getOrderGroupsByEncounter(existingEncounter);
assertEquals(1, ordergroups.size());
}
/**
* @see {@link HibernateOrderDAO#getOrderGroupsByPatient(Patient)}
* @throws Exception
*/
@Test
public void getOrderGroupsByPatient_shouldGetOrderGroupsGivenPatient() {
Patient existingPatient = Context.getPatientService().getPatient(8);
List<OrderGroup> ordergroups = Context.getOrderService().getOrderGroupsByPatient(existingPatient);
assertEquals(1, ordergroups.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license.
Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
graphic logo is a trademark of OpenMRS Inc.
-->

<dataset>
<order_group order_group_id="1" order_set_id="2000" patient_id="7" encounter_id="3" creator="1" date_created="2012-02-19 12:24:10.0" voided="false" uuid="1c96f25c-4949-4f72-9931-d808fbcdb613" />
<order_group order_group_id="2" order_set_id="2000" patient_id="8" encounter_id="4" creator="1" date_created="2012-02-19 12:24:10.0" voided="false" uuid="1c96f25c-4949-4f72-9931-d808fbcdb714" />
<order_group order_group_id="3" order_set_id="2000" patient_id="7" encounter_id="3" creator="1" date_created="2012-02-19 12:24:10.0" voided="false" uuid="1c96f25c-4949-4f72-9931-d808fbcdb425" />
<order_group order_group_id="4" order_set_id="2000" patient_id="2" encounter_id="6" creator="1" date_created="2012-01-01 00:00:00.0" voided="false" uuid="1c96f25c-4949-4f72-9931-d808fbcdb142" />

</dataset>

0 comments on commit 94d6517

Please sign in to comment.