Last active
February 17, 2017 16:43
-
-
Save fernandodof/eca664081c5982b6e055512106256688 to your computer and use it in GitHub Desktop.
Enum problem with querydsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface CRUDService<T, K> { | |
public T create(T entity) throws ServiceException; | |
public T update(T entity) throws ServiceException; | |
public void remove(T entity) throws ServiceException; | |
public T findById(K id) throws ServiceException; | |
public Iterable<T> findAll() throws ServiceException; | |
public Page<T> findByFilter(String filter, String value, Pageable pageable) throws ServiceException; | |
default public Page<T> findByFilter(Predicate predicate, Pageable pageable) throws ServiceException { | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Caused by: java.lang.IllegalArgumentException: Parameter value [ACTIVE] did not match expected type [UserStatus (n/a)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SearchCriteria { | |
private String key; | |
private String operation; | |
private Object value; | |
//Constructor, getters and setters | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class User { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long id; | |
private String name; | |
private int age; | |
private UserStatus status; | |
//Constructor, getters and setters | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserPredicate { | |
private SearchCriteria criteria; | |
public BooleanExpression getPredicate() { | |
PathBuilder<User> entityPath = new PathBuilder<User>(User.class, "user"); | |
if (isNumeric(criteria.getValue().toString())) { | |
NumberPath<Integer> path = entityPath.getNumber(criteria.getKey(), Integer.class); | |
int value = Integer.parseInt(criteria.getValue().toString()); | |
if (criteria.getOperation().equalsIgnoreCase("=")) { | |
return path.eq(value); | |
} | |
else if (criteria.getOperation().equalsIgnoreCase(">")) { | |
return path.goe(value); | |
} | |
else if (criteria.getOperation().equalsIgnoreCase("<")) { | |
return path.loe(value); | |
} | |
} | |
else { | |
StringPath path = entityPath.getString(criteria.getKey()); | |
if (criteria.getOperation().equalsIgnoreCase("=")) { | |
return path.containsIgnoreCase(criteria.getValue().toString()); | |
} | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserPredicatesBuilder { | |
private List<SearchCriteria> params; | |
public UserPredicatesBuilder() { | |
params = new ArrayList<SearchCriteria>(); | |
} | |
public UserPredicatesBuilder with(String key, String operation, Object value) { | |
params.add(new SearchCriteria(key, operation, value)); | |
return this; | |
} | |
public BooleanExpression build() { | |
if (params.size() == 0) { | |
return null; | |
} | |
List<BooleanExpression> predicates = new ArrayList<BooleanExpression>(); | |
UserPredicate predicate; | |
for (SearchCriteria param : params) { | |
predicate = new UserPredicate(param); | |
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository("userRepository") | |
public interface UserRepository extends CrudRepository<User, Long>, QueryDslPredicateExecutor<User>, | |
QuerydslBinderCustomizer<QUser> { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Transactional | |
@Component | |
@RequestMapping("/users") | |
public class UserREST { | |
@Autowired | |
private UserPredicatesBuilder userPredicatesBuilder; | |
@RequestMapping(method = RequestMethod.GET, produces = "application/json") | |
public @ResponseBody ResponseEntity<?> getUsers( | |
@SortDefault(value = "name", direction = Direction.ASC) Pageable pageable, | |
@RequestParam Map<String, String> allParams) { | |
try { | |
for (Map.Entry<String, String> entry : allParams.entrySet()) { | |
String key = entry.getKey(); | |
String value = entry.getValue(); | |
if (!key.equals("page") && !key.equals("size")) { | |
this.userPredicatesBuilder.with(key, "=", value); | |
} | |
} | |
BooleanExpression booleanExpression = this.userPredicatesBuilder.build(); | |
Iterable<User> users = this.userService.findAll(booleanExpression, pageable); | |
return ResponseREST.getOkResponse(users); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
this.logger.error("", e); | |
return ResponseREST.getInternalServerErrorResponse(this.messageUtils.getUnexpectedErrorMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service("userService") | |
public class UserService implements CRUDService<User, Long> { | |
@Override | |
public Iterable<User> findAll() throws ServiceException { | |
try { | |
return this.userRepository.findAll(); | |
} catch (Exception e) { | |
throw new ServiceException(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum UserStatus { | |
ACTIVE, INACTIVE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment