Search in sources :

Example 1 with UserSpecificationsBuilder

use of org.baeldung.persistence.dao.UserSpecificationsBuilder in project tutorials by eugenp.

the class UserController method findAllBySpecification.

@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
@ResponseBody
public List<User> findAllBySpecification(@RequestParam(value = "search") String search) {
    UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
    String operationSetExper = Joiner.on("|").join(SearchOperation.SIMPLE_OPERATION_SET);
    Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
    Matcher matcher = pattern.matcher(search + ",");
    while (matcher.find()) {
        builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
    }
    Specification<User> spec = builder.build();
    return dao.findAll(spec);
}
Also used : Pattern(java.util.regex.Pattern) User(org.baeldung.persistence.model.User) MyUser(org.baeldung.persistence.model.MyUser) Matcher(java.util.regex.Matcher) UserSpecificationsBuilder(org.baeldung.persistence.dao.UserSpecificationsBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with UserSpecificationsBuilder

use of org.baeldung.persistence.dao.UserSpecificationsBuilder in project tutorials by eugenp.

the class UserController method resolveSpecification.

protected Specification<User> resolveSpecification(String searchParameters) {
    UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
    String operationSetExper = Joiner.on("|").join(SearchOperation.SIMPLE_OPERATION_SET);
    Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
    Matcher matcher = pattern.matcher(searchParameters + ",");
    while (matcher.find()) {
        builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
    }
    return builder.build();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) UserSpecificationsBuilder(org.baeldung.persistence.dao.UserSpecificationsBuilder)

Example 3 with UserSpecificationsBuilder

use of org.baeldung.persistence.dao.UserSpecificationsBuilder in project tutorials by eugenp.

the class JPASpecificationIntegrationTest method givenFirstOrLastName_whenGettingListOfUsers_thenCorrect.

@Test
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
    UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
    SpecSearchCriteria spec = new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john");
    SpecSearchCriteria spec1 = new SpecSearchCriteria("'", "lastName", SearchOperation.EQUALITY, "doe");
    List<User> results = repository.findAll(builder.with(spec).with(spec1).build());
    assertThat(results, hasSize(2));
    assertThat(userJohn, isIn(results));
    assertThat(userTom, isIn(results));
}
Also used : User(org.baeldung.persistence.model.User) UserSpecificationsBuilder(org.baeldung.persistence.dao.UserSpecificationsBuilder) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Aggregations

UserSpecificationsBuilder (org.baeldung.persistence.dao.UserSpecificationsBuilder)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 User (org.baeldung.persistence.model.User)2 MyUser (org.baeldung.persistence.model.MyUser)1 SpecSearchCriteria (org.baeldung.web.util.SpecSearchCriteria)1 Test (org.junit.Test)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1