Search in sources :

Example 1 with PageableDefault

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);
}
Also used : PagedResourcesAssembler(org.springframework.data.web.PagedResourcesAssembler) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ApiParam(io.swagger.annotations.ApiParam) StringUtils(org.apache.commons.lang3.StringUtils) Valid(javax.validation.Valid) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreGroupPermission(eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission) ApiOperation(io.swagger.annotations.ApiOperation) DataFilter(eu.bcvsolutions.idm.core.api.dto.filter.DataFilter) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) SwaggerConfig(eu.bcvsolutions.idm.core.api.config.swagger.SwaggerConfig) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) AuthorizationScope(io.swagger.annotations.AuthorizationScope) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) Api(io.swagger.annotations.Api) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) MultiValueMap(org.springframework.util.MultiValueMap) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) SchedulerManager(eu.bcvsolutions.idm.core.scheduler.api.service.SchedulerManager) Page(org.springframework.data.domain.Page) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ParameterConverter(eu.bcvsolutions.idm.core.api.utils.ParameterConverter) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) BaseController(eu.bcvsolutions.idm.core.api.rest.BaseController) AbstractTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) PageableDefault(org.springframework.data.web.PageableDefault) Resources(org.springframework.hateoas.Resources) ResponseEntity(org.springframework.http.ResponseEntity) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) PageImpl(org.springframework.data.domain.PageImpl) Authorization(io.swagger.annotations.Authorization) PageImpl(org.springframework.data.domain.PageImpl) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) Sort(org.springframework.data.domain.Sort) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

SwaggerConfig (eu.bcvsolutions.idm.core.api.config.swagger.SwaggerConfig)1 DataFilter (eu.bcvsolutions.idm.core.api.dto.filter.DataFilter)1 BaseController (eu.bcvsolutions.idm.core.api.rest.BaseController)1 LookupService (eu.bcvsolutions.idm.core.api.service.LookupService)1 ParameterConverter (eu.bcvsolutions.idm.core.api.utils.ParameterConverter)1 CoreGroupPermission (eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission)1 AbstractTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger)1 CronTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger)1 DependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger)1 SimpleTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)1 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)1 SchedulerManager (eu.bcvsolutions.idm.core.scheduler.api.service.SchedulerManager)1 Api (io.swagger.annotations.Api)1 ApiImplicitParam (io.swagger.annotations.ApiImplicitParam)1 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiParam (io.swagger.annotations.ApiParam)1 Authorization (io.swagger.annotations.Authorization)1 AuthorizationScope (io.swagger.annotations.AuthorizationScope)1 List (java.util.List)1