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.jspresso.framework.model.persistence.hibernate.criterion.DefaultCriteriaFactory.java

License:Open Source License

/**
 * Creates a criterion by processing a comparable query structure.
 *
 * @param path// w w  w .j  a  va2  s.co m
 *     the path to the comparable property.
 * @param queryStructure
 *     the comparable query structure.
 * @param componentDescriptor
 *     the component descriptor
 * @param queryComponent
 *     the query component
 * @param context
 *     the context
 * @return the created criterion or null if no criterion necessary.
 */
protected Criterion createComparableQueryStructureRestriction(String path,
        ComparableQueryStructure queryStructure, IComponentDescriptor<?> componentDescriptor,
        IQueryComponent queryComponent, Map<String, Object> context) {
    Junction queryStructureRestriction = null;
    if (queryStructure.isRestricting()) {
        queryStructureRestriction = Restrictions.conjunction();
        String comparator = queryStructure.getComparator();
        Object infValue = queryStructure.getInfValue();
        Object supValue = queryStructure.getSupValue();
        Object compareValue = infValue;
        if (compareValue == null) {
            compareValue = supValue;
        }
        switch (comparator) {
        case ComparableQueryStructureDescriptor.EQ:
            queryStructureRestriction.add(Restrictions.eq(path, compareValue));
            break;
        case ComparableQueryStructureDescriptor.GT:
            queryStructureRestriction.add(Restrictions.gt(path, compareValue));
            break;
        case ComparableQueryStructureDescriptor.GE:
            queryStructureRestriction.add(Restrictions.ge(path, compareValue));
            break;
        case ComparableQueryStructureDescriptor.LT:
            queryStructureRestriction.add(Restrictions.lt(path, compareValue));
            break;
        case ComparableQueryStructureDescriptor.LE:
            queryStructureRestriction.add(Restrictions.le(path, compareValue));
            break;
        case ComparableQueryStructureDescriptor.NU:
            queryStructureRestriction.add(Restrictions.isNull(path));
            break;
        case ComparableQueryStructureDescriptor.NN:
            queryStructureRestriction.add(Restrictions.isNotNull(path));
            break;
        case ComparableQueryStructureDescriptor.BE:
            if (infValue != null && supValue != null) {
                queryStructureRestriction.add(Restrictions.between(path, infValue, supValue));
            } else if (infValue != null) {
                queryStructureRestriction.add(Restrictions.ge(path, infValue));
            } else {
                queryStructureRestriction.add(Restrictions.le(path, supValue));
            }
            break;
        default:
            break;
        }
    }
    return queryStructureRestriction;
}

From source file:org.LexGrid.LexBIG.caCore.dao.orm.translators.GridCQLToDetachedCriteria.java

License:Open Source License

public DetachedCriteria translate(CQLQuery query) throws TranslationException {
    //Do a little housekeeping and make sure everything is in order
    if (query.getTarget() == null) {
        throw new TranslationException("Target of the CQLQuery cannot be null, please specify a Target.");
    }//from   ww  w . ja  va  2s.  c o  m

    Object target = query.getTarget();
    String targetClassName = target.getName();

    DetachedCriteria crit = DetachedCriteria.forEntityName(targetClassName);

    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    //Conjunction all the properties
    Conjunction con = Restrictions.conjunction();

    //Process Attribute
    Attribute attribute = target.getAttribute();
    if (attribute != null) {
        con.add(processAttribute(attribute, crit.getAlias()));
    }

    //Process Association
    Association association = target.getAssociation();
    if (association != null) {
        con.add(processAssociation(association, crit.getAlias(), crit, targetClassName));
    }

    //Process Group
    Group group = target.getGroup();
    if (group != null) {
        crit.add(processGroup(group, crit.getAlias(), crit, targetClassName));
    }

    crit.add(con);

    QueryModifier modifiers = query.getQueryModifier();

    crit = handleQueryOptions(crit, modifiers);

    return crit;
}

From source file:org.LexGrid.LexBIG.caCore.dao.orm.translators.GridCQLToDetachedCriteria.java

License:Open Source License

private Criterion processGroup(Group group, String alias, DetachedCriteria criteria, String parentClass)
        throws TranslationException {
    LogicalOperator groupOperator = group.getLogicRelation();
    String operator = groupOperator.getValue();

    Junction op = null;/*from   ww  w  .j  a  v  a 2  s . c om*/
    if (operator.equals(LogicalOperator.AND.getValue())) {
        op = Restrictions.conjunction();
    }
    if (operator.equals(LogicalOperator.OR.getValue())) {
        op = Restrictions.disjunction();
    }

    //Process the Attributes
    Attribute[] atts = group.getAttribute();
    if (atts != null) {
        for (Attribute at : atts) {
            op.add(processAttribute(at, alias));
        }
    }

    //Process the Associations
    Association[] assocs = group.getAssociation();
    if (assocs != null) {
        for (Association assoc : assocs) {
            op.add(processAssociation(assoc, alias, criteria, parentClass));
        }
    }

    //Process Nested Groups
    Group[] groups = group.getGroup();
    if (groups != null) {
        for (Group nestedGroup : groups) {
            op.add(processGroup(nestedGroup, alias, criteria, parentClass));
        }
    }
    return op;
}

From source file:org.LexGrid.LexBIG.caCore.dao.orm.translators.GridCQLToDetachedCriteria.java

License:Open Source License

private Criterion processAssociation(Association assoc, String alias, DetachedCriteria criteria,
        String parentClass) throws TranslationException {
    String assocClassName = assoc.getName();
    String sourceRoleName = assoc.getRoleName();

    String roleName = LexEVSTranslatorsUtil.getRoleName(parentClass, assocClassName, sourceRoleName);

    Conjunction con = Restrictions.conjunction();

    criteria.createAlias(alias + "." + roleName, roleName);

    //Process Attribute
    Attribute at = assoc.getAttribute();
    if (at != null) {
        con.add(processAttribute(at, roleName));
    }/*from   ww w. ja va2 s.  c  o m*/

    //Process Group
    Group group = assoc.getGroup();
    if (group != null) {
        con.add(processGroup(group, roleName, criteria, assocClassName));
    }

    //Process Nested Associations
    Association nestedAssoc = assoc.getAssociation();
    if (nestedAssoc != null) {
        con.add(processAssociation(nestedAssoc, roleName, criteria, assocClassName));
    }
    return con;
}

From source file:org.LexGrid.LexBIG.caCore.dao.orm.translators.NestedObjectToCriterion.java

License:Open Source License

public Criterion buildCriterionFromNestedObjects(Object object, String parentAlias, DetachedCriteria crit)
        throws TranslationException {
    Conjunction conjunction = Restrictions.conjunction();

    Class c = object.getClass();/*from w ww .  ja v  a  2 s.  c o m*/

    while (c != null) {
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Object fieldVal;
            try {
                fieldVal = field.get(object);
            } catch (Exception e) {
                log.error(e.getMessage());
                throw new TranslationException(this.getClass().getName(), e);
            }

            //Only process if the field value is not null
            if (fieldVal != null) {
                Class fieldValClass = fieldVal.getClass();
                String fieldName = field.getName();
                if (LexEVSTranslatorsUtil.isCollectionClass(fieldValClass)) {
                    log.debug("Processing a Collection");

                    //check if the List is empty
                    List list = (List) fieldVal;
                    if (list.size() > 0) {
                        log.debug("Collection associated with : " + fieldName + " is non-empty.");
                        crit.createAlias(parentAlias + "." + fieldName, fieldName);

                        //Iterate through the list
                        Iterator itr = list.iterator();
                        while (itr.hasNext()) {
                            conjunction.add(buildCriterionFromNestedObjects(itr.next(), fieldName, crit));
                        }
                    }

                } else if (!LexEVSTranslatorsUtil.isPrimitiveClass(fieldValClass)) {
                    log.debug("Processing a non-primitive association : " + fieldName + " " + fieldValClass);
                    crit = crit.createAlias(parentAlias + "." + fieldName, fieldName);
                    conjunction.add(buildCriterionFromNestedObjects(fieldVal, fieldName, crit));
                } else {
                    log.debug("Processing a primitive : " + fieldName + " value: " + fieldVal);

                    //check for a wildcard
                    if (fieldVal instanceof String) {
                        String value = (String) fieldVal;

                        //if the String value is blank, don't add it as a restriction.
                        //The LexEVS Model sometimes initializes Strings with a "" value.
                        //We don't want to add these as restrictions -- treat them as null values.
                        if (!value.equals("")) {
                            //check if it contains a wildcard
                            if (value.contains("*")) {
                                value = value.replace("*", "%");
                                conjunction.add(Restrictions.like(parentAlias + "." + fieldName, value));
                            } else {
                                conjunction.add(Restrictions.eq(parentAlias + "." + fieldName, fieldVal));
                            }
                        } else {
                            log.debug("Skipping empty String value : " + fieldName + " value: " + fieldVal);
                        }
                    } else {
                        conjunction.add(Restrictions.eq(parentAlias + "." + fieldName, fieldVal));
                    }
                }
            }
        }
        c = c.getSuperclass();
    }
    return conjunction;
}

From source file:org.LexGrid.LexBIG.caCore.dao.orm.translators.SDKCQLToDetachedCriteria.java

License:Open Source License

public DetachedCriteria translate(CQLQuery query) throws TranslationException {
    CQLObject target = query.getTarget();
    String targetClassName = target.getName();

    DetachedCriteria crit = DetachedCriteria.forEntityName(targetClassName);
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    //Conjunction all the properties
    Conjunction con = Restrictions.conjunction();

    //Process Attribute
    CQLAttribute attribute = target.getAttribute();
    if (attribute != null) {
        con.add(processAttribute(attribute, crit.getAlias()));
    }//  w  w w .  j a va  2  s .  c om

    //Process Association
    CQLAssociation association = target.getAssociation();
    if (association != null) {
        con.add(processAssociation(association, crit.getAlias(), crit, targetClassName));
    }

    //Process Group
    CQLGroup group = target.getGroup();
    if (group != null) {
        crit.add(processGroup(group, crit.getAlias(), crit, targetClassName));
    }

    crit.add(con);

    return crit;
}

From source file:org.LexGrid.LexBIG.caCore.dao.orm.translators.SDKCQLToDetachedCriteria.java

License:Open Source License

private Criterion processGroup(CQLGroup group, String alias, DetachedCriteria criteria, String parentClass)
        throws TranslationException {
    CQLLogicalOperator groupOperator = group.getLogicOperator();
    String operator = groupOperator.getValue();

    Junction op = null;/*w  w  w  .ja  v a  2  s. c  o m*/
    if (operator.equals(CQLLogicalOperator.AND.getValue())) {
        op = Restrictions.conjunction();
    }
    if (operator.equals(CQLLogicalOperator.OR.getValue())) {
        op = Restrictions.disjunction();
    }

    //Process the Attributes
    Collection<CQLAttribute> atts = group.getAttributeCollection();
    if (atts != null) {
        for (CQLAttribute at : atts) {
            op.add(processAttribute(at, alias));
        }
    }

    //Process the Associations
    Collection<CQLAssociation> assocs = group.getAssociationCollection();
    if (assocs != null) {
        for (CQLAssociation assoc : assocs) {
            op.add(processAssociation(assoc, alias, criteria, parentClass));
        }
    }

    //Process Nested Groups
    Collection<CQLGroup> groups = group.getGroupCollection();
    if (groups != null) {
        for (CQLGroup nestedGroup : groups) {
            op.add(processGroup(nestedGroup, alias, criteria, parentClass));
        }
    }
    return op;
}

From source file:org.LexGrid.LexBIG.caCore.dao.orm.translators.SDKCQLToDetachedCriteria.java

License:Open Source License

private Criterion processAssociation(CQLAssociation assoc, String alias, DetachedCriteria criteria,
        String parentClass) throws TranslationException {
    String assocClassName = assoc.getName();
    String sourceRoleName = assoc.getSourceRoleName();

    String roleName = LexEVSTranslatorsUtil.getRoleName(parentClass, assocClassName, sourceRoleName);

    Conjunction con = Restrictions.conjunction();

    criteria.createAlias(alias + "." + roleName, roleName);

    //Process Attribute
    CQLAttribute at = assoc.getAttribute();
    if (at != null) {
        con.add(processAttribute(at, roleName));
    }//from   w w  w.  j  av a  2s .c om

    //Process Group
    CQLGroup group = assoc.getGroup();
    if (group != null) {
        con.add(processGroup(group, roleName, criteria, assocClassName));
    }

    //Process Nested Associations
    CQLAssociation nestedAssoc = assoc.getAssociation();
    if (nestedAssoc != null) {
        con.add(processAssociation(nestedAssoc, roleName, criteria, assocClassName));
    }
    return con;
}

From source file:org.linagora.linshare.core.repository.hibernate.MailContentRepositoryImpl.java

License:Open Source License

@Override
public List<MailContent> findAll(AbstractDomain domain, Language lang, MailContentType type) {
    Conjunction and = Restrictions.conjunction();
    and.add(Restrictions.eq("domain", domain));
    and.add(Restrictions.eq("mailContentType", type.toInt()));
    and.add(Restrictions.eq("language", lang.toInt()));
    return findByCriteria(and);
}

From source file:org.linagora.linshare.core.repository.hibernate.UploadRequestRepositoryImpl.java

License:Open Source License

@Override
public List<UploadRequest> findByDomainsAndStatus(List<AbstractDomain> domains,
        List<UploadRequestStatus> status, Date after, Date before) {
    Junction add = Restrictions.conjunction().add(Restrictions.in("abstractDomain", domains))
            .add(Restrictions.between("creationDate", after, before));
    if (!status.isEmpty()) {
        add.add(Restrictions.in("status", status));
    }/*w ww .j av a  2 s  . c  o  m*/
    return findByCriteria(add);
}