builder improvements

This commit is contained in:
eugenp 2015-01-29 14:14:36 +02:00
parent 425c681858
commit f0e18fc212
2 changed files with 22 additions and 26 deletions

View File

@ -8,40 +8,36 @@ import org.baeldung.web.util.SearchCriteria;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications; 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() { public UserSpecificationsBuilder() {
params = new ArrayList<SearchCriteria>(); params = new ArrayList<SearchCriteria>();
} }
public UserSpecificationsBuilder with(final String key, final String operation, final Object value) { // API
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
final List<SearchCriteria> newParams = new ArrayList<SearchCriteria>(params); public final UserSpecificationsBuilder with(final String key, final String operation, final Object value) {
newParams.add(new SearchCriteria(key, operation, value)); params.add(new SearchCriteria(key, operation, value));
builder.params = newParams; return this;
return builder;
} }
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 = new ArrayList<Specification<User>>(); final List<Specification<User>> specs = new ArrayList<Specification<User>>();
for (final SearchCriteria param : params) { for (final SearchCriteria param : params) {
specs.add(new UserSpecification(param)); specs.add(new UserSpecification(param));
} }
if (specs.size() == 0)
return null;
Specification<User> result = specs.get(0); Specification<User> result = specs.get(0);
for (int i = 1; i < specs.size(); i++) { for (int i = 1; i < specs.size(); i++) {
result = Specifications.where(result).and(specs.get(i)); result = Specifications.where(result).and(specs.get(i));
} }
return result; return result;
} }
} }

View File

@ -39,11 +39,12 @@ public class UserController {
super(); super();
} }
// API - READ
@RequestMapping(method = RequestMethod.GET, value = "/users") @RequestMapping(method = RequestMethod.GET, value = "/users")
@ResponseBody @ResponseBody
public List<User> findAll(@RequestParam(value = "search", required = false) final String search) { public List<User> findAll(@RequestParam(value = "search", required = false) final String search) {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>(); final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
if (search != null) { if (search != null) {
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),"); final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
final Matcher matcher = pattern.matcher(search + ","); final Matcher matcher = pattern.matcher(search + ",");
@ -57,19 +58,15 @@ public class UserController {
@RequestMapping(method = RequestMethod.GET, value = "/users/spec") @RequestMapping(method = RequestMethod.GET, value = "/users/spec")
@ResponseBody @ResponseBody
public List<User> findAllBySpecification(@RequestParam(value = "search") final String search) { public List<User> findAllBySpecification(@RequestParam(value = "search") final String search) {
UserSpecificationsBuilder builder = new UserSpecificationsBuilder(); final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
if (search != null) { final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),"); final Matcher matcher = pattern.matcher(search + ",");
final Matcher matcher = pattern.matcher(search + ","); while (matcher.find()) {
while (matcher.find()) { builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
builder = builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
}
} }
final Specification<User> spec = builder.build(); final Specification<User> spec = builder.build();
if (spec == null) return dao.findAll(spec);
return dao.findAll();
else
return dao.findAll(spec);
} }
@RequestMapping(method = RequestMethod.GET, value = "/myusers") @RequestMapping(method = RequestMethod.GET, value = "/myusers")
@ -91,6 +88,8 @@ public class UserController {
return mydao.findAll(exp); return mydao.findAll(exp);
} }
// API - WRITE
@RequestMapping(method = RequestMethod.GET, value = "/users/new") @RequestMapping(method = RequestMethod.GET, value = "/users/new")
@ResponseBody @ResponseBody
public long addUser(@RequestParam("first") final String first, @RequestParam("last") final String last, @RequestParam("age") final int age) { 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); mydao.save(user);
return user.getId(); return user.getId();
} }
} }