use of eu.bcvsolutions.idm.core.api.dto.IdmRequestItemDto in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method executeRequestInternal.
@SuppressWarnings("unchecked")
private IdmRequestDto executeRequestInternal(UUID requestId) {
Assert.notNull(requestId, "Role request ID is required!");
IdmRequestDto request = requestService.get(requestId);
Assert.notNull(request, "Role request is required!");
// Validate request
List<IdmRequestItemDto> items = this.findRequestItems(request.getId(), null);
if (items.isEmpty()) {
throw new ResultCodeException(CoreResultCode.REQUEST_CANNOT_BE_EXECUTED_NONE_ITEMS, ImmutableMap.of("request", request.toString()));
}
List<IdmRequestItemDto> sortedItems = items.stream().sorted(Comparator.comparing(IdmRequestItemDto::getCreated)).collect(Collectors.toList());
// Validate items
//
sortedItems.stream().filter(//
item -> !item.getState().isTerminatedState()).filter(//
item -> !(RequestState.CONCEPT == item.getState() || RequestState.APPROVED == item.getState())).forEach(item -> {
//
throw new ResultCodeException(CoreResultCode.REQUEST_ITEM_CANNOT_BE_EXECUTED, ImmutableMap.of("item", item.toString(), "state", item.getState()));
});
//
sortedItems.stream().filter(item -> RequestOperationType.ADD == item.getOperation() || //
RequestOperationType.UPDATE == item.getOperation()).forEach(item -> {
//
// Get DTO service
Requestable dto = null;
try {
Class<? extends Requestable> dtoClass = (Class<? extends Requestable>) Class.forName(item.getOwnerType());
ReadWriteDtoService<Requestable, BaseFilter> dtoService = (ReadWriteDtoService<Requestable, BaseFilter>) getServiceByItem(item, dtoClass);
dto = this.convertItemToDto(item, dtoClass);
dtoService.validateDto((Requestable) dto);
} catch (Exception e) {
throw new RoleRequestException(CoreResultCode.REQUEST_ITEM_IS_NOT_VALID, ImmutableMap.of("dto", dto != null ? dto.toString() : null, "item", item.toString()), e);
}
});
// We have to ensure the referential integrity, because some items (his DTOs)
// could be child of terminated (Disapproved, Cancelled) item (DTO)
//
sortedItems.stream().filter(// We check terminated ADDed items (Executed state could not yet occur)
item -> item.getState().isTerminatedState()).filter(//
item -> RequestOperationType.ADD == item.getOperation()).filter(//
item -> item.getOwnerId() != null).forEach(terminatedItem -> {
// Create predicate - find all DTOs with that UUID value in any fields
ImmutableList<RequestPredicate> predicates = ImmutableList.of(new RequestPredicate(terminatedItem.getOwnerId(), null));
//
sortedItems.stream().filter(//
item -> !item.getState().isTerminatedState()).filter(item -> {
// Is that item child of terminated item?
try {
Class<? extends Requestable> ownerType = (Class<? extends Requestable>) Class.forName(item.getOwnerType());
Requestable requestable = requestManager.convertItemToDto(item, ownerType);
List<Requestable> filteredDtos = requestManager.filterDtosByPredicates(ImmutableList.of(requestable), ownerType, predicates);
return filteredDtos.contains(requestable);
} catch (ClassNotFoundException | IOException e) {
throw new CoreException(e);
}
}).forEach(itemToCancel -> {
// This item could be not executed, because is use in other
// already terminated (added) item.
itemToCancel.setState(RequestState.CANCELED);
itemToCancel.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setException(new RoleRequestException(CoreResultCode.REQUEST_ITEM_NOT_EXECUTED_PARENT_CANCELED, ImmutableMap.of("item", itemToCancel.toString(), "terminatedItem", terminatedItem.toString()))).build());
requestItemService.save(itemToCancel);
});
});
// Reload items ... could be changed
items = this.findRequestItems(request.getId(), null);
List<IdmRequestItemDto> sortedItemsResult = items.stream().sorted(Comparator.comparing(IdmRequestItemDto::getCreated)).collect(Collectors.toList());
//
sortedItemsResult.stream().filter(//
item -> !item.getState().isTerminatedState()).forEach(item -> {
try {
this.resolveItem(item);
} catch (ClassNotFoundException | IOException e) {
throw new CoreException(e);
}
});
request.setState(RequestState.EXECUTED);
request.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).build());
return requestService.save(request);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRequestItemDto in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method getFormInstance.
@SuppressWarnings("unchecked")
@Override
public <R extends Requestable> IdmFormInstanceDto getFormInstance(UUID requestId, R owner, IdmFormDefinitionDto formDefinition, BasePermission... permission) {
Assert.notNull(requestId, "ID of request is required!");
Assert.notNull(owner, "Owner is required!");
Assert.notNull(formDefinition, "Form definition is required!");
boolean isNew = getDtoService(owner).isNew(owner);
List<IdmFormValueDto> requestValues = new ArrayList<>();
// (owner does not exist) would be throw.
if (!isNew) {
IdmFormInstanceDto previousFormInstance = formService.getFormInstance(owner, formDefinition, permission);
List<IdmFormValueDto> originalValues = previousFormInstance.getValues();
originalValues.forEach(value -> {
requestValues.add((IdmFormValueDto) this.get(requestId, (R) value));
});
}
IdmRequestDto request = requestService.get(requestId);
// Load all added items for that request
List<IdmRequestItemDto> addedItems = this.findRequestItems(request.getId(), IdmFormValueDto.class, RequestOperationType.ADD);
// Find added items for that owner ID
List<R> relatedAddedItems = this.findRelatedAddedItems(request, ImmutableList.of(new RequestPredicate((UUID) owner.getId(), "ownerId")), addedItems, (Class<? extends R>) IdmFormValueDto.class);
requestValues.addAll((Collection<? extends IdmFormValueDto>) relatedAddedItems);
return new IdmFormInstanceDto(owner, formDefinition, requestValues);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRequestItemDto in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method delete.
@SuppressWarnings("unchecked")
@Override
@Transactional
public <R extends Requestable> R delete(Serializable requestId, R dto, BasePermission... permission) {
Assert.notNull(dto, "DTO is required!");
Assert.notNull(requestId, "Request ID is required!");
IdmRequestDto request = requestService.get(requestId);
Assert.notNull(request, "Request is required!");
// update existing item
if (request != null && !(RequestState.CONCEPT == request.getState() || RequestState.IN_PROGRESS == request.getState() || RequestState.EXCEPTION == request.getState())) {
throw new ResultCodeException(CoreResultCode.REQUEST_ITEM_CANNOT_BE_CREATED, ImmutableMap.of("dto", dto.toString(), "state", request.getState().name()));
}
// Exists item for same original owner?
IdmRequestItemDto item = this.findRequestItem(request.getId(), dto);
// him.
if (item != null) {
requestItemService.delete(item);
return this.get(request.getId(), (UUID) dto.getId(), (Class<R>) dto.getClass(), permission);
}
// Check permissions on the target service
ReadDtoService<R, ?> dtoReadService = getDtoService(dto);
dtoReadService.checkAccess(dto, permission);
if (item == null) {
item = createRequestItem(request.getId(), dto);
}
item.setOwnerId((UUID) dto.getId());
item.setOperation(RequestOperationType.REMOVE);
item.setData(null);
// Update or create new request item
item = requestItemService.save(item);
// Set ID of request item to result DTO
dto.setRequestItem(item.getId());
return get(request.getId(), dto);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRequestItemDto in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method createRequestItem.
private IdmRequestItemDto createRequestItem(UUID requestId, Requestable dto) {
IdmRequestItemDto item = new IdmRequestItemDto();
if (dto instanceof IdmFormValueDto) {
IdmFormValueDto formValueDto = (IdmFormValueDto) dto;
item.setSuperOwnerType(formValueDto.getOwnerType().getName());
item.setSuperOwnerId((UUID) formValueDto.getOwnerId());
}
item.setRequest(requestId);
item.setOwnerType(dto.getClass().getName());
item.setResult(new OperationResultDto(OperationState.CREATED));
return item;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRequestItemDto in project CzechIdMng by bcvsolutions.
the class RequestManagerTest method testDeleteRequestable.
@Test
public void testDeleteRequestable() {
// Create role
IdmRoleDto changedRole = getHelper().createRole();
// Create request
IdmRequestDto request = requestManager.createRequest(changedRole);
Assert.assertNotNull(request);
Assert.assertEquals(request.getOwnerType(), changedRole.getClass().getName());
Assert.assertEquals(request.getOwnerId(), changedRole.getId());
// Change role (without save)
changedRole.setDescription(getHelper().createName());
changedRole.setPriority(1000);
// Create request item
Requestable requestable = requestManager.post(request.getId(), changedRole);
Assert.assertNotNull(requestable);
Assert.assertNotNull(requestable.getRequestItem());
Assert.assertTrue(requestable instanceof IdmRoleDto);
IdmRoleDto roleFromRequest = (IdmRoleDto) requestable;
// Is not same instance
Assert.assertTrue(changedRole != roleFromRequest);
// Has same values as new role
Assert.assertEquals(changedRole.getPriority(), roleFromRequest.getPriority());
Assert.assertEquals(changedRole.getDescription(), roleFromRequest.getDescription());
IdmRoleDto currentRole = roleService.get(changedRole.getId());
Assert.assertNotEquals(changedRole.getPriority(), currentRole.getPriority());
Assert.assertNotEquals(changedRole.getDescription(), currentRole.getDescription());
Assert.assertNotNull(requestItemService.get(requestable.getRequestItem()));
// Delete the role
roleService.delete(changedRole);
// Request item must be canceled
IdmRequestItemDto requestItem = requestItemService.get(requestable.getRequestItem());
Assert.assertNotNull(requestItem);
Assert.assertEquals(RequestState.CANCELED, requestItem.getState());
// Request must be canceled
request = requestService.get(request.getId());
Assert.assertNotNull(request);
Assert.assertEquals(RequestState.CANCELED, request.getState());
}
Aggregations