Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Comparable> clazz = (Class<? extends Comparable>)valueClazz;
Expression<? extends Comparable> exp = path.as(clazz);
Expand Down Expand Up @@ -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<Integer> exp = builder.size((Expression<? extends Collection>)path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ public void tearDown() throws Exception {
}
}

protected EntityManager getEntityManager() {
return em;
}



protected List<Book> queryBooks(String expression) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Book, Book> {

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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Book> filter = getParser().parse("bookTitle==NUM9");

JPACriteriaQueryVisitor<Book, Book> visitor = new CustomJPACriteriaVisitor(getEntityManager());

filter.accept(visitor);

List<Book> result = getEntityManager().createQuery(visitor.getQuery()).getResultList();

assertEquals(1, result.size());
}

@Test
public void testCollectionPredicateOverrideIsUsed() throws Exception {
SearchCondition<Book> filter = getParser().parse("authors==John");

JPACriteriaQueryVisitor<Book, Book> visitor = new CustomJPACriteriaVisitor(getEntityManager());

filter.accept(visitor);

CriteriaQuery<Book> query = visitor.getQuery();
List<Book> results = getEntityManager().createQuery(query).getResultList();

// Without override -> 2 results
// With override -> 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);
}

}
Loading