List of usage examples for org.hibernate.criterion Restrictions gtProperty
public static PropertyExpression gtProperty(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 {/*from w ww . ja v a 2s .co m*/ 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/*from w ww .j av a 2s.co m*/ * @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.ihsolution.hqipo.dao.utils.QueryHelper.java
License:Open Source License
/** * Main conversion method//from w ww . ja v a 2s . co m * @param fieldName * @param fieldVal * @param oper (=, equal, IN ...) * @param j (AND OR) * @return */ public QueryHelper addFieldAndVal(String fieldName, Object fieldVal, String oper, Junction j) { boolean isValString = fieldVal instanceof String; String str = ""; if (oper == null || "".equals(oper)) { oper = "equal"; } if (isValString) str = ((String) fieldVal).trim(); if ("equal".equals(oper)) { if (isValString) { j.add(Restrictions.eq(fieldName, str).ignoreCase()); } else j.add(Restrictions.eq(fieldName, fieldVal)); } else if ("notEqual".equals(oper)) { if (isValString) { j.add(Restrictions.ne(fieldName, str).ignoreCase()); } else j.add(Restrictions.ne(fieldName, fieldVal)); } else if ("null".equals(oper)) { j.add(Restrictions.isNull(fieldName)); } else if ("notNull".equals(oper)) { j.add(Restrictions.isNotNull(fieldName)); } else if ("notExists".equals(oper)) { j.add(Restrictions.sqlRestriction(fieldVal.toString())); } else if ("Exists".equals(oper)) { j.add(Restrictions.sqlRestriction(fieldVal.toString())); } else if (isValString) { MatchMode mm = getMatchMode(oper); if (mm != null) j.add(Restrictions.ilike(fieldName, str, mm)); } else if ("le".equals(oper)) j.add(Restrictions.le(fieldName, fieldVal)); else if ("ge".equals(oper)) j.add(Restrictions.ge(fieldName, fieldVal)); else if ("gtProperty".equals(oper)) { String[] spl = ((String) fieldVal).split(";"); if (spl.length == 2) j.add(Restrictions.gtProperty(spl[0], spl[1])); else j.add(Restrictions.gt(fieldName, fieldVal)); } else if ("in".equals(oper)) { if (fieldVal instanceof Collection) j.add(Restrictions.in(fieldName, (Collection) fieldVal)); else if (fieldVal instanceof Object[]) j.add(Restrictions.in(fieldName, (Object[]) fieldVal)); else throw new IllegalArgumentException( "QueryHelper.IN illegal argument type. Should be Collection or Object[]"); } else if ("notIn".equals(oper)) { if (fieldVal instanceof Collection) j.add(Restrictions.not(Restrictions.in(fieldName, (Collection) fieldVal))); else if (fieldVal instanceof Object[]) j.add(Restrictions.not(Restrictions.in(fieldName, (Object[]) fieldVal))); else throw new IllegalArgumentException( "QueryHelper.NOTIN illegal argument type. Should be Collection or Object[]"); } else if ("between".equals(oper)) { Collection objs = (Collection) fieldVal; Iterator it2 = objs.iterator(); Object obj1 = it2.next(); Object obj2 = it2.next(); j.add(Restrictions.between(fieldName, obj1 instanceof String ? obj1.toString().toLowerCase() : obj1, obj2 instanceof String ? obj2.toString().toLowerCase() : obj2)); } else j.add(Restrictions.eq(fieldName, fieldVal)); return this; }
From source file:com.qcadoo.model.api.search.SearchRestrictions.java
License:Open Source License
/** * Creates criterion which checks if field is greater than other field. * //w ww . j a v a 2 s . co m * @param field * field * @param otherField * other field * @return criterion */ public static SearchCriterion gtField(final String field, final String otherField) { return new SearchCriterionImpl(Restrictions.gtProperty(field, otherField)); }
From source file:ejer3.E4anadirIng.java
/** * Creates new form E4anadirIng//from w w w . ja va 2 s .co m */ private void cargarIngs() { sese.beginTransaction(); Criteria cri = sese.createCriteria(Ingenieros.class) .add(Restrictions.gtProperty("horasdisponible", "horasminimo")); List<Ingenieros> l = cri.list(); List<String> li = new ArrayList<String>(); li.add("Seleccione un Ingeniero:"); for (Object o : l) { Ingenieros i = (Ingenieros) o; li.add(i.getId() + ""); } DefaultComboBoxModel modelo = new DefaultComboBoxModel(li.toArray()); cbIng.setModel(modelo); sese.getTransaction().commit(); }
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;/*from w w w . j a v a 2 s.c om*/ 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 greater than the second property * @param propertyName The first property name * @param otherPropertyName The second property name * @return A Criterion instance/*from ww w.j a v a2s . c o m*/ */ public org.grails.datastore.mapping.query.api.Criteria gtProperty(String propertyName, String otherPropertyName) { if (!validateSimpleExpression()) { throwRuntimeException(new IllegalArgumentException("Call to [gtProperty] with propertyName [" + propertyName + "] and other property name [" + otherPropertyName + "] not allowed here.")); } propertyName = calculatePropertyName(propertyName); otherPropertyName = calculatePropertyName(otherPropertyName); addToCriteria(Restrictions.gtProperty(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;/*from ww w .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 a 2s. c om 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.candlepin.model.PoolCurator.java
License:Open Source License
/** * Attempts to find pools which are over subscribed after the creation or modification * of the given entitlement./*from w w w .j a v a 2s . c om*/ * * To do this we search for only the pools related to the subscription ID which * could have changed, the two cases where this can happen are: * * 1. Bonus pool (not derived from any entitlement) after a bind. (in cases such as * exporting to downstream) * 2. A derived pool whose source entitlement just had it's quantity reduced. * * This has to be done carefully to avoid potential performance problems with * virt_bonus on-site subscriptions where one pool is created per physical * entitlement. * * @param subId Subscription ID of the pool. * @param ent Entitlement just created or modified. * @return Pools with too many entitlements for their new quantity. */ @SuppressWarnings("unchecked") public List<Pool> lookupOversubscribedBySubscriptionId(String subId, Entitlement ent) { return currentSession().createCriteria(Pool.class).createAlias("sourceSubscription", "sourceSub") .add(Restrictions.eq("sourceSub.subscriptionId", subId)).add(Restrictions.ge("quantity", 0L)) .add(Restrictions.gtProperty("consumed", "quantity")) .add(Restrictions.or(Restrictions.isNull("sourceEntitlement"), Restrictions.eqOrIsNull("sourceEntitlement", ent))) .list(); }