Search in sources :

Example 1 with QueryPath

use of org.jowidgets.cap.service.jpa.api.query.QueryPath 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 2 with QueryPath

use of org.jowidgets.cap.service.jpa.api.query.QueryPath 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

ManagedType (javax.persistence.metamodel.ManagedType)2 IPropertyMap (org.jowidgets.cap.common.api.bean.IPropertyMap)2 PropertyMapQueryPath (org.jowidgets.cap.service.jpa.api.query.PropertyMapQueryPath)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 Attribute (javax.persistence.metamodel.Attribute)1 PluralAttribute (javax.persistence.metamodel.PluralAttribute)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1 Type (javax.persistence.metamodel.Type)1