From 5226139e1ca1b9ee56e6d52c9faac8d3bc822571 Mon Sep 17 00:00:00 2001 From: Ryan McCauley <32387857+k4pran@users.noreply.github.com> Date: Thu, 11 Jan 2024 22:16:29 +0000 Subject: [PATCH] TRUNK-6202 Replace Hibernate Criteria API with JPA for HibernateAlertDAO (#4520) --- .../db/hibernate/HibernateAlertDAO.java | 70 +++++++++++-------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/api/src/main/java/org/openmrs/notification/db/hibernate/HibernateAlertDAO.java b/api/src/main/java/org/openmrs/notification/db/hibernate/HibernateAlertDAO.java index 4853d69d42d6..7a4cb4edc426 100644 --- a/api/src/main/java/org/openmrs/notification/db/hibernate/HibernateAlertDAO.java +++ b/api/src/main/java/org/openmrs/notification/db/hibernate/HibernateAlertDAO.java @@ -9,14 +9,17 @@ */ package org.openmrs.notification.db.hibernate; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; -import org.hibernate.Criteria; +import org.hibernate.Session; import org.hibernate.SessionFactory; -import org.hibernate.criterion.Order; -import org.hibernate.criterion.Restrictions; import org.openmrs.User; import org.openmrs.api.db.DAOException; import org.openmrs.notification.Alert; @@ -62,7 +65,7 @@ public Alert saveAlert(Alert alert) throws DAOException { */ @Override public Alert getAlert(Integer alertId) throws DAOException { - return (Alert) sessionFactory.getCurrentSession().get(Alert.class, alertId); + return sessionFactory.getCurrentSession().get(Alert.class, alertId); } /** @@ -77,54 +80,65 @@ public void deleteAlert(Alert alert) throws DAOException { * @see org.openmrs.notification.AlertService#getAllAlerts(boolean) */ @Override - @SuppressWarnings("unchecked") public List getAllAlerts(boolean includeExpired) throws DAOException { - Criteria crit = sessionFactory.getCurrentSession().createCriteria(Alert.class); - + Session session = sessionFactory.getCurrentSession(); + CriteriaBuilder cb = session.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(Alert.class); + Root root = cq.from(Alert.class); + // exclude the expired alerts unless requested if (!includeExpired) { - crit.add(Restrictions.or(Restrictions.isNull("dateToExpire"), Restrictions.gt("dateToExpire", new Date()))); + cq.where(cb.or( + cb.isNull(root.get("dateToExpire")), + cb.greaterThan(root.get("dateToExpire"), new Date())) + ); } - - return crit.list(); + + return session.createQuery(cq).getResultList(); } - + /** * @see org.openmrs.notification.db.AlertDAO#getAlerts(org.openmrs.User, boolean, boolean) */ @Override - @SuppressWarnings("unchecked") public List getAlerts(User user, boolean includeRead, boolean includeExpired) throws DAOException { log.debug("Getting alerts for user " + user + " read? " + includeRead + " expired? " + includeExpired); - - Criteria crit = sessionFactory.getCurrentSession().createCriteria(Alert.class, "alert"); - + + Session session = sessionFactory.getCurrentSession(); + CriteriaBuilder cb = session.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(Alert.class); + Root root = cq.from(Alert.class); + + List predicates = new ArrayList<>(); + if (user != null && user.getUserId() != null) { - crit.createCriteria("recipients", "recipient"); - crit.add(Restrictions.eq("recipient.recipient", user)); + predicates.add(cb.equal(root.join("recipients").get("recipientId"), user.getUserId())); } else { // getting here means we passed in no user or a blank user. // a null recipient column means get stuff for the anonymous user - + // returning an empty list for now because the above throws an error. // we may need to remodel how recipients are handled to get anonymous users alerts return Collections.emptyList(); } - + // exclude the expired alerts unless requested if (!includeExpired) { - crit.add(Restrictions.or(Restrictions.isNull("dateToExpire"), Restrictions.gt("dateToExpire", new Date()))); + Predicate dateToExpireIsNull = cb.isNull(root.get("dateToExpire")); + Predicate dateToExpireIsGreater = cb.greaterThan(root.get("dateToExpire"), new Date()); + predicates.add(cb.or(dateToExpireIsNull, dateToExpireIsGreater)); } - + // exclude the read alerts unless requested if (!includeRead && user.getUserId() != null) { - crit.add(Restrictions.eq("alertRead", false)); - crit.add(Restrictions.eq("recipient.alertRead", false)); + predicates.add(cb.isFalse(root.get("alertRead"))); + predicates.add(cb.isFalse(root.join("recipients").get("alertRead"))); } - - crit.addOrder(Order.desc("dateChanged")); - - return crit.list(); + + cq.where(predicates.toArray(new Predicate[]{})) + .orderBy(cb.desc(root.get("dateChanged"))); + + return session.createQuery(cq).getResultList(); } - + }