Example usage for org.hibernate.criterion DetachedCriteria getAlias

List of usage examples for org.hibernate.criterion DetachedCriteria getAlias

Introduction

In this page you can find the example usage for org.hibernate.criterion DetachedCriteria getAlias.

Prototype

public String getAlias() 

Source Link

Document

Obtain the alias associated with this DetachedCriteria

Usage

From source file:de.decidr.model.filters.StartableWorkflowModelFilter.java

License:Apache License

/**
 * {@inheritDoc}/* ww  w  . j a v a2  s.co  m*/
 */
public void apply(Criteria criteria) {
    DetachedCriteria exists = DetachedCriteria.forClass(StartableWorkflowModelView.class, "startableFilter");
    exists.add(Restrictions.eqProperty(exists.getAlias() + ".id", criteria.getAlias() + ".id"));
    /*
     * Workaround for Hibernate issue HHH-993: Criteria subquery without
     * projection fails throwing NullPointerException.
     * 
     * Additionally, Mysql doesn't seem to like aliases in EXISTS
     * subqueries, so we have to explicitly specify "*"
     */
    exists.setProjection(Projections.sqlProjection("*", new String[0], new Type[0]));
    criteria.add(Subqueries.exists(exists));
}

From source file:org.forzaframework.core.persistance.hibernate.CriteriaTranslatorImpl.java

License:Apache License

private DetachedCriteria getCriteria(String path) {
    for (DetachedCriteria criteria : criterias) {
        if (path.equals(criteria.getAlias())) {
            return criteria;
        }/*  w w  w. j  a  v a  2  s .com*/
    }
    return null;
}

From source file:org.jspresso.framework.model.persistence.hibernate.criterion.DefaultCriteriaFactory.java

License:Open Source License

@SuppressWarnings("ConstantConditions")
private boolean completeCriteria(EnhancedDetachedCriteria rootCriteria, DetachedCriteria currentCriteria,
        String path, IQueryComponent aQueryComponent, Map<String, Object> context) {
    boolean abort = false;
    IComponentDescriptor<?> componentDescriptor = aQueryComponent.getQueryDescriptor();
    if (aQueryComponent instanceof ComparableQueryStructure) {
        completeCriteria(currentCriteria, createComparableQueryStructureRestriction(path,
                (ComparableQueryStructure) aQueryComponent, componentDescriptor, aQueryComponent, context));
    } else {//from w ww.ja  v a2 s.c  o m
        String translationsPath = AbstractComponentDescriptor.getComponentTranslationsDescriptorTemplate()
                .getName();
        String translationsAlias = currentCriteria.getAlias() + "_"
                + componentDescriptor.getComponentContract().getSimpleName() + "_" + translationsPath;
        if (componentDescriptor.isTranslatable()) {
            rootCriteria.getSubCriteriaFor(currentCriteria, translationsPath, translationsAlias,
                    JoinType.LEFT_OUTER_JOIN);
        }
        for (Map.Entry<String, Object> property : aQueryComponent.entrySet()) {
            String propertyName = property.getKey();
            Object propertyValue = property.getValue();
            IPropertyDescriptor propertyDescriptor = componentDescriptor.getPropertyDescriptor(propertyName);
            if (propertyDescriptor != null) {
                boolean isEntityRef = false;
                if (componentDescriptor.isEntity() && aQueryComponent.containsKey(IEntity.ID)) {
                    isEntityRef = true;
                }
                if ((!PropertyViewDescriptorHelper.isComputed(componentDescriptor, propertyName)
                        || (propertyDescriptor instanceof IStringPropertyDescriptor
                                && ((IStringPropertyDescriptor) propertyDescriptor).isTranslatable()))
                        && (!isEntityRef || IEntity.ID.equals(propertyName))) {
                    String prefixedProperty = PropertyHelper.toJavaBeanPropertyName(propertyName);
                    if (path != null) {
                        prefixedProperty = path + "." + prefixedProperty;
                    }
                    if (propertyValue instanceof IEntity) {
                        if (!((IEntity) propertyValue).isPersistent()) {
                            abort = true;
                        } else {
                            completeCriteria(currentCriteria, Restrictions.eq(prefixedProperty, propertyValue));
                        }
                    } else if (propertyValue instanceof Boolean
                            && (isTriStateBooleanSupported() || (Boolean) propertyValue)) {
                        completeCriteria(currentCriteria, Restrictions.eq(prefixedProperty, propertyValue));
                    } else if (IEntity.ID.equalsIgnoreCase(propertyName)) {
                        completeCriteria(currentCriteria,
                                createIdRestriction(propertyDescriptor, prefixedProperty, propertyValue,
                                        componentDescriptor, aQueryComponent, context));
                    } else if (propertyValue instanceof String) {
                        completeCriteriaWithTranslations(currentCriteria, translationsPath, translationsAlias,
                                property, propertyDescriptor, prefixedProperty,
                                getBackendController(context).getLocale(), componentDescriptor, aQueryComponent,
                                context);
                    } else if (propertyValue instanceof Number || propertyValue instanceof Date) {
                        completeCriteria(currentCriteria, Restrictions.eq(prefixedProperty, propertyValue));
                    } else if (propertyValue instanceof EnumQueryStructure) {
                        completeCriteria(currentCriteria, createEnumQueryStructureRestriction(prefixedProperty,
                                ((EnumQueryStructure) propertyValue)));
                    } else if (propertyValue instanceof IQueryComponent) {
                        IQueryComponent joinedComponent = ((IQueryComponent) propertyValue);
                        if (!isQueryComponentEmpty(joinedComponent, propertyDescriptor)) {
                            if (joinedComponent.isInlineComponent()/* || path != null */) {
                                // the joined component is an inline component so we must use
                                // dot nested properties. Same applies if we are in a nested
                                // path i.e. already on an inline component.
                                abort = abort || completeCriteria(rootCriteria, currentCriteria,
                                        prefixedProperty, (IQueryComponent) propertyValue, context);
                            } else {
                                // the joined component is an entity so we must use
                                // nested criteria; unless the autoComplete property
                                // is a special char.
                                boolean digDeeper = true;
                                String autoCompleteProperty = joinedComponent.getQueryDescriptor()
                                        .getAutoCompleteProperty();
                                if (autoCompleteProperty != null) {
                                    String val = (String) joinedComponent.get(autoCompleteProperty);
                                    if (val != null) {
                                        boolean negate = false;
                                        if (val.startsWith(IQueryComponent.NOT_VAL)) {
                                            val = val.substring(1);
                                            negate = true;
                                        }
                                        if (IQueryComponent.NULL_VAL.equals(val)) {
                                            Criterion crit = Restrictions.isNull(prefixedProperty);
                                            if (negate) {
                                                crit = Restrictions.not(crit);
                                            }
                                            completeCriteria(currentCriteria, crit);
                                            digDeeper = false;
                                        }
                                    }
                                }
                                if (digDeeper) {
                                    DetachedCriteria joinCriteria;
                                    if (isUseAliasesForJoins()) {
                                        joinCriteria = rootCriteria.getSubCriteriaFor(currentCriteria,
                                                prefixedProperty, prefixedProperty, JoinType.INNER_JOIN);
                                    } else {
                                        joinCriteria = rootCriteria.getSubCriteriaFor(currentCriteria,
                                                prefixedProperty, JoinType.INNER_JOIN);
                                    }
                                    abort = abort || completeCriteria(rootCriteria, joinCriteria, null,
                                            joinedComponent, context);
                                }
                            }
                        }
                    } else if (propertyValue != null) {
                        // Unknown property type. Assume equals.
                        completeCriteria(currentCriteria, Restrictions.eq(prefixedProperty, propertyValue));
                    }
                }
            }
        }
    }
    return abort;
}

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  w w w . ja v a 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.NestedObjectToCriterion.java

License:Open Source License

public Criterion buildCriterionFromNestedObjects(Object object, DetachedCriteria crit)
        throws TranslationException {
    String alias = crit.getAlias();
    return this.buildCriterionFromNestedObjects(object, alias, crit);
}

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

License:Open Source License

public DetachedCriteria translate(String path, List searchObjectList) throws TranslationException {
    List<String> pathList = new ArrayList<String>();
    StringTokenizer tokens = new StringTokenizer(path, ",");
    while (tokens.hasMoreTokens()) {
        pathList.add(tokens.nextToken().trim());
    }//from   w  ww.  j  a v  a2  s .  c o m
    String rootClass = pathList.remove(0);
    DetachedCriteria dCrit = DetachedCriteria.forEntityName(rootClass);
    dCrit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    //Set the Association Path
    String alias = dCrit.getAlias();
    for (String pathElement : pathList) {
        String roleName = LexEVSTranslatorsUtil.getRoleName(rootClass, pathElement);
        dCrit.createAlias(alias + "." + roleName, roleName);

        rootClass = pathElement;
        alias = roleName;
    }

    //Apply the List of Associated Objects (OR'ed)
    Disjunction disjunction = Restrictions.disjunction();
    for (Object searchObject : searchObjectList) {
        Criterion crit = translator.buildCriterionFromNestedObjects(searchObject, alias, dCrit);
        disjunction.add(crit);
    }
    dCrit.add(disjunction);

    return dCrit;
}

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()));
    }// ww w.j  a v  a  2 s.c  o  m

    //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.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

private DetachedCriteria toDetachedCriteria(Query query, String alias) {
    boolean hasOneToManyAssociation = false;
    DetachedCriteria detachedCriteria;
    if (alias != null) {
        detachedCriteria = DetachedCriteria.forClass(query.getTargetType(), alias);
    } else {//from  w  w  w.java  2 s. c  o  m
        detachedCriteria = DetachedCriteria.forClass(query.getTargetType());
    }

    final Map<String, String> aliases = new HashMap<String, String>();
    for (final Criterion w4tjCriterion : query.getCriteria()) {
        if (w4tjCriterion.getCondition() != null
                && (w4tjCriterion.getCondition().getOperandsNo() == 0 || (w4tjCriterion.getValue() != null
                        && StringUtils.hasText(w4tjCriterion.getValue().toString())))) {
            if (!hasOneToManyAssociation) {
                hasOneToManyAssociation = w4tjCriterion.getPropertyPath().hasOneToManySteps();
            }

            org.hibernate.criterion.Criterion hibCriterion;

            if (w4tjCriterion.isLocal()) {
                hibCriterion = toHibernateCriterion(w4tjCriterion, detachedCriteria.getAlias());
            } else {
                String aliasPath = null;
                for (final PropertyMetadata propertyMetadata : w4tjCriterion.getPropertyPath().getSteps()) {
                    if (propertyMetadata.equals(w4tjCriterion.getPropertyPath().getLastStep())) {
                        break;
                    } else if (propertyMetadata.equals(w4tjCriterion.getPropertyPath().getFirstStep())) {
                        aliasPath = propertyMetadata.getName();
                    } else {
                        aliasPath += "." + propertyMetadata.getName();
                    }

                    buildAlias(detachedCriteria, aliases, aliasPath);
                }

                hibCriterion = toHibernateCriterion(w4tjCriterion, aliases.get(aliasPath));
            }

            detachedCriteria = detachedCriteria.add(hibCriterion);
        }
    }

    if (!query.getSubqueries().isEmpty()) {
        String masterId = detachedCriteria.getAlias() + "."
                + ContextUtil.getMRS().getEntityMetadata(query.getTargetType()).getIdentifierName();
        int subqindex = 0;
        for (Subquery subquery : query.getSubqueries()) {
            subqindex += 1;
            detachedCriteria = detachedCriteria
                    .add(toHibernateSubcriterion(masterId, String.valueOf(subqindex), subquery));
        }
    }

    for (final OrderBy orderBy : query.getOrderings()) {
        PathMetadata pathMetadata = ContextUtil.getMRS().getPropertyPath(query.getTargetType(),
                StringUtils.delimitedListToStringArray(orderBy.getProperty(), Path.DELIMITER));

        String property;
        if (pathMetadata.isMultiStep()) {
            String aliasPath = null;

            if (!hasOneToManyAssociation) {
                hasOneToManyAssociation = pathMetadata.hasOneToManySteps();
            }

            for (PropertyMetadata propertyMetadata : pathMetadata.getSteps()) {
                if (propertyMetadata.equals(pathMetadata.getLastStep())) {
                    break;
                } else if (propertyMetadata.equals(pathMetadata.getFirstStep())) {
                    aliasPath = propertyMetadata.getName();
                } else {
                    aliasPath += "." + propertyMetadata.getName();
                }

                buildAlias(detachedCriteria, aliases, aliasPath);
            }
            property = aliases.get(aliasPath) + "." + pathMetadata.getLastStep().getName();
        } else {
            property = orderBy.getProperty();
        }

        if (orderBy.isDescending()) {
            detachedCriteria = detachedCriteria.addOrder(Order.desc(property));
        } else {
            detachedCriteria = detachedCriteria.addOrder(Order.asc(property));
        }
    }

    if (hasOneToManyAssociation) {
        detachedCriteria = detachedCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    }

    return detachedCriteria;
}