Skip to content

Instantly share code, notes, and snippets.

@fernandodof
Last active February 17, 2017 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandodof/eca664081c5982b6e055512106256688 to your computer and use it in GitHub Desktop.
Save fernandodof/eca664081c5982b6e055512106256688 to your computer and use it in GitHub Desktop.
Enum problem with querydsl
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;
}
}
Caused by: java.lang.IllegalArgumentException: Parameter value [ACTIVE] did not match expected type [UserStatus (n/a)]
public class SearchCriteria {
private String key;
private String operation;
private Object value;
//Constructor, getters and setters
}
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private int age;
private UserStatus status;
//Constructor, getters and setters
}
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;
}
}
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;
}
}
@Repository("userRepository")
public interface UserRepository extends CrudRepository<User, Long>, QueryDslPredicateExecutor<User>,
QuerydslBinderCustomizer<QUser> {
}
@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());
}
}
}
@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);
}
}
}
public enum UserStatus {
ACTIVE, INACTIVE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment