use of org.baeldung.web.util.SearchCriteria in project tutorials by eugenp.
the class UserController method findAll.
// API - READ
@RequestMapping(method = RequestMethod.GET, value = "/users")
@ResponseBody
public List<User> findAll(@RequestParam(value = "search", required = false) String search) {
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
if (search != null) {
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
Matcher matcher = pattern.matcher(search + ",");
while (matcher.find()) {
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
}
}
return service.searchUser(params);
}
use of org.baeldung.web.util.SearchCriteria in project tutorials by eugenp.
the class JPACriteriaQueryIntegrationTest method givenFirstAndLastName_whenGettingListOfUsers_thenCorrect.
@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 List<User> results = userApi.searchUser(params);
assertThat(userJohn, isIn(results));
assertThat(userTom, not(isIn(results)));
}
use of org.baeldung.web.util.SearchCriteria in project tutorials by eugenp.
the class JPACriteriaQueryIntegrationTest method givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect.
@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 List<User> results = userApi.searchUser(params);
assertThat(userJohn, not(isIn(results)));
assertThat(userTom, not(isIn(results)));
}
use of org.baeldung.web.util.SearchCriteria in project tutorials by eugenp.
the class UserDAO method searchUser.
@Override
public List<User> searchUser(final List<SearchCriteria> params) {
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<User> query = builder.createQuery(User.class);
final Root r = query.from(User.class);
Predicate predicate = builder.conjunction();
for (final SearchCriteria param : params) {
if (param.getOperation().equalsIgnoreCase(">")) {
predicate = builder.and(predicate, builder.greaterThanOrEqualTo(r.get(param.getKey()), param.getValue().toString()));
} else if (param.getOperation().equalsIgnoreCase("<")) {
predicate = builder.and(predicate, builder.lessThanOrEqualTo(r.get(param.getKey()), param.getValue().toString()));
} else if (param.getOperation().equalsIgnoreCase(":")) {
if (r.get(param.getKey()).getJavaType() == String.class) {
predicate = builder.and(predicate, builder.like(r.get(param.getKey()), "%" + param.getValue() + "%"));
} else {
predicate = builder.and(predicate, builder.equal(r.get(param.getKey()), param.getValue()));
}
}
}
query.where(predicate);
return entityManager.createQuery(query).getResultList();
}
use of org.baeldung.web.util.SearchCriteria in project tutorials by eugenp.
the class JPACriteriaQueryIntegrationTest method givenLastAndAge_whenGettingListOfUsers_thenCorrect.
@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 List<User> results = userApi.searchUser(params);
assertThat(userTom, isIn(results));
assertThat(userJohn, not(isIn(results)));
}
Aggregations