refactor spring rest query lang

This commit is contained in:
Loredana 2018-10-28 09:31:42 +02:00
parent a8b29dd140
commit 600f416c1b
3 changed files with 21 additions and 33 deletions

View File

@ -45,26 +45,17 @@ public final class UserSpecificationsBuilder {
} }
public Specification<User> build() { public Specification<User> build() {
if (params.size() == 0) if (params.size() == 0)
return null; return null;
final List<Specification<User>> specs = params.stream() Specification<User> result = new UserSpecification(params.get(0));
.map(UserSpecification::new)
.collect(Collectors.toList());
Specification<User> result = specs.get(0);
for (int i = 1; i < params.size(); i++) { for (int i = 1; i < params.size(); i++) {
result = params.get(i) result = params.get(i).isOrPredicate()
.isOrPredicate() ? Specifications.where(result).or(new UserSpecification(params.get(i)))
? Specifications.where(result) : Specifications.where(result).and(new UserSpecification(params.get(i)));
.or(specs.get(i))
: Specifications.where(result)
.and(specs.get(i));
} }
return result; return result;
} }

View File

@ -31,16 +31,17 @@ public class GenericRsqlSpecBuilder<T> {
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
Specifications<T> initialSpec = specs.stream().findFirst().get(); Specifications<T> result = specs.get(0);
if (logicalNode.getOperator() == LogicalOperator.AND) {
Specifications<T> result = specs.stream().skip(1).reduce(initialSpec, (firstSpec, secondSpec) -> { for (int i = 1; i < specs.size(); i++) {
if (logicalNode.getOperator() == LogicalOperator.AND) { result = Specifications.where(result).and(specs.get(i));
return Specifications.where(firstSpec).and(secondSpec); }
} else if (logicalNode.getOperator() == LogicalOperator.OR) { }
return Specifications.where(firstSpec).or(secondSpec); else if (logicalNode.getOperator() == LogicalOperator.OR) {
} for (int i = 1; i < specs.size(); i++) {
return firstSpec; result = Specifications.where(result).or(specs.get(i));
}); }
}
return result; return result;
} }

View File

@ -77,17 +77,13 @@ public class GenericRsqlSpecification<T> implements Specification<T> {
final Class<? extends Object> type = root.get(property).getJavaType(); final Class<? extends Object> type = root.get(property).getJavaType();
final List<Object> args = arguments.stream().map(arg -> { final List<Object> args = arguments.stream().map(arg -> {
Object obj;
if (type.equals(Integer.class)) { if (type.equals(Integer.class)) {
obj = Integer.parseInt(arg); return Integer.parseInt(arg);
} else if (type.equals(Long.class)) { } else if (type.equals(Long.class)) {
obj = Long.parseLong(arg); return Long.parseLong(arg);
} else { } else {
obj = arg; return arg;
} }
return obj;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
return args; return args;