List of usage examples for org.hibernate.criterion Restrictions ltProperty
public static PropertyExpression ltProperty(String propertyName, String otherPropertyName)
From source file:ar.com.zauber.commons.repository.query.visitor.CriteriaFilterVisitor.java
License:Apache License
/** calculate a criterion */ private Criterion createPropertyCriterion(final BinaryPropertyFilter binaryPropertyFilter, final String otherProperty) { final String fieldName = getFieldName(binaryPropertyFilter.getProperty()); final Criterion ret; if (binaryPropertyFilter instanceof EqualsPropertyFilter) { ret = Restrictions.eqProperty(fieldName, otherProperty); } else if (binaryPropertyFilter instanceof LessThanPropertyFilter) { ret = Restrictions.ltProperty(fieldName, otherProperty); } else if (binaryPropertyFilter instanceof LessThanEqualsPropertyFilter) { ret = Restrictions.leProperty(fieldName, otherProperty); } else if (binaryPropertyFilter instanceof GreaterThanPropertyFilter) { ret = Restrictions.gtProperty(fieldName, otherProperty); } else if (binaryPropertyFilter instanceof GreaterThanEqualsPropertyFilter) { ret = Restrictions.geProperty(fieldName, otherProperty); } else {// www . j ava 2 s . c om throw new IllegalStateException("Unable to process filter" + binaryPropertyFilter); } return ret; }
From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java
License:Apache License
/** * Element Start SAX ContentHandler Method. * @param uri// w ww . j a v a2s. com * @param localName * @param qName * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) * @throws SAXException * TODO: Document supported tags in javadoc. */ @SuppressWarnings({ "unchecked" }) public void startElement(String uri, String localNameX, String qName, Attributes attrs) throws SAXException { if ("Query".equalsIgnoreCase(qName)) { queryName = attrs.getValue("name"); rootElementName = queryName; } else if ("Class".equalsIgnoreCase(qName)) { processCriteria(attrs); } else if ("Union".equalsIgnoreCase(qName)) { inDetached = true; DetachedCriteria dc = DetachedCriteria.forEntityName(className); criteriaStack.push(dc); } else if ("NamedQuery".equalsIgnoreCase(qName)) { isNamedQuery = true; namedQuery = attrs.getValue("name"); rootElementName = namedQuery; try { query = session.getNamedQuery(namedQuery); } catch (HibernateException he) { throw new SAXException("Failed to retrieve named query[" + namedQuery + "]", he); } } else if ("qparam".equalsIgnoreCase(qName)) { processQueryBind(attrs); } else if ("Join".equalsIgnoreCase(qName)) { processCriteria(attrs); } else if ("Projections".equalsIgnoreCase(qName)) { startProjection(attrs); } else if ("Projection".equalsIgnoreCase(qName)) { addProjection(attrs); } else if ("Order".equalsIgnoreCase(qName)) { if (isRowCountOnly() == false) { try { String name = attrs.getValue("name"); String type = attrs.getValue("type"); ((Criteria) criteriaStack.peek()) .addOrder(type.equalsIgnoreCase("asc") ? Order.asc(name) : Order.desc(name)); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThan:" + attrs.getValue("name"), e); } } } else if ("GreaterThan".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.gt(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThan:" + attrs.getValue("name"), e); } } else if ("GreaterThanOrEqual".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.ge(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThanOrEqual:" + attrs.getValue("name"), e); } } else if ("LessThan".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.lt(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse LessThan:" + attrs.getValue("name"), e); } } else if ("LessThanOrEqual".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.le(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse LessThanOrEqual:" + attrs.getValue("name"), e); } } else if ("Equals".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); if ("true".equalsIgnoreCase(attrs.getValue("not"))) { addCriterion(Restrictions.not(Restrictions.eq(operator.getName(), operator.getNativeValue()))); } else { addCriterion(Restrictions.eq(operator.getName(), operator.getNativeValue())); } } catch (Exception e) { throw new SAXException("Unable To Parse Equals:" + attrs.getValue("name"), e); } } else if ("Alias".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); ((Criteria) criteriaStack.peek()).createAlias(operator.getName(), operator.getValue()); } catch (Exception e) { throw new SAXException("Unable To Create Alias:" + attrs.getValue("name"), e); } } else if ("GreaterThanProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.gtProperty(operator.getName(), operator.getName2())); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThanProperty:" + attrs.getValue("name"), e); } } else if ("GreaterThanOrEqualProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.geProperty(operator.getName(), operator.getName2())); } catch (Exception e) { throw new SAXException("Unable To Parse GreaterThanOrEqualProperty:" + attrs.getValue("name"), e); } } else if ("LessThanProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.ltProperty(operator.getName(), operator.getName2())); } catch (Exception e) { throw new SAXException("Unable To Parse LessThanProperty:" + attrs.getValue("name"), e); } } else if ("LessThanOrEqualProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.leProperty(operator.getName(), operator.getName2())); } catch (Exception e) { throw new SAXException("Unable To Parse LessThanOrEqualProperty:" + attrs.getValue("name"), e); } } else if ("EqualsProperty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); if ("true".equalsIgnoreCase(attrs.getValue("not"))) { addCriterion( Restrictions.not(Restrictions.eqProperty(operator.getName(), operator.getName2()))); } else { addCriterion(Restrictions.eqProperty(operator.getName(), operator.getName2())); } } catch (Exception e) { throw new SAXException("Unable To Parse EqualsProperty:" + attrs.getValue("name"), e); } } else if ("Like".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.like(operator.getName(), operator.getNativeValue())); } catch (Exception e) { throw new SAXException("Unable To Parse Like:" + attrs.getValue("name"), e); } } else if ("Between".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.between(operator.getName(), operator.getNativeValue(), operator.getNativeValue2())); } catch (Exception e) { throw new SAXException("Unable To Parse Between:" + attrs.getValue("name"), e); } } else if ("IsEmpty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.isEmpty(operator.getName())); } catch (Exception e) { throw new SAXException("Unable To Parse IsEmpty:" + attrs.getValue("name"), e); } } else if ("IsNotEmpty".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.isNotEmpty(operator.getName())); } catch (Exception e) { throw new SAXException("Unable To Parse IsNotEmpty:" + attrs.getValue("name"), e); } } else if ("IsNull".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.isNull(operator.getName())); } catch (Exception e) { throw new SAXException("Unable To Parse IsNull:" + attrs.getValue("name"), e); } } else if ("IsNotNull".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); addCriterion(Restrictions.isNotNull(operator.getName())); } catch (Exception e) { throw new SAXException("Unable To Parse IsNotNull:" + attrs.getValue("name"), e); } } else if ("In".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); if (operator.isLiteral()) { addCriterion(new LiteralInExpression(operator.getName(), (String) operator.getNativeValue())); } else { addCriterion(Restrictions.in(operator.getName(), (Collection) operator.getNativeValue())); } } catch (Exception e) { throw new SAXException("Unable To Parse In:" + attrs.getValue("name"), e); } } else if ("SizeEquals".equalsIgnoreCase(qName)) { try { Operator operator = new Operator(attrs); int i = ((Integer) operator.getNativeValue()).intValue(); addCriterion(Restrictions.sizeEq(operator.getName(), i)); } catch (Exception e) { throw new SAXException("Unable To Parse SizeEquals:" + attrs.getValue("name"), e); } } else if ("Not".equalsIgnoreCase(qName)) { notStack.push(new Object()); } else if ("Or".equalsIgnoreCase(qName)) { opStack.push(Restrictions.disjunction()); } else if ("And".equalsIgnoreCase(qName)) { opStack.push(Restrictions.conjunction()); } else { throw new SAXException("Element Name[" + qName + "] Not Recognized."); } }
From source file:com.qcadoo.model.api.search.SearchRestrictions.java
License:Open Source License
/** * Creates criterion which checks if field is less than other field. * //from w w w .j av a2 s.c o m * @param field * field * @param otherField * other field * @return criterion */ public static SearchCriterion ltField(final String field, final String otherField) { return new SearchCriterionImpl(Restrictions.ltProperty(field, otherField)); }
From source file:com.square.adherent.noyau.dao.implementations.contrat.ContratDaoImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/* ww w .j a va2s .com*/ public List<AjustementTarif> getListeAjustementsFromContrat(Long uidContrat) { final Criteria crit = createCriteria(AjustementTarif.class); crit.createAlias("contrat", "contrat"); crit.add(Restrictions.eq("contrat.id", uidContrat)); crit.add(Restrictions.eq("contrat.isVisible", true)); crit.add(Restrictions.or(Restrictions.ltProperty("dateDebut", "dateFin"), Restrictions.isNull("dateFin"))); return crit.list(); }
From source file:edu.utah.further.core.data.hibernate.query.CriterionBuilderHibernateImpl.java
License:Apache License
/** * @param <E>/*from w ww .j av a 2 s .c om*/ * @param criterion * @see #visit(edu.utah.further.core.search.PropertyExpression) */ public <E> void visitPropertyExpression() { final Relation relation = (Relation) criterion.getParameter(0); final String propertyName = (String) criterion.getParameter(1); final String otherPropertyName = (String) criterion.getParameter(2); switch (relation) { case EQ: { result = eqProperty(propertyName, otherPropertyName); break; } case NE: { result = neProperty(propertyName, otherPropertyName); break; } case GT: { result = gtProperty(propertyName, otherPropertyName); break; } case GE: { result = geProperty(propertyName, otherPropertyName); break; } case LT: { result = Restrictions.ltProperty(propertyName, otherPropertyName); break; } case LE: { result = Restrictions.leProperty(propertyName, otherPropertyName); break; } default: { throw new BusinessRuleException(unsupportedMessage(relation)); } } }
From source file:fr.gael.dhus.olingo.v1.SQLVisitor.java
License:Open Source License
private Criterion getCriterionComparative(BinaryOperator operator, Object left, Object right) { Criterion criterion = null;// w w w . ja va 2 s.com if (left instanceof Member) { if (right instanceof Member) { // property <operator> property String lvalue = ((Member) left).getName(); String rvalue = ((Member) right).getName(); switch (operator) { case EQ: { criterion = Restrictions.eqProperty(lvalue, rvalue); break; } case NE: { criterion = Restrictions.neProperty(lvalue, rvalue); break; } case GT: { criterion = Restrictions.gtProperty(lvalue, rvalue); break; } case GE: { criterion = Restrictions.geProperty(lvalue, rvalue); break; } case LT: { criterion = Restrictions.ltProperty(lvalue, rvalue); break; } case LE: { criterion = Restrictions.leProperty(lvalue, rvalue); break; } default: throw new UnsupportedOperationException("Unsupported operation: " + operator.toUriLiteral()); } } else { // property <operator> literal String property = ((Member) left).getName(); criterion = internalCriterionComparative(operator, property, right); } } else if (right instanceof Member) { // literal <operator> property String property = ((Member) right).getName(); criterion = internalCriterionComparative(operator, property, left); } else if (left instanceof Comparable) { // literal <operator> literal Comparable comparable = (Comparable) left; boolean bool; int result = comparable.compareTo(right); switch (operator) { case EQ: { bool = result == 0; break; } case NE: { bool = result != 0; break; } case GT: { bool = result > 0; break; } case GE: { bool = result >= 0; break; } case LT: { bool = result < 0; break; } case LE: { bool = result <= 0; break; } default: throw new UnsupportedOperationException("Unsupported operation: " + operator.toUriLiteral()); } if (bool) { criterion = Restrictions.sqlRestriction("0=0"); } else { criterion = Restrictions.sqlRestriction("0<>0"); } } return criterion; }
From source file:grails.orm.HibernateCriteriaBuilder.java
License:Apache License
/** * Creates a Criterion that tests if the first property is less than the second property * @param propertyName The first property name * @param otherPropertyName The second property name * @return A Criterion instance//from w w w. j a v a 2 s . c o m */ public org.grails.datastore.mapping.query.api.Criteria ltProperty(String propertyName, String otherPropertyName) { if (!validateSimpleExpression()) { throwRuntimeException(new IllegalArgumentException("Call to [ltProperty] with propertyName [" + propertyName + "] and other property name [" + otherPropertyName + "] not allowed here.")); } propertyName = calculatePropertyName(propertyName); otherPropertyName = calculatePropertyName(otherPropertyName); addToCriteria(Restrictions.ltProperty(propertyName, otherPropertyName)); return this; }
From source file:net.firejack.platform.core.store.AbstractStore.java
License:Apache License
protected Criterion getRestrictions(SearchQuery query, Class<?> type) { Criterion criterion;/* w ww . j a v a 2 s. c o m*/ Object value = query.getValue(); QueryOperation operation = query.getOperation(); if (value != null && !(QueryOperation.FIELDEQUALS.equals(operation) || QueryOperation.FIELDNOTEQUALS.equals(operation) || QueryOperation.FIELDGREATERTHAN.equals(operation) || QueryOperation.FIELDLESSTHAN.equals(operation))) { if (value instanceof Collection) { Collection values = (Collection) value; if (Integer.class.equals(type)) { List<Integer> list = new ArrayList<Integer>(); for (Object item : values) { list.add(Integer.parseInt(item.toString())); } value = list; } else if (Long.class.equals(type)) { List<Long> list = new ArrayList<Long>(); for (Object item : values) { list.add(Long.parseLong(item.toString())); } value = list; } else if (java.sql.Date.class.equals(type) || Date.class.equals(type)) { List<Date> list = new ArrayList<Date>(); for (Object item : values) { Tuple<Date, QueryOperation> tuple = convertToDate(item, operation); operation = tuple.getValue(); list.add(tuple.getKey()); } value = list; } else if (Enum.class.isAssignableFrom(type)) { List<Enum> enumValues = new ArrayList<Enum>(values.size()); for (Object item : values) { Enum enumItem = prepareEnumFromSearchCriteria((Class<? extends Enum>) type, item); enumValues.add(enumItem); } value = enumValues; } } else { if (Integer.class.equals(type)) { value = Integer.parseInt(value.toString()); } else if (Long.class.equals(type)) { value = Long.parseLong(value.toString()); } else if (Double.class.equals(type)) { value = Double.parseDouble(value.toString()); } else if (java.sql.Date.class.equals(type) || Date.class.equals(type)) { Tuple<Date, QueryOperation> tuple = convertToDate(value, operation); value = tuple.getKey(); operation = tuple.getValue(); } else if (Enum.class.isAssignableFrom(type)) { value = prepareEnumFromSearchCriteria((Class<? extends Enum>) type, value); } } } if (!String.class.equals(type) && (QueryOperation.LIKECS.equals(operation) || QueryOperation.LIKECSFIRST.equals(operation) || QueryOperation.LIKE.equals(operation) || QueryOperation.LIKEFIRST.equals(operation))) { operation = QueryOperation.EQUALS; } switch (operation) { case LIKECS: criterion = Restrictions.like(query.getField(), "%" + value + "%"); break; case LIKECSFIRST: criterion = Restrictions.like(query.getField(), value + "%"); break; case LIKE: criterion = Restrictions.ilike(query.getField(), "%" + value + "%"); break; case LIKEFIRST: criterion = Restrictions.ilike(query.getField(), value + "%"); break; case EQUALS: criterion = Restrictions.eq(query.getField(), value); break; case LESSTHAN: criterion = Restrictions.lt(query.getField(), value); break; case GREATERTHAN: criterion = Restrictions.gt(query.getField(), value); break; case ISNULL: criterion = Restrictions.isNull(query.getField()); break; case ISNOTNULL: criterion = Restrictions.isNotNull(query.getField()); break; case ISEMPTY: criterion = Restrictions.isEmpty(query.getField()); break; case ISNOTEMPTY: criterion = Restrictions.isNotEmpty(query.getField()); break; case NOTEQUALS: criterion = Restrictions.ne(query.getField(), value); break; case IN: criterion = generateInRestriction(query.getField(), (Collection) value); break; case NOTIN: criterion = Restrictions.not(generateInRestriction(query.getField(), (Collection) value)); break; case FIELDEQUALS: criterion = Restrictions.eqProperty(query.getField(), value != null ? value.toString() : ""); break; case FIELDNOTEQUALS: criterion = Restrictions.neProperty(query.getField(), value != null ? value.toString() : ""); break; case FIELDGREATERTHAN: criterion = Restrictions.gtProperty(query.getField(), value != null ? value.toString() : ""); break; case FIELDLESSTHAN: criterion = Restrictions.ltProperty(query.getField(), value != null ? value.toString() : ""); break; default: throw new RuntimeException("Operation " + query.getField() + " is not a valid operation"); } return criterion; }
From source file:org.balisunrise.daa.hibernate.HCriteria.java
License:Open Source License
private org.hibernate.criterion.Criterion makeCriterion(Criterion.Comparator comparator) { String fp = aliasMap.getPropertyName(comparator.getFirstProperty()); String sp = aliasMap.getPropertyName(comparator.getSecondProperty()); switch (comparator.getComparatorType()) { case EQUALS://from w w w .j a v a2 s.c o m return Restrictions.eqProperty(fp, sp); case DIFFERENT: return Restrictions.neProperty(fp, sp); case GREATHER: return Restrictions.gtProperty(fp, sp); case GREATHER_EQUALS: return Restrictions.geProperty(fp, sp); case LESS: return Restrictions.ltProperty(fp, sp); case LESS_EQUALS: return Restrictions.leProperty(fp, sp); } return null; }
From source file:org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateCriterionAdapter.java
License:Apache License
protected void addPropertyComparisonCriterionAdapters() { criterionAdaptors.put(Query.EqualsProperty.class, new CriterionAdaptor<Query.EqualsProperty>() { @Override//from w w w . j a va2 s . c o m public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.EqualsProperty criterion, String alias) { String propertyName = getPropertyName(criterion, alias); return Restrictions.eqProperty(propertyName, criterion.getOtherProperty()); } }); criterionAdaptors.put(Query.GreaterThanProperty.class, new CriterionAdaptor<Query.GreaterThanProperty>() { @Override public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.GreaterThanProperty criterion, String alias) { String propertyName = getPropertyName(criterion, alias); return Restrictions.gtProperty(propertyName, criterion.getOtherProperty()); } }); criterionAdaptors.put(Query.GreaterThanEqualsProperty.class, new CriterionAdaptor<Query.GreaterThanEqualsProperty>() { @Override public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.GreaterThanEqualsProperty criterion, String alias) { String propertyName = getPropertyName(criterion, alias); return Restrictions.geProperty(propertyName, criterion.getOtherProperty()); } }); criterionAdaptors.put(Query.LessThanProperty.class, new CriterionAdaptor<Query.LessThanProperty>() { @Override public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.LessThanProperty criterion, String alias) { String propertyName = getPropertyName(criterion, alias); return Restrictions.ltProperty(propertyName, criterion.getOtherProperty()); } }); criterionAdaptors.put(Query.LessThanEqualsProperty.class, new CriterionAdaptor<Query.LessThanEqualsProperty>() { @Override public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.LessThanEqualsProperty criterion, String alias) { String propertyName = getPropertyName(criterion, alias); return Restrictions.leProperty(propertyName, criterion.getOtherProperty()); } }); criterionAdaptors.put(Query.NotEqualsProperty.class, new CriterionAdaptor<Query.NotEqualsProperty>() { @Override public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.NotEqualsProperty criterion, String alias) { String propertyName = getPropertyName(criterion, alias); return Restrictions.neProperty(propertyName, criterion.getOtherProperty()); } }); }