Example usage for org.hibernate.criterion Restrictions sizeLe

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

Introduction

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

Prototype

@SuppressWarnings("UnusedDeclaration")
public static Criterion sizeLe(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   w w w  .  j  av  a  2  s . c  o  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 less than or equal to given size.
 * /*from www  .j ava  2 s .c  o m*/
 * @param field
 *            field
 * @param size
 *            size
 * @return criterion
 */
public static SearchCriterion sizeLe(final String field, final int size) {
    return new SearchCriterionImpl(Restrictions.sizeLe(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  va  2 s  .c o m*/
     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 less than or equal to the given size
 *
 * @param propertyName The property name
 * @param size The size to constrain by/*from ww w.  j  a  v  a2  s . c  om*/
 *
 * @return A Criterion instance
 */
public org.grails.datastore.mapping.query.api.Criteria sizeLe(String propertyName, int size) {
    if (!validateSimpleExpression()) {
        throwRuntimeException(new IllegalArgumentException("Call to [sizeLe] with propertyName [" + propertyName
                + "] and size [" + size + "] not allowed here."));
    }

    propertyName = calculatePropertyName(propertyName);
    addToCriteria(Restrictions.sizeLe(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//from ww 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:org.riotfamily.dbmsgsrc.model.MessageBundleEntry.java

License:Apache License

@SuppressWarnings("unchecked")
public static void removeEmptyEntries(String bundle) {
    List<MessageBundleEntry> entries = getSession().createCriteria(MessageBundleEntry.class).setCacheable(true)
            .setCacheRegion("messages").add(Restrictions.sizeLe("messages", 1))
            .add(Restrictions.naturalId().set("bundle", bundle)).list();

    for (MessageBundleEntry entry : entries) {
        entry.delete();/*from  w  w  w .  j a  v a  2 s  . c  o  m*/
    }

}