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.
/*
* The MIT License
*
* Copyright 2013 Jakub Jirutka .
* Copyright 2015 Antonio Rabelo.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.github.tennaito.rsql.jpa;
import com.github.tennaito.rsql.builder.BuilderTools;
import com.github.tennaito.rsql.parser.ast.ComparisonOperatorProxy;
import cz.jirutka.rsql.parser.ast.ComparisonNode;
import cz.jirutka.rsql.parser.ast.ComparisonOperator;
import cz.jirutka.rsql.parser.ast.LogicalNode;
import cz.jirutka.rsql.parser.ast.Node;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Attribute.PersistentAttributeType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.PluralAttribute;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* PredicateBuilder
*
* Classe with utility methods for Predicate creation from RSQL AST nodes.
*
* @author AntonioRabelo
*
* Based from CriterionBuilders of rsql-hibernate created by Jakub Jirutka .
*
* @since 2015-02-05
*/
public final class PredicateBuilder {
private static final Logger LOG = Logger.getLogger(PredicateBuilder.class.getName());
public static final Character LIKE_WILDCARD = '*';
/**
* Private constructor.
*/
private PredicateBuilder(){
super();
}
/**
* Create a Predicate from the RSQL AST node.
*
* @param node RSQL AST node.
* @param root From that predicate expression paths depends on.
* @param entity The main entity of the query.
* @param manager JPA EntityManager.
* @param misc Facade with all necessary tools for predicate creation.
* @return Predicate a predicate representation of the Node.
*/
public static Predicate createPredicate(Node node, From root, Class entity, EntityManager manager, BuilderTools misc) {
LOG.log(Level.INFO, "Creating Predicate for: {0}", node);
if (node instanceof LogicalNode) {
return createPredicate((LogicalNode)node, root, entity, manager, misc);
}
if (node instanceof ComparisonNode) {
return createPredicate((ComparisonNode)node, root, entity, manager, misc);
}
throw new IllegalArgumentException("Unknown expression type: " + node.getClass());
}
/**
* Create a Predicate from the RSQL AST logical node.
*
* @param logical RSQL AST logical node.
* @param root From that predicate expression paths depends on.
* @param entity The main entity of the query.
* @param entityManager JPA EntityManager.
* @param misc Facade with all necessary tools for predicate creation.
* @return Predicate a predicate representation of the Node.
*/
public static Predicate createPredicate(LogicalNode logical, From root, Class entity, EntityManager entityManager, BuilderTools misc) {
LOG.log(Level.INFO, "Creating Predicate for logical node: {0}", logical);
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
List predicates = new ArrayList();
LOG.log(Level.INFO, "Creating Predicates from all children nodes.");
for (Node node : logical.getChildren()) {
predicates.add(createPredicate(node, root, entity, entityManager, misc));
}
switch (logical.getOperator()) {
case AND : return builder.and(predicates.toArray(new Predicate[predicates.size()]));
case OR : return builder.or(predicates.toArray(new Predicate[predicates.size()]));
}
throw new IllegalArgumentException("Unknown operator: " + logical.getOperator());
}
/**
* Create a Predicate from the RSQL AST comparison node.
*
* @param comparison RSQL AST comparison node.
* @param startRoot From that predicate expression paths depends on.
* @param entity The main entity of the query.
* @param entityManager JPA EntityManager.
* @param misc Facade with all necessary tools for predicate creation.
* @return Predicate a predicate representation of the Node.
*/
public static Predicate createPredicate(ComparisonNode comparison, From startRoot, Class entity, EntityManager entityManager, BuilderTools misc) {
if (startRoot == null) {
String msg = "From root node was undefined.";
LOG.log(Level.SEVERE, msg);
throw new IllegalArgumentException(msg);
}
LOG.log(Level.INFO, "Creating Predicate for comparison node: {0}", comparison);
LOG.log(Level.INFO, "Property graph path : {0}", comparison.getSelector());
Expression propertyPath = findPropertyPath(comparison.getSelector(), startRoot, entityManager, misc);
LOG.log(Level.INFO, "Cast all arguments to type {0}.", propertyPath.getJavaType().getName());
List