use of org.springframework.data.web.PageableDefault in project CzechIdMng by bcvsolutions.
the class SchedulerController method find.
/**
* Finds scheduled tasks
*
* @return all tasks
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@ResponseBody
@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("hasAuthority('" + CoreGroupPermission.SCHEDULER_READ + "')")
@ApiOperation(value = "Search scheduled tasks", nickname = "searchSchedulerTasks", tags = { SchedulerController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = CoreGroupPermission.SCHEDULER_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = CoreGroupPermission.SCHEDULER_READ, description = "") }) })
@ApiImplicitParams({ @ApiImplicitParam(name = "page", dataType = "string", paramType = "query", value = "Results page you want to retrieve (0..N)"), @ApiImplicitParam(name = "size", dataType = "string", paramType = "query", value = "Number of records per page."), @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "Sorting criteria in the format: property(,asc|desc). " + "Default sort order is ascending. " + "Multiple sort criteria are supported.") })
public Resources<Task> find(@RequestParam(required = false) MultiValueMap<String, Object> parameters, @PageableDefault Pageable pageable) {
String text = getParameterConverter().toString(parameters, DataFilter.PARAMETER_TEXT);
List<Task> tasks = schedulerService.getAllTasks().stream().filter(task -> {
// filter - like name or description only
return StringUtils.isEmpty(text) || task.getTaskType().getSimpleName().toLowerCase().contains(text.toLowerCase()) || (task.getDescription() != null && task.getDescription().toLowerCase().contains(text.toLowerCase()));
}).sorted((taskOne, taskTwo) -> {
Sort sort = pageable.getSort();
if (pageable.getSort() == null) {
return 0;
}
int compareAscValue = 0;
boolean asc = true;
// "naive" sort implementation
if (sort.getOrderFor(PROPERTY_TASK_TYPE) != null) {
asc = sort.getOrderFor(PROPERTY_TASK_TYPE).isAscending();
compareAscValue = taskOne.getTaskType().getSimpleName().compareTo(taskTwo.getTaskType().getSimpleName());
}
if (sort.getOrderFor(PROPERTY_DESCRIPTION) != null) {
asc = sort.getOrderFor(PROPERTY_DESCRIPTION).isAscending();
compareAscValue = taskOne.getDescription().compareTo(taskTwo.getDescription());
}
if (sort.getOrderFor(PROPERTY_INSTANCE_ID) != null) {
asc = sort.getOrderFor(PROPERTY_INSTANCE_ID).isAscending();
compareAscValue = taskOne.getInstanceId().compareTo(taskTwo.getInstanceId());
}
return asc ? compareAscValue : compareAscValue * -1;
}).collect(Collectors.toList());
// "naive" pagination
int first = pageable.getPageNumber() * pageable.getPageSize();
int last = pageable.getPageSize() + first;
List<Task> taskPage = tasks.subList(first < tasks.size() ? first : tasks.size() > 0 ? tasks.size() - 1 : 0, last < tasks.size() ? last : tasks.size());
//
return pageToResources(new PageImpl(taskPage, pageable, tasks.size()), Task.class);
}
Aggregations