forked from perplexhub/rsql-jpa-specification
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b65fa99
Showing
9 changed files
with
723 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
*/bin | ||
*/build | ||
*/target | ||
*/.classpath | ||
*/.settings | ||
*/.project | ||
.classpath | ||
.settings | ||
.project | ||
**/*.swp | ||
**/*.log | ||
**/*.bak | ||
**/.springBeans | ||
**/.mvn | ||
**/target | ||
**/.factorypath | ||
**/*.iml | ||
**/.idea | ||
|
||
*/sys.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 perplexhub | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# rsql-jpa-specification | ||
|
||
Idea from rsql-parser, rsql-jpa and Dynamic-Specification-RSQL | ||
Visit [RSQL / FIQL parser](https://github.com/jirutka/rsql-parser) | ||
Visit [RSQL for JPA](https://github.com/tennaito/rsql-jpa) | ||
Visit [Dynamic-Specification-RSQL](https://github.com/srigalamilitan/Dynamic-Specification-RSQL) | ||
|
||
## 1) Inject the EntityManager | ||
|
||
### 1.1) | ||
|
||
```java | ||
@ComponentScan("com.perplexhub.rsql.jpa") | ||
``` | ||
|
||
### 1.2) | ||
|
||
```java | ||
@Bean | ||
public com.perplexhub.rsql.jpa.RsqlJpaSpecification RsqlJpaSpecification(EntityManager entityManager) { | ||
return new com.perplexhub.rsql.jpa.RsqlJpaSpecification(entityManager); | ||
} | ||
``` | ||
|
||
### 1.3) | ||
|
||
```java | ||
new RsqlJpaSpecification(entityManager); | ||
``` | ||
|
||
## 2) Add JpaSpecificationExecutor to your JPA repository class | ||
|
||
```java | ||
package com.perplexhub.rsql.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
|
||
import com.perplexhub.rsql.model.User; | ||
|
||
public interface UserRepository extends JpaRepository<User, String>, JpaSpecificationExecutor<User> { | ||
} | ||
``` | ||
|
||
## 3) Obtain the Specification from RsqlJpaSpecification.rsql(filter) using RSQL syntax | ||
|
||
```java | ||
filter = "company.code==perplexhub"; //equal | ||
filter = "company.code==perplex*"; //like perplex% | ||
filter = "company.code==*hub"; //like %hub | ||
filter = "company.code==*plex*"; //like %plex% | ||
filter = "company.code==^*PLEX*"; //ignore case like %PLEX% | ||
repository.findAll(RsqlJpaSpecification.rsql(filter)); | ||
repository.findAll(RsqlJpaSpecification.rsql(filter), pageable); | ||
``` | ||
|
||
Example: | ||
[RSQL / FIQL parser](https://github.com/jirutka/rsql-parser#examples) | ||
[RSQL for JPA](https://github.com/tennaito/rsql-jpa#examples-of-rsql) | ||
[Dynamic-Specification-RSQL](https://github.com/srigalamilitan/Dynamic-Specification-RSQL#implementation-rsql-in-services-layer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.perplexhub</groupId> | ||
<artifactId>rsql-jpa-specification</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<scm> | ||
<url>https://github.com/perplexhub/rsql-jpa-specification</url> | ||
<connection>scm:git:ssh://[email protected]/perplexhub/rsql-jpa-specification.git</connection> | ||
<developerConnection>scm:git:ssh://[email protected]/perplexhub/rsql-jpa-specification.git</developerConnection> | ||
<tag>HEAD</tag> | ||
</scm> | ||
<properties> | ||
<java.version>1.8</java.version> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<spring.boot.version>2.0.2.RELEASE</spring.boot.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.tennaito</groupId> | ||
<artifactId>rsql-jpa</artifactId> | ||
<version>2.0.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cz.jirutka.rsql</groupId> | ||
<artifactId>rsql-parser</artifactId> | ||
<version>2.1.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.7.0</version> | ||
<executions> | ||
<execution> | ||
<id>default-compile</id> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>default-testCompile</id> | ||
<phase>test-compile</phase> | ||
<goals> | ||
<goal>testCompile</goal> | ||
</goals> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
168 changes: 168 additions & 0 deletions
168
src/main/java/com/perplexhub/rsql/jpa/RsqlJpaConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package com.perplexhub.rsql.jpa; | ||
|
||
import static cz.jirutka.rsql.parser.ast.RSQLOperators.EQUAL; | ||
import static cz.jirutka.rsql.parser.ast.RSQLOperators.GREATER_THAN; | ||
import static cz.jirutka.rsql.parser.ast.RSQLOperators.GREATER_THAN_OR_EQUAL; | ||
import static cz.jirutka.rsql.parser.ast.RSQLOperators.IN; | ||
import static cz.jirutka.rsql.parser.ast.RSQLOperators.LESS_THAN; | ||
import static cz.jirutka.rsql.parser.ast.RSQLOperators.LESS_THAN_OR_EQUAL; | ||
import static cz.jirutka.rsql.parser.ast.RSQLOperators.NOT_EQUAL; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.Path; | ||
import javax.persistence.criteria.Predicate; | ||
import javax.persistence.criteria.Root; | ||
import javax.persistence.metamodel.Attribute; | ||
|
||
import org.springframework.core.convert.ConversionService; | ||
import org.springframework.core.convert.support.DefaultConversionService; | ||
import org.springframework.util.StringUtils; | ||
|
||
import cz.jirutka.rsql.parser.ast.AndNode; | ||
import cz.jirutka.rsql.parser.ast.ComparisonNode; | ||
import cz.jirutka.rsql.parser.ast.ComparisonOperator; | ||
import cz.jirutka.rsql.parser.ast.Node; | ||
import cz.jirutka.rsql.parser.ast.OrNode; | ||
import cz.jirutka.rsql.parser.ast.RSQLVisitor; | ||
|
||
// clone from com.putracode.utils.JPARsqlConverter | ||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public class RsqlJpaConverter implements RSQLVisitor<Predicate, Root> { | ||
private final CriteriaBuilder builder; | ||
private final ConversionService conversionService = new DefaultConversionService(); | ||
|
||
public RsqlJpaConverter(CriteriaBuilder builder) { | ||
this.builder = builder; | ||
} | ||
|
||
public Predicate visit(AndNode node, Root root) { | ||
|
||
return builder.and(processNodes(node.getChildren(), root)); | ||
} | ||
|
||
public Predicate visit(OrNode node, Root root) { | ||
|
||
return builder.or(processNodes(node.getChildren(), root)); | ||
} | ||
|
||
public Predicate visit(ComparisonNode node, Root root) { | ||
ComparisonOperator op = node.getOperator(); | ||
//Path attrPath = root.get(node.getSelector()); | ||
//Attribute attribute = root.getModel().getAttribute(node.getSelector()); | ||
RsqlJpaHolder holder = RsqlJpaSpecification.findPropertyPath(node.getSelector(), root); | ||
Path attrPath = holder.path; | ||
Attribute attribute = holder.attribute; | ||
Class type = attribute.getJavaType(); | ||
if (node.getArguments().size() > 1) { | ||
/** | ||
* Implementasi List | ||
*/ | ||
List<Object> listObject = new ArrayList<>(); | ||
for (String argument : node.getArguments()) { | ||
listObject.add(castDynamicClass(type, argument)); | ||
} | ||
if (op.equals(IN)) { | ||
return attrPath.in(listObject); | ||
} else { | ||
return builder.not(attrPath.in(listObject)); | ||
} | ||
|
||
} else { | ||
/** | ||
* Searching With One Value | ||
*/ | ||
Object argument = castDynamicClass(type, node.getArguments().get(0)); | ||
if (op.equals(EQUAL)) { | ||
if (type.equals(String.class)) { | ||
if (argument.toString().contains("*") && argument.toString().contains("^")) { | ||
return builder.like(builder.upper(attrPath), argument.toString().replace("*", "%").replace("^", "").toUpperCase()); | ||
} else if (argument.toString().contains("*")) { | ||
return builder.like(attrPath, argument.toString().replace('*', '%')); | ||
} else if (argument.toString().contains("^")) { | ||
|
||
return builder.equal(builder.upper(attrPath), argument.toString().replace("^", "").toUpperCase()); | ||
} else { | ||
return builder.equal(attrPath, argument); | ||
} | ||
} else if (argument == null) { | ||
return builder.isNull(attrPath); | ||
} else { | ||
return builder.equal(attrPath, argument); | ||
} | ||
} | ||
if (op.equals(NOT_EQUAL)) { | ||
if (type.equals(String.class)) { | ||
if (argument.toString().contains("*") && argument.toString().contains("^")) { | ||
return builder.notLike(builder.upper(attrPath), argument.toString().replace("*", "%").replace("^", "").toUpperCase()); | ||
} else if (argument.toString().contains("*")) { | ||
return builder.notLike(attrPath, argument.toString().replace('*', '%')); | ||
} else if (argument.toString().contains("^")) { | ||
return builder.notEqual(builder.upper(attrPath), argument.toString().replace("^", "").toUpperCase()); | ||
} else { | ||
return builder.notEqual(attrPath, argument); | ||
} | ||
} else if (argument == null) { | ||
return builder.isNotNull(attrPath); | ||
} else { | ||
return builder.notEqual(attrPath, argument); | ||
} | ||
} | ||
if (!Comparable.class.isAssignableFrom(type)) { | ||
throw new IllegalArgumentException(String.format( | ||
"Operator %s can be used only for Comparables", op)); | ||
} | ||
Comparable comparable = (Comparable) conversionService.convert(argument, type); | ||
|
||
if (op.equals(GREATER_THAN)) { | ||
return builder.greaterThan(attrPath, comparable); | ||
} | ||
if (op.equals(GREATER_THAN_OR_EQUAL)) { | ||
return builder.greaterThanOrEqualTo(attrPath, comparable); | ||
} | ||
if (op.equals(LESS_THAN)) { | ||
return builder.lessThan(attrPath, comparable); | ||
} | ||
if (op.equals(LESS_THAN_OR_EQUAL)) { | ||
return builder.lessThanOrEqualTo(attrPath, comparable); | ||
} | ||
} | ||
throw new IllegalArgumentException("Unknown operator: " + op); | ||
} | ||
|
||
private Predicate[] processNodes(List<Node> nodes, Root root) { | ||
|
||
Predicate[] predicates = new Predicate[nodes.size()]; | ||
|
||
for (int i = 0; i < nodes.size(); i++) { | ||
predicates[i] = nodes.get(i).accept(this, root); | ||
} | ||
return predicates; | ||
} | ||
|
||
public Object castDynamicClass(Class dynamicClass, String value) { | ||
Object object = null; | ||
try { | ||
if (dynamicClass.equals(LocalDate.class)) { | ||
object = LocalDate.parse(value); | ||
} else if (dynamicClass.equals(LocalDateTime.class)) { | ||
object = LocalDateTime.parse(value); | ||
} else if (dynamicClass.equals(Character.class)) { | ||
object = (!StringUtils.isEmpty(value) ? value.charAt(0) : null); | ||
} else { | ||
Constructor<?> cons = (Constructor<?>) dynamicClass.getConstructor(new Class<?>[] { String.class }); | ||
object = cons.newInstance(new Object[] { value }); | ||
} | ||
|
||
return object; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.perplexhub.rsql.jpa; | ||
|
||
import javax.persistence.criteria.Path; | ||
import javax.persistence.metamodel.Attribute; | ||
|
||
class RsqlJpaHolder<X, Y> { | ||
Path<X> path; | ||
Attribute<X, Y> attribute; | ||
} |
Oops, something went wrong.