This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new 95a5856dc2 CXF-9174: Make doBuildPredicate and
doBuildCollectionPredicate protected for extensibility (#2798)
95a5856dc2 is described below
commit 95a5856dc223777f8c4390b8379277a605b008a2
Author: Raoua <[email protected]>
AuthorDate: Wed Mar 11 13:30:11 2026 +0100
CXF-9174: Make doBuildPredicate and doBuildCollectionPredicate protected
for extensibility (#2798)
Allow subclasses of AbstractJPATypedQueryVisitor to customize JPA predicate
building by changing doBuildPredicate() and doBuildCollectionPredicate()
from
private to protected visibility. This enables the Template Method pattern
for
custom FIQL-to-JPA predicate generation without modifying the core visitor.
Includes tests demonstrating a custom visitor with case-insensitive LIKE
matching and collection predicate override.
Co-authored-by: Guillaume Nodet <[email protected]>
---
.../search/jpa/AbstractJPATypedQueryVisitor.java | 4 +-
.../jpa/AbstractJPATypedQueryVisitorTest.java | 4 +-
.../ext/search/jpa/CustomJPACriteriaVisitor.java | 59 +++++++++++++++++
.../jpa/JPACriteriaQueryVisitorExtensionTest.java | 77 ++++++++++++++++++++++
4 files changed, 141 insertions(+), 3 deletions(-)
diff --git
a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
index 37f30867f2..954e559bc4 100644
---
a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
+++
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
@@ -184,7 +184,7 @@ public abstract class AbstractJPATypedQueryVisitor<T, T1, E>
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- private Predicate doBuildPredicate(ConditionType ct, Path<?> path,
Class<?> valueClazz, Object value) {
+ protected Predicate doBuildPredicate(ConditionType ct, Path<?> path,
Class<?> valueClazz, Object value) {
Class<? extends Comparable> clazz = (Class<? extends
Comparable>)valueClazz;
Expression<? extends Comparable> exp = path.as(clazz);
@@ -246,7 +246,7 @@ public abstract class AbstractJPATypedQueryVisitor<T, T1, E>
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- private Predicate doBuildCollectionPredicate(ConditionType ct, Path<?>
path, CollectionCheckInfo collInfo) {
+ protected Predicate doBuildCollectionPredicate(ConditionType ct, Path<?>
path, CollectionCheckInfo collInfo) {
Predicate pred = null;
Expression<Integer> exp = builder.size((Expression<? extends
Collection>)path);
diff --git
a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitorTest.java
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitorTest.java
index 6dd245c658..07d67636e2 100644
---
a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitorTest.java
+++
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitorTest.java
@@ -173,7 +173,9 @@ public abstract class AbstractJPATypedQueryVisitorTest {
}
}
-
+ protected EntityManager getEntityManager() {
+ return em;
+ }
protected List<Book> queryBooks(String expression) throws Exception {
return queryBooks(expression, null, null, null);
diff --git
a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/CustomJPACriteriaVisitor.java
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/CustomJPACriteriaVisitor.java
new file mode 100644
index 0000000000..5285f0307b
--- /dev/null
+++
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/CustomJPACriteriaVisitor.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.ext.search.jpa;
+
+import jakarta.persistence.EntityManager;
+import jakarta.persistence.criteria.Path;
+import jakarta.persistence.criteria.Predicate;
+import jakarta.persistence.metamodel.Attribute;
+import jakarta.persistence.metamodel.Bindable;
+import org.apache.cxf.jaxrs.ext.search.ConditionType;
+import org.apache.cxf.jaxrs.ext.search.collections.CollectionCheckInfo;
+
+public class CustomJPACriteriaVisitor extends JPACriteriaQueryVisitor<Book,
Book> {
+
+ CustomJPACriteriaVisitor(EntityManager em) {
+ super(em, Book.class, Book.class);
+ }
+
+ @Override
+ protected Predicate doBuildPredicate(ConditionType ct, Path<?> path,
Class<?> valueClazz, Object value) {
+ String name = getAttributeName(path);
+ if ("bookTitle".equals(name)) {
+ return getCriteriaBuilder().like(
+ getCriteriaBuilder().lower(path.as(String.class)),
+ "%" + value.toString().toLowerCase() + "%");
+ }
+
+ return super.doBuildPredicate(ct, path, valueClazz, value);
+ }
+
+ @Override
+ protected Predicate doBuildCollectionPredicate(ConditionType ct, Path<?>
path, CollectionCheckInfo collInfo) {
+ return getCriteriaBuilder().disjunction();
+ }
+
+ private String getAttributeName(Path<?> path) {
+ Bindable<?> model = path.getModel();
+ if (model instanceof Attribute) {
+ return ((Attribute<?, ?>)model).getName();
+ }
+ return null;
+ }
+}
diff --git
a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitorExtensionTest.java
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitorExtensionTest.java
new file mode 100644
index 0000000000..81bf1890a8
--- /dev/null
+++
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitorExtensionTest.java
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.ext.search.jpa;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+import org.apache.cxf.jaxrs.ext.search.SearchConditionParser;
+import org.apache.cxf.jaxrs.ext.search.fiql.FiqlParser;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class JPACriteriaQueryVisitorExtensionTest extends
AbstractJPATypedQueryVisitorTest {
+
+ @Test
+ public void testCustomPredicateExtensionIsUsed() throws Exception {
+ SearchCondition<Book> filter = getParser().parse("bookTitle==NUM9");
+
+ JPACriteriaQueryVisitor<Book, Book> visitor = new
CustomJPACriteriaVisitor(getEntityManager());
+
+ filter.accept(visitor);
+
+ List<Book> result =
getEntityManager().createQuery(visitor.getQuery()).getResultList();
+
+ // The custom visitor uses a case-insensitive LIKE query for bookTitle,
+ // so "NUM9" matches "num9"
+ assertEquals(1, result.size());
+ }
+
+ @Test
+ public void testCollectionPredicateOverrideIsUsed() throws Exception {
+ // count(authors) triggers the collection predicate path
+ SearchCondition<Book> filter = getParser().parse("count(authors)==1");
+
+ JPACriteriaQueryVisitor<Book, Book> visitor = new
CustomJPACriteriaVisitor(getEntityManager());
+
+ filter.accept(visitor);
+
+ List<Book> results =
getEntityManager().createQuery(visitor.getQuery()).getResultList();
+
+ // Without override -> books with exactly 1 author would be returned
+ // With override (disjunction) -> 0 results
+ assertTrue(results.isEmpty());
+ }
+
+ @Override
+ protected SearchConditionParser<Book> getParser() {
+ return new FiqlParser<>(Book.class);
+ }
+
+ @Override
+ protected SearchConditionParser<Book> getParser(Map<String, String>
visitorProps,
+ Map<String, String> parserBinProps) {
+ return new FiqlParser<>(Book.class, parserBinProps);
+ }
+
+}