PAA :
How to return using if else using Lambda expression ?
public static Specification<Employee> textInAllColumns(Object value) {
if (value instanceof String) {
return (root, query, builder) -> builder
.or(root.getModel().getDeclaredSingularAttributes().stream()
.filter(a -> {
return a.getJavaType()
.getSimpleName()
.equalsIgnoreCase("String") ? true : false;
})
.map(a -> builder.like(root.get(a.getName()), getString((String) value)))
.toArray(Predicate[]::new));
} else if (value instanceof Integer) {
return (root, query, builder) -> builder
.or(root.getModel().getDeclaredSingularAttributes().stream()
.filter(a -> {
return a.getJavaType()
.getSimpleName()
.equalsIgnoreCase("Integer") ? true : false;
})
.map(a -> builder.equal(root.get(a.getName()), value))
.toArray(Predicate[]::new));
}
}
I am getting below error:
This method must return a result of type Specification
@GetMapping("/findEmployees")
public ResponseEntity<List<Employee>> findEmployees(@RequestParam Object searchValue) {
List<Employee> employees = employeeService.searchGlobally(searchValue);
return new ResponseEntity<>(employees, HttpStatus.OK);
}
buræquete :
You can do something like this;
public static Specification<Employee> textInAllColumns(Object value) {
return (root, query, builder) -> builder.or(root.getModel().getDeclaredSingularAttributes().stream()
.filter(attr -> attr.getJavaType().equals(value.getClass()))
.map(attr -> map(value, root, builder, attr))
.toArray(Predicate[]::new));
}
private static Object map(Object value, Root root, CriteriaBuilder builder, SingularAttribute a) {
switch (value.getClass().getSimpleName()) {
case "String":
return builder.like(root.get(a.getName()), getString((String) value));
case "Integer":
return builder.equal(root.get(a.getName()), value);
case "Date":
return //date mapping
default:
return //default;
}
}
hiding the if
logic in the map()
method...
Update your endpoint to;
findEmployees(@RequestParam String searchValue) { }
Since all input can be accepted as type String
;
Object finalValue = searchValue;
try {
finalValue = Integer.parseInt(searchValue);
} catch (Exception e) {
// ignore
}
try {
finalValue = parseDateFromStr(searchValue);
} catch (Exception e) {
// ignore
}
// use finalValue as input to your logic, it will contain correct type