Search in sources :

Example 1 with PageImpl

use of org.springframework.data.domain.PageImpl in project CzechIdMng by bcvsolutions.

the class DefaultWorkflowTaskInstanceService method internalSearch.

private PageImpl<WorkflowTaskInstanceDto> internalSearch(WorkflowFilterDto filter, Pageable pageable) {
    // if currently logged user can read all task continue
    if (!canReadAllTask()) {
        // if user can't read all task check filter
        if (filter.getCandidateOrAssigned() == null) {
            filter.setCandidateOrAssigned(securityService.getCurrentId().toString());
        } else {
            IdmIdentityDto identity = (IdmIdentityDto) lookupService.lookupDto(IdmIdentityDto.class, filter.getCandidateOrAssigned());
            if (!identity.getId().equals(securityService.getCurrentId())) {
                throw new ResultCodeException(CoreResultCode.FORBIDDEN, "You do not have permission for access to all tasks!");
            }
        }
    // else is filled candidate and it is equals currently logged user
    }
    String processDefinitionId = filter.getProcessDefinitionId();
    Map<String, Object> equalsVariables = filter.getEqualsVariables();
    TaskQuery query = taskService.createTaskQuery();
    query.active();
    query.includeProcessVariables();
    if (processDefinitionId != null) {
        query.processDefinitionId(processDefinitionId);
    }
    if (filter.getProcessDefinitionKey() != null) {
        query.processDefinitionKey(filter.getProcessDefinitionKey());
    }
    if (filter.getProcessInstanceId() != null) {
        query.processInstanceId(filter.getProcessInstanceId());
    }
    if (filter.getId() != null) {
        query.taskId(filter.getId().toString());
    }
    if (filter.getCreatedAfter() != null) {
        query.taskCreatedAfter(filter.getCreatedAfter().toDate());
    }
    if (filter.getCreatedBefore() != null) {
        query.taskCreatedBefore(filter.getCreatedBefore().toDate());
    }
    if (equalsVariables != null) {
        for (Entry<String, Object> entry : equalsVariables.entrySet()) {
            query.processVariableValueEquals(entry.getKey(), entry.getValue());
        }
    }
    if (filter.getCandidateOrAssigned() != null) {
        BaseDto dto = lookupService.lookupDto(IdmIdentityDto.class, filter.getCandidateOrAssigned());
        Assert.notNull(dto);
        query.taskCandidateOrAssigned(String.valueOf(dto.getId()));
    }
    query.orderByTaskCreateTime();
    query.desc();
    long count = query.count();
    // it's possible that pageable is null
    List<Task> tasks = null;
    if (pageable == null) {
        tasks = query.list();
    } else {
        tasks = query.listPage((pageable.getPageNumber()) * pageable.getPageSize(), pageable.getPageSize());
    }
    List<WorkflowTaskInstanceDto> dtos = new ArrayList<>();
    if (tasks != null) {
        for (Task task : tasks) {
            dtos.add(toResource(task));
        }
    }
    return new PageImpl<WorkflowTaskInstanceDto>(dtos, pageable, count);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Task(org.activiti.engine.task.Task) WorkflowTaskInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) ArrayList(java.util.ArrayList) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) TaskQuery(org.activiti.engine.task.TaskQuery) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)

Example 2 with PageImpl

use of org.springframework.data.domain.PageImpl 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)

Example 3 with PageImpl

use of org.springframework.data.domain.PageImpl in project CzechIdMng by bcvsolutions.

the class AbstractReadDtoController method toResources.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Resources<?> toResources(Iterable<?> source, Class<?> domainType) {
    if (source == null) {
        return new Resources(Collections.emptyList());
    }
    Page<Object> page;
    if (source instanceof Page) {
        page = (Page<Object>) source;
    } else {
        // Iterable to Page
        List records = Lists.newArrayList(source);
        page = new PageImpl(records, new PageRequest(0, records.size()), records.size());
    }
    return pageToResources(page, domainType);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) List(java.util.List) Resources(org.springframework.hateoas.Resources)

Example 4 with PageImpl

use of org.springframework.data.domain.PageImpl in project CzechIdMng by bcvsolutions.

the class AbstractSchedulableStatefulExecutor method getItemFromQueue.

private Page<IdmProcessedTaskItemDto> getItemFromQueue(UUID entityRef) {
    // TODO: this is probably not good idea, but for now it is only choice
    if (this.getScheduledTaskId() == null) {
        return new PageImpl<>(Collections.emptyList());
    }
    IdmProcessedTaskItemFilter filter = new IdmProcessedTaskItemFilter();
    filter.setReferencedEntityId(entityRef);
    filter.setScheduledTaskId(this.getScheduledTaskId());
    Page<IdmProcessedTaskItemDto> p = itemService.find(filter, new PageRequest(0, 1));
    if (p.getTotalElements() > 1) {
        LOG.warn("Multiple same item references found in [{}] process queue.", this.getClass());
    }
    return p;
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) PageRequest(org.springframework.data.domain.PageRequest) IdmProcessedTaskItemFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmProcessedTaskItemFilter) IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto)

Example 5 with PageImpl

use of org.springframework.data.domain.PageImpl in project CzechIdMng by bcvsolutions.

the class AbstractSchedulableStatefulExecutorUnitTest method testDontTouchProcessed.

@Test
public void testDontTouchProcessed() {
    IdmLongRunningTaskDto lrt = new IdmLongRunningTaskDto();
    IdmScheduledTaskDto scheduledTask = new IdmScheduledTaskDto();
    scheduledTask.setId(UUID.randomUUID());
    // 
    IdmIdentityDto dto1 = getTestIdentityDto();
    IdmIdentityDto dto2 = getTestIdentityDto();
    // stubs
    doReturn(true).when(executor).isInProcessedQueue(any(IdmIdentityDto.class));
    doReturn(Lists.newArrayList(dto1.getId(), dto2.getId())).when(executor).getProcessedItemRefsFromQueue();
    doReturn(scheduledTask.getId()).when(executor).getScheduledTaskId();
    // matchers
    when(longRunningTaskService.get(any(UUID.class))).thenReturn(lrt);
    when(executor.getItemsToProcess(any(Pageable.class))).thenReturn(new PageImpl<>(Lists.newArrayList(dto1, dto2))).thenReturn(new PageImpl<>(Lists.newArrayList()));
    // 
    // 
    Boolean processingResult = executor.process();
    assertTrue(processingResult);
    // 
    // 
    verify(longRunningTaskService, times(2)).get(any(UUID.class));
    // 
    verify(executor, times(1)).getItemsToProcess(any(Pageable.class));
    verify(executor, times(1)).isInProcessedQueue(dto1);
    verify(executor, times(1)).isInProcessedQueue(dto2);
    verify(executor, times(1)).getProcessedItemRefsFromQueue();
    verify(executor, never()).getScheduledTaskId();
    verify(executor, never()).removeFromProcessedQueue(any(UUID.class));
    verify(executor, never()).addToProcessedQueue(any(IdmIdentityDto.class), any(OperationResult.class));
    verify(executor, never()).processItem(any(IdmIdentityDto.class));
    verify(itemService, never()).saveInternal(any(IdmProcessedTaskItemDto.class));
    verify(itemService, never()).deleteInternal(any(IdmProcessedTaskItemDto.class));
    verify(itemService, never()).find(any(IdmProcessedTaskItemFilter.class), any(Pageable.class));
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) Pageable(org.springframework.data.domain.Pageable) IdmScheduledTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) IdmProcessedTaskItemFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmProcessedTaskItemFilter) IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) AbstractVerifiableUnitTest(eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest) Test(org.junit.Test)

Aggregations

PageImpl (org.springframework.data.domain.PageImpl)18 ArrayList (java.util.ArrayList)9 PageRequest (org.springframework.data.domain.PageRequest)7 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)4 List (java.util.List)4 Test (org.junit.Test)4 IdmProcessedTaskItemDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto)3 IdmProcessedTaskItemFilter (eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmProcessedTaskItemFilter)3 UUID (java.util.UUID)3 Page (org.springframework.data.domain.Page)3 Pageable (org.springframework.data.domain.Pageable)3 Sort (org.springframework.data.domain.Sort)3 Order (org.springframework.data.domain.Sort.Order)3 IdmEntityEventDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityEventDto)2 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)2 LookupService (eu.bcvsolutions.idm.core.api.service.LookupService)2 Collectors (java.util.stream.Collectors)2 Resources (org.springframework.hateoas.Resources)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Lists (com.google.common.collect.Lists)1