Skip to content

Commit

Permalink
TRUNK-6202 Replace Hibernate Criteria API with JPA for HibernateAlert…
Browse files Browse the repository at this point in the history
…DAO (openmrs#4520)
  • Loading branch information
k4pran authored Jan 11, 2024
1 parent df241b5 commit 5226139
Showing 1 changed file with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -77,54 +80,65 @@ public void deleteAlert(Alert alert) throws DAOException {
* @see org.openmrs.notification.AlertService#getAllAlerts(boolean)
*/
@Override
@SuppressWarnings("unchecked")
public List<Alert> getAllAlerts(boolean includeExpired) throws DAOException {
Criteria crit = sessionFactory.getCurrentSession().createCriteria(Alert.class);

Session session = sessionFactory.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Alert> cq = cb.createQuery(Alert.class);
Root<Alert> 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<Alert> 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<Alert> cq = cb.createQuery(Alert.class);
Root<Alert> root = cq.from(Alert.class);

List<Predicate> 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();
}

}

0 comments on commit 5226139

Please sign in to comment.