Skip to content

Commit 628d300

Browse files
author
u2waremanager@gamil.com
committed
Merge branch '2.1.5.3.RELEASE'
2 parents 1689ca8 + 396fb28 commit 628d300

38 files changed

+768
-199
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>io.github.u2ware</groupId>
77
<artifactId>spring-data-rest-jpa-specification</artifactId>
8-
<version>2.1.5.2.RELEASE</version>
8+
<version>2.1.5.3.RELEASE</version>
99
<packaging>jar</packaging>
1010

1111
<name>spring-data-rest-jpa-specification</name>

src/main/java/org/springframework/data/jpa/repository/query/PartTreePredicateBuilder.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.Collection;
1010
import java.util.Collections;
1111
import java.util.Iterator;
12+
import java.util.Map;
13+
import java.util.concurrent.atomic.AtomicInteger;
1214

1315
import javax.persistence.criteria.CriteriaBuilder;
1416
import javax.persistence.criteria.CriteriaQuery;
@@ -21,6 +23,7 @@
2123
import org.apache.commons.logging.LogFactory;
2224
import org.springframework.beans.BeanWrapper;
2325
import org.springframework.beans.BeanWrapperImpl;
26+
import org.springframework.beans.BeansException;
2427
import org.springframework.data.mapping.PropertyPath;
2528
import org.springframework.data.repository.query.parser.Part;
2629
import org.springframework.data.repository.query.parser.Part.IgnoreCaseType;
@@ -29,6 +32,7 @@
2932
import org.springframework.data.repository.query.parser.PartTree.OrPart;
3033
import org.springframework.util.Assert;
3134
import org.springframework.util.ClassUtils;
35+
import org.springframework.util.MultiValueMap;
3236
import org.springframework.util.ObjectUtils;
3337

3438
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -61,8 +65,17 @@ public PartTreePredicateBuilder(Root<X> root, CriteriaQuery<?> query, CriteriaBu
6165
this.builder = builder;
6266
}
6367

64-
public Predicate build(PartTree partTree, X value) {
65-
return toPredicate(partTree, new BeanWrapperImpl(value));
68+
public Predicate build(PartTree partTree, X params){
69+
return build(partTree, new BeanWrapperImpl(params));
70+
}
71+
public Predicate build(PartTree partTree, Object... params){
72+
return build(partTree, new BeanWrapperObjectArray(params));
73+
}
74+
public Predicate build(PartTree partTree, MultiValueMap<String,Object> params){
75+
return build(partTree, new BeanWrapperMultiValue(params));
76+
}
77+
public Predicate build(PartTree partTree, BeanWrapper value) {
78+
return toPredicate(partTree, value);
6679
}
6780

6881
private Predicate toPredicate(PartTree tree, BeanWrapper parameter) {
@@ -335,4 +348,38 @@ private <T> Expression<T> getIgnoreCasedPath(Root<?> root, Part part) {
335348
private boolean canUpperCase(Expression<?> expression) {
336349
return String.class.equals(expression.getJavaType());
337350
}
351+
352+
//////////////////////////////////////////////////////////////////////
353+
//
354+
//////////////////////////////////////////////////////////////////////
355+
public static class BeanWrapperMultiValue extends BeanWrapperImpl {
356+
357+
private Map<String, ?> source;
358+
359+
public BeanWrapperMultiValue(Map<String, ?> source) {
360+
this.source = source;
361+
}
362+
363+
@Override
364+
public Object getPropertyValue(String propertyName) throws BeansException {
365+
return source.get(propertyName);
366+
}
367+
}
368+
369+
public static class BeanWrapperObjectArray extends BeanWrapperImpl {
370+
371+
private Object[] source;
372+
private AtomicInteger index;
373+
374+
public BeanWrapperObjectArray(Object... source ) {
375+
this.source = source;
376+
this.index = new AtomicInteger(0);
377+
}
378+
379+
@Override
380+
public Object getPropertyValue(String propertyName) throws BeansException {
381+
return source[index.getAndAdd(1)];
382+
}
383+
}
384+
338385
}

src/main/java/org/springframework/data/jpa/repository/query/PartTreeSpecification.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,31 @@
55
import javax.persistence.criteria.Predicate;
66
import javax.persistence.criteria.Root;
77

8+
import org.springframework.beans.BeanWrapper;
9+
import org.springframework.beans.BeanWrapperImpl;
810
import org.springframework.data.jpa.domain.Specification;
11+
import org.springframework.data.jpa.repository.query.PartTreePredicateBuilder.BeanWrapperMultiValue;
12+
import org.springframework.data.jpa.repository.query.PartTreePredicateBuilder.BeanWrapperObjectArray;
913
import org.springframework.data.repository.query.parser.PartTree;
14+
import org.springframework.util.MultiValueMap;
1015

1116
@SuppressWarnings("serial")
1217
public class PartTreeSpecification<T> implements Specification<T> {
1318

1419
private String source;
15-
private T params;
20+
private BeanWrapper params;
1621

1722
public PartTreeSpecification(String source, T params) {
1823
this.source = source;
19-
this.params = params;
24+
this.params = new BeanWrapperImpl(params);
25+
}
26+
public PartTreeSpecification(String source, Object... params) {
27+
this.source = source;
28+
this.params = new BeanWrapperObjectArray(params);
29+
}
30+
public PartTreeSpecification(String source, MultiValueMap<String,Object> params) {
31+
this.source = source;
32+
this.params = new BeanWrapperMultiValue(params);
2033
}
2134

2235
@Override
@@ -25,4 +38,7 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
2538
PartTree partTree = new PartTree(source, root.getJavaType());
2639
return new PartTreePredicateBuilder<T>(root, query, builder).build(partTree, params);
2740
}
41+
42+
43+
2844
}

src/main/java/org/springframework/data/jpa/repository/query/PredicateBuilder.java

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
import org.apache.commons.logging.Log;
1414
import org.apache.commons.logging.LogFactory;
15+
import org.springframework.beans.BeanWrapper;
16+
import org.springframework.beans.BeanWrapperImpl;
17+
import org.springframework.data.jpa.repository.query.PartTreePredicateBuilder.BeanWrapperMultiValue;
18+
import org.springframework.data.jpa.repository.query.PartTreePredicateBuilder.BeanWrapperObjectArray;
1519
import org.springframework.data.repository.query.parser.Part;
1620
import org.springframework.data.repository.query.parser.PartTree;
1721
import org.springframework.util.MultiValueMap;
@@ -29,22 +33,31 @@ private enum State{ AND, AND_START, AND_END, OR, OR_START, OR_END }
2933

3034
private Predicate predicate;
3135
private Predicate subPredicate;
32-
private MultiValueMap<String,Object> parameters;
36+
private MultiValueMap<String,Object> requestParam;
37+
private T requestParamToEntity;
3338

3439
public PredicateBuilder(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
35-
this(root, query, builder, null);
36-
}
37-
38-
public PredicateBuilder(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder, MultiValueMap<String,Object> parameters) {
3940
this.predicateBuilder = new CriteriaBuilderSupport<>(root, query, builder, this);
4041
this.orderBuilder = new CriteriaQuerySupport<>(root, query, builder, this);
41-
this.parameters = parameters;
4242
}
43-
44-
public MultiValueMap<String,Object> getParameters() {
45-
return parameters;
43+
44+
public void setRequestParam(MultiValueMap<String,Object> requestParam) {
45+
this.requestParam = requestParam;
46+
}
47+
public MultiValueMap<String,Object> getRequestParam() {
48+
return requestParam;
49+
}
50+
public T getRequestParamToEntity() {
51+
return requestParamToEntity;
52+
}
53+
public void setRequestParamToEntity(T requestParamToEntity) {
54+
this.requestParamToEntity = requestParamToEntity;
4655
}
4756

57+
public Class<? extends T> getEntityType() {
58+
return predicateBuilder.getRoot().getJavaType();
59+
}
60+
4861
public Root<T> getRoot() {
4962
return predicateBuilder.getRoot();
5063
}
@@ -218,12 +231,19 @@ private CriteriaBuilder getCriteriaBuilder() {
218231
return builder;
219232
}
220233

221-
// public PredicateBuilder<T> partTree(String source){
222-
// return partTree(source, chain.getParameters());
223-
// }
224-
234+
public PredicateBuilder<T> partTree(String source){
235+
return partTree(source, chain.getRequestParam());
236+
}
225237
public PredicateBuilder<T> partTree(String source, T params){
226-
238+
return partTree(source, new BeanWrapperImpl(params));
239+
}
240+
public PredicateBuilder<T> partTree(String source, Object... params){
241+
return partTree(source, new BeanWrapperObjectArray(params));
242+
}
243+
public PredicateBuilder<T> partTree(String source, MultiValueMap<String,Object> params){
244+
return partTree(source, new BeanWrapperMultiValue(params));
245+
}
246+
public PredicateBuilder<T> partTree(String source, BeanWrapper params){
227247
try {
228248
PartTree partTree = new PartTree(source, root.getJavaType());
229249
Predicate predicate = new PartTreePredicateBuilder<>(root, query, builder).build(partTree, params);
@@ -234,6 +254,7 @@ public PredicateBuilder<T> partTree(String source, T params){
234254
return chain.join((Predicate)null);
235255
}
236256
}
257+
237258

238259
public PredicateBuilder<T> part(String source, Object params){
239260

@@ -288,6 +309,42 @@ public PredicateBuilder<T> in(String property, Object value) {
288309
public PredicateBuilder<T> notIn(String property, Object value) {
289310
return part(property+"IsNotIn", value);
290311
}
312+
313+
314+
public PredicateBuilder<T> eq(String property){
315+
return part(property, chain.getRequestParam().get(property));
316+
}
317+
public PredicateBuilder<T> notEq(String property){
318+
return part(property+"Not", chain.getRequestParam().get(property));
319+
}
320+
public PredicateBuilder<T> like(String property){
321+
return part(property+"ContainingIgnoreCase", chain.getRequestParam().get(property));
322+
}
323+
public PredicateBuilder<T> notLike(String property){
324+
return part(property+"NotContainingIgnoreCase", chain.getRequestParam().get(property));
325+
}
326+
public PredicateBuilder<T> between(String property) {
327+
return part(property+"IsBetween", chain.getRequestParam().get(property));
328+
}
329+
public PredicateBuilder<T> gt(String property) {
330+
return part(property+"IsGreaterThan", chain.getRequestParam().get(property));
331+
}
332+
public PredicateBuilder<T> gte(String property) {
333+
return part(property+"IsGreaterThanEqual", chain.getRequestParam().get(property));
334+
}
335+
public PredicateBuilder<T> lt(String property) {
336+
return part(property+"IsLessThan", chain.getRequestParam().get(property));
337+
}
338+
public PredicateBuilder<T> lte(String property) {
339+
return part(property+"IsLessThanEqual", chain.getRequestParam().get(property));
340+
}
341+
public PredicateBuilder<T> in(String property) {
342+
return part(property+"IsIn", chain.getRequestParam().get(property));
343+
}
344+
public PredicateBuilder<T> notIn(String property) {
345+
return part(property+"IsNotIn", chain.getRequestParam().get(property));
346+
}
347+
291348
}
292349

293350

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.springframework.data.rest.core.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Inherited;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Target({ ElementType.TYPE, ElementType.METHOD })
10+
@Retention(RetentionPolicy.RUNTIME)
11+
@Inherited
12+
public @interface HandleHibernatePostLoad {
13+
14+
}

src/main/java/org/springframework/data/rest/core/annotation/HandleAfterRead.java renamed to src/main/java/org/springframework/data/rest/core/annotation/HandleHibernatePreLoad.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
@Target({ ElementType.TYPE, ElementType.METHOD })
1010
@Retention(RetentionPolicy.RUNTIME)
1111
@Inherited
12-
public @interface HandleAfterRead {
12+
public @interface HandleHibernatePreLoad {
1313

1414
}

src/main/java/org/springframework/data/rest/core/annotation/HandleBeforeRead.java renamed to src/main/java/org/springframework/data/rest/core/annotation/HandlePredicateBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
@Target({ ElementType.TYPE, ElementType.METHOD })
1010
@Retention(RetentionPolicy.RUNTIME)
1111
@Inherited
12-
public @interface HandleBeforeRead {
12+
public @interface HandlePredicateBuilder {
1313

1414
}

src/main/java/org/springframework/data/rest/core/event/AfterReadEvent.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/org/springframework/data/rest/core/event/BeforeReadEvent.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package org.springframework.data.rest.core.event.hibernate;
1+
package org.springframework.data.rest.core.event;
22

33
import org.springframework.data.rest.core.event.RepositoryEvent;
44

5+
@SuppressWarnings("serial")
56
public class HibernatePostLoadEvent extends RepositoryEvent {
67

7-
private static final long serialVersionUID = 1932567984753687446L;
8-
98
public HibernatePostLoadEvent(Object source) {
109
super(source);
1110
}

0 commit comments

Comments
 (0)