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);
}
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);
}
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);
}
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;
}
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));
}
Aggregations