Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding BigDecimal Support #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.github.tennaito</groupId>
<artifactId>rsql-jpa</artifactId>
<version>2.0.2-SNAPSHOT</version>
<version>2.0.3-SNAPSHOT</version>
<packaging>jar</packaging>

<!--//////////////////// ABOUT ////////////////////-->
Expand Down
33 changes: 33 additions & 0 deletions rsql-jpa.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: cz.jirutka.rsql:rsql-parser:2.0.0" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:javax.persistence:2.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hibernate:hibernate-entitymanager:4.3.10.Final" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jboss.logging:jboss-logging:3.1.3.GA" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jboss.logging:jboss-logging-annotations:1.2.0.Beta1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hibernate:hibernate-core:4.3.10.Final" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: antlr:antlr:2.7.7" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jboss:jandex:1.1.0.Final" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: dom4j:dom4j:1.6.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: xml-apis:xml-apis:1.0.b2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hibernate.common:hibernate-commons-annotations:4.0.5.Final" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.0.0.Final" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.javassist:javassist:3.18.1-GA" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.persistence:eclipselink:2.6.0-M3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.eclipse.persistence:commonj.sdo:2.1.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hsqldb:hsqldb:1.8.0.10" level="project" />
</component>
</module>
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* The MIT License
*
* 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 java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import cz.jirutka.rsql.parser.ast.AndNode;
import cz.jirutka.rsql.parser.ast.ComparisonNode;
import cz.jirutka.rsql.parser.ast.OrNode;
import cz.jirutka.rsql.parser.ast.RSQLVisitor;

/**
* JpaCriteriaQueryVisitor
*
* Visitor class for Criteria Query count creation from RSQL AST Nodes.
*
* @author sza
*
* @param <T> Entity type
*/
public class JpaCriteriaCountQueryVisitor<T> extends AbstractJpaVisitor<CriteriaQuery<Long>, T> implements RSQLVisitor<CriteriaQuery<Long>, EntityManager> {

private static final Logger LOG = Logger.getLogger(JpaCriteriaCountQueryVisitor.class.getName());

private final JpaPredicateVisitor<T> predicateVisitor;

private Root<T> root;

/**
* Construtor with template varargs for entityClass discovery.
*
* @param t not for usage
*/
@SafeVarargs
public JpaCriteriaCountQueryVisitor(T... t) {
super(t);
this.predicateVisitor = new JpaPredicateVisitor<T>(t);
}

/**
* Get the Predicate Visitor instance.
*
* @return Return the Predicate Visitor.
*/
protected JpaPredicateVisitor<T> getPredicateVisitor() {
this.predicateVisitor.setBuilderTools(this.getBuilderTools());
return this.predicateVisitor;
}

/* (non-Javadoc)
* @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.AndNode, java.lang.Object)
*/
public CriteriaQuery<Long> visit(AndNode node, EntityManager entityManager) {
LOG.log(Level.INFO, "Creating CriteriaQuery for AndNode: {0}", node);

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
root = cq.from(entityClass);
cq.select(cb.countDistinct(root));
cq.where(this.getPredicateVisitor().defineRoot(root).visit(node, entityManager));

return cq;
}

/* (non-Javadoc)
* @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.OrNode, java.lang.Object)
*/
public CriteriaQuery<Long> visit(OrNode node, EntityManager entityManager) {
LOG.log(Level.INFO, "Creating CriteriaQuery for OrNode: {0}", node);

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
root = cq.from(entityClass);
cq.select(cb.countDistinct(root));
root = cq.from(entityClass);
cq.where(this.getPredicateVisitor().defineRoot(root).visit(node, entityManager));
return cq;
}

/* (non-Javadoc)
* @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.ComparisonNode, java.lang.Object)
*/
public CriteriaQuery<Long> visit(ComparisonNode node, EntityManager entityManager) {
LOG.log(Level.INFO, "Creating CriteriaQuery for ComparisonNode: {0}", node);

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
root = cq.from(entityClass);
cq.select(cb.countDistinct(root));
cq.where(this.getPredicateVisitor().defineRoot(root).visit(node, entityManager));
return cq;
}

public Root<T> getRoot() {
return root;
}

public void setRoot(Root<T> root) {
this.root = root;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Root;

import cz.jirutka.rsql.parser.ast.AndNode;
import cz.jirutka.rsql.parser.ast.ComparisonNode;
Expand All @@ -49,12 +49,15 @@ public class JpaCriteriaQueryVisitor<T> extends AbstractJpaVisitor<CriteriaQuery
private static final Logger LOG = Logger.getLogger(JpaCriteriaQueryVisitor.class.getName());

private final JpaPredicateVisitor<T> predicateVisitor;

private Root<T> root;

/**
* Construtor with template varargs for entityClass discovery.
*
* @param t not for usage
*/
@SafeVarargs
public JpaCriteriaQueryVisitor(T... t) {
super(t);
this.predicateVisitor = new JpaPredicateVisitor<T>(t);
Expand All @@ -76,7 +79,7 @@ protected JpaPredicateVisitor<T> getPredicateVisitor() {
public CriteriaQuery<T> visit(AndNode node, EntityManager entityManager) {
LOG.log(Level.INFO, "Creating CriteriaQuery for AndNode: {0}", node);
CriteriaQuery<T> criteria = entityManager.getCriteriaBuilder().createQuery(entityClass);
From root = criteria.from(entityClass);
root = criteria.from(entityClass);
return criteria.where(this.getPredicateVisitor().defineRoot(root).visit(node, entityManager));
}

Expand All @@ -86,7 +89,7 @@ public CriteriaQuery<T> visit(AndNode node, EntityManager entityManager) {
public CriteriaQuery<T> visit(OrNode node, EntityManager entityManager) {
LOG.log(Level.INFO, "Creating CriteriaQuery for OrNode: {0}", node);
CriteriaQuery<T> criteria = entityManager.getCriteriaBuilder().createQuery(entityClass);
From root = criteria.from(entityClass);
root = criteria.from(entityClass);
return criteria.where(this.getPredicateVisitor().defineRoot(root).visit(node, entityManager));
}

Expand All @@ -96,7 +99,17 @@ public CriteriaQuery<T> visit(OrNode node, EntityManager entityManager) {
public CriteriaQuery<T> visit(ComparisonNode node, EntityManager entityManager) {
LOG.log(Level.INFO, "Creating CriteriaQuery for ComparisonNode: {0}", node);
CriteriaQuery<T> criteria = entityManager.getCriteriaBuilder().createQuery(entityClass);
From root = criteria.from(entityClass);
root = criteria.from(entityClass);
return criteria.where(this.getPredicateVisitor().defineRoot(root).visit(node, entityManager));
}

public Root<T> getRoot() {
return root;
}

public void setRoot(Root<T> root) {
this.root = root;
}


}
46 changes: 43 additions & 3 deletions src/main/java/com/github/tennaito/rsql/jpa/PredicateBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

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;
Expand All @@ -42,7 +43,10 @@
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.PluralAttribute;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
Expand All @@ -65,11 +69,20 @@ public final class PredicateBuilder {

public static final Character LIKE_WILDCARD = '*';

private static final Date START_DATE = new Date(0L) ;

private static final Date END_DATE;

static {
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(9000, 01, 01);
END_DATE = endCalendar.getTime();
}
/**
* Private constructor.
*/
private PredicateBuilder(){
super();
super();
}

/**
Expand Down Expand Up @@ -249,15 +262,28 @@ private static Predicate createPredicate(Expression propertyPath, ComparisonOper
}
case GREATER_THAN_OR_EQUAL : {
Object argument = arguments.get(0);
return createGreaterEqual(propertyPath, (Number)argument, manager);
Predicate predicate;
if (argument instanceof Date){
predicate = createBetweenThan(propertyPath, (Date)argument, END_DATE, manager);
}else{
predicate = createGreaterEqual(propertyPath, (Number)argument, manager);
}
return predicate;
}
case LESS_THAN : {
Object argument = arguments.get(0);
return createLessThan(propertyPath, (Number)argument, manager);
}
case LESS_THAN_OR_EQUAL : {
Object argument = arguments.get(0);
return createLessEqual(propertyPath, (Number)argument, manager);

Predicate predicate;
if (argument instanceof Date){
predicate = createBetweenThan(propertyPath,START_DATE, (Date)argument, manager);
}else{
predicate = createLessEqual(propertyPath, (Number)argument, manager);
}
return predicate;
}
case IN : return createIn(propertyPath, arguments, manager);
case NOT_IN : return createNotIn(propertyPath, arguments, manager);
Expand All @@ -267,6 +293,20 @@ private static Predicate createPredicate(Expression propertyPath, ComparisonOper
}

/**
* Creates the between than.
*
* @param propertyPath the property path
* @param startDate the start date
* @param argument the argument
* @param manager the manager
* @return the predicate
*/
private static Predicate createBetweenThan(Expression propertyPath, Date start, Date end, EntityManager manager) {
CriteriaBuilder builder = manager.getCriteriaBuilder();
return builder.between(propertyPath, start, end);
}

/**
* Apply a case-insensitive "like" constraint to the property path. Value
* should contains wildcards "*" (% in SQL) and "_".
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Indicate that argument is not in suitable format required by entity's
* property, i.e. is not parseable to the specified type.
*
* @author Jakub Jirutka <[email protected]>
* @author Jakub Jirutka [email protected]
* @author AntonioRabelo
*/
public class ArgumentFormatException extends RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Interface for Argument Parser that is used for parsing given string argument
* from RSQL query according to type of the target property.
*
* @author Jakub Jirutka <[email protected]>
* @author Jakub Jirutka [email protected]
* @author AntonioRabelo
*/
public interface ArgumentParser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -72,6 +73,7 @@ public <T> T parse(String argument, Class<T> type)
if (type.equals(Float.class) || type.equals(float.class)) return (T) Float.valueOf(argument);
if (type.equals(Double.class) || type.equals(double.class)) return (T) Double.valueOf(argument);
if (type.equals(Long.class) || type.equals(long.class)) return (T) Long.valueOf(argument);
if (type.equals(BigDecimal.class)) return (T) new BigDecimal(argument);
} catch (IllegalArgumentException ex) {
throw new ArgumentFormatException(argument, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
public class SimpleMapper implements Mapper {

private static final Logger LOG = Logger.getLogger(DefaultArgumentParser.class.getName());
private static final Logger LOG = Logger.getLogger(SimpleMapper.class.getName());

private Map<Class<?>, Map<String, String>> mapping;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.tennaito.rsql.jpa;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -88,6 +89,7 @@ public static void setUpBefore() throws Exception {
c.setCredits(10);
c.setName("Testing Course");
c.setDepartment(department);
c.setDate(Calendar.getInstance().getTime());
c.setDetails(CourseDetails.of("test"));
entityManager.persist(c);

Expand Down
Loading