commit
2b1243e6aa
|
@ -8,7 +8,21 @@ import org.baeldung.web.util.SearchCriteria;
|
|||
import com.mysema.query.types.expr.BooleanExpression;
|
||||
|
||||
public class MyUserPredicatesBuilder {
|
||||
public static BooleanExpression buildUserPredicates(final List<SearchCriteria> params) {
|
||||
private List<SearchCriteria> params;
|
||||
|
||||
public MyUserPredicatesBuilder() {
|
||||
params = new ArrayList<SearchCriteria>();
|
||||
}
|
||||
|
||||
public MyUserPredicatesBuilder with(final String key, final String operation, final Object value) {
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||
final List<SearchCriteria> newParams = new ArrayList<SearchCriteria>(params);
|
||||
newParams.add(new SearchCriteria(key, operation, value));
|
||||
builder.params = newParams;
|
||||
return builder;
|
||||
}
|
||||
|
||||
public BooleanExpression build() {
|
||||
if (params.size() == 0)
|
||||
return null;
|
||||
|
||||
|
|
|
@ -10,7 +10,21 @@ import org.springframework.data.jpa.domain.Specifications;
|
|||
|
||||
public class UserSpecificationsBuilder {
|
||||
|
||||
public static Specification<User> buildUserSpecs(final List<SearchCriteria> params) {
|
||||
private 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;
|
||||
}
|
||||
|
||||
public Specification<User> build() {
|
||||
if (params.size() == 0)
|
||||
return null;
|
||||
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
package org.baeldung.persistence.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.dao.IUserDAO;
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.web.util.SearchCriteria;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class UserService {
|
||||
@Autowired
|
||||
private IUserDAO dao;
|
||||
|
||||
@Autowired
|
||||
private UserRepository repository;
|
||||
|
||||
public UserService() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void saveUser(final User user) {
|
||||
dao.save(user);
|
||||
}
|
||||
|
||||
public List<User> searchUser(final List<SearchCriteria> params) {
|
||||
return dao.searchUser(params);
|
||||
}
|
||||
|
||||
public List<User> searchBySpecification(final List<SearchCriteria> params) {
|
||||
final Specification<User> spec = UserSpecificationsBuilder.buildUserSpecs(params);
|
||||
if (spec == null)
|
||||
return repository.findAll();
|
||||
return repository.findAll(spec);
|
||||
}
|
||||
}
|
|
@ -5,26 +5,35 @@ import java.util.List;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.baeldung.persistence.dao.IUserDAO;
|
||||
import org.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||
import org.baeldung.persistence.dao.MyUserRepository;
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import org.baeldung.persistence.model.MyUser;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.persistence.service.impl.MyUserService;
|
||||
import org.baeldung.persistence.service.impl.UserService;
|
||||
import org.baeldung.web.util.SearchCriteria;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import com.mysema.query.types.expr.BooleanExpression;
|
||||
|
||||
@Controller
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService service;
|
||||
private IUserDAO service;
|
||||
|
||||
@Autowired
|
||||
private MyUserService myService;
|
||||
private UserRepository dao;
|
||||
|
||||
@Autowired
|
||||
private MyUserRepository mydao;
|
||||
|
||||
public UserController() {
|
||||
super();
|
||||
|
@ -47,32 +56,39 @@ public class UserController {
|
|||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
||||
@ResponseBody
|
||||
public List<User> findAllBySpecification(@RequestParam(value = "search", required = false) final String search) {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
|
||||
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()) {
|
||||
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
||||
builder = builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||
}
|
||||
}
|
||||
return service.searchBySpecification(params);
|
||||
final Specification<User> spec = builder.build();
|
||||
if (spec == null)
|
||||
return dao.findAll();
|
||||
else
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search", required = false) final String search) {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {
|
||||
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||
|
||||
if (search != null) {
|
||||
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
final Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
||||
builder = builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||
}
|
||||
}
|
||||
return myService.search(params);
|
||||
final BooleanExpression exp = builder.build();
|
||||
if (exp == null)
|
||||
return mydao.findAll();
|
||||
else
|
||||
return mydao.findAll(exp);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/new")
|
||||
|
@ -83,7 +99,7 @@ public class UserController {
|
|||
user.setLastName(last);
|
||||
user.setEmail("john@doe.com");
|
||||
user.setAge(age);
|
||||
service.saveUser(user);
|
||||
dao.save(user);
|
||||
return user.getId();
|
||||
}
|
||||
|
||||
|
@ -95,7 +111,7 @@ public class UserController {
|
|||
user.setLastName(last);
|
||||
user.setEmail("john@doe.com");
|
||||
user.setAge(age);
|
||||
myService.save(user);
|
||||
mydao.save(user);
|
||||
return user.getId();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,13 +6,10 @@ import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInA
|
|||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||
import org.baeldung.persistence.dao.MyUserRepository;
|
||||
import org.baeldung.persistence.model.MyUser;
|
||||
import org.baeldung.persistence.service.impl.MyUserService;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.baeldung.web.util.SearchCriteria;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -29,7 +26,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
public class JPAQuerydslTest {
|
||||
|
||||
@Autowired
|
||||
private MyUserService service;
|
||||
private MyUserRepository repo;
|
||||
|
||||
private MyUser userJohn;
|
||||
|
||||
|
@ -42,32 +39,29 @@ public class JPAQuerydslTest {
|
|||
userJohn.setLastName("Doe");
|
||||
userJohn.setEmail("john@doe.com");
|
||||
userJohn.setAge(22);
|
||||
service.save(userJohn);
|
||||
repo.save(userJohn);
|
||||
|
||||
userTom = new MyUser();
|
||||
userTom.setFirstName("Tom");
|
||||
userTom.setLastName("Doe");
|
||||
userTom.setEmail("tom@doe.com");
|
||||
userTom.setAge(26);
|
||||
service.save(userTom);
|
||||
repo.save(userTom);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("lastName", ":", "Doe"));
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "Doe");
|
||||
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
assertThat(results, containsInAnyOrder(userJohn, userTom));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "John"));
|
||||
params.add(new SearchCriteria("lastName", ":", "Doe"));
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "John").with("lastName", ":", "Doe");
|
||||
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
|
||||
assertThat(results, contains(userJohn));
|
||||
assertThat(results, not(contains(userTom)));
|
||||
|
@ -75,11 +69,9 @@ public class JPAQuerydslTest {
|
|||
|
||||
@Test
|
||||
public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("lastName", ":", "Doe"));
|
||||
params.add(new SearchCriteria("age", ">", "25"));
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "Doe").with("age", ">", "25");
|
||||
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
|
||||
assertThat(results, contains(userTom));
|
||||
assertThat(results, not(contains(userJohn)));
|
||||
|
@ -87,20 +79,17 @@ public class JPAQuerydslTest {
|
|||
|
||||
@Test
|
||||
public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "Adam"));
|
||||
params.add(new SearchCriteria("lastName", ":", "Fox"));
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "Adam").with("lastName", ":", "Fox");
|
||||
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
assertThat(results, emptyIterable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "jo"));
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "jo");
|
||||
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
|
||||
assertThat(results, contains(userJohn));
|
||||
assertThat(results, not(contains(userTom)));
|
||||
|
|
Loading…
Reference in New Issue