Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CompoundSelection;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.SingularAttribute;
public class JPACriteriaQueryVisitor extends AbstractJPATypedQueryVisitor> {
public JPACriteriaQueryVisitor(EntityManager em,
Class tClass,
Class queryClass) {
this(em, tClass, queryClass, null, null);
}
public JPACriteriaQueryVisitor(EntityManager em,
Class tClass,
Class queryClass,
List joinProps) {
this(em, tClass, queryClass, null, null);
}
public JPACriteriaQueryVisitor(EntityManager em,
Class tClass,
Class queryClass,
Map fieldMap) {
super(em, tClass, queryClass, fieldMap);
}
public JPACriteriaQueryVisitor(EntityManager em,
Class tClass,
Class queryClass,
Map fieldMap,
List joinProps) {
super(em, tClass, queryClass, fieldMap, joinProps);
}
public CriteriaQuery getQuery() {
return getCriteriaQuery();
}
public Long count() {
if (super.getQueryClass() != Long.class) {
throw new IllegalStateException("Query class needs to be of type Long");
}
@SuppressWarnings("unchecked")
CriteriaQuery countQuery = (CriteriaQuery)getCriteriaQuery();
countQuery.select(getCriteriaBuilder().count(getRoot()));
return super.getEntityManager().createQuery(countQuery).getSingleResult();
}
public TypedQuery getOrderedTypedQuery(List> attributes, boolean asc) {
CriteriaQuery cQuery = orderBy(attributes, asc);
return getTypedQuery(cQuery);
}
public CriteriaQuery orderBy(List> attributes, boolean asc) {
CriteriaBuilder cb = getCriteriaBuilder();
List orders = new ArrayList();
for (SingularAttribute attribute : attributes) {
Path selection = getRoot().get(attribute);
Order order = asc ? cb.asc(selection) : cb.desc(selection);
orders.add(order);
}
return getCriteriaQuery().orderBy(orders);
}
public TypedQuery getArrayTypedQuery(List> attributes) {
CriteriaQuery cQuery = selectArraySelections(toSelectionsArray(toSelectionsList(attributes, false)));
return getTypedQuery(cQuery);
}
public CriteriaQuery selectArray(List> attributes) {
return selectArraySelections(toSelectionsArray(toSelectionsList(attributes, false)));
}
private CriteriaQuery selectArraySelections(Selection... selections) {
@SuppressWarnings("unchecked")
CompoundSelection selection = (CompoundSelection)getCriteriaBuilder().array(selections);
getQuery().select(selection);
return getQuery();
}
public CriteriaQuery selectConstruct(List> attributes) {
return selectConstructSelections(toSelectionsArray(toSelectionsList(attributes, false)));
}
public TypedQuery getConstructTypedQuery(List> attributes) {
CriteriaQuery cQuery = selectConstructSelections(toSelectionsArray(toSelectionsList(attributes, false)));
return getTypedQuery(cQuery);
}
private CriteriaQuery selectConstructSelections(Selection... selections) {
getQuery().select(getCriteriaBuilder().construct(getQueryClass(), selections));
return getQuery();
}
public CriteriaQuery selectTuple(List> attributes) {
return selectTupleSelections(toSelectionsArray(toSelectionsList(attributes, true)));
}
public TypedQuery getTupleTypedQuery(List> attributes) {
CriteriaQuery cQuery = selectTupleSelections(toSelectionsArray(toSelectionsList(attributes, true)));
return getTypedQuery(cQuery);
}
private CriteriaQuery selectTupleSelections(Selection... selections) {
@SuppressWarnings("unchecked")
CompoundSelection selection =
(CompoundSelection)getCriteriaBuilder().tuple(selections);
getQuery().select(selection);
return getQuery();
}
private List> toSelectionsList(List> attributes, boolean setAlias) {
List> selections = new ArrayList>(attributes.size());
for (SingularAttribute attr : attributes) {
Path path = getRoot().get(attr);
path.alias(attr.getName());
selections.add(path);
}
return selections;
}
private static Selection[] toSelectionsArray(List> selections) {
return selections.toArray(new Selection[]{});
}
private TypedQuery getTypedQuery(CriteriaQuery theCriteriaQuery) {
return super.getEntityManager().createQuery(theCriteriaQuery);
}
}