/** * Adds an <code>Expression</code> to a <code>Criteria</code>. * * @param criteria * @param parameterName * @param parameterValue * @param comparatorID * @param matchMode */ private void addExpression( org.hibernate.Criteria criteria, String parameterName, Object parameterValue, int comparatorID, org.hibernate.criterion.MatchMode matchMode) { if (parameterValue != null) { switch (comparatorID) { case CriteriaSearchParameter.LIKE_COMPARATOR: { if ((matchMode != null) && (parameterValue instanceof String)) { criteria.add( org.hibernate.criterion.Expression.like( parameterName, (String) parameterValue, matchMode)); } else { criteria.add(org.hibernate.criterion.Expression.like(parameterName, parameterValue)); } break; } case CriteriaSearchParameter.INSENSITIVE_LIKE_COMPARATOR: { if ((matchMode != null) && (parameterValue instanceof String)) { criteria.add( org.hibernate.criterion.Expression.ilike( parameterName, (String) parameterValue, matchMode)); } else { criteria.add(org.hibernate.criterion.Expression.ilike(parameterName, parameterValue)); } break; } case CriteriaSearchParameter.EQUAL_COMPARATOR: { criteria.add(org.hibernate.criterion.Expression.eq(parameterName, parameterValue)); break; } case CriteriaSearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR: { criteria.add(org.hibernate.criterion.Expression.ge(parameterName, parameterValue)); break; } case CriteriaSearchParameter.GREATER_THAN_COMPARATOR: { criteria.add(org.hibernate.criterion.Expression.gt(parameterName, parameterValue)); break; } case CriteriaSearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR: { criteria.add(org.hibernate.criterion.Expression.le(parameterName, parameterValue)); break; } case CriteriaSearchParameter.LESS_THAN_COMPARATOR: { criteria.add(org.hibernate.criterion.Expression.lt(parameterName, parameterValue)); break; } } } else { criteria.add(org.hibernate.criterion.Expression.isNull(parameterName)); } }
/** @see LocationDAO#getRootLocations(boolean) */ @SuppressWarnings("unchecked") @Override public List<Location> getRootLocations(boolean includeRetired) throws DAOException { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Location.class); if (!includeRetired) criteria.add(Expression.eq("retired", false)); criteria.add(Expression.isNull("parentLocation")); criteria.addOrder(Order.asc("name")); return criteria.list(); }
/** @see org.openmrs.notification.AlertService#getAllAlerts(boolean) */ @SuppressWarnings("unchecked") public List<Alert> getAllAlerts(boolean includeExpired) throws DAOException { Criteria crit = sessionFactory.getCurrentSession().createCriteria(Alert.class); // exclude the expired alerts unless requested if (includeExpired == false) crit.add( Expression.or( Expression.isNull("dateToExpire"), Expression.gt("dateToExpire", new Date()))); return crit.list(); }
/** @see org.openmrs.notification.db.AlertDAO#getAlerts(org.openmrs.User, boolean, boolean) */ @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"); if (user != null && user.getUserId() != null) { crit.createCriteria("recipients", "recipient"); crit.add(Expression.eq("recipient.recipient", user)); } else { // getting here means we passed in no user or a blank user. // a null recipient column means get stuff for the anonymous user // crit.add(Expression.isNull("recipient.recipient")); // 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 == false) crit.add( Expression.or( Expression.isNull("dateToExpire"), Expression.gt("dateToExpire", new Date()))); // exclude the read alerts unless requested if (includeRead == false && (user != null && user.getUserId() != null)) { crit.add(Expression.eq("alertRead", false)); crit.add(Expression.eq("recipient.alertRead", false)); } crit.addOrder(Order.desc("dateChanged")); return crit.list(); }
public WeblogCategoryData getRootWeblogCategory(WebsiteData website) throws RollerException { if (website == null) throw new RollerException("website is null"); try { Session session = ((HibernatePersistenceStrategy) this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogCategoryAssoc.class); criteria.createAlias("category", "c"); criteria.add(Expression.eq("c.website", website)); criteria.add(Expression.isNull("ancestorCategory")); criteria.add(Expression.eq("relation", WeblogCategoryAssoc.PARENT)); criteria.setMaxResults(1); List list = criteria.list(); return ((WeblogCategoryAssoc) list.get(0)).getCategory(); } catch (HibernateException e) { throw new RollerException(e); } }
/** * Adds an <code>Expression</code> to a <code>Criteria</code>. The given <code>parameterValues * </code> represents either an array of <code>String</code> or another object. The different * values in the array are added to a disjunction which is connected with logical and to the other * criteria of the search. * * @param criteria * @param parameterName * @param parameterValues * @param searchIfNull * @param comparatorID * @param matchMode */ private void addExpression( org.hibernate.Criteria criteria, String parameterName, Object[] parameterValues, int comparatorID, org.hibernate.criterion.MatchMode matchMode) { if (parameterValues != null) { org.hibernate.criterion.Disjunction disjunction = org.hibernate.criterion.Expression.disjunction(); switch (comparatorID) { case CriteriaSearchParameter.LIKE_COMPARATOR: { if ((matchMode != null) && (parameterValues instanceof String[])) { String[] stringParameterValues = (String[]) parameterValues; for (int index = 0; index < parameterValues.length; index++) { if (stringParameterValues[index] != null) { disjunction.add( org.hibernate.criterion.Expression.like( parameterName, stringParameterValues[index], matchMode)); } else { disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName)); } } } else { for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add( org.hibernate.criterion.Expression.like( parameterName, parameterValues[index])); } else { disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName)); } } } break; } case CriteriaSearchParameter.INSENSITIVE_LIKE_COMPARATOR: { if ((matchMode != null) && (parameterValues instanceof String[])) { String[] stringParameterValues = (String[]) parameterValues; for (int index = 0; index < parameterValues.length; index++) { if (stringParameterValues[index] != null) { disjunction.add( org.hibernate.criterion.Expression.ilike( parameterName, stringParameterValues[index], matchMode)); } else { disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName)); } } } else { for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add( org.hibernate.criterion.Expression.ilike( parameterName, parameterValues[index])); } else { disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName)); } } } break; } case CriteriaSearchParameter.EQUAL_COMPARATOR: { for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add( org.hibernate.criterion.Expression.eq(parameterName, parameterValues[index])); } else { disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName)); } } break; } case CriteriaSearchParameter.GREATER_THAN_OR_EQUAL_COMPARATOR: { for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add( org.hibernate.criterion.Expression.ge(parameterName, parameterValues[index])); } else { disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName)); } } break; } case CriteriaSearchParameter.GREATER_THAN_COMPARATOR: { for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add( org.hibernate.criterion.Expression.gt(parameterName, parameterValues[index])); } else { disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName)); } } break; } case CriteriaSearchParameter.LESS_THAN_OR_EQUAL_COMPARATOR: { for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add( org.hibernate.criterion.Expression.le(parameterName, parameterValues[index])); } else { disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName)); } } break; } case CriteriaSearchParameter.LESS_THAN_COMPARATOR: { for (int index = 0; index < parameterValues.length; index++) { if (parameterValues[index] != null) { disjunction.add( org.hibernate.criterion.Expression.lt(parameterName, parameterValues[index])); } else { disjunction.add(org.hibernate.criterion.Expression.isNull(parameterName)); } } break; } } criteria.add(disjunction); } else { criteria.add(org.hibernate.criterion.Expression.isNull(parameterName)); } }