Skip to content

Commit 70cf5f3

Browse files
authored
Merge pull request #4 from mathianasj/release/0.0.2
Release/0.0.2
2 parents bcf56ed + 092c257 commit 70cf5f3

File tree

4 files changed

+78
-75
lines changed

4 files changed

+78
-75
lines changed

.classpath

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9-
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10-
<attributes>
11-
<attribute name="maven.pomderived" value="true"/>
12-
</attributes>
13-
</classpathentry>
149
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
1510
<attributes>
1611
<attribute name="optional" value="true"/>

.project

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
<arguments>
1111
</arguments>
1212
</buildCommand>
13+
<buildCommand>
14+
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
1318
<buildCommand>
1419
<name>org.eclipse.m2e.core.maven2Builder</name>
1520
<arguments>
@@ -19,5 +24,6 @@
1924
<natures>
2025
<nature>org.eclipse.jdt.core.javanature</nature>
2126
<nature>org.eclipse.m2e.core.maven2Nature</nature>
27+
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
2228
</natures>
2329
</projectDescription>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.github.mathianasj</groupId>
55
<artifactId>spring-data-jpa-rsql</artifactId>
6-
<version>0.0.1</version>
6+
<version>0.0.2</version>
77

88
<developers>
99
<developer>

src/main/java/com/mathianasj/spring/rsql/GenericRsqlSpecification.java

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,93 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5-
5+
import java.util.UUID;
66
import javax.persistence.criteria.CriteriaBuilder;
77
import javax.persistence.criteria.CriteriaQuery;
88
import javax.persistence.criteria.Predicate;
99
import javax.persistence.criteria.Root;
10-
1110
import org.springframework.data.jpa.domain.Specification;
12-
1311
import cz.jirutka.rsql.parser.ast.ComparisonOperator;
1412

1513
public class GenericRsqlSpecification<T> implements Specification<T> {
1614

17-
private String property;
18-
private ComparisonOperator operator;
19-
private List<String> arguments;
15+
private String property;
16+
private ComparisonOperator operator;
17+
private List<String> arguments;
2018

21-
public GenericRsqlSpecification(final String property, final ComparisonOperator operator,
22-
final List<String> arguments) {
23-
super();
24-
this.property = property;
25-
this.operator = operator;
26-
this.arguments = arguments;
27-
}
19+
public GenericRsqlSpecification(final String property, final ComparisonOperator operator,
20+
final List<String> arguments) {
21+
super();
22+
this.property = property;
23+
this.operator = operator;
24+
this.arguments = arguments;
25+
}
2826

29-
public Predicate toPredicate(final Root<T> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) {
30-
final List<Object> args = castArguments(root);
31-
final Object argument = args.get(0);
32-
switch (RsqlSearchOperation.getSimpleOperator(operator)) {
27+
@Override
28+
public Predicate toPredicate(final Root<T> root, final CriteriaQuery<?> query,
29+
final CriteriaBuilder builder) {
30+
final List<Object> args = castArguments(root);
31+
final Object argument = args.get(0);
32+
switch (RsqlSearchOperation.getSimpleOperator(operator)) {
3333

34-
case EQUAL: {
35-
if (argument instanceof String) {
36-
return builder.like(root.<String>get(property), argument.toString().replace('*', '%'));
37-
} else if (argument == null) {
38-
return builder.isNull(root.get(property));
39-
} else {
40-
return builder.equal(root.get(property), argument);
41-
}
42-
}
43-
case NOT_EQUAL: {
44-
if (argument instanceof String) {
45-
return builder.notLike(root.<String>get(property), argument.toString().replace('*', '%'));
46-
} else if (argument == null) {
47-
return builder.isNotNull(root.get(property));
48-
} else {
49-
return builder.notEqual(root.get(property), argument);
50-
}
51-
}
52-
case GREATER_THAN: {
53-
return builder.greaterThan(root.<String>get(property), argument.toString());
54-
}
55-
case GREATER_THAN_OR_EQUAL: {
56-
return builder.greaterThanOrEqualTo(root.<String>get(property), argument.toString());
57-
}
58-
case LESS_THAN: {
59-
return builder.lessThan(root.<String>get(property), argument.toString());
60-
}
61-
case LESS_THAN_OR_EQUAL: {
62-
return builder.lessThanOrEqualTo(root.<String>get(property), argument.toString());
63-
}
64-
case IN:
65-
return root.get(property).in(args);
66-
case NOT_IN:
67-
return builder.not(root.get(property).in(args));
68-
}
34+
case EQUAL: {
35+
if (argument instanceof String) {
36+
return builder.like(root.<String>get(property), argument.toString().replace('*', '%'));
37+
} else if (argument == null) {
38+
return builder.isNull(root.get(property));
39+
} else {
40+
return builder.equal(root.get(property), argument);
41+
}
42+
}
43+
case NOT_EQUAL: {
44+
if (argument instanceof String) {
45+
return builder.notLike(root.<String>get(property), argument.toString().replace('*', '%'));
46+
} else if (argument == null) {
47+
return builder.isNotNull(root.get(property));
48+
} else {
49+
return builder.notEqual(root.get(property), argument);
50+
}
51+
}
52+
case GREATER_THAN: {
53+
return builder.greaterThan(root.<String>get(property), argument.toString());
54+
}
55+
case GREATER_THAN_OR_EQUAL: {
56+
return builder.greaterThanOrEqualTo(root.<String>get(property), argument.toString());
57+
}
58+
case LESS_THAN: {
59+
return builder.lessThan(root.<String>get(property), argument.toString());
60+
}
61+
case LESS_THAN_OR_EQUAL: {
62+
return builder.lessThanOrEqualTo(root.<String>get(property), argument.toString());
63+
}
64+
case IN:
65+
return root.get(property).in(args);
66+
case NOT_IN:
67+
return builder.not(root.get(property).in(args));
68+
}
6969

70-
return null;
71-
}
70+
return null;
71+
}
7272

73-
// === private
73+
// === private
7474

75-
private List<Object> castArguments(final Root<T> root) {
76-
final List<Object> args = new ArrayList<Object>();
77-
final Class<? extends Object> type = root.get(property).getJavaType();
75+
private List<Object> castArguments(final Root<T> root) {
76+
final List<Object> args = new ArrayList<Object>();
77+
final Class<? extends Object> type = root.get(property).getJavaType();
7878

79-
for (final String argument : arguments) {
80-
if (type.equals(Integer.class)) {
81-
args.add(Integer.parseInt(argument));
82-
} else if (type.equals(Long.class)) {
83-
args.add(Long.parseLong(argument));
84-
} else {
85-
args.add(argument);
86-
}
87-
}
79+
for (final String argument : arguments) {
80+
if (type.equals(Integer.class)) {
81+
args.add(Integer.parseInt(argument));
82+
} else if (type.equals(Long.class)) {
83+
args.add(Long.parseLong(argument));
84+
} else if (type.equals(UUID.class)) {
85+
args.add(UUID.fromString(argument));
86+
} else {
87+
args.add(argument);
88+
}
89+
}
8890

89-
return args;
90-
}
91+
return args;
92+
}
9193

9294
}

0 commit comments

Comments
 (0)