use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.
the class AbstractReadDtoService method toDtos.
/**
* Converts list of entities to list of DTOs
*
* @param entities
* @param trimmed
* @return
*/
protected List<DTO> toDtos(List<E> entities, boolean trimmed) {
if (entities == null) {
return null;
}
List<DTO> dtos = new ArrayList<>();
entities.forEach(entity -> {
try {
DTO newDto = this.getDtoClass(entity).newInstance();
if (newDto instanceof AbstractDto) {
((AbstractDto) newDto).setTrimmed(trimmed);
}
dtos.add(this.toDto(entity, newDto));
} catch (InstantiationException | IllegalAccessException e) {
throw new CoreException(e);
}
});
return dtos;
}
use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.
the class AbstractSynchronizationExecutor method loggingException.
/**
* Log exception to SyncLog and SyncItemLog, do init syncActionLog
*
* @param synchronizationActionType
* @param log
* @param logItem
* @param actionLogs
* @param uid
* @param e
*/
private void loggingException(SynchronizationActionType synchronizationActionType, SysSyncLogDto log, SysSyncItemLogDto logItem, List<SysSyncActionLogDto> actionLogs, String uid, Exception e) {
String message = MessageFormat.format("Synchronization - exception during [{0}] for UID [{1}]", synchronizationActionType, uid);
log.setContainsError(true);
logItem.setMessage(message);
// prefer IdM exception on the top
Throwable ex = e;
if (!(e instanceof CoreException)) {
Throwable idmEx = ExceptionUtils.resolveException(e);
if (idmEx != e) {
addToItemLog(logItem, Throwables.getStackTraceAsString(idmEx));
ex = idmEx;
} else {
addToItemLog(logItem, Throwables.getStackTraceAsString(e));
}
} else {
addToItemLog(logItem, Throwables.getStackTraceAsString(e));
}
initSyncActionLog(synchronizationActionType, OperationResultType.ERROR, logItem, log, actionLogs);
LOG.error(message, ex);
}
use of eu.bcvsolutions.idm.core.api.exception.CoreException 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.exception.CoreException in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method dtoToMap.
private Map<String, Object> dtoToMap(AbstractDto dto) {
Map<String, Object> results = new HashMap<>();
if (dto == null) {
return results;
}
try {
List<PropertyDescriptor> descriptors = Lists.newArrayList(Introspector.getBeanInfo(dto.getClass()).getPropertyDescriptors());
List<Field> fields = //
Lists.newArrayList(dto.getClass().getDeclaredFields()).stream().filter(//
field -> !Requestable.REQUEST_ITEM_FIELD.equals(field.getName())).filter(//
field -> !Requestable.REQUEST_FIELD.equals(field.getName())).filter(//
field -> !field.isAnnotationPresent(JsonIgnore.class)).collect(//
Collectors.toList());
// Embedded objects
//
fields.stream().filter(//
field -> field.isAnnotationPresent(Embedded.class)).forEach(field -> {
results.put(field.getName(), dto.getEmbedded().get(field.getName()));
});
// Others objects
//
fields.stream().filter(//
field -> !field.isAnnotationPresent(Embedded.class)).forEach(field -> {
try {
PropertyDescriptor fieldDescriptor = //
descriptors.stream().filter(//
descriptor -> field.getName().equals(descriptor.getName())).findFirst().orElse(//
null);
if (fieldDescriptor != null) {
Object value = fieldDescriptor.getReadMethod().invoke(dto);
results.put(field.getName(), value);
}
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
throw new CoreException(e);
}
});
} catch (IntrospectionException e) {
throw new CoreException(e);
}
return results;
}
use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.
the class CzechIdMIcConfigurationService method createConnectorInstance.
/**
* Create instance of connector
*
* @param connectorInstance
* @return
*/
private IcConnector createConnectorInstance(IcConnectorInstance connectorInstance) {
IcConnector connector = null;
Class<? extends IcConnector> connectorClass = this.getConnectorClass(connectorInstance);
try {
connector = connectorClass.getDeclaredConstructor().newInstance();
// Manually autowire on this connector instance
this.applicationContext.getAutowireCapableBeanFactory().autowireBean(connector);
} catch (ReflectiveOperationException e) {
throw new CoreException(e);
}
return connector;
}
Aggregations