use of com.blazebit.persistence.view.metamodel.ViewRoot in project blaze-persistence by Blazebit.
the class ManagedViewTypeImpl method checkAttributes.
@Override
public void checkAttributes(MetamodelBuildingContext context) {
if (inheritanceMapping != null) {
ScalarTargetResolvingExpressionVisitor visitor = new ScalarTargetResolvingExpressionVisitor(jpaManagedType, context.getEntityMetamodel(), context.getJpqlFunctions(), viewRootTypes);
try {
context.getExpressionFactory().createBooleanExpression(inheritanceMapping, false).accept(visitor);
} catch (RuntimeException ex) {
context.addError("Invalid inheritance mapping expression '" + inheritanceMapping + "' on the entity view " + javaType.getName() + ". Encountered error: " + ex.getMessage());
}
}
// Ensure that a plural entity attribute is not used multiple times in different plural entity view attributes
// If it were used multiple times, the second collection would not receive all expected elements, because both are based on the same join
// and the first collection will already cause a "fold" of the results for materializing the collection in the entity view
// We could theoretically try to defer the "fold" action, but the current model makes this pretty hard. The obvious workaround is to map a plural subview attribute
// and put all mappings into that. This will guarantee that the "fold" action only happens after all properties have been processed
Map<String, List<String>> collectionMappings = new HashMap<>();
Map<String, List<String>> collectionMappingSingulars = new HashMap<>();
for (AbstractMethodAttribute<? super X, ?> attribute : attributes.values()) {
attribute.checkAttribute(jpaManagedType, context);
for (Map.Entry<String, Boolean> entry : attribute.getCollectionJoinMappings(jpaManagedType, context).entrySet()) {
if (entry.getValue()) {
List<String> locations = collectionMappingSingulars.get(entry.getKey());
if (locations == null) {
locations = new ArrayList<>(2);
collectionMappingSingulars.put(entry.getKey(), locations);
}
locations.add("Attribute '" + attribute.getName() + "' in entity view '" + javaType.getName() + "'");
} else {
List<String> locations = collectionMappings.get(entry.getKey());
if (locations == null) {
locations = new ArrayList<>(2);
collectionMappings.put(entry.getKey(), locations);
}
locations.add("Attribute '" + attribute.getName() + "' in entity view '" + javaType.getName() + "'");
}
}
}
if (!constructorIndex.isEmpty()) {
for (MappingConstructorImpl<X> constructor : constructorIndex.values()) {
Map<String, List<String>> constructorCollectionMappings = new HashMap<>();
for (Map.Entry<String, List<String>> entry : collectionMappings.entrySet()) {
constructorCollectionMappings.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
constructor.checkParameters(jpaManagedType, constructorCollectionMappings, collectionMappingSingulars, context);
reportCollectionMappingErrors(context, collectionMappings, collectionMappingSingulars);
}
} else {
reportCollectionMappingErrors(context, collectionMappings, collectionMappingSingulars);
}
for (ViewRoot viewRoot : viewRoots) {
if (viewRoot.getType() != null) {
String location = "entity view root with the name '" + viewRoot.getName() + "' on type '" + javaType.getName() + "'";
ScalarTargetResolvingExpressionVisitor visitor = new ScalarTargetResolvingExpressionVisitor((ManagedType<?>) viewRoot.getType(), context.getEntityMetamodel(), context.getJpqlFunctions(), viewRootTypes);
String[] fetches = viewRoot.getFetches();
if (fetches.length != 0) {
if (!(viewRoot.getType() instanceof ManagedType<?>)) {
context.addError("Specifying fetches for non-entity attribute type [" + Arrays.toString(fetches) + "] at the " + location + " is not allowed!");
} else {
for (int i = 0; i < fetches.length; i++) {
final String fetch = fetches[i];
final String errorLocation;
if (fetches.length == 1) {
errorLocation = "the fetch expression";
} else {
errorLocation = "the " + (i + 1) + ". fetch expression";
}
visitor.clear();
try {
// Validate the fetch expression parses
context.getExpressionFactory().createPathExpression(fetch).accept(visitor);
} catch (SyntaxErrorException ex) {
try {
context.getExpressionFactory().createSimpleExpression(fetch, false, false, true);
// The used expression is not usable for fetches
context.addError("Invalid fetch expression '" + fetch + "' of the " + location + ". Simplify the fetch expression to a simple path expression. Encountered error: " + ex.getMessage());
} catch (SyntaxErrorException ex2) {
// This is a real syntax error
context.addError("Syntax error in " + errorLocation + " '" + fetch + "' of the " + location + ": " + ex.getMessage());
}
} catch (IllegalArgumentException ex) {
context.addError("An error occurred while trying to resolve the " + errorLocation + " '" + fetch + "' of the " + location + ": " + ex.getMessage());
}
}
}
}
String limitExpression = viewRoot.getLimitExpression();
if (limitExpression != null) {
try {
Expression inItemExpression = context.getTypeValidationExpressionFactory().createInItemExpression(limitExpression);
if (!(inItemExpression instanceof ParameterExpression) && !(inItemExpression instanceof NumericLiteral) || inItemExpression instanceof NumericLiteral && ((NumericLiteral) inItemExpression).getNumericType() != NumericType.INTEGER) {
context.addError("Syntax error in the limit expression '" + limitExpression + "' of the " + location + ": The expression must be a integer literal or a parameter expression");
}
} catch (SyntaxErrorException ex) {
context.addError("Syntax error in the limit expression '" + limitExpression + "' of the " + location + ": " + ex.getMessage());
} catch (IllegalArgumentException ex) {
context.addError("An error occurred while trying to resolve the limit expression '" + limitExpression + "' of the " + location + ": " + ex.getMessage());
}
String offsetExpression = viewRoot.getOffsetExpression();
try {
Expression inItemExpression = context.getTypeValidationExpressionFactory().createInItemExpression(offsetExpression);
if (!(inItemExpression instanceof ParameterExpression) && !(inItemExpression instanceof NumericLiteral) || inItemExpression instanceof NumericLiteral && ((NumericLiteral) inItemExpression).getNumericType() != NumericType.INTEGER) {
context.addError("Syntax error in the offset expression '" + offsetExpression + "' of the " + location + ": The expression must be a integer literal or a parameter expression");
}
} catch (SyntaxErrorException ex) {
context.addError("Syntax error in the offset expression '" + offsetExpression + "' of the " + location + ": " + ex.getMessage());
} catch (IllegalArgumentException ex) {
context.addError("An error occurred while trying to resolve the offset expression '" + offsetExpression + "' of the " + location + ": " + ex.getMessage());
}
List<OrderByItem> orderByItems = viewRoot.getOrderByItems();
for (int i = 0; i < orderByItems.size(); i++) {
OrderByItem orderByItem = orderByItems.get(i);
String expression = orderByItem.getExpression();
try {
visitor.clear();
context.getTypeValidationExpressionFactory().createSimpleExpression(expression, false, false, true).accept(visitor);
} catch (SyntaxErrorException ex) {
context.addError("Syntax error in the " + (i + 1) + "th order by expression '" + expression + "' of the " + location + ": " + ex.getMessage());
} catch (IllegalArgumentException ex) {
context.addError("An error occurred while trying to resolve the " + (i + 1) + "th order by expression '" + expression + "' of the " + location + ": " + ex.getMessage());
}
}
}
CorrelationProviderFactory correlationProviderFactory = viewRoot.getCorrelationProviderFactory();
Predicate correlationPredicate = null;
if (correlationProviderFactory instanceof StaticCorrelationProvider) {
correlationPredicate = ((StaticCorrelationProvider) correlationProviderFactory).getCorrelationPredicate();
} else if (correlationProviderFactory instanceof StaticPathCorrelationProvider) {
correlationPredicate = ((StaticPathCorrelationProvider) correlationProviderFactory).getCorrelationPredicate();
String correlationPath = ((StaticPathCorrelationProvider) correlationProviderFactory).getCorrelationPath();
try {
ScalarTargetResolvingExpressionVisitor correlationPathVisitor = new ScalarTargetResolvingExpressionVisitor(getJpaManagedType(), context.getEntityMetamodel(), context.getJpqlFunctions(), viewRootTypes);
context.getTypeValidationExpressionFactory().createSimpleExpression(correlationPath, false, false, true).accept(correlationPathVisitor);
} catch (SyntaxErrorException ex) {
context.addError("Syntax error in the expression '" + correlationPath + "' of the " + location + ": " + ex.getMessage());
} catch (IllegalArgumentException ex) {
context.addError("An error occurred while trying to resolve the expression '" + correlationPath + "' of the " + location + ": " + ex.getMessage());
}
}
if (correlationPredicate != null) {
try {
visitor.clear();
correlationPredicate.accept(visitor);
} catch (SyntaxErrorException ex) {
context.addError("Syntax error in the condition expression '" + correlationPredicate + "' of the " + location + ": " + ex.getMessage());
} catch (IllegalArgumentException ex) {
context.addError("An error occurred while trying to resolve the condition expression '" + correlationPredicate + "' of the " + location + ": " + ex.getMessage());
}
}
}
}
}
use of com.blazebit.persistence.view.metamodel.ViewRoot in project blaze-persistence by Blazebit.
the class ManagedViewTypeImpl method renderSecondaryMappings.
@Override
public void renderSecondaryMappings(String viewPath, BaseQueryBuilder<?, ?> baseQueryBuilder, Map<String, Object> optionalParameters, boolean renderFetches) {
if (baseQueryBuilder instanceof CTEBuilder) {
CTEBuilder<?> cteBuilder = (CTEBuilder<?>) baseQueryBuilder;
for (CTEProvider cteProvider : getCteProviders()) {
cteProvider.applyCtes(cteBuilder, optionalParameters);
}
}
for (ViewRoot viewRoot : viewRoots) {
String entityViewRootName = viewRoot.getName();
CorrelationProvider correlationProvider = viewRoot.getCorrelationProviderFactory().create(baseQueryBuilder, optionalParameters);
ExpressionFactory expressionFactory = baseQueryBuilder.getService(ExpressionFactory.class);
Limiter limiter = createLimiter(expressionFactory, viewPath, viewRoot.getLimitExpression(), viewRoot.getOffsetExpression(), viewRoot.getOrderByItems());
String correlationAlias;
if (limiter == null) {
correlationAlias = entityViewRootName;
} else {
correlationAlias = "_sub_" + entityViewRootName;
}
JoinCorrelationBuilder correlationBuilder = new JoinCorrelationBuilder(baseQueryBuilder, optionalParameters, baseQueryBuilder, viewPath, correlationAlias, entityViewRootName, null, viewRoot.getJoinType(), limiter);
correlationProvider.applyCorrelation(correlationBuilder, viewPath);
correlationBuilder.finish();
if (renderFetches && baseQueryBuilder instanceof FetchBuilder<?>) {
((FetchBuilder<?>) baseQueryBuilder).fetch(viewRoot.getFetches());
}
}
}
Aggregations