Search in sources :

Example 1 with MappingIndex

use of com.blazebit.persistence.view.MappingIndex in project blaze-persistence by Blazebit.

the class MetamodelBuildingContextImpl method getPossibleTargetTypes.

@Override
public List<ScalarTargetResolvingExpressionVisitor.TargetType> getPossibleTargetTypes(Class<?> entityClass, Attribute<?, ?> rootAttribute, Annotation mapping, Map<String, javax.persistence.metamodel.Type<?>> rootTypes) {
    ManagedType<?> managedType = entityMetamodel.getManagedType(entityClass);
    String expression;
    if (mapping instanceof Mapping) {
        expression = ((Mapping) mapping).value();
    } else if (mapping instanceof IdMapping) {
        expression = ((IdMapping) mapping).value();
    } else if (mapping instanceof MappingIndex) {
        expression = ((MappingIndex) mapping).value();
    } else if (mapping instanceof MappingCorrelatedSimple) {
        MappingCorrelatedSimple m = (MappingCorrelatedSimple) mapping;
        managedType = entityMetamodel.getManagedType(m.correlated());
        expression = m.correlationResult();
        // Correlation result is the correlated type, so the possible target type is the managed type
        if (expression.isEmpty()) {
            return Collections.<ScalarTargetResolvingExpressionVisitor.TargetType>singletonList(new ScalarTargetResolvingExpressionVisitor.TargetTypeImpl(false, null, managedType.getJavaType(), null, null));
        }
    } else if (mapping instanceof MappingCorrelated) {
        MappingCorrelated m = (MappingCorrelated) mapping;
        CorrelationProviderFactory correlationProviderFactory = CorrelationProviderHelper.getFactory(m.correlator());
        ScalarTargetResolvingExpressionVisitor resolver = new ScalarTargetResolvingExpressionVisitor(entityClass, getEntityMetamodel(), getJpqlFunctions(), rootTypes);
        javax.persistence.metamodel.Type<?> type = TypeExtractingCorrelationBuilder.extractType(correlationProviderFactory, "_alias", this, resolver);
        if (type == null) {
            // We can't determine the managed type
            return Collections.emptyList();
        }
        managedType = (ManagedType<?>) type;
        expression = m.correlationResult();
        // Correlation result is the correlated type, so the possible target type is the managed type
        if (expression.isEmpty()) {
            return Collections.<ScalarTargetResolvingExpressionVisitor.TargetType>singletonList(new ScalarTargetResolvingExpressionVisitor.TargetTypeImpl(false, null, managedType.getJavaType(), null, null));
        }
    } else if (mapping instanceof MappingSubquery) {
        MappingSubquery mappingSubquery = (MappingSubquery) mapping;
        if (!mappingSubquery.expression().isEmpty()) {
            Expression simpleExpression = typeValidationExpressionFactory.createSimpleExpression(((MappingSubquery) mapping).expression(), false, true, false);
            AliasReplacementVisitor aliasReplacementVisitor = new AliasReplacementVisitor(NullExpression.INSTANCE, mappingSubquery.subqueryAlias());
            simpleExpression.accept(aliasReplacementVisitor);
            ScalarTargetResolvingExpressionVisitor visitor = new ScalarTargetResolvingExpressionVisitor(entityClass, entityMetamodel, jpqlFunctions, rootTypes);
            simpleExpression.accept(visitor);
            return visitor.getPossibleTargetTypes();
        }
        // We can't determine the managed type
        return Collections.emptyList();
    } else {
        // There is no entity model type for other mappings
        return Collections.emptyList();
    }
    // Don't bother with empty expressions or missing managed type, let the validation process handle this
    if (expression.isEmpty() || managedType == null) {
        return Collections.emptyList();
    }
    expression = AbstractAttribute.stripThisFromMapping(expression);
    // The result is "THIS" apparently, so the possible target type is the managed type
    if (expression.isEmpty()) {
        Class<?> leafBaseClass = managedType.getJavaType();
        Class<?> leafBaseKeyClass = null;
        Class<?> leafBaseValueClass = null;
        if (rootAttribute instanceof PluralAttribute<?, ?, ?>) {
            leafBaseClass = rootAttribute.getJavaType();
            leafBaseValueClass = ((PluralAttribute<?, ?, ?>) rootAttribute).getElementType().getJavaType();
            if (rootAttribute instanceof MapAttribute<?, ?, ?>) {
                leafBaseKeyClass = ((MapAttribute<?, ?, ?>) rootAttribute).getKeyJavaType();
            }
        } else if (rootAttribute instanceof SingularAttribute<?, ?>) {
            leafBaseClass = rootAttribute.getJavaType();
        }
        return Collections.<ScalarTargetResolvingExpressionVisitor.TargetType>singletonList(new ScalarTargetResolvingExpressionVisitor.TargetTypeImpl(leafBaseValueClass != null, rootAttribute, leafBaseClass, leafBaseKeyClass, leafBaseValueClass));
    }
    Expression simpleExpression = typeValidationExpressionFactory.createSimpleExpression(expression, false, false, true);
    if (simpleExpression instanceof EntityLiteral) {
        // This is a special case where the mapping i.e. attribute name matches an entity name
        try {
            Attribute<?, ?> attribute = managedType.getAttribute(expression);
            return Collections.<ScalarTargetResolvingExpressionVisitor.TargetType>singletonList(new ScalarTargetResolvingExpressionVisitor.TargetTypeImpl(false, attribute, attribute.getJavaType(), null, attribute.getJavaType()));
        } catch (IllegalArgumentException ex) {
        // Apparently it's not an attribute, so let it run through
        }
    }
    ScalarTargetResolvingExpressionVisitor visitor = new ScalarTargetResolvingExpressionVisitor(managedType, rootAttribute, entityMetamodel, jpqlFunctions, rootTypes);
    simpleExpression.accept(visitor);
    return visitor.getPossibleTargetTypes();
}
Also used : EntityLiteral(com.blazebit.persistence.parser.expression.EntityLiteral) Mapping(com.blazebit.persistence.view.Mapping) IdMapping(com.blazebit.persistence.view.IdMapping) MappingSubquery(com.blazebit.persistence.view.MappingSubquery) IdMapping(com.blazebit.persistence.view.IdMapping) AliasReplacementVisitor(com.blazebit.persistence.parser.AliasReplacementVisitor) CorrelationProviderFactory(com.blazebit.persistence.view.CorrelationProviderFactory) MappingCorrelated(com.blazebit.persistence.view.MappingCorrelated) PluralAttribute(javax.persistence.metamodel.PluralAttribute) MappingIndex(com.blazebit.persistence.view.MappingIndex) ScalarTargetResolvingExpressionVisitor(com.blazebit.persistence.view.impl.ScalarTargetResolvingExpressionVisitor) MapAttribute(javax.persistence.metamodel.MapAttribute) MappingCorrelatedSimple(com.blazebit.persistence.view.MappingCorrelatedSimple) Expression(com.blazebit.persistence.parser.expression.Expression) NullExpression(com.blazebit.persistence.parser.expression.NullExpression)

Aggregations

AliasReplacementVisitor (com.blazebit.persistence.parser.AliasReplacementVisitor)1 EntityLiteral (com.blazebit.persistence.parser.expression.EntityLiteral)1 Expression (com.blazebit.persistence.parser.expression.Expression)1 NullExpression (com.blazebit.persistence.parser.expression.NullExpression)1 CorrelationProviderFactory (com.blazebit.persistence.view.CorrelationProviderFactory)1 IdMapping (com.blazebit.persistence.view.IdMapping)1 Mapping (com.blazebit.persistence.view.Mapping)1 MappingCorrelated (com.blazebit.persistence.view.MappingCorrelated)1 MappingCorrelatedSimple (com.blazebit.persistence.view.MappingCorrelatedSimple)1 MappingIndex (com.blazebit.persistence.view.MappingIndex)1 MappingSubquery (com.blazebit.persistence.view.MappingSubquery)1 ScalarTargetResolvingExpressionVisitor (com.blazebit.persistence.view.impl.ScalarTargetResolvingExpressionVisitor)1 MapAttribute (javax.persistence.metamodel.MapAttribute)1 PluralAttribute (javax.persistence.metamodel.PluralAttribute)1