Example usage for org.hibernate.criterion Restrictions sizeNe

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

Introduction

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

Prototype

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

Source Link

Document

Constrain a collection valued property by size

Usage

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

License:Open Source License

/**
 * Creates criterion which checks if "collection" field's size isn't equal to given size.
 * //from  w  ww .ja  va 2  s  . co m
 * @param field
 *            field
 * @param size
 *            size
 * @return criterion
 */
public static SearchCriterion sizeNe(final String field, final int size) {
    return new SearchCriterionImpl(Restrictions.sizeNe(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();
    /*/*  w  w  w.  jav  a 2s.  co  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 not equal to the given size
 *
 * @param propertyName The property name
 * @param size The size to constrain by/*from  ww w  .j a  va2  s.  c o m*/
 *
 * @return A Criterion instance
 */
public org.grails.datastore.mapping.query.api.Criteria sizeNe(String propertyName, int size) {
    if (!validateSimpleExpression()) {
        throwRuntimeException(new IllegalArgumentException("Call to [sizeNe] with propertyName [" + propertyName
                + "] and size [" + size + "] not allowed here."));
    }

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