Skip to content

Commit

Permalink
Slight rsql code improvements (#1840)
Browse files Browse the repository at this point in the history
* Slight RSQL code improvements(2)

Signed-off-by: Marinov Avgustin <[email protected]>

* Generics from Path<Object> to Path<?>

Signed-off-by: Marinov Avgustin <[email protected]>

---------

Signed-off-by: Marinov Avgustin <[email protected]>
  • Loading branch information
avgustinmm authored Sep 10, 2024
1 parent f64d6eb commit b1f9991
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class JpaQueryRsqlVisitorG2<A extends Enum<A> & RsqlQueryField, T>
private final SimpleTypeConverter simpleTypeConverter = new SimpleTypeConverter();

private boolean inOr;
private final Map<Class<?>, Path<Object>> javaTypeToPath = new HashMap<>();
private final Map<Class<?>, Path<?>> javaTypeToPath = new HashMap<>();

public JpaQueryRsqlVisitorG2(final Class<A> enumType,
final Root<T> root, final CriteriaQuery<?> query, final CriteriaBuilder cb,
Expand Down Expand Up @@ -112,7 +112,7 @@ public List<Predicate> visit(final ComparisonNode node, final String param) {

final List<String> values = node.getArguments();
final List<Object> transformedValues = new ArrayList<>();
final Path<Object> fieldPath = getFieldPath(root, queryField);
final Path<?> fieldPath = getFieldPath(root, queryField);

for (final String value : values) {
transformedValues.add(convertValueIfNecessary(node, queryField.getEnumValue(), fieldPath, value));
Expand All @@ -122,7 +122,7 @@ public List<Predicate> visit(final ComparisonNode node, final String param) {
}

private List<Predicate> mapToPredicate(final ComparisonNode node, final QuertPath queryField,
final Path<Object> fieldPath,
final Path<?> fieldPath,
final List<String> values, final List<Object> transformedValues) {
// if lookup is available, replace macros ...
final String value = virtualPropertyReplacer == null ? values.get(0) : virtualPropertyReplacer.replace(values.get(0));
Expand All @@ -133,15 +133,16 @@ private List<Predicate> mapToPredicate(final ComparisonNode node, final QuertPat

return Collections.singletonList(mapPredicate != null ? cb.and(mapPredicate, valuePredicate) : valuePredicate);
}
private Path<Object> getValueFieldPath(final A enumField, final Path<Object> fieldPath) {
private Path<?> getValueFieldPath(final A enumField, final Path<?> fieldPath) {
if (enumField.isMap()) {
return enumField.getSubEntityMapTuple().map(Entry::getValue).map(fieldPath::get).orElse(fieldPath);
final Path<?> mapValuePath = enumField.getSubEntityMapTuple().map(Entry::getValue).map(fieldPath::get).orElse(null);
return mapValuePath == null ? fieldPath : mapValuePath;
} else {
return fieldPath;
}
}
@SuppressWarnings("unchecked")
private Predicate mapToMapPredicate(final QuertPath queryField, final Path<Object> fieldPath) {
private Predicate mapToMapPredicate(final QuertPath queryField, final Path<?> fieldPath) {
final String[] graph = queryField.getJpaPath();
final String keyValue = graph[graph.length - 1];
if (fieldPath instanceof MapJoin) {
Expand All @@ -156,7 +157,7 @@ private Predicate mapToMapPredicate(final QuertPath queryField, final Path<Objec
return equal(fieldPath.get(keyFieldName), keyValue);
}
private Predicate addOperatorPredicate(final ComparisonNode node, final QuertPath queryField,
final Path<Object> fieldPath, final List<Object> transformedValues, final String value) {
final Path<?> fieldPath, final List<Object> transformedValues, final String value) {
// only 'equal' and 'notEqual' can handle transformed value like enums.
// The JPA API cannot handle object types for greaterThan etc. methods.
final Object transformedValue = transformedValues.get(0);
Expand All @@ -174,7 +175,7 @@ private Predicate addOperatorPredicate(final ComparisonNode node, final QuertPat
"Operator symbol {" + operator + "} is either not supported or not implemented");
};
}
private Predicate getEqualToPredicate(final Path<Object> fieldPath, final Object transformedValue) {
private Predicate getEqualToPredicate(final Path<?> fieldPath, final Object transformedValue) {
if (transformedValue == null) {
return cb.isNull(fieldPath);
}
Expand All @@ -184,18 +185,19 @@ private Predicate getEqualToPredicate(final Path<Object> fieldPath, final Object
return cb.or(cb.isNull(fieldPath), cb.equal(pathOfString(fieldPath), ""));
}

final Expression<String> stringExpression = pathOfString(fieldPath);
if (isPattern(transformedValueStr)) { // a pattern, use like
return like(pathOfString(fieldPath), toSQL(transformedValueStr));
return like(stringExpression, toSQL(transformedValueStr));
} else {
return equal(pathOfString(fieldPath), transformedValueStr);
return equal(stringExpression, transformedValueStr);
}
}

return cb.equal(fieldPath, transformedValue);
}

private Predicate getNotEqualToPredicate(final QuertPath queryField,
final Path<Object> fieldPath, final Object transformedValue) {
final Path<?> fieldPath, final Object transformedValue) {
if (transformedValue == null) {
return cb.isNotNull(fieldPath);
}
Expand Down Expand Up @@ -224,7 +226,7 @@ private Predicate getNotEqualToPredicate(final QuertPath queryField,
return toNullOrNotEqualPredicate(fieldPath, transformedValue);
}

private Predicate getOutPredicate(final QuertPath queryField, final Path<Object> fieldPath,
private Predicate getOutPredicate(final QuertPath queryField, final Path<?> fieldPath,
final List<Object> transformedValues) {
final String[] subAttributes = queryField.getJpaPath();

Expand All @@ -235,9 +237,9 @@ private Predicate getOutPredicate(final QuertPath queryField, final Path<Object>
return toNotExistsSubQueryPredicate(queryField, fieldPath, expressionToCompare -> in(expressionToCompare, transformedValues));
}

private Path<Object> getFieldPath(final Root<?> root, final QuertPath queryField) {
private Path<?> getFieldPath(final Root<?> root, final QuertPath queryField) {
final String[] split = queryField.getJpaPath();
Path<Object> fieldPath = null;
Path<?> fieldPath = null;
for (int i = 0, end = queryField.getEnumValue().isMap() ? split.length - 1 : split.length; i < end; i++) {
final String fieldNameSplit = split[i];
fieldPath = fieldPath == null ? getPath(root, fieldNameSplit) : fieldPath.get(fieldNameSplit);
Expand All @@ -251,7 +253,7 @@ private Path<Object> getFieldPath(final Root<?> root, final QuertPath queryField
// to include rows for missing in particular table / criteria (root.get creates INNER JOIN)
// (see org.eclipse.persistence.internal.jpa.querydef.FromImpl implementation for more details)
// otherwise delegate to root.get
private Path<Object> getPath(final Root<?> root, final String fieldNameSplit) {
private Path<?> getPath(final Root<?> root, final String fieldNameSplit) {
// see org.eclipse.persistence.internal.jpa.querydef.FromImpl implementation for more details
// when root.get creates a join
final Attribute<?, ?> attribute = root.getModel().getAttribute(fieldNameSplit);
Expand All @@ -269,7 +271,7 @@ private Path<Object> getPath(final Root<?> root, final String fieldNameSplit) {
}

private Object convertValueIfNecessary(
final ComparisonNode node, final A fieldName, final Path<Object> fieldPath, final String value) {
final ComparisonNode node, final A fieldName, final Path<?> fieldPath, final String value) {
// in case the value of an RSQL query e.g. type==application is an
// enum we need to handle it separately because JPA needs the
// correct java-type to build an expression. So String and numeric
Expand Down Expand Up @@ -331,7 +333,7 @@ private Object convertFieldConverterValue(final ComparisonNode node, final A fie
}
}

private Predicate toNullOrNotEqualPredicate(final Path<Object> fieldPath, final Object transformedValue) {
private Predicate toNullOrNotEqualPredicate(final Path<?> fieldPath, final Object transformedValue) {
return cb.or(
cb.isNull(fieldPath),
transformedValue instanceof String transformedValueStr
Expand All @@ -340,7 +342,7 @@ private Predicate toNullOrNotEqualPredicate(final Path<Object> fieldPath, final
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private Predicate toNotExistsSubQueryPredicate(final QuertPath queryField, final Path<Object> fieldPath, final Function<Expression<String>, Predicate> subQueryPredicateProvider) {
private Predicate toNotExistsSubQueryPredicate(final QuertPath queryField, final Path<?> fieldPath, final Function<Expression<String>, Predicate> subQueryPredicateProvider) {
// if a subquery the field's parent joins are not actually used
if (!inOr) {
// so, if not in or (hence not reused) we remove them. Parent shall be a Join
Expand All @@ -350,13 +352,13 @@ private Predicate toNotExistsSubQueryPredicate(final QuertPath queryField, final
final Class<?> javaType = root.getJavaType();
final Subquery<?> subquery = query.subquery(javaType);
final Root subqueryRoot = subquery.from(javaType);
final Predicate equalPredicate = cb.equal(root.get(queryField.getEnumValue().identifierFieldName()),
subqueryRoot.get(queryField.getEnumValue().identifierFieldName()));
final Expression<String> expressionToCompare = getExpressionToCompare(queryField.getEnumValue(),
getFieldPath(subqueryRoot, queryField));
final Predicate subQueryPredicate = subQueryPredicateProvider.apply(expressionToCompare);
subquery.select(subqueryRoot).where(cb.and(equalPredicate, subQueryPredicate));
return cb.not(cb.exists(subquery));
return cb.not(cb.exists(
subquery.select(subqueryRoot)
.where(cb.and(
cb.equal(root.get(queryField.getEnumValue().identifierFieldName()),
subqueryRoot.get(queryField.getEnumValue().identifierFieldName())),
subQueryPredicateProvider.apply(getExpressionToCompare(queryField.getEnumValue(),
getFieldPath(subqueryRoot, queryField)))))));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private Expression<String> getExpressionToCompare(final A enumField, final Path fieldPath) {
Expand All @@ -367,10 +369,11 @@ private Expression<String> getExpressionToCompare(final A enumField, final Path
// Currently we support only string key. So below cast is safe.
return (Expression<String>) (((MapJoin<?, ?, ?>) fieldPath).value());
}
final String valueFieldName = enumField.getSubEntityMapTuple().map(Entry::getValue)
return enumField.getSubEntityMapTuple()
.map(Entry::getValue)
.map(valueFieldName -> fieldPath.<String>get(valueFieldName))
.orElseThrow(() -> new UnsupportedOperationException(
"For the fields, defined as Map, only Map java type or tuple in the form of SimpleImmutableEntry are allowed. Neither of those could be found!"));
return pathOfString(fieldPath).get(valueFieldName);
}

private String toSQL(final String transformedValue) {
Expand Down Expand Up @@ -436,8 +439,8 @@ private static boolean isSimpleField(final String[] split, final boolean isMapKe
}

@SuppressWarnings("unchecked")
private static <Y> Path<Y> pathOfString(final Path<?> path) {
return (Path<Y>) path;
private static Path<String> pathOfString(final Path<?> path) {
return (Path<String>) path;
}

private static boolean isPattern(final String transformedValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ public void correctRsqlBuildsNotSimpleNotLikePredicate() {

when(subqueryMock.from(SoftwareModule.class)).thenReturn(subqueryRootMock);
when(subqueryMock.select(subqueryRootMock)).thenReturn(subqueryMock);
when(subqueryMock.where(any(Expression.class))).thenReturn(subqueryMock);

// test
RSQLUtility.buildRsqlSpecification(correctRsql, SoftwareModuleFields.class, null, testDb)
Expand Down

0 comments on commit b1f9991

Please sign in to comment.