/** * 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)); } }
public List<Label> getByExmaple(Label example) { DetachedCriteria criteria = newCriteria(); if (example.getParentId() != null) criteria.add(Expression.eq("parentId", example.getParentId())); if (example.getName() != null) criteria.add(Expression.ilike("name", example.getName(), MatchMode.START)); criteria.addOrder(Order.asc("name")); return findByCriteria(criteria); }
/** @see org.openmrs.api.db.LocationDAO#getLocations(java.lang.String) */ @SuppressWarnings("unchecked") public List<LocationTag> getLocationTags(String search) { return sessionFactory .getCurrentSession() .createCriteria(LocationTag.class) // 'ilike' case insensitive search .add(Expression.ilike("name", search, MatchMode.START)) .addOrder(Order.asc("name")) .list(); }
/** @see org.openmrs.api.db.LocationDAO#getCountOfLocations(String, Boolean) */ @Override public Long getCountOfLocations(String nameFragment, Boolean includeRetired) { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Location.class); if (!includeRetired) criteria.add(Expression.eq("retired", false)); if (StringUtils.isNotBlank(nameFragment)) criteria.add(Expression.ilike("name", nameFragment, MatchMode.START)); criteria.setProjection(Projections.rowCount()); return (Long) criteria.uniqueResult(); }
@Override public List<Credentials> searchSite(String searchText) { try { session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Credentials.class); Criterion criterion1 = Expression.ilike("siteName", "%" + searchText + "%"); criteria.add(criterion1).add(criterion1); List<Credentials> list = criteria.list(); return list; } finally { if (session != null && session.isOpen()) session.close(); } }
/** @see SerializedObjectDAO#getAllObjectsByName(Class, String) */ @SuppressWarnings("unchecked") public List<SerializedObject> getAllSerializedObjectsByName( Class<?> type, String name, boolean exactMatchOnly) throws DAOException { Criteria c = sessionFactory.getCurrentSession().createCriteria(SerializedObject.class); c.add( Expression.or( Expression.eq("type", type.getName()), Expression.eq("subtype", type.getName()))); if (exactMatchOnly) { c.add(Expression.eq("name", name)); } else { c.add(Expression.ilike("name", name, MatchMode.ANYWHERE)); } return (List<SerializedObject>) c.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)); } }
public List getWeblogEntries( WebsiteData website, UserData user, Date startDate, Date endDate, String catName, String status, String sortby, String locale, int offset, int length) throws RollerException { WeblogCategoryData cat = null; if (StringUtils.isNotEmpty(catName) && website != null) { cat = getWeblogCategoryByPath(website, catName); if (cat == null) catName = null; } if (catName != null && catName.trim().equals("/")) { catName = null; } try { Session session = ((HibernatePersistenceStrategy) this.strategy).getSession(); Criteria criteria = session.createCriteria(WeblogEntryData.class); if (website != null) { criteria.add(Expression.eq("website", website)); } else { criteria.createAlias("website", "w"); criteria.add(Expression.eq("w.enabled", Boolean.TRUE)); } if (user != null) { criteria.add(Expression.eq("creator", user)); } if (startDate != null) { criteria.add(Expression.ge("pubTime", startDate)); } if (endDate != null) { criteria.add(Expression.le("pubTime", endDate)); } if (cat != null && website != null) { criteria.add(Expression.eq("category", cat)); } if (status != null) { criteria.add(Expression.eq("status", status)); } if (locale != null) { criteria.add(Expression.ilike("locale", locale, MatchMode.START)); } if (sortby != null && sortby.equals("updateTime")) { criteria.addOrder(Order.desc("updateTime")); } else { criteria.addOrder(Order.desc("pubTime")); } if (offset != 0) { criteria.setFirstResult(offset); } if (length != -1) { criteria.setMaxResults(length); } return criteria.list(); } catch (HibernateException e) { log.error(e); throw new RollerException(e); } }