Search in sources :

Example 21 with ExtendedAttribute

use of com.blazebit.persistence.spi.ExtendedAttribute in project blaze-persistence by Blazebit.

the class EmbeddableSplittingVisitor method collectSplittedOffExpressions.

protected boolean collectSplittedOffExpressions(Expression expression) {
    splittedOffExpressions.clear();
    if (expressionToSplit != null) {
        JoinNode baseNode;
        String field;
        if (expressionToSplit instanceof PathExpression) {
            PathReference pathReference = ((PathExpression) expressionToSplit).getPathReference();
            baseNode = (JoinNode) pathReference.getBaseNode();
            field = pathReference.getField();
        } else if (expressionToSplit instanceof MapKeyExpression) {
            baseNode = ((JoinNode) ((MapKeyExpression) expressionToSplit).getPath().getBaseNode()).getKeyJoinNode();
            field = null;
        } else {
            // This should never happen
            return false;
        }
        String fieldPrefix = field == null ? "" : field + ".";
        ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getManagedType());
        Map<String, Boolean> orderedAttributes = new TreeMap<>();
        EntityType<?> ownerType;
        if ((baseNode.getParentTreeNode() == null || splitEntity && baseNode.getManagedType() instanceof EntityType<?>) && field == null) {
            ownerType = baseNode.getEntityType();
            for (SingularAttribute<?, ?> idAttribute : managedType.getIdAttributes()) {
                addAttributes(ownerType, null, fieldPrefix, "", idAttribute, orderedAttributes);
            }
        } else {
            Map<String, ? extends ExtendedAttribute<?, ?>> ownedAttributes;
            String prefix = field;
            if (baseNode.getParentTreeNode() != null && jpaProvider.getJpaMetamodelAccessor().isElementCollection(baseNode.getParentTreeNode().getAttribute())) {
                String elementCollectionPath = baseNode.getParentTreeNode().getRelationName();
                ExtendedManagedType entityManagedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getParent().getEntityType());
                ownedAttributes = entityManagedType.getAttributes();
                if (prefix == null) {
                    prefix = elementCollectionPath;
                } else {
                    prefix = elementCollectionPath + "." + prefix;
                }
            } else {
                ownedAttributes = managedType.getOwnedSingularAttributes();
            }
            for (String embeddedPropertyPath : JpaUtils.getEmbeddedPropertyPaths((Map<String, ExtendedAttribute<?, ?>>) ownedAttributes, prefix, false, false)) {
                orderedAttributes.put(embeddedPropertyPath, Boolean.FALSE);
            }
        }
        // Signal the caller that the expression was eliminated
        if (orderedAttributes.isEmpty()) {
            return true;
        }
        for (String orderedAttribute : orderedAttributes.keySet()) {
            splittedOffExpressions.add(splittingVisitor.splitOff(expression, expressionToSplit, orderedAttribute));
        }
    }
    return false;
}
Also used : PathReference(com.blazebit.persistence.parser.expression.PathReference) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) TreeMap(java.util.TreeMap) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) EntityType(javax.persistence.metamodel.EntityType) MapKeyExpression(com.blazebit.persistence.parser.expression.MapKeyExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression)

Aggregations

ExtendedAttribute (com.blazebit.persistence.spi.ExtendedAttribute)21 ExtendedManagedType (com.blazebit.persistence.spi.ExtendedManagedType)13 Map (java.util.Map)7 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)5 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 Attribute (javax.persistence.metamodel.Attribute)4 PluralAttribute (javax.persistence.metamodel.PluralAttribute)4 PathReference (com.blazebit.persistence.parser.expression.PathReference)3 EntityMetamodel (com.blazebit.persistence.parser.EntityMetamodel)2 ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)2 Expression (com.blazebit.persistence.parser.expression.Expression)2 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)2 MapKeyExpression (com.blazebit.persistence.parser.expression.MapKeyExpression)2 NullExpression (com.blazebit.persistence.parser.expression.NullExpression)2 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)2 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)2 IdentityHashMap (java.util.IdentityHashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2