Search in sources :

Example 6 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class CzechIdMIcConfigurationService method initDefaultConfiguration.

/**
 * Create instance of default connector configuration
 *
 * @param configurationClass
 * @return
 */
private IcConnectorConfiguration initDefaultConfiguration(Class<? extends ConfigurationClass> configurationClass) {
    try {
        ConfigurationClass configurationClassInstance = configurationClass.getDeclaredConstructor().newInstance();
        List<IcConfigurationProperty> properties = new ArrayList<>();
        PropertyDescriptor[] descriptors = Introspector.getBeanInfo(configurationClass).getPropertyDescriptors();
        Lists.newArrayList(descriptors).stream().forEach(descriptor -> {
            Method readMethod = descriptor.getReadMethod();
            String propertyName = descriptor.getName();
            ConfigurationClassProperty property = readMethod.getAnnotation(ConfigurationClassProperty.class);
            if (property != null) {
                IcConfigurationPropertyImpl icProperty = (IcConfigurationPropertyImpl) CzechIdMIcConvertUtil.convertConfigurationProperty(property);
                icProperty.setName(propertyName);
                icProperty.setType(readMethod.getGenericReturnType().getTypeName());
                try {
                    icProperty.setValue(readMethod.invoke(configurationClassInstance));
                } catch (IllegalArgumentException | ReflectiveOperationException e) {
                    throw new CoreException("Cannot read value of connector configuration property!", e);
                }
                properties.add(icProperty);
            }
        });
        // Sort by order
        properties.sort(Comparator.comparing(IcConfigurationProperty::getOrder));
        IcConfigurationPropertiesImpl icProperties = new IcConfigurationPropertiesImpl();
        icProperties.setProperties(properties);
        IcConnectorConfigurationImpl configuration = new IcConnectorConfigurationImpl();
        configuration.setConnectorPoolingSupported(false);
        configuration.setConfigurationProperties(icProperties);
        return configuration;
    } catch (IntrospectionException | ReflectiveOperationException e) {
        throw new CoreException("Cannot read connector configuration property!", e);
    }
}
Also used : ConfigurationClass(eu.bcvsolutions.idm.core.api.domain.ConfigurationClass) IcConnectorConfigurationImpl(eu.bcvsolutions.idm.ic.impl.IcConnectorConfigurationImpl) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) IcConfigurationPropertiesImpl(eu.bcvsolutions.idm.ic.impl.IcConfigurationPropertiesImpl) ConfigurationClassProperty(eu.bcvsolutions.idm.core.api.domain.ConfigurationClassProperty) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) IcConfigurationProperty(eu.bcvsolutions.idm.ic.api.IcConfigurationProperty) IcConfigurationPropertyImpl(eu.bcvsolutions.idm.ic.impl.IcConfigurationPropertyImpl)

Example 7 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class AbstractReportExecutor method generate.

@Override
public RptReportDto generate(RptReportDto report) {
    try {
        IdmAttachmentDto data = generateData(report);
        report.setData(data.getId());
        return report;
    } catch (ResultCodeException ex) {
        throw ex;
    } catch (Exception ex) {
        // TODO: better exception
        throw new CoreException(ex);
    }
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) FileNotFoundException(java.io.FileNotFoundException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException)

Example 8 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class AbstractRemoveBulkAction method processDto.

@Override
protected OperationResult processDto(DTO dto) {
    try {
        if (dto instanceof Requestable && requestConfiguration.isRequestModeEnabled(dto.getClass())) {
            // Request mode is enabled for that DTO
            Requestable requestable = (Requestable) dto;
            IdmRequestDto request = requestManager.deleteRequestable(requestable, false);
            if (RequestState.IN_PROGRESS == request.getState()) {
                throw new AcceptedException(request.getId().toString());
            }
            if (RequestState.EXCEPTION == request.getState()) {
                throw new CoreException(ExceptionUtils.resolveException(request.getResult().getException()));
            }
            return new OperationResult.Builder(request.getResult().getState()).setCause(request.getResult().getException()).build();
        }
        this.getService().delete(dto);
        return new OperationResult.Builder(OperationState.EXECUTED).build();
    } catch (AcceptedException ex) {
        return new OperationResult.Builder(OperationState.RUNNING).setException(ex).build();
    } catch (ResultCodeException ex) {
        return new OperationResult.Builder(OperationState.EXCEPTION).setException(ex).build();
    } catch (Exception ex) {
        Throwable resolvedException = ExceptionUtils.resolveException(ex);
        if (resolvedException instanceof ResultCodeException) {
            return // 
            new OperationResult.Builder(OperationState.EXCEPTION).setException(// 
            (ResultCodeException) resolvedException).build();
        }
        return new OperationResult.Builder(OperationState.EXCEPTION).setCause(ex).build();
    }
}
Also used : CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) Requestable(eu.bcvsolutions.idm.core.api.domain.Requestable) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) IdmRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRequestDto) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException)

Example 9 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class AbstractBulkAction method internalProcess.

@SuppressWarnings("unchecked")
public OperationResult internalProcess() {
    IdmBulkActionDto localAction = this.getAction();
    if (localAction == null) {
        // try to load bulk action from LRT persisted properties
        localAction = (IdmBulkActionDto) super.getProperties().get(PARAMETER_BULK_ACTION);
        // transform filter is needed
        if (localAction.getFilter() != null) {
            MultiValueMap<String, Object> multivaluedMap = new LinkedMultiValueMap<>();
            Map<String, Object> properties = localAction.getFilter();
            // 
            properties.entrySet().forEach((entry) -> {
                Object value = entry.getValue();
                if (value instanceof List<?>) {
                    multivaluedMap.put(entry.getKey(), (List<Object>) value);
                } else {
                    multivaluedMap.add(entry.getKey(), entry.getValue());
                }
            });
            // 
            Class<F> filterClass = getService().getFilterClass();
            F filter;
            if (DataFilter.class.isAssignableFrom(filterClass)) {
                // All filter properties has to be controlled by data filter (=> static fields only).
                try {
                    // data vs field properties
                    MultiValueMap<String, Object> fieldProperties = new LinkedMultiValueMap<>();
                    fieldProperties.addAll(multivaluedMap);
                    // 
                    for (Field declaredField : filterClass.getDeclaredFields()) {
                        // TODO: refactor all filters to pure DataFilter
                        if (!Modifier.isStatic(declaredField.getModifiers())) {
                            throw new CoreException(String.format("Declared filter [%s] has to fully support DataFilter, " + "refactor field [%s] to data usage before action can be executed asynchronously.", filterClass.getCanonicalName(), declaredField.getName()));
                        }
                    }
                    filter = filterClass.getDeclaredConstructor(MultiValueMap.class).newInstance(multivaluedMap);
                } catch (ReflectiveOperationException | IllegalArgumentException | SecurityException ex) {
                    throw new CoreException(String.format("Declared filter [%s] has to support constructor with parameters.", filterClass.getCanonicalName()), ex);
                }
            } else {
                filter = mapper.convertValue(multivaluedMap, filterClass);
            }
            localAction.setTransformedFilter(filter);
        }
    }
    Assert.notNull(localAction, "Bulk action is required.");
    // 
    StringBuilder description = new StringBuilder();
    IdmLongRunningTaskDto longRunningTask = this.getLongRunningTaskService().get(this.getLongRunningTaskId());
    description.append(longRunningTask.getTaskDescription());
    // 
    List<UUID> entities = getEntities(localAction, description);
    // 
    this.count = Long.valueOf(entities.size());
    this.counter = 0l;
    // 
    // update description
    longRunningTask.setTaskDescription(description.toString());
    longRunningTask.setCount(this.count);
    longRunningTask.setCounter(this.counter);
    this.getLongRunningTaskService().save(longRunningTask);
    // 
    return processEntities(entities);
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) IdmBulkActionDto(eu.bcvsolutions.idm.core.api.bulk.action.dto.IdmBulkActionDto) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

Example 10 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class UuidToUuidConverter method convert.

@Override
public UUID convert(MappingContext<UUID, UUID> context) {
    if (context.getSource() == null) {
        return null;
    }
    MappingContext<?, ?> parentContext = context.getParent();
    if (parentContext != null && parentContext.getDestination() != null && AbstractDto.class.isAssignableFrom(parentContext.getDestinationType()) && parentContext.getSource() != null && BaseEntity.class.isAssignableFrom(parentContext.getSourceType())) {
        try {
            AbstractDto parentDto = (AbstractDto) parentContext.getDestination();
            UUID entityId = context.getSource();
            Map<String, BaseDto> embedded = parentDto.getEmbedded();
            PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
            // Find name of field by property mapping
            String field = propertyMapping.getLastDestinationProperty().getName();
            // Find field in DTO class
            Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getDestinationType(), field);
            if (fieldTyp.isAnnotationPresent(Embedded.class)) {
                Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
                if (embeddedAnnotation.enabled()) {
                    // Load DTO service by dtoClass and get DTO by UUID
                    ReadDtoService<?, ?> lookup = getLookupService().getDtoService(embeddedAnnotation.dtoClass());
                    if (lookup != null) {
                        AbstractDto dto = (AbstractDto) lookup.get(entityId);
                        dto.setTrimmed(true);
                        embedded.put(field, dto);
                        // Add filled DTO to embedded map to parent DTO
                        parentDto.setEmbedded(embedded);
                    }
                }
            }
        } catch (NoSuchFieldException | SecurityException e) {
            throw new CoreException(e);
        }
    }
    return context.getSource();
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) PropertyMapping(org.modelmapper.spi.PropertyMapping) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) UUID(java.util.UUID)

Aggregations

CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)64 UUID (java.util.UUID)16 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)15 Test (org.junit.Test)14 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)11 AcceptedException (eu.bcvsolutions.idm.core.api.exception.AcceptedException)10 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)9 Field (java.lang.reflect.Field)9 Embedded (eu.bcvsolutions.idm.core.api.domain.Embedded)8 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)8 BaseEntity (eu.bcvsolutions.idm.core.api.entity.BaseEntity)8 List (java.util.List)8 Requestable (eu.bcvsolutions.idm.core.api.domain.Requestable)7 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)7 IdmRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRequestDto)7 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)7 RequestOperationType (eu.bcvsolutions.idm.core.api.domain.RequestOperationType)6 IntrospectionException (java.beans.IntrospectionException)6