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 37f30867f28..c54d7fa5873 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,9 @@ private Predicate buildPredicate(PrimitiveStatement ps) { } @SuppressWarnings({ "unchecked", "rawtypes" }) - private Predicate doBuildPredicate(ConditionType ct, Path path, Class valueClazz, Object value) { + protected Predicate doBuildPredicate(final ConditionType ct, + final Path path, final Class valueClazz, + final Object value) { Class clazz = (Class)valueClazz; Expression exp = path.as(clazz); @@ -246,7 +248,7 @@ private Predicate doBuildPredicate(ConditionType ct, Path path, Class valu } @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 exp = builder.size((Expression)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 6dd245c6585..5cb3cb29dac 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,6 +173,10 @@ public void tearDown() throws Exception { } } + protected EntityManager getEntityManager() { + return em; + } + protected List queryBooks(String expression) throws Exception { 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 00000000000..c5c4ab1939f --- /dev/null +++ b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/CustomJPACriteriaVisitor.java @@ -0,0 +1,50 @@ +/** + * 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 org.apache.cxf.jaxrs.ext.search.ConditionType; +import org.apache.cxf.jaxrs.ext.search.collections.CollectionCheckInfo; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.criteria.Path; +import jakarta.persistence.criteria.Predicate; + +public class CustomJPACriteriaVisitor extends JPACriteriaQueryVisitor { + + boolean customPredicateUsed = false; + + CustomJPACriteriaVisitor(EntityManager em) { + super(em, Book.class, Book.class); + } + + @Override + protected Predicate doBuildPredicate(ConditionType ct, Path path, Class valueClazz, Object value) { + + if ("bookTitle".equals(path.getAlias())) { + return getCriteriaBuilder().like(path.as(String.class), "%" + value + "%"); + } + + return super.doBuildPredicate(ct, path, valueClazz, value); + } + + @Override + protected Predicate doBuildCollectionPredicate(ConditionType ct, Path path, CollectionCheckInfo collInfo) { + return getCriteriaBuilder().disjunction(); + } +} 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 00000000000..e2533b42e59 --- /dev/null +++ b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitorExtensionTest.java @@ -0,0 +1,76 @@ +/** + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +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 jakarta.persistence.criteria.CriteriaQuery; + +public class JPACriteriaQueryVisitorExtensionTest extends AbstractJPATypedQueryVisitorTest { + + @Test + public void testCustomPredicateExtensionIsUsed() throws Exception { + SearchCondition filter = getParser().parse("bookTitle==NUM9"); + + JPACriteriaQueryVisitor visitor = new CustomJPACriteriaVisitor(getEntityManager()); + + filter.accept(visitor); + + List result = getEntityManager().createQuery(visitor.getQuery()).getResultList(); + + assertEquals(1, result.size()); + } + + @Test + public void testCollectionPredicateOverrideIsUsed() throws Exception { + SearchCondition filter = getParser().parse("authors==John"); + + JPACriteriaQueryVisitor visitor = new CustomJPACriteriaVisitor(getEntityManager()); + + filter.accept(visitor); + + CriteriaQuery query = visitor.getQuery(); + List results = getEntityManager().createQuery(query).getResultList(); + + // Without override -> 2 results + // With override -> 0 results + assertTrue(results.isEmpty()); + } + + @Override + protected SearchConditionParser getParser() { + return new FiqlParser<>(Book.class); + } + + @Override + protected SearchConditionParser getParser(Map visitorProps, + Map parserBinProps) { + return new FiqlParser<>(Book.class, parserBinProps); + } + +}