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();
}
Aggregations