Example usage for org.hibernate.criterion Restrictions conjunction

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

Introduction

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

Prototype

public static Conjunction conjunction() 

Source Link

Document

Group expressions together in a single conjunction (A and B and C...).

Usage

From source file:org.apache.ode.daohib.bpel.ql.HibernateInstancesQueryCompiler.java

License:Apache License

protected EqualityEvaluator<String, Criterion, Object> compileEqual(final Equality eq) {
    if (eq.getIdentifier() instanceof Property) {
        propertyInQuery = true;//from  w  ww  . j a v a  2 s. co  m
        final Property property = (Property) eq.getIdentifier();
        return new EqualityEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                Conjunction conj = Restrictions.conjunction();
                if (!StringUtils.isEmpty(property.getNamespace())) {
                    conj.add(Restrictions.eq(PROPERTY_NS_DB_FIELD, property.getNamespace()));
                }
                conj.add(Restrictions.eq(PROPERTY_NAME_DB_FIELD, property.getName()));
                conj.add(Restrictions.eq(PROPERTY_VALUE_DB_FIELD, eq.getValue().getValue()));

                return conj;
            };

            public String getIdentifier() {
                return property.toString();
            };
        };
    } else {
        final String fieldName = eq.getIdentifier().getName();
        final Object value = eq.getValue().getValue();

        final String dbField = getDBField(fieldName);

        if (INSTANCE_STATUS_FIELD.equals(fieldName)) {
            return new FieldValueEquality(INSTANCE_STATUS_FIELD) {
                /**
                 * @see org.apache.ode.ql.eval.skel.CommandEvaluator#evaluate(java.lang.Object)
                 */
                public Criterion evaluate(Object paramValue) {
                    short noState = 200; // TODO move to constants
                    Disjunction disj = Restrictions.disjunction();

                    if (STATUS_ACTIVE.equals(paramValue)) {
                        disj.add(Restrictions.eq(dbField, ProcessState.STATE_NEW));
                        disj.add(Restrictions.eq(dbField, ProcessState.STATE_ACTIVE));
                        disj.add(Restrictions.eq(dbField, ProcessState.STATE_READY));
                    } else if (STATUS_SUSPENDED.equals(paramValue)) {
                        disj.add(Restrictions.eq(dbField, ProcessState.STATE_SUSPENDED));
                    } else if (STATUS_ERROR.equals(value)) {
                        disj.add(Restrictions.eq(dbField, noState)); // Error instance state doesn't exist yet
                    } else if (STATUS_COMPLETED.equals(paramValue)) {
                        disj.add(Restrictions.eq(dbField, ProcessState.STATE_COMPLETED_OK));
                    } else if (STATUS_TERMINATED.equals(paramValue)) {
                        disj.add(Restrictions.eq(dbField, ProcessState.STATE_TERMINATED));
                    } else if (STATUS_FAULTED.equals(paramValue)) {
                        disj.add(Restrictions.eq(dbField, ProcessState.STATE_COMPLETED_WITH_FAULT));
                    } else {
                        disj.add(Restrictions.eq(dbField, noState)); // Non existent state
                    }
                    return disj;
                }
            };
        }

        return new DBFieldValueEq(dbField, value);
    }
}

From source file:org.apache.ode.daohib.bpel.ql.HibernateInstancesQueryCompiler.java

License:Apache License

protected GEEvaluator<String, Criterion, Object> compileGE(final GE ge) {
    if (ge.getIdentifier() instanceof Property) {
        propertyInQuery = true;//from  ww w.  j  ava2s.  c o  m
        final Property property = (Property) ge.getIdentifier();
        return new GEEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                Conjunction conj = Restrictions.conjunction();
                if (!StringUtils.isEmpty(property.getNamespace())) {
                    conj.add(Restrictions.ge(PROPERTY_NS_DB_FIELD, property.getNamespace()));
                }
                conj.add(Restrictions.ge(PROPERTY_NAME_DB_FIELD, property.getName()));
                conj.add(Restrictions.ge(PROPERTY_VALUE_DB_FIELD, ge.getValue().getValue()));

                return conj;
            };

            public String getIdentifier() {
                return property.toString();
            };
        };
    } else {
        final String fieldName = ge.getIdentifier().getName();
        final Object objValue = ge.getValue().getValue();

        if (INSTANCE_STATUS_FIELD.equals(fieldName)) {
            throw new IllegalArgumentException("Field " + INSTANCE_STATUS_FIELD + " is not supported.");
        }

        final String dbField = getDBField(fieldName);

        return new GEEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                return Restrictions.ge(dbField, objValue);
            }

            public String getIdentifier() {
                return fieldName;
            }
        };
    }
}

From source file:org.apache.ode.daohib.bpel.ql.HibernateInstancesQueryCompiler.java

License:Apache License

protected GreaterEvaluator<String, Criterion, Object> compileGreater(final Greater gt) {
    if (gt.getIdentifier() instanceof Property) {
        propertyInQuery = true;//from   www. ja  v  a 2  s.co  m
        final Property property = (Property) gt.getIdentifier();
        return new GreaterEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                Conjunction conj = Restrictions.conjunction();
                if (!StringUtils.isEmpty(property.getNamespace())) {
                    conj.add(Restrictions.gt(PROPERTY_NS_DB_FIELD, property.getNamespace()));
                }
                conj.add(Restrictions.gt(PROPERTY_NAME_DB_FIELD, property.getName()));
                conj.add(Restrictions.gt(PROPERTY_VALUE_DB_FIELD, gt.getValue().getValue()));

                return conj;
            };

            public String getIdentifier() {
                return property.toString();
            };
        };
    } else {
        final String fieldName = gt.getIdentifier().getName();
        final Object value = gt.getValue().getValue();

        if (INSTANCE_STATUS_FIELD.equals(fieldName)) {
            throw new IllegalArgumentException("Field " + INSTANCE_STATUS_FIELD + " is not supported.");
        }

        final String dbField = getDBField(fieldName);

        return new GreaterEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                return Restrictions.gt(dbField, value);
            }

            public String getIdentifier() {
                return fieldName;
            }
        };
    }
}

From source file:org.apache.ode.daohib.bpel.ql.HibernateInstancesQueryCompiler.java

License:Apache License

protected INEvaluator<String, Criterion, Object> compileIn(final In in) {
    if (in.getIdentifier() instanceof Property) {
        propertyInQuery = true;/*from   ww  w  .j a va2s.  c  o m*/
        final Property property = (Property) in.getIdentifier();
        return new INEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                Disjunction disj = Restrictions.disjunction();

                String propertyNS = property.getNamespace();
                String propertyName = property.getName();

                for (Value value : in.getValues()) {
                    Conjunction conj = Restrictions.conjunction();
                    if (!StringUtils.isEmpty(property.getNamespace())) {
                        conj.add(Restrictions.gt(PROPERTY_NS_DB_FIELD, propertyNS));
                    }
                    conj.add(Restrictions.gt(PROPERTY_NAME_DB_FIELD, propertyName));
                    conj.add(Restrictions.gt(PROPERTY_VALUE_DB_FIELD, value.getValue()));

                    disj.add(conj);
                }
                return disj;
            };

            public String getIdentifier() {
                return property.toString();
            };
        };
    } else {
        final String fieldName = in.getIdentifier().getName();

        if (INSTANCE_STATUS_FIELD.equals(fieldName)) {
            short noState = 200; // TODO move to constants
            final Disjunction disj = Restrictions.disjunction();

            final Collection values = ValuesHelper.extract((Collection<Value>) in.getValues());

            if (values.contains(STATUS_ACTIVE)) {
                disj.add(Restrictions.eq(INSTANCE_STATUS_DB_FIELD, ProcessState.STATE_NEW));
                disj.add(Restrictions.eq(INSTANCE_STATUS_DB_FIELD, ProcessState.STATE_ACTIVE));
                disj.add(Restrictions.eq(INSTANCE_STATUS_DB_FIELD, ProcessState.STATE_READY));
            }
            if (values.contains(STATUS_SUSPENDED)) {
                disj.add(Restrictions.eq(INSTANCE_STATUS_DB_FIELD, ProcessState.STATE_SUSPENDED));
            }
            if (values.contains(STATUS_ERROR)) {
                disj.add(Restrictions.eq(INSTANCE_STATUS_DB_FIELD, noState)); // Error instance state doesn't exist yet
            }
            if (values.contains(STATUS_COMPLETED)) {
                disj.add(Restrictions.eq(INSTANCE_STATUS_DB_FIELD, ProcessState.STATE_COMPLETED_OK));
            }
            if (values.contains(STATUS_TERMINATED)) {
                disj.add(Restrictions.eq(INSTANCE_STATUS_DB_FIELD, ProcessState.STATE_TERMINATED));
            }
            if (values.contains(STATUS_FAULTED)) {
                disj.add(Restrictions.eq(INSTANCE_STATUS_DB_FIELD, ProcessState.STATE_COMPLETED_WITH_FAULT));
            }
            return new INEvaluator<String, Criterion, Object>() {
                public Criterion evaluate(Object paramValue) {
                    return disj;
                };

                public String getIdentifier() {
                    return INSTANCE_STATUS_DB_FIELD;
                };
            };
        } else {
            final Collection objValues;
            final Collection<Value> values = in.getValues();
            if (INSTANCE_ID_FIELD.equals(fieldName)) {
                objValues = new ArrayList<Long>(values.size());
                for (Value value : values) {
                    objValues.add(Long.valueOf((String) value.getValue()));
                }
            } else if (INSTANCE_STARTED_FIELD.equals(fieldName)
                    || INSTANCE_LAST_ACTIVE_FIELD.equals(fieldName)) {
                objValues = new ArrayList<Date>(values.size());
                try {
                    for (Value value : values) {
                        objValues.add(ISO8601DateParser.parse((String) value.getValue()));
                    }
                } catch (ParseException ex) {
                    // TODO
                    throw new RuntimeException(ex);
                }
            } else {
                objValues = ValuesHelper.extract((Collection<Value>) values);
            }
            final String dbField = getDBField(fieldName);
            return new INEvaluator<String, Criterion, Object>() {
                /**
                 * @see org.apache.ode.ql.eval.skel.CommandEvaluator#evaluate(java.lang.Object)
                 */
                public Criterion evaluate(Object paramValue) {
                    return Restrictions.in(dbField, objValues);
                }

                /**
                 * @see org.apache.ode.ql.eval.skel.Identified#getIdentifier()
                 */
                public String getIdentifier() {
                    return dbField;
                }
            };
        }
    }
}

From source file:org.apache.ode.daohib.bpel.ql.HibernateInstancesQueryCompiler.java

License:Apache License

protected LEEvaluator<String, Criterion, Object> compileLE(final LE le) {
    if (le.getIdentifier() instanceof Property) {
        final Property property = (Property) le.getIdentifier();
        return new LEEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                Conjunction conj = Restrictions.conjunction();
                if (!StringUtils.isEmpty(property.getNamespace())) {
                    conj.add(Restrictions.le(PROPERTY_NS_DB_FIELD, property.getNamespace()));
                }//from  w w  w.j  a  v  a 2s.com
                conj.add(Restrictions.le(PROPERTY_NAME_DB_FIELD, property.getName()));
                conj.add(Restrictions.le(PROPERTY_VALUE_DB_FIELD, le.getValue().getValue()));

                return conj;
            };

            public String getIdentifier() {
                return property.toString();
            };
        };
    } else {
        final String fieldName = le.getIdentifier().getName();
        final Object value = le.getValue().getValue();

        if (INSTANCE_STATUS_FIELD.equals(fieldName)) {
            throw new IllegalArgumentException("Field " + INSTANCE_STATUS_FIELD + " is not supported.");
        }

        final String dbField = getDBField(fieldName);

        return new LEEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                return Restrictions.le(dbField, value);
            }

            public String getIdentifier() {
                return fieldName;
            }
        };
    }
}

From source file:org.apache.ode.daohib.bpel.ql.HibernateInstancesQueryCompiler.java

License:Apache License

protected LessEvaluator<String, Criterion, Object> compileLess(final Less less) {
    if (less.getIdentifier() instanceof Property) {
        propertyInQuery = true;/*from  w w w . j a va2  s . co  m*/
        final Property property = (Property) less.getIdentifier();
        return new LessEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                Conjunction conj = Restrictions.conjunction();
                if (!StringUtils.isEmpty(property.getNamespace())) {
                    conj.add(Restrictions.lt(PROPERTY_NS_DB_FIELD, property.getNamespace()));
                }
                conj.add(Restrictions.lt(PROPERTY_NAME_DB_FIELD, property.getName()));
                conj.add(Restrictions.lt(PROPERTY_VALUE_DB_FIELD, less.getValue().getValue()));

                return conj;
            };

            public String getIdentifier() {
                return property.toString();
            };
        };
    } else {
        final String fieldName = less.getIdentifier().getName();
        final Object value = less.getValue().getValue();

        if (INSTANCE_STATUS_FIELD.equals(fieldName)) {
            throw new IllegalArgumentException("Field " + INSTANCE_STATUS_FIELD + " is not supported.");
        }

        final String dbField = getDBField(fieldName);

        return new LessEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                return Restrictions.lt(dbField, value);
            }

            public String getIdentifier() {
                return fieldName;
            }
        };
    }
}

From source file:org.apache.ode.daohib.bpel.ql.HibernateInstancesQueryCompiler.java

License:Apache License

protected LikeEvaluator<String, Criterion, Object> compileLike(final Like like) {
    if (like.getIdentifier() instanceof Property) {
        propertyInQuery = true;//from   ww w  .  j  a v  a2 s  . c  om
        final Property property = (Property) like.getIdentifier();
        return new LikeEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                Conjunction conj = Restrictions.conjunction();
                if (!StringUtils.isEmpty(property.getNamespace())) {
                    conj.add(Restrictions.like(PROPERTY_NS_DB_FIELD, property.getNamespace()));
                }
                conj.add(Restrictions.like(PROPERTY_NAME_DB_FIELD, property.getName()));
                conj.add(Restrictions.like(PROPERTY_VALUE_DB_FIELD, like.getValue().getValue()));

                return conj;
            };

            public String getIdentifier() {
                return property.toString();
            };
        };
    } else {
        final String fieldName = like.getIdentifier().getName();
        if (INSTANCE_STATUS_FIELD.equals(fieldName)) {
            throw new IllegalArgumentException(
                    "Field " + INSTANCE_STATUS_FIELD + " is not supported by like operation.");
        }
        if (INSTANCE_ID_FIELD.equals(fieldName)) {
            throw new IllegalArgumentException(
                    "Field " + INSTANCE_ID_FIELD + " is not supported by like operation.");
        }

        final Object value = like.getValue().getValue();
        final String dbField = getDBField(fieldName);

        return new LikeEvaluator<String, Criterion, Object>() {
            public Criterion evaluate(Object paramValue) {
                return Restrictions.like(dbField, value);
            };

            public String getIdentifier() {
                return dbField;
            }
        };
    }
}

From source file:org.archiviststoolkit.mydomain.DomainAccessObjectImpl.java

License:Open Source License

private Criteria processCriteria(Session session, Class clazz,
        QueryEditor.CriteriaRelationshipPairs criteriaPair, boolean appendToHumanReadableSearchScreen) {
    Criteria criteria;//from   ww  w .j  av  a  2s.c om
    Conjunction junction;
    Criteria addedCriteria;
    criteria = session.createCriteria(clazz);

    if (appendToHumanReadableSearchScreen) {
        humanReadableSearchString = StringHelper.concat(" <font color='red'>and</font> ",
                humanReadableSearchString, criteriaPair.getHumanReadableSearchString());
    }
    junction = Restrictions.conjunction();
    if (criteriaPair.getSecondPropertyName() == null) {
        addedCriteria = criteria.createCriteria(criteriaPair.getPropertyName());
    } else {
        addedCriteria = criteria.createCriteria(criteriaPair.getPropertyName())
                .createCriteria(criteriaPair.getSecondPropertyName());
    }
    for (Criterion criterion : criteriaPair.getCriteriaList()) {
        junction.add(criterion);
    }
    addedCriteria.add(junction);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria;
}

From source file:org.brushingbits.jnap.persistence.hibernate.DynaQueryBuilder.java

License:Apache License

public Criteria build() throws QueryException {
    Matcher matcher = DYNA_QUERY_PATTERN.matcher(this.dynaQuery);
    if (!matcher.matches()) {
        throw new QueryException("The DynaQuery syntax is incorrect. It must start with 'findBy' "
                + ", findUniqueBy or 'countBy' expression followed by property expressions and operators.",
                this.dynaQuery);
    }/*from  ww w  .jav  a2  s .c om*/
    Criteria criteria = this.createCriteria(matcher.group(1).equals("countBy"));
    String dynaQueryExpression = matcher.group(2);

    // order by
    Matcher orderByMatcher = ORDER_BY_PATTERN.matcher(dynaQueryExpression);
    if (orderByMatcher.find()) {
        dynaQueryExpression = StringUtils.remove(dynaQueryExpression, orderByMatcher.group());
        String orderByProperty = normalizePropertyName(orderByMatcher.group(3));
        String orderByDirection = orderByMatcher.group(4);
        Order orderBy = "Desc".equals(orderByDirection) ? Order.desc(orderByProperty)
                : Order.asc(orderByProperty);
        criteria.addOrder(orderBy);
    }

    // split properties
    String[] properties = DYNA_QUERY_OPERATOR_PATTERN.split(dynaQueryExpression);
    if (properties.length == 0 || properties.length > 2) {
        throw new QueryException(format(
                "The DynaQuery syntax is incorrect. Dynamic queries must have "
                        + "at least one property an no more than two (yours has {0}).\nYou can use one of "
                        + "the following logical operators ({1}) and these expression operators ({2})",
                properties.length, LOGICAL_OPERATOR_AND + ", " + LOGICAL_OPERATOR_OR,
                StringUtils.join(EXPRESSION_OPERATORS, ", ")), this.dynaQuery);
    }
    Matcher logicalOperatorsMatcher = DYNA_QUERY_OPERATOR_PATTERN.matcher(dynaQueryExpression);
    String logicalOperator = logicalOperatorsMatcher.find() ? logicalOperatorsMatcher.group(1)
            : LOGICAL_OPERATOR_AND;
    Junction junction = LOGICAL_OPERATOR_OR.equals(logicalOperator) ? Restrictions.disjunction()
            : Restrictions.conjunction();
    for (String property : properties) {
        junction.add(this.createCriterion(property));
    }
    criteria.add(junction);
    return criteria;
}

From source file:org.candlepin.auth.permissions.CheckJobStatusPermission.java

License:Open Source License

@Override
@SuppressWarnings("checkstyle:indentation")
public Criterion getCriteriaRestrictions(Class entityClass) {
    if (!entityClass.equals(JobStatus.class)) {
        return null;
    }//ww  w.  j  a  v  a  2s. c  om

    Conjunction conjunction = Restrictions.conjunction();
    // Org has to match.
    conjunction.add(Restrictions.in("ownerId", allowedOrgKeys));

    conjunction.add(Restrictions.or(Restrictions.ne("targetType", JobStatus.TargetType.OWNER),
            Restrictions.and(Restrictions.eq("targetType", JobStatus.TargetType.OWNER),
                    Restrictions.eqProperty("ownerId", "targetId"))));

    // If the principal is not a user, make sure to enforce a principalName match.
    if (!"user".equalsIgnoreCase(principalType)) {
        conjunction.add(Restrictions.eq("principalName", principalName));
    }
    return conjunction;
}