/** * 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 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(); }
@SuppressWarnings("unchecked") public List<GraffitiDTO> getListByID(int lastID) throws DataAccessException { Criteria c = getCurrentSession() .createCriteria(Graffiti.class) .createAlias("member", "member") .add(Expression.gt("this.id", lastID)) .addOrder(Order.asc("writeDate")) .setProjection( Projections.projectionList() .add(Property.forName("id").as("id")) .add(Property.forName("member.name").as("writerName")) .add(Property.forName("member.avatar").as("writerAvatar")) .add(Property.forName("contents").as("contents")) .add(Projections.property("writeDate").as("writtenDate"))) .setResultTransformer(new AliasToBeanResultTransformer(GraffitiDTO.class)); return c.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 List getNextPrevEntries( WeblogEntryData current, String catName, String locale, int maxEntries, boolean next) throws RollerException { Junction conjunction = Expression.conjunction(); conjunction.add(Expression.eq("website", current.getWebsite())); conjunction.add(Expression.eq("status", WeblogEntryData.PUBLISHED)); if (next) { conjunction.add(Expression.gt("pubTime", current.getPubTime())); } else { conjunction.add(Expression.lt("pubTime", current.getPubTime())); } if (catName != null && !catName.trim().equals("/")) { WeblogCategoryData category = getWeblogCategoryByPath(current.getWebsite(), null, catName); if (category != null) { conjunction.add(Expression.eq("category", category)); } else { throw new RollerException("Cannot find category: " + catName); } } if (locale != null) { conjunction.add(Expression.ilike("locale", locale, MatchMode.START)); } try { Session session = ((HibernatePersistenceStrategy) this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogEntryData.class); criteria.addOrder(next ? Order.asc("pubTime") : Order.desc("pubTime")); criteria.add(conjunction); criteria.setMaxResults(maxEntries); List results = criteria.list(); return results; } 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)); } }