Search in sources :

Example 1 with PropertyMapQueryPath

use of org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath in project jo-client-platform by jo-source.

the class CriteriaQueryCreator method createQueryPathGenericArithmeticFilterPredicate.

@SuppressWarnings("unchecked")
private Predicate createQueryPathGenericArithmeticFilterPredicate(final CriteriaBuilder criteriaBuilder, final Root<?> bean, final CriteriaQuery<?> query, final IArithmeticFilter filter) {
    final Class<Object> javaType = (Class<Object>) bean.getJavaType();
    final Subquery<Object> subquery = query.subquery(javaType);
    final Root<Object> subqueryRoot = subquery.from(javaType);
    subquery.select(subqueryRoot.get(IBean.ID_PROPERTY));
    final boolean existanceFilter = ArithmeticOperator.EMPTY == filter.getOperator();
    final Path<?> joinQueryPath = getJoinQueryPath(subqueryRoot, filter.getPropertyName());
    final Predicate predicate = createArithmeticFilterPredicate(criteriaBuilder, subqueryRoot, subquery, filter, joinQueryPath, existanceFilter);
    final PropertyMapQueryPath propertyMapQueryPath = bean.getJavaType().getAnnotation(PropertyMapQueryPath.class);
    if (IPropertyMap.class.isAssignableFrom(bean.getJavaType()) && joinQueryPath.getParentPath() != null && joinQueryPath.getParentPath().getAlias() != null && propertyMapQueryPath != null && joinQueryPath.getParentPath().getAlias().equals(PROPERTY_MAP_JOIN_ALIAS)) {
        final Predicate propertyMapPredicate = criteriaBuilder.equal(joinQueryPath.getParentPath().get(propertyMapQueryPath.propertyNamePath()), filter.getPropertyName());
        subquery.where(predicate, propertyMapPredicate);
    } else {
        subquery.where(predicate);
    }
    if (filter.isInverted() && !existanceFilter) {
        return criteriaBuilder.not(bean.get(IBean.ID_PROPERTY).in(subquery));
    } else {
        return bean.get(IBean.ID_PROPERTY).in(subquery);
    }
}
Also used : Predicate(javax.persistence.criteria.Predicate) PropertyMapQueryPath(org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath)

Example 2 with PropertyMapQueryPath

use of org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath in project jo-client-platform by jo-source.

the class CriteriaQueryCreator method getJoinQueryPath.

private Path<?> getJoinQueryPath(final Root<?> bean, final String propertyName) {
    final QueryPath queryPath = getQueryPathAnno(bean, propertyName);
    if (queryPath != null) {
        Path<?> parentPath = bean;
        Type<?> type = bean.getModel();
        for (final String pathSegment : queryPath.path()) {
            final Attribute<?, ?> attribute;
            if (type != null && type instanceof ManagedType) {
                attribute = ((ManagedType<?>) type).getAttribute(pathSegment);
                type = getType(attribute);
            } else {
                attribute = null;
            }
            if (isJoinableType(attribute) && parentPath instanceof From) {
                final From<?, ?> from = (From<?, ?>) parentPath;
                parentPath = from.join(pathSegment, JoinType.LEFT);
            } else {
                parentPath = parentPath.get(pathSegment);
            }
        }
        return parentPath;
    } else if (IPropertyMap.class.isAssignableFrom(bean.getJavaType())) {
        final PropertyMapQueryPath propertyMapQueryPath = bean.getJavaType().getAnnotation(PropertyMapQueryPath.class);
        if (propertyMapQueryPath != null) {
            Path<?> parentPath = bean;
            Type<?> type = bean.getModel();
            for (final String pathSegment : propertyMapQueryPath.path()) {
                final Attribute<?, ?> attribute;
                if (type != null && type instanceof ManagedType) {
                    attribute = ((ManagedType<?>) type).getAttribute(pathSegment);
                    type = getType(attribute);
                } else {
                    attribute = null;
                }
                if (isJoinableType(attribute) && parentPath instanceof From) {
                    final From<?, ?> from = (From<?, ?>) parentPath;
                    parentPath = from.join(pathSegment, JoinType.LEFT);
                } else {
                    parentPath = parentPath.get(pathSegment);
                }
            }
            parentPath.alias(PROPERTY_MAP_JOIN_ALIAS);
            return parentPath.get(propertyMapQueryPath.valuePath());
        }
    }
    return null;
}
Also used : PropertyMapQueryPath(org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath) QueryPath(org.jowidgets.cap.service.jpa.api.query.QueryPath) Path(javax.persistence.criteria.Path) ManagedType(javax.persistence.metamodel.ManagedType) IPropertyMap(org.jowidgets.cap.common.api.bean.IPropertyMap) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) From(javax.persistence.criteria.From) PropertyMapQueryPath(org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath) PropertyMapQueryPath(org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath) QueryPath(org.jowidgets.cap.service.jpa.api.query.QueryPath) JoinType(javax.persistence.criteria.JoinType) ManagedType(javax.persistence.metamodel.ManagedType) Type(javax.persistence.metamodel.Type)

Example 3 with PropertyMapQueryPath

use of org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath in project jo-client-platform by jo-source.

the class CriteriaQueryCreator method isJoinQueryPath.

private boolean isJoinQueryPath(final Root<?> bean, final String propertyName) {
    final QueryPath queryPath = getQueryPathAnno(bean, propertyName);
    if (queryPath != null) {
        for (final String pathSegment : queryPath.path()) {
            Path<?> path = bean;
            Type<?> type = bean.getModel();
            final Attribute<?, ?> attribute;
            if (type != null && type instanceof ManagedType) {
                attribute = ((ManagedType<?>) type).getAttribute(pathSegment);
                type = getType(attribute);
            } else {
                attribute = null;
            }
            if (isJoinableType(attribute)) {
                return true;
            } else {
                path = path.get(pathSegment);
            }
        }
    } else {
        try {
            bean.get(propertyName);
            return false;
        } catch (final IllegalArgumentException illegalArgumentException) {
            if (IPropertyMap.class.isAssignableFrom(bean.getJavaType())) {
                final PropertyMapQueryPath propertyMapQueryPath = bean.getJavaType().getAnnotation(PropertyMapQueryPath.class);
                if (propertyMapQueryPath != null) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : PropertyMapQueryPath(org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath) QueryPath(org.jowidgets.cap.service.jpa.api.query.QueryPath) ManagedType(javax.persistence.metamodel.ManagedType) IPropertyMap(org.jowidgets.cap.common.api.bean.IPropertyMap) PropertyMapQueryPath(org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath)

Aggregations

PropertyMapQueryPath (org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath)3 ManagedType (javax.persistence.metamodel.ManagedType)2 IPropertyMap (org.jowidgets.cap.common.api.bean.IPropertyMap)2 QueryPath (org.jowidgets.cap.service.jpa.api.query.QueryPath)2 From (javax.persistence.criteria.From)1 JoinType (javax.persistence.criteria.JoinType)1 Path (javax.persistence.criteria.Path)1 Predicate (javax.persistence.criteria.Predicate)1 Attribute (javax.persistence.metamodel.Attribute)1 PluralAttribute (javax.persistence.metamodel.PluralAttribute)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1 Type (javax.persistence.metamodel.Type)1