builder improvements
This commit is contained in:
parent
425c681858
commit
f0e18fc212
|
@ -8,40 +8,36 @@ import org.baeldung.web.util.SearchCriteria;
|
|||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.domain.Specifications;
|
||||
|
||||
public class UserSpecificationsBuilder {
|
||||
public final class UserSpecificationsBuilder {
|
||||
|
||||
private List<SearchCriteria> params;
|
||||
private final List<SearchCriteria> params;
|
||||
|
||||
public UserSpecificationsBuilder() {
|
||||
params = new ArrayList<SearchCriteria>();
|
||||
}
|
||||
|
||||
public UserSpecificationsBuilder with(final String key, final String operation, final Object value) {
|
||||
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
final List<SearchCriteria> newParams = new ArrayList<SearchCriteria>(params);
|
||||
newParams.add(new SearchCriteria(key, operation, value));
|
||||
builder.params = newParams;
|
||||
return builder;
|
||||
// API
|
||||
|
||||
public final UserSpecificationsBuilder with(final String key, final String operation, final Object value) {
|
||||
params.add(new SearchCriteria(key, operation, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Specification<User> build() {
|
||||
if (params.size() == 0)
|
||||
if (params.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<Specification<User>> specs = new ArrayList<Specification<User>>();
|
||||
|
||||
for (final SearchCriteria param : params) {
|
||||
specs.add(new UserSpecification(param));
|
||||
}
|
||||
|
||||
if (specs.size() == 0)
|
||||
return null;
|
||||
|
||||
Specification<User> result = specs.get(0);
|
||||
for (int i = 1; i < specs.size(); i++) {
|
||||
result = Specifications.where(result).and(specs.get(i));
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -39,11 +39,12 @@ public class UserController {
|
|||
super();
|
||||
}
|
||||
|
||||
// API - READ
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
||||
@ResponseBody
|
||||
public List<User> findAll(@RequestParam(value = "search", required = false) final String search) {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
|
||||
if (search != null) {
|
||||
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
final Matcher matcher = pattern.matcher(search + ",");
|
||||
|
@ -57,19 +58,15 @@ public class UserController {
|
|||
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
||||
@ResponseBody
|
||||
public List<User> findAllBySpecification(@RequestParam(value = "search") final String search) {
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
if (search != null) {
|
||||
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
final Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder = builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||
}
|
||||
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
final Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||
}
|
||||
|
||||
final Specification<User> spec = builder.build();
|
||||
if (spec == null)
|
||||
return dao.findAll();
|
||||
else
|
||||
return dao.findAll(spec);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||
|
@ -91,6 +88,8 @@ public class UserController {
|
|||
return mydao.findAll(exp);
|
||||
}
|
||||
|
||||
// API - WRITE
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/new")
|
||||
@ResponseBody
|
||||
public long addUser(@RequestParam("first") final String first, @RequestParam("last") final String last, @RequestParam("age") final int age) {
|
||||
|
@ -114,4 +113,5 @@ public class UserController {
|
|||
mydao.save(user);
|
||||
return user.getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue