Skip to content

Commit

Permalink
TRUNK-6202 Replace Hibernate Criteria API with JPA for HibernateOrder…
Browse files Browse the repository at this point in the history
…SetDAO (#4489)
  • Loading branch information
k4pran authored Dec 22, 2023
1 parent ff02174 commit 794dae9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
*/
package org.openmrs.api.db.hibernate;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
Expand Down Expand Up @@ -68,48 +70,55 @@ public OrderSet save(OrderSet orderSet) throws DAOException {
*/
@Override
public List<OrderSet> getOrderSets(boolean includeRetired) throws DAOException {
Criteria crit = sessionFactory.getCurrentSession().createCriteria(OrderSet.class, "orderSet");

Session session = sessionFactory.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<OrderSet> cq = cb.createQuery(OrderSet.class);
Root<OrderSet> root = cq.from(OrderSet.class);

if (!includeRetired) {
crit.add(Restrictions.eq("retired", Boolean.FALSE));
cq.where(cb.isFalse(root.get("retired")));
}
return crit.list();

return session.createQuery(cq).getResultList();
}



/**
* @see org.openmrs.api.db.OrderSetDAO#getOrderSetById(Integer)
*/
@Override
public OrderSet getOrderSetById(Integer orderSetId) throws DAOException {
return (OrderSet) sessionFactory.getCurrentSession().get(OrderSet.class, orderSetId);
return sessionFactory.getCurrentSession().get(OrderSet.class, orderSetId);
}

/**
* @see org.openmrs.api.db.OrderSetDAO#getOrderSetByUniqueUuid(String)
*/
@Override
public OrderSet getOrderSetByUniqueUuid(String orderSetUuid) throws DAOException {
return (OrderSet) sessionFactory.getCurrentSession().createQuery("from OrderSet o where o.uuid = :uuid").setString(
"uuid", orderSetUuid).uniqueResult();
return HibernateUtil.getUniqueEntityByUUID(sessionFactory, OrderSet.class, orderSetUuid);
}


/**
* @see org.openmrs.api.db.OrderSetDAO#getOrderSetMemberByUuid(String)
*/
@Override
public OrderSetMember getOrderSetMemberByUuid(String uuid) throws DAOException {
return (OrderSetMember) sessionFactory.getCurrentSession().createQuery("from OrderSetMember osm where osm.uuid = :uuid").setString(
"uuid", uuid).uniqueResult();
return HibernateUtil.getUniqueEntityByUUID(sessionFactory, OrderSetMember.class, uuid);
}

/**
* @see org.openmrs.api.db.OrderSetDAO#getAllOrderSetAttributeTypes()
*/
@SuppressWarnings("unchecked")
@Override
public List<OrderSetAttributeType> getAllOrderSetAttributeTypes() {
return sessionFactory.getCurrentSession().createCriteria(OrderSetAttributeType.class).list();
Session session = sessionFactory.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<OrderSetAttributeType> cq = cb.createQuery(OrderSetAttributeType.class);
cq.from(OrderSetAttributeType.class);

return session.createQuery(cq).getResultList();

}

/**
Expand All @@ -125,8 +134,7 @@ public OrderSetAttributeType getOrderSetAttributeType(Integer id) {
*/
@Override
public OrderSetAttributeType getOrderSetAttributeTypeByUuid(String uuid) {
return (OrderSetAttributeType) sessionFactory.getCurrentSession().createCriteria(OrderSetAttributeType.class).add(
Restrictions.eq("uuid", uuid)).uniqueResult();
return HibernateUtil.getUniqueEntityByUUID(sessionFactory, OrderSetAttributeType.class, uuid);
}

/**
Expand All @@ -151,17 +159,21 @@ public void deleteOrderSetAttributeType(OrderSetAttributeType orderSetAttributeT
*/
@Override
public OrderSetAttribute getOrderSetAttributeByUuid(String uuid) {
return (OrderSetAttribute) sessionFactory.getCurrentSession().createCriteria(OrderSetAttribute.class).add(
Restrictions.eq("uuid", uuid)).uniqueResult();
return HibernateUtil.getUniqueEntityByUUID(sessionFactory, OrderSetAttribute.class, uuid);
}

/**
* @see org.openmrs.api.db.OrderSetDAO#getOrderSetAttributeTypeByName(java.lang.String)
*/
@Override
public OrderSetAttributeType getOrderSetAttributeTypeByName(String name) {
return (OrderSetAttributeType) sessionFactory.getCurrentSession().createCriteria(OrderSetAttributeType.class).add(
Restrictions.eq("name", name)).uniqueResult();
}
Session session = sessionFactory.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<OrderSetAttributeType> cq = cb.createQuery(OrderSetAttributeType.class);
Root<OrderSetAttributeType> root = cq.from(OrderSetAttributeType.class);

cq.where(cb.equal(root.get("name"), name));

return session.createQuery(cq).uniqueResult();
}
}
28 changes: 27 additions & 1 deletion api/src/test/java/org/openmrs/api/OrderSetServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class OrderSetServiceTest extends BaseContextSensitiveTest {

protected ConceptService conceptService;

protected static final String AUDIT_DATE = "Audit Date";
protected static final String INVALID_AUDIT_DATE = "Non existent name";

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

protected static final String ORDER_SET_ATTRIBUTES = "org/openmrs/api/include/OrderSetServiceTest-attributes.xml";
Expand Down Expand Up @@ -383,7 +386,7 @@ public void getOrderSetAttributeByUuid_shouldReturnNullIfNoOrderSetAttributeHasT
@Test
public void getOrderSetAttributeTypeByUuid_shouldReturnTheOrderSetAttributeTypeWithTheGivenUuid() {
executeDataSet(ORDER_SET_ATTRIBUTES);
assertEquals("Audit Date", Context.getOrderSetService().getOrderSetAttributeTypeByUuid(
assertEquals(AUDIT_DATE, Context.getOrderSetService().getOrderSetAttributeTypeByUuid(
"8516cc50-6f9f-33e0-8414-001e648eb67e").getName());
}

Expand Down Expand Up @@ -475,5 +478,28 @@ public void unretireOrderSetAttributeType_shouldUnretireARetiredOrderSetAttribut
assertNull(orderSetAttributeType.getRetireReason());
}

/**
* @see OrderSetService#getOrderSetAttributeTypeByName(String)
*/
@Test
public void getOrderSetAttributeTypeByName_shouldGetMatchingOrderSetAttributeType() {
executeDataSet(ORDER_SET_ATTRIBUTES);

OrderSetAttributeType attributeType = orderSetService.getOrderSetAttributeTypeByName(AUDIT_DATE);

assertNotNull(attributeType, "The fetched OrderSetAttributeType should not be null");
assertEquals(AUDIT_DATE, attributeType.getName(), "The name of the fetched attribute type should match the requested name");
}

/**
* @see OrderSetService#getOrderSetAttributeTypeByName(String)
*/
@Test
public void getOrderSetAttributeTypeByName_shouldReturnNullForNonExistentName() {
executeDataSet(ORDER_SET_ATTRIBUTES);

OrderSetAttributeType attributeType = orderSetService.getOrderSetAttributeTypeByName(INVALID_AUDIT_DATE);

assertNull(attributeType, "The fetched OrderSetAttributeType should be null");
}
}

0 comments on commit 794dae9

Please sign in to comment.