Search in sources :

Example 1 with SearchCriteria

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);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) SearchCriteria(org.baeldung.web.util.SearchCriteria) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with SearchCriteria

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)));
}
Also used : User(org.baeldung.persistence.model.User) ArrayList(java.util.ArrayList) SearchCriteria(org.baeldung.web.util.SearchCriteria) Test(org.junit.Test)

Example 3 with SearchCriteria

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)));
}
Also used : User(org.baeldung.persistence.model.User) ArrayList(java.util.ArrayList) SearchCriteria(org.baeldung.web.util.SearchCriteria) Test(org.junit.Test)

Example 4 with SearchCriteria

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) User(org.baeldung.persistence.model.User) Root(javax.persistence.criteria.Root) SearchCriteria(org.baeldung.web.util.SearchCriteria) Predicate(javax.persistence.criteria.Predicate)

Example 5 with SearchCriteria

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)));
}
Also used : User(org.baeldung.persistence.model.User) ArrayList(java.util.ArrayList) SearchCriteria(org.baeldung.web.util.SearchCriteria) Test(org.junit.Test)

Aggregations

SearchCriteria (org.baeldung.web.util.SearchCriteria)8 ArrayList (java.util.ArrayList)7 User (org.baeldung.persistence.model.User)6 Test (org.junit.Test)5 BooleanExpression (com.querydsl.core.types.dsl.BooleanExpression)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 Root (javax.persistence.criteria.Root)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1