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() {
if (params.size() == 0)
return null;
final List<Specification<User>> specs = params.stream()
.map(UserSpecification::new)
.collect(Collectors.toList());
Specification<User> result = specs.get(0);
Specification<User> result = new UserSpecification(params.get(0));
for (int i = 1; i < params.size(); i++) {
result = params.get(i)
.isOrPredicate()
? Specifications.where(result)
.or(specs.get(i))
: Specifications.where(result)
.and(specs.get(i));
result = params.get(i).isOrPredicate()
? Specifications.where(result).or(new UserSpecification(params.get(i)))
: Specifications.where(result).and(new UserSpecification(params.get(i)));
}
return result;
}

View File

@ -31,16 +31,17 @@ public class GenericRsqlSpecBuilder<T> {
.filter(Objects::nonNull)
.collect(Collectors.toList());
Specifications<T> initialSpec = specs.stream().findFirst().get();
Specifications<T> result = specs.stream().skip(1).reduce(initialSpec, (firstSpec, secondSpec) -> {
if (logicalNode.getOperator() == LogicalOperator.AND) {
return Specifications.where(firstSpec).and(secondSpec);
} else if (logicalNode.getOperator() == LogicalOperator.OR) {
return Specifications.where(firstSpec).or(secondSpec);
}
return firstSpec;
});
Specifications<T> result = specs.get(0);
if (logicalNode.getOperator() == LogicalOperator.AND) {
for (int i = 1; i < specs.size(); i++) {
result = Specifications.where(result).and(specs.get(i));
}
}
else if (logicalNode.getOperator() == LogicalOperator.OR) {
for (int i = 1; i < specs.size(); i++) {
result = Specifications.where(result).or(specs.get(i));
}
}
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 List<Object> args = arguments.stream().map(arg -> {
Object obj;
if (type.equals(Integer.class)) {
obj = Integer.parseInt(arg);
return Integer.parseInt(arg);
} else if (type.equals(Long.class)) {
obj = Long.parseLong(arg);
return Long.parseLong(arg);
} else {
obj = arg;
}
return obj;
return arg;
}
}).collect(Collectors.toList());
return args;