Skip to content
Permalink
Browse files
[CXF-5430]: Added initial support for OData 2.0 query language. Cover…
…ed most of advanced use cases (6 test cases to fix)

git-svn-id: https://svn.apache.org/repos/asf/cxf/trunk@1567829 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Andriy Redko committed Feb 13, 2014
1 parent 5a140bb commit 1429dd09caa3d0e0637669dbbf2b512f4c8bed24
Show file tree
Hide file tree
Showing 5 changed files with 971 additions and 607 deletions.
@@ -31,6 +31,8 @@
import org.apache.cxf.jaxrs.ext.search.PrimitiveSearchCondition;
import org.apache.cxf.jaxrs.ext.search.SearchCondition;
import org.apache.cxf.jaxrs.ext.search.SearchParseException;
import org.apache.cxf.jaxrs.ext.search.collections.CollectionCheckCondition;
import org.apache.cxf.jaxrs.ext.search.collections.CollectionCheckInfo;
import org.apache.olingo.odata2.api.edm.EdmLiteral;
import org.apache.olingo.odata2.api.edm.EdmLiteralKind;
import org.apache.olingo.odata2.api.edm.EdmSimpleType;
@@ -166,6 +168,12 @@ public Object visitBinary(BinaryExpression binaryExpression, BinaryOperator oper
typedValue = parseType(property.propertyName, null, null, property.propertyName,
property.typeInfo, value.literal);
}

final CollectionCheckInfo checkInfo = property.typeInfo.getCollectionCheckInfo();
if (checkInfo != null) {
return new CollectionCheckCondition< T >(property.propertyName, typedValue,
property.typeInfo.getGenericType(), conditionType, condition, checkInfo);
}

return new PrimitiveSearchCondition< T >(property.propertyName,
typedValue, conditionType, condition);
@@ -92,7 +92,7 @@ private ClassValue doGetPrimitiveFieldClass(PrimitiveStatement ps,
valueCls = value.getClass();
type = m.getGenericReturnType();
} catch (Throwable ex) {
throw new RuntimeException();
throw new RuntimeException(ex);
}
return doGetPrimitiveFieldClass(ps, name, valueCls, type, value, set);
}
@@ -0,0 +1,304 @@
/**
* 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.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.cxf.jaxrs.ext.search.SearchCondition;
import org.apache.cxf.jaxrs.ext.search.SearchConditionParser;
import org.apache.cxf.jaxrs.ext.search.SearchConditionVisitor;
import org.apache.cxf.jaxrs.ext.search.SearchUtils;
import org.apache.cxf.jaxrs.ext.search.jpa.BookReview.Review;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

public abstract class AbstractJPATypedQueryVisitorTest extends Assert {

private EntityManagerFactory emFactory;

private EntityManager em;

private Connection connection;

protected abstract SearchConditionParser<Book> getParser();
protected abstract SearchConditionParser<Book> getParser(Map<String, String> visitorProps,
Map<String, String> parserBinProps);

@Before
public void setUp() throws Exception {
try {
Class.forName("org.hsqldb.jdbcDriver");
connection = DriverManager.getConnection("jdbc:hsqldb:mem:books-jpa", "sa", "");
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception during HSQL database init.");
}
try {
emFactory = Persistence.createEntityManagerFactory("testUnitHibernate");
em = emFactory.createEntityManager();

em.getTransaction().begin();

Library lib = new Library();
lib.setId(1);
lib.setAddress("town");
em.persist(lib);
assertTrue(em.contains(lib));

BookReview br1 = new BookReview();
br1.setId(1);
br1.setReview(Review.BAD);
br1.getAuthors().add("Ted");
em.persist(br1);

Book b1 = new Book();

br1.setBook(b1);
b1.getReviews().add(br1);


b1.setId(9);
b1.setBookTitle("num9");
b1.setAddress(new OwnerAddress("Street1"));
OwnerInfo info1 = new OwnerInfo();
info1.setName(new Name("Fred"));
info1.setDateOfBirth(parseDate("2000-01-01"));
b1.setOwnerInfo(info1);
b1.setLibrary(lib);
b1.getAuthors().add("John");
em.persist(b1);
assertTrue(em.contains(b1));

BookReview br2 = new BookReview();
br2.setId(2);
br2.setReview(Review.GOOD);
br2.getAuthors().add("Ted");
em.persist(br2);

Book b2 = new Book();
b2.getReviews().add(br2);
br2.setBook(b2);

b2.setId(10);
b2.setBookTitle("num10");
b2.setAddress(new OwnerAddress("Street2"));
OwnerInfo info2 = new OwnerInfo();
info2.setName(new Name("Barry"));
info2.setDateOfBirth(parseDate("2001-01-01"));
b2.setOwnerInfo(info2);
b2.setLibrary(lib);
b2.getAuthors().add("John");
em.persist(b2);
assertTrue(em.contains(b2));

BookReview br3 = new BookReview();
br3.setId(3);
br3.setReview(Review.GOOD);
br3.getAuthors().add("Ted");
em.persist(br3);

Book b3 = new Book();
b3.getReviews().add(br3);
br3.setBook(b3);
b3.setId(11);
b3.setBookTitle("num11");
b3.setAddress(new OwnerAddress("Street3"));
b3.getAuthors().add("Barry");
OwnerInfo info3 = new OwnerInfo();
info3.setName(new Name("Bill"));
info3.setDateOfBirth(parseDate("2002-01-01"));
b3.setOwnerInfo(info3);
b3.setLibrary(lib);
em.persist(b3);
assertTrue(em.contains(b3));

em.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception during JPA EntityManager creation.");
}
}

@After
public void tearDown() throws Exception {
try {
if (em != null) {
em.close();
}
if (emFactory != null) {
emFactory.close();
}
} catch (Throwable ex) {
ex.printStackTrace();
} finally {
try {
connection.createStatement().execute("SHUTDOWN");
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}



protected List<Book> queryBooks(String expression) throws Exception {
return queryBooks(expression, null, null, null);
}

protected List<Book> queryBooks(String expression,
Map<String, String> visitorProps) throws Exception {
return queryBooks(expression, visitorProps, null, null);
}

protected List<Book> queryBooks(String expression,
Map<String, String> visitorProps,
Map<String, String> parserBinProps) throws Exception {
return queryBooks(expression, visitorProps, parserBinProps, null);
}

protected List<Book> queryBooks(String expression,
Map<String, String> visitorProps,
Map<String, String> parserBinProps,
List<String> joinProps) throws Exception {
SearchCondition<Book> filter = getParser(visitorProps, parserBinProps)
.parse(expression);
SearchConditionVisitor<Book, TypedQuery<Book>> jpa =
new JPATypedQueryVisitor<Book>(em, Book.class, visitorProps, joinProps);
filter.accept(jpa);
TypedQuery<Book> query = jpa.getQuery();
return query.getResultList();
}

protected List<Tuple> criteriaQueryBooksTuple(String expression) throws Exception {
SearchCondition<Book> filter = getParser().parse(expression);
JPACriteriaQueryVisitor<Book, Tuple> jpa =
new JPACriteriaQueryVisitor<Book, Tuple>(em, Book.class, Tuple.class);
filter.accept(jpa);

List<SingularAttribute<Book, ?>> selections =
new ArrayList<SingularAttribute<Book, ?>>();
selections.add(Book_.id);

jpa.selectTuple(selections);

CriteriaQuery<Tuple> cquery = jpa.getQuery();
return em.createQuery(cquery).getResultList();
}

protected long criteriaQueryBooksCount(String expression) throws Exception {
SearchCondition<Book> filter = getParser().parse(expression);
JPACriteriaQueryVisitor<Book, Long> jpa =
new JPACriteriaQueryVisitor<Book, Long>(em, Book.class, Long.class);
filter.accept(jpa);
return jpa.count();
}

protected List<Book> criteriaQueryBooksOrderBy(String expression, boolean asc) throws Exception {
SearchCondition<Book> filter = getParser().parse(expression);
JPACriteriaQueryVisitor<Book, Book> jpa =
new JPACriteriaQueryVisitor<Book, Book>(em, Book.class, Book.class);
filter.accept(jpa);

List<SingularAttribute<Book, ?>> selections =
new ArrayList<SingularAttribute<Book, ?>>();
selections.add(Book_.id);

return jpa.getOrderedTypedQuery(selections, asc).getResultList();
}

protected List<BookInfo> criteriaQueryBooksConstruct(String expression) throws Exception {
SearchCondition<Book> filter = getParser().parse(expression);
JPACriteriaQueryVisitor<Book, BookInfo> jpa =
new JPACriteriaQueryVisitor<Book, BookInfo>(em, Book.class, BookInfo.class);
filter.accept(jpa);

List<SingularAttribute<Book, ?>> selections =
new ArrayList<SingularAttribute<Book, ?>>();
selections.add(Book_.id);
selections.add(Book_.bookTitle);

jpa.selectConstruct(selections);

CriteriaQuery<BookInfo> cquery = jpa.getQuery();
return em.createQuery(cquery).getResultList();
}

protected List<Object[]> criteriaQueryBooksArray(String expression) throws Exception {
SearchCondition<Book> filter = getParser().parse(expression);
JPACriteriaQueryVisitor<Book, Object[]> jpa =
new JPACriteriaQueryVisitor<Book, Object[]>(em, Book.class, Object[].class);
filter.accept(jpa);

List<SingularAttribute<Book, ?>> selections =
new ArrayList<SingularAttribute<Book, ?>>();
selections.add(Book_.id);
selections.add(Book_.bookTitle);

return jpa.getArrayTypedQuery(selections).getResultList();
}

protected Date parseDate(String value) throws Exception {
return new SimpleDateFormat(SearchUtils.DEFAULT_DATE_FORMAT).parse(value);
}

public static class BookInfo {
private int id;
private String title;

public BookInfo() {

}

public BookInfo(Integer id, String title) {
this.id = id;
this.title = title;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
}

0 comments on commit 1429dd0

Please sign in to comment.