Search in sources :

Example 6 with SpecSearchCriteria

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

the class JPASpecificationIntegrationTest method givenFirstOrLastNameAndAgeGenericBuilder_whenGettingListOfUsers_thenCorrect.

@Test
public void givenFirstOrLastNameAndAgeGenericBuilder_whenGettingListOfUsers_thenCorrect() {
    GenericSpecificationsBuilder<User> builder = new GenericSpecificationsBuilder<>();
    Function<SpecSearchCriteria, Specification<User>> converter = UserSpecification::new;
    CriteriaParser parser = new CriteriaParser();
    List<User> results = repository.findAll(builder.build(parser.parse("( lastName:doe OR firstName:john ) AND age:22"), converter));
    assertThat(results, hasSize(1));
    assertThat(userJohn, isIn(results));
    assertThat(userTom, not(isIn(results)));
}
Also used : User(org.baeldung.persistence.model.User) CriteriaParser(org.baeldung.web.util.CriteriaParser) GenericSpecificationsBuilder(org.baeldung.persistence.dao.GenericSpecificationsBuilder) UserSpecification(org.baeldung.persistence.dao.UserSpecification) Specification(org.springframework.data.jpa.domain.Specification) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Example 7 with SpecSearchCriteria

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

the class JPASpecificationIntegrationTest method givenMinAge_whenGettingListOfUsers_thenCorrect.

@Test
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
    final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "25"));
    final List<User> results = repository.findAll(Specifications.where(spec));
    assertThat(userTom, isIn(results));
    assertThat(userJohn, not(isIn(results)));
}
Also used : UserSpecification(org.baeldung.persistence.dao.UserSpecification) User(org.baeldung.persistence.model.User) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Example 8 with SpecSearchCriteria

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

the class JPASpecificationIntegrationTest method givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect.

@Test
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
    final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.ENDS_WITH, "n"));
    final List<User> results = repository.findAll(spec);
    assertThat(userJohn, isIn(results));
    assertThat(userTom, not(isIn(results)));
}
Also used : UserSpecification(org.baeldung.persistence.dao.UserSpecification) User(org.baeldung.persistence.model.User) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Example 9 with SpecSearchCriteria

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

the class JPASpecificationIntegrationTest method givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect.

@Test
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
    final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.STARTS_WITH, "jo"));
    final List<User> results = repository.findAll(spec);
    assertThat(userJohn, isIn(results));
    assertThat(userTom, not(isIn(results)));
}
Also used : UserSpecification(org.baeldung.persistence.dao.UserSpecification) User(org.baeldung.persistence.model.User) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Example 10 with SpecSearchCriteria

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

the class GenericSpecificationsBuilder method with.

public final GenericSpecificationsBuilder<U> with(final String precedenceIndicator, final String key, final String operation, final Object value, final String prefix, final String suffix) {
    SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
    if (op != null) {
        if (// the operation may be complex operation
        op == SearchOperation.EQUALITY) {
            final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
            final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
            if (startWithAsterisk && endWithAsterisk) {
                op = SearchOperation.CONTAINS;
            } else if (startWithAsterisk) {
                op = SearchOperation.ENDS_WITH;
            } else if (endWithAsterisk) {
                op = SearchOperation.STARTS_WITH;
            }
        }
        params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
    }
    return this;
}
Also used : SearchOperation(org.baeldung.web.util.SearchOperation) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria)

Aggregations

SpecSearchCriteria (org.baeldung.web.util.SpecSearchCriteria)11 User (org.baeldung.persistence.model.User)9 Test (org.junit.Test)9 UserSpecification (org.baeldung.persistence.dao.UserSpecification)8 SearchOperation (org.baeldung.web.util.SearchOperation)2 GenericSpecificationsBuilder (org.baeldung.persistence.dao.GenericSpecificationsBuilder)1 UserSpecificationsBuilder (org.baeldung.persistence.dao.UserSpecificationsBuilder)1 CriteriaParser (org.baeldung.web.util.CriteriaParser)1 Specification (org.springframework.data.jpa.domain.Specification)1