Search in sources :

Example 6 with SearchCriteria

use of org.baeldung.web.util.SearchCriteria in project tutorials by eugenp.

the class JPACriteriaQueryIntegrationTest method givenPartialFirst_whenGettingListOfUsers_thenCorrect.

@Test
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
    final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
    params.add(new SearchCriteria("firstName", ":", "jo"));
    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 7 with SearchCriteria

use of org.baeldung.web.util.SearchCriteria in project tutorials by eugenp.

the class JPACriteriaQueryIntegrationTest method givenLast_whenGettingListOfUsers_thenCorrect.

@Test
public void givenLast_whenGettingListOfUsers_thenCorrect() {
    final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
    params.add(new SearchCriteria("lastName", ":", "doe"));
    final List<User> results = userApi.searchUser(params);
    assertThat(userJohn, isIn(results));
    assertThat(userTom, 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 8 with SearchCriteria

use of org.baeldung.web.util.SearchCriteria in project tutorials by eugenp.

the class MyUserPredicatesBuilder method build.

public BooleanExpression build() {
    if (params.size() == 0) {
        return null;
    }
    final List<BooleanExpression> predicates = new ArrayList<>();
    MyUserPredicate predicate;
    for (final SearchCriteria param : params) {
        predicate = new MyUserPredicate(param);
        final BooleanExpression exp = predicate.getPredicate();
        if (exp != null) {
            predicates.add(exp);
        }
    }
    BooleanExpression result = predicates.get(0);
    for (int i = 1; i < predicates.size(); i++) {
        result = result.and(predicates.get(i));
    }
    return result;
}
Also used : BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) ArrayList(java.util.ArrayList) SearchCriteria(org.baeldung.web.util.SearchCriteria)

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