use of eu.bcvsolutions.idm.core.api.dto.IdmRequestDto in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method find.
@Override
public <R extends Requestable> Page<R> find(Class<? extends R> dtoClass, Serializable requestId, BaseFilter filter, Pageable pageable, IdmBasePermission... permission) {
if (pageable == null) {
// pageable is required in spring data
pageable = PageRequest.of(0, Integer.MAX_VALUE);
}
ReadDtoService<R, BaseFilter> dtoReadService = getDtoService(dtoClass);
Page<R> originalPage = dtoReadService.find(filter, pageable, permission);
List<R> originals = originalPage.getContent();
List<R> results = new ArrayList<>();
IdmRequestDto request = requestService.get(requestId);
List<IdmRequestItemDto> items = this.findRequestItems(request.getId(), dtoClass);
originals.stream().forEach(dto -> {
IdmRequestItemDto item = //
items.stream().filter(//
i -> dto.getId().equals(i.getOwnerId())).findFirst().orElse(//
null);
if (item == null) {
// None item found -> result is original DTO
results.add(dto);
return;
}
if (Strings.isNullOrEmpty(item.getData())) {
// Item found, but does not contains any DTO. So original DTO will be result
// (with connected item)
addRequestItemToDto(dto, item);
results.add(dto);
return;
}
try {
// Item with data found. Data in the request is result
R requestedDto = this.convertItemToDto(item, dtoClass);
addEmbedded((AbstractDto) requestedDto, request.getId());
addRequestItemToDto((Requestable) requestedDto, item);
results.add(requestedDto);
return;
} catch (IOException | ReflectiveOperationException | IllegalArgumentException | IntrospectionException e) {
throw new ResultCodeException(CoreResultCode.JSON_CANNOT_BE_CONVERT_TO_DTO, ImmutableMap.of("json", item.getData()));
}
});
// !!Searching of added DTOs are very naive!!
// We use all UUID value in the filter and try to find it in the DTOs. It means
// only equals is implemented.
// Find potential parents
List<RequestPredicate> potencialParents = this.findPotencialParents(filter);
results.addAll(this.findRelatedAddedItems(request, potencialParents, items, dtoClass));
// Set all results as trimmed = true. FE expects trimmed value in the table.
results.forEach(result -> ((AbstractDto) result).setTrimmed(true));
return new PageImpl<>(results, pageable, originalPage.getTotalElements());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRequestDto in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method deleteRequestable.
@Override
@Transactional(noRollbackFor = { AcceptedException.class })
public <R extends Requestable> IdmRequestDto deleteRequestable(R dto, boolean executeImmediately) {
Assert.notNull(dto, "DTO is required.");
Assert.notNull(dto.getId(), "Requestable DTO cannot be null!");
// Create and save request
IdmRequestDto request = new IdmRequestDto();
this.initRequest(request, dto);
request.setExecuteImmediately(executeImmediately);
request = requestService.save(request);
// Create item
this.delete(request.getId(), dto);
// Start request
return this.startRequestInternal(request.getId(), true);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRequestDto in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method onDeleteRequestable.
@Override
@Transactional
public <R extends Requestable> void onDeleteRequestable(R requestable) {
Assert.notNull(requestable, "Requestable DTO cannot be null!");
// Search requests with that deleting owner
IdmRequestFilter requestFilter = new IdmRequestFilter();
requestFilter.setOwnerType(requestable.getClass().getName());
requestFilter.setOwnerId((UUID) requestable.getId());
List<IdmRequestDto> requests = requestService.find(requestFilter, null).getContent();
//
requests.stream().filter(// We need filtered request which
request -> RequestState.APPROVED != request.getState()).forEach(request -> {
//
request = changeRequestState(//
requestable, //
request, new //
ResultCodeException(//
CoreResultCode.REQUEST_OWNER_WAS_DELETED, //
ImmutableMap.of("owner", requestable.toString())));
requestService.save(request);
});
// Search request items with that deleting owner
IdmRequestItemFilter requestItemFilter = new IdmRequestItemFilter();
requestItemFilter.setOwnerType(requestable.getClass().getName());
requestItemFilter.setOwnerId((UUID) requestable.getId());
List<IdmRequestItemDto> requestItems = requestItemService.find(requestItemFilter, null).getContent();
//
requestItems.stream().filter(// We need filtered request which invoked that
item -> RequestState.APPROVED != item.getState()).forEach(item -> {
//
item = changeItemState(//
requestable, //
item, new //
ResultCodeException(//
CoreResultCode.REQUEST_OWNER_WAS_DELETED, //
ImmutableMap.of("owner", requestable.toString())));
requestItemService.save(item);
IdmRequestItemFilter subItemFilter = new IdmRequestItemFilter();
subItemFilter.setRequestId(item.getRequest());
// Search all items for that request
List<IdmRequestItemDto> subItems = requestItemService.find(subItemFilter, null).getContent();
// TODO: This can be (maybe) removed ... because that 'cancel' is implemented
// during realization of item
// Check if items in same request does not contains same ID of deleting owner in
// the DATA Json.
// If yes, then state will be changed to cancel.
//
subItems.stream().filter(// We need filtered request
subItem -> RequestState.APPROVED != subItem.getState()).filter(//
subItem -> !requestable.getId().equals(subItem.getOwnerId())).filter(//
subItem -> subItem.getData() != null).filter(subItem -> //
subItem.getData().indexOf(requestable.getId().toString()) != //
-1).forEach(subItem -> {
//
subItem = changeItemState(//
requestable, //
subItem, new //
ResultCodeException(//
CoreResultCode.REQUEST_OWNER_FROM_OTHER_REQUEST_WAS_DELETED, ImmutableMap.of("owner", requestable.toString(), "otherRequest", //
subItem.toString())));
requestItemService.save(subItem);
});
});
//
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRequestDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmRequestService method toDto.
@Override
public IdmRequestDto toDto(IdmRequest entity, IdmRequestDto dto) {
IdmRequestDto requestDto = super.toDto(entity, dto);
// Load and add WF process DTO to embedded. Prevents of many requests from FE.
if (requestDto != null && requestDto.getWfProcessId() != null) {
if (RequestState.IN_PROGRESS == requestDto.getState()) {
// Instance of process should exists only in 'IN_PROGRESS' state
WorkflowProcessInstanceDto processInstanceDto = workflowProcessInstanceService.get(requestDto.getWfProcessId());
// size
if (processInstanceDto != null) {
processInstanceDto.setProcessVariables(null);
}
requestDto.getEmbedded().put(AbstractRequestDto.WF_PROCESS_FIELD, processInstanceDto);
} else {
// In others states we need load historic process
WorkflowHistoricProcessInstanceDto processHistDto = workflowHistoricProcessInstanceService.get(requestDto.getWfProcessId());
// size
if (processHistDto != null) {
processHistDto.setProcessVariables(null);
}
requestDto.getEmbedded().put(AbstractRequestDto.WF_PROCESS_FIELD, processHistDto);
}
}
// Load and add owner DTO to embedded. Prevents of many requests from FE.
if (requestDto != null && requestDto.getOwnerId() != null && requestDto.getOwnerType() != null) {
try {
@SuppressWarnings("unchecked") Requestable requestable = requestManager.get(requestDto.getId(), requestDto.getOwnerId(), (Class<Requestable>) Class.forName(requestDto.getOwnerType()));
if (requestable instanceof AbstractDto) {
// If is requestable realized REMOVE, then requestable DTO does not contains
// data (only ID). In this case we don't want send this DTO to FE.
AbstractDto requestableDto = (AbstractDto) requestable;
IdmRequestItemDto itemDto = DtoUtils.getEmbedded(requestableDto, Requestable.REQUEST_ITEM_FIELD, IdmRequestItemDto.class, null);
if (itemDto != null && RequestOperationType.REMOVE == itemDto.getOperation() && itemDto.getState().isTerminatedState()) {
requestable = null;
}
// Minimise response size
requestableDto.setEmbedded(null);
}
if (requestable == null) {
// Entity was not found ... maybe was deleted or not exists yet
LOG.debug(MessageFormat.format("Owner [{0}, {1}] not found for request {2}.", requestDto.getOwnerType(), requestDto.getOwnerId(), requestDto.getId()));
}
requestDto.getEmbedded().put(IdmRequestDto.OWNER_FIELD, requestable);
} catch (ClassNotFoundException e) {
// Only print warning
LOG.warn(MessageFormat.format("Class not found for request {0}.", requestDto.getId()), e);
}
}
return requestDto;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRequestDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmRequestService method toEntity.
@Override
protected IdmRequest toEntity(IdmRequestDto dto, IdmRequest entity) {
if (this.isNew(dto)) {
dto.setResult(new OperationResultDto(OperationState.CREATED));
dto.setState(RequestState.CONCEPT);
} else if (dto.getResult() == null) {
IdmRequestDto persistedDto = this.get(dto.getId());
dto.setResult(persistedDto.getResult());
}
IdmRequest requestEntity = super.toEntity(dto, entity);
return requestEntity;
}
Aggregations