Example usage for org.hibernate.criterion Restrictions sizeGt

List of usage examples for org.hibernate.criterion Restrictions sizeGt

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions sizeGt.

Prototype

@SuppressWarnings("UnusedDeclaration")
public static Criterion sizeGt(String propertyName, int size) 

Source Link

Document

Constrain a collection valued property by size

Usage

From source file:com.bloatit.data.queries.DaoAbstractQuery.java

License:Open Source License

/**
 * Creates a criterion on the size of a collection.
 * <p>/*from ww  w.j a v  a2s. co m*/
 * For example: <code>
 * createSizeCriterion(GREATER, &quot;offers&quot;, 12)
 * </code> Will select elements which has more than 12 offers associated
 * with.
 * </p>
 * 
 * @param cmp the comparator
 * @param element the element name. It must be a collection (a mapped
 *            association).
 * @param nb the number on which we compare the size of the collection
 * @return the criterion
 */
protected final Criterion createSizeCriterion(final Comparator cmp, final String element, final int nb) {
    switch (cmp) {
    case EQUAL:
        return Restrictions.sizeEq(element, nb);
    case LESS:
        return Restrictions.sizeLt(element, nb);
    case LESS_EQUAL:
        return Restrictions.sizeLe(element, nb);
    case GREATER:
        return Restrictions.sizeGt(element, nb);
    case GREATER_EQUAL:
        return Restrictions.sizeGe(element, nb);
    default:
        return Restrictions.sizeEq(element, nb);
    }
}

From source file:com.qcadoo.model.api.search.SearchRestrictions.java

License:Open Source License

/**
 * Creates criterion which checks if "collection" field's size is greater than given size.
 * /*www.j  ava  2s . c om*/
 * @param field
 *            field
 * @param size
 *            size
 * @return criterion
 */
public static SearchCriterion sizeGt(final String field, final int size) {
    return new SearchCriterionImpl(Restrictions.sizeGt(field, size));
}

From source file:cz.jirutka.rsql.visitor.hibernate.HibernateCriterionVisitor.java

License:Apache License

@Override
public Criterion visit(ComparisonNode node) {
    String name = node.getSelector();
    /*//from  w  w w .j  a v  a 2s.  c om
     Get the path and property names
     */
    Tuple<Type, String>[] path = getPath(root, name);
    Type type = getType(path);
    boolean isPrimitive = type instanceof StringRepresentableType;
    boolean isCustom = type instanceof CustomType;
    boolean isCollection = type instanceof CollectionType;

    String exp = getExpression(path, isCollection);
    List<Object> arguments = new ArrayList<Object>(node.getArguments().size());
    for (String argument : node.getArguments()) {
        Object value = argument;
        if (isPrimitive) {
            value = ((StringRepresentableType) type).fromStringValue((String) value);
        } else if (isCustom) {
            value = ((CustomType) type).stringToObject((String) value);
        } else if (isCollection) {
            value = IntegerType.INSTANCE.fromString((String) value);
        }
        if (value instanceof String) {
            value = toSqlWildcardString((String) value);
        }
        arguments.add(value);
    }
    ComparisonOperator operator = node.getOperator();

    assert arguments.size() >= 1;

    Object firstArgument = arguments.get(0);
    if (operator.equals(RSQLOperators.EQUAL)) {
        if (!isCollection) {
            if (String.class.isInstance(firstArgument) && ((String) firstArgument).contains("%")) {
                return Restrictions.ilike(exp, firstArgument);
            } else {
                return Restrictions.eq(exp, firstArgument);
            }
        } else {
            return Restrictions.sizeEq(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.NOT_EQUAL)) {
        if (!isCollection) {
            if (String.class.isInstance(firstArgument) && ((String) firstArgument).contains("%")) {
                return Restrictions.not(Restrictions.ilike(exp, firstArgument));
            } else {
                return Restrictions.ne(exp, firstArgument);
            }
        } else {
            return Restrictions.sizeNe(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.GREATER_THAN)) {
        if (!isCollection) {
            return Restrictions.gt(exp, firstArgument);
        } else {
            return Restrictions.sizeGt(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.GREATER_THAN_OR_EQUAL)) {
        if (!isCollection) {
            return Restrictions.ge(exp, firstArgument);
        } else {
            return Restrictions.sizeGe(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.LESS_THAN)) {
        if (!isCollection) {
            return Restrictions.lt(exp, firstArgument);
        } else {
            return Restrictions.sizeLt(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.LESS_THAN_OR_EQUAL)) {
        if (!isCollection) {
            return Restrictions.le(exp, firstArgument);
        } else {
            return Restrictions.sizeLe(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.IN)) {
        if (!isCollection) {
            return Restrictions.in(exp, arguments);
        }
    } else if (operator.equals(RSQLOperators.NOT_IN)) {
        if (!isCollection) {
            return Restrictions.not(Restrictions.in(exp, arguments));
        }
    }
    throw new IllegalArgumentException("Unknown operation " + operator.toString() + " for property" + name);
}

From source file:grails.orm.HibernateCriteriaBuilder.java

License:Apache License

/**
 * Creates a Criterion that contrains a collection property to be greater than the given size
 *
 * @param propertyName The property name
 * @param size The size to constrain by/* ww w . j a va 2 s .  c o  m*/
 *
 * @return A Criterion instance
 */
public org.grails.datastore.mapping.query.api.Criteria sizeGt(String propertyName, int size) {
    if (!validateSimpleExpression()) {
        throwRuntimeException(new IllegalArgumentException("Call to [sizeGt] with propertyName [" + propertyName
                + "] and size [" + size + "] not allowed here."));
    }

    propertyName = calculatePropertyName(propertyName);
    addToCriteria(Restrictions.sizeGt(propertyName, size));
    return this;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateCriterionAdapter.java

License:Apache License

protected void addSizeComparisonCriterionAdapters() {
    criterionAdaptors.put(Query.SizeEquals.class, new CriterionAdaptor<Query.SizeEquals>() {
        @Override// w  w  w  .  j a  v a2  s. co  m
        public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.SizeEquals criterion,
                String alias) {
            String propertyName = getPropertyName(criterion, alias);
            Object value = criterion.getValue();
            int size = value instanceof Number ? ((Number) value).intValue()
                    : Integer.parseInt(value.toString());
            return Restrictions.sizeEq(propertyName, size);
        }
    });

    criterionAdaptors.put(Query.SizeGreaterThan.class, new CriterionAdaptor<Query.SizeGreaterThan>() {
        @Override
        public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery,
                Query.SizeGreaterThan criterion, String alias) {
            String propertyName = getPropertyName(criterion, alias);
            Object value = criterion.getValue();
            int size = value instanceof Number ? ((Number) value).intValue()
                    : Integer.parseInt(value.toString());
            return Restrictions.sizeGt(propertyName, size);
        }
    });

    criterionAdaptors.put(Query.SizeGreaterThanEquals.class,
            new CriterionAdaptor<Query.SizeGreaterThanEquals>() {
                @Override
                public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery,
                        Query.SizeGreaterThanEquals criterion, String alias) {
                    String propertyName = getPropertyName(criterion, alias);
                    Object value = criterion.getValue();
                    int size = value instanceof Number ? ((Number) value).intValue()
                            : Integer.parseInt(value.toString());
                    return Restrictions.sizeGe(propertyName, size);
                }
            });

    criterionAdaptors.put(Query.SizeLessThan.class, new CriterionAdaptor<Query.SizeLessThan>() {
        @Override
        public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery,
                Query.SizeLessThan criterion, String alias) {
            String propertyName = getPropertyName(criterion, alias);
            Object value = criterion.getValue();
            int size = value instanceof Number ? ((Number) value).intValue()
                    : Integer.parseInt(value.toString());
            return Restrictions.sizeLt(propertyName, size);
        }
    });

    criterionAdaptors.put(Query.SizeLessThanEquals.class, new CriterionAdaptor<Query.SizeLessThanEquals>() {
        @Override
        public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery,
                Query.SizeLessThanEquals criterion, String alias) {
            String propertyName = getPropertyName(criterion, alias);
            Object value = criterion.getValue();
            int size = value instanceof Number ? ((Number) value).intValue()
                    : Integer.parseInt(value.toString());
            return Restrictions.sizeLe(propertyName, size);
        }
    });
}

From source file:test.eurocarbdb.dataaccess.core.ReferenceTest.java

License:Open Source License

@Test(dependsOnMethods = { "coreReferenceDataExists" })
public void coreReferenceSequenceAssociations() throws Exception {
    super.setup();

    Criteria q = getEntityManager().createQuery(Reference.class)
            .add(Restrictions.sizeGt("glycanSequenceReferences", 0)).setMaxResults(20);

    List<Reference> refs = (List<Reference>) q.list();

    // if ( refs == null || refs.size() == 0 ) 
    //     throw new SkipException();

    for (Reference r : refs) {
        System.out.println("reference: " + r);

        assert r.getGlycanSequenceReferences() != null;
        assert r.getGlycanSequenceReferences().size() > 0;

        System.out.println("glycanSequenceReferences:");
        for (GlycanSequenceReference gsr : r.getGlycanSequenceReferences()) {
            System.out.println(" -> " + gsr);
        }//from  w  ww  .  ja va 2s .com

        System.out.println();
    }

    super.teardown();
}

From source file:test.eurocarbdb.dataaccess.core.ReferenceTest.java

License:Open Source License

@Test(dependsOnMethods = { "coreReferenceDataExists" })
public void coreReferenceEvidenceAssociations() throws Exception {
    super.setup();

    Criteria q = getEntityManager().createQuery(Reference.class)
            .add(Restrictions.sizeGt("referencedEvidence", 0)).setMaxResults(20);

    List<Reference> refs = (List<Reference>) q.list();

    // if ( refs == null || refs.size() == 0 ) 
    //     throw new SkipException();

    for (Reference r : refs) {
        System.out.println("reference: " + r);

        assert r.getReferencedEvidence() != null;
        assert r.getReferencedEvidence().size() > 0;

        System.out.println("referencedEvidence:");
        for (ReferencedEvidence re : r.getReferencedEvidence()) {
            System.out.println(" -> " + re);
        }/*from w w  w  .  ja  va2 s  .  c  om*/

        System.out.println();
    }

    super.teardown();
}