Search in sources :

Example 6 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class DefaultImportManager method removeRedundant.

/**
 * Ensures delete redundant entities in target IdM. Deletes DTOs are within
 * golden DTO type (super parent = typically system, role, ...).
 *
 * @param descriptor
 * @param context
 */
private void removeRedundant(ExportDescriptorDto descriptor, ImportContext context) {
    Class<? extends BaseDto> dtoClass = descriptor.getDtoClass();
    // Does this DTO support the authoritative mode?
    boolean supportsAuthoritativeMode = descriptor.isSupportsAuthoritativeMode();
    if (!supportsAuthoritativeMode) {
        return;
    }
    String superParentFilterProperty = descriptor.getSuperParentFilterProperty();
    Assert.notNull(superParentFilterProperty, "For authoritative mode must be superParentFilterProperty defined!");
    // Find super parent (gold) DTO (typically it is DTO for system, role ...)
    Class<? extends AbstractDto> superParentDtoClass = getSuperParentDtoClass(descriptor, context);
    Assert.notNull(superParentDtoClass, "Supper parent cannot be null here!");
    Path superParentDtoTypePath = Paths.get(context.getTempDirectory().toString(), superParentDtoClass.getSimpleName());
    try {
        // Find all super parent IDs for this DTO type in batch.
        Set<UUID> superParentIdsInBatch;
        try (Stream<Path> paths = Files.walk(superParentDtoTypePath)) {
            superParentIdsInBatch = // 
            paths.filter(// 
            Files::isRegularFile).map(path -> {
                BaseDto dto = convertFileToDto(path.toFile(), superParentDtoClass, context);
                return (UUID) dto.getId();
            }).collect(Collectors.toSet());
        }
        Set<Class<? extends BaseDto>> inheritedClasses = getInheritedClasses(dtoClass, context.getManifest());
        // Find all IDs for all children classes
        Set<Serializable> childrenIdsInBatch = Sets.newHashSet();
        for (Class<? extends BaseDto> inheritedClass : inheritedClasses) {
            // Find all IDs for this DTO type in batch.
            Path dtoTypePath = Paths.get(context.getTempDirectory().toString(), inheritedClass.getSimpleName());
            try (Stream<Path> paths = Files.walk(dtoTypePath)) {
                Set<Serializable> childrenIds = // 
                paths.filter(// 
                Files::isRegularFile).map(// 
                path -> convertFileToDto(path.toFile(), inheritedClass, context)).map(dto -> {
                    // If ID has been replaced, then we need to also replace it.
                    if (context.getReplacedIDs().containsKey((UUID) dto.getId())) {
                        return context.getReplacedIDs().get((UUID) dto.getId());
                    }
                    return dto.getId();
                }).collect(Collectors.toSet());
                childrenIdsInBatch.addAll(childrenIds);
            }
        }
        superParentIdsInBatch.forEach(superParentId -> {
            try {
                Class<? extends BaseDto> serviceDtoClass = dtoClass;
                if (dtoClass.isAnnotationPresent(Inheritable.class)) {
                    serviceDtoClass = dtoClass.getAnnotation(Inheritable.class).dtoService();
                }
                ReadWriteDtoService<BaseDto, BaseFilter> dtoService = getDtoService(serviceDtoClass);
                BaseFilter filterBase = dtoService.getFilterClass().getDeclaredConstructor().newInstance();
                // Fill super-parent-property by superParentId (call setter = check if filter is
                // implemented).
                new PropertyDescriptor(superParentFilterProperty, dtoService.getFilterClass()).getWriteMethod().invoke(filterBase, superParentId);
                // Load all IDs in IdM for this parent ID.
                List<UUID> childrenIdsInIdM = // 
                dtoService.find(filterBase, null).getContent().stream().map(// 
                childDto -> ((AbstractDto) childDto).getId()).collect(Collectors.toList());
                // IDs to delete = entities missing in the batch.
                Set<UUID> idsToDelete = // 
                childrenIdsInIdM.stream().filter(// 
                idmId -> !childrenIdsInBatch.contains(idmId)).collect(Collectors.toSet());
                idsToDelete.forEach(id -> {
                    BaseDto baseDto = dtoService.get(id);
                    IdmImportLogDto dtoLog = new IdmImportLogDto(context.getBatch(), baseDto, RequestOperationType.REMOVE, superParentId);
                    if (!context.isDryRun()) {
                        dtoService.delete(baseDto);
                        dtoLog.setResult(new OperationResultDto(OperationState.EXECUTED));
                    } else {
                        dtoLog.setResult(new OperationResultDto.Builder(OperationState.NOT_EXECUTED).setModel(// 
                        new DefaultResultModel(CoreResultCode.IMPORT_EXECUTED_AS_DRYRUN)).build());
                    }
                    importLogService.saveDistinct(dtoLog);
                });
            } catch (ReflectiveOperationException | IllegalArgumentException | IntrospectionException e) {
                throw new ResultCodeException(CoreResultCode.EXPORT_IMPORT_REFLECTION_FAILED, e);
            }
        });
    } catch (IOException e) {
        throw new ResultCodeException(CoreResultCode.EXPORT_IMPORT_IO_FAILED, e);
    }
}
Also used : Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) ImportContext(eu.bcvsolutions.idm.core.api.dto.ImportContext) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) Autowired(org.springframework.beans.factory.annotation.Autowired) FormService(eu.bcvsolutions.idm.core.eav.api.service.FormService) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) IdmImportLogDto(eu.bcvsolutions.idm.core.api.dto.IdmImportLogDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Map(java.util.Map) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission) JsonNode(com.fasterxml.jackson.databind.JsonNode) AbstractLongRunningTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.AbstractLongRunningTaskExecutor) AttachmentConfiguration(eu.bcvsolutions.idm.core.ecm.api.config.AttachmentConfiguration) Path(java.nio.file.Path) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ImmutableMap(com.google.common.collect.ImmutableMap) ImportTaskExecutor(eu.bcvsolutions.idm.core.scheduler.task.impl.ImportTaskExecutor) IdmImportLogFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmImportLogFilter) ReadWriteDtoService(eu.bcvsolutions.idm.core.api.service.ReadWriteDtoService) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) Set(java.util.Set) IdmExportImportService(eu.bcvsolutions.idm.core.api.service.IdmExportImportService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) FileSystemUtils(org.springframework.util.FileSystemUtils) Sets(com.google.common.collect.Sets) IntrospectionException(java.beans.IntrospectionException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) Inheritable(eu.bcvsolutions.idm.core.api.domain.Inheritable) Objects(java.util.Objects) EmbeddedDto(eu.bcvsolutions.idm.core.api.dto.EmbeddedDto) List(java.util.List) Stream(java.util.stream.Stream) PropertyDescriptor(java.beans.PropertyDescriptor) CollectionUtils(org.springframework.util.CollectionUtils) Entry(java.util.Map.Entry) Strings(org.apache.logging.log4j.util.Strings) ExportImportType(eu.bcvsolutions.idm.core.api.domain.ExportImportType) ZipUtils(eu.bcvsolutions.idm.core.api.utils.ZipUtils) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) RequestOperationType(eu.bcvsolutions.idm.core.api.domain.RequestOperationType) ExportManager(eu.bcvsolutions.idm.core.api.service.ExportManager) ImportManager(eu.bcvsolutions.idm.core.api.service.ImportManager) IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) AttachableEntity(eu.bcvsolutions.idm.core.ecm.api.entity.AttachableEntity) Session(org.hibernate.Session) MessageFormat(java.text.MessageFormat) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) Lists(com.google.common.collect.Lists) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) IdmImportLogService(eu.bcvsolutions.idm.core.api.service.IdmImportLogService) Service(org.springframework.stereotype.Service) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) EntityUtils(eu.bcvsolutions.idm.core.api.utils.EntityUtils) IdmFormInstanceDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormInstanceDto) IdmExportImportDto(eu.bcvsolutions.idm.core.api.dto.IdmExportImportDto) Codeable(eu.bcvsolutions.idm.core.api.domain.Codeable) Files(java.nio.file.Files) LongRunningTaskManager(eu.bcvsolutions.idm.core.scheduler.api.service.LongRunningTaskManager) AttachmentManager(eu.bcvsolutions.idm.core.ecm.api.service.AttachmentManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) Field(java.lang.reflect.Field) File(java.io.File) BaseFilter(eu.bcvsolutions.idm.core.api.dto.filter.BaseFilter) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) ExportDescriptorDto(eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) Paths(java.nio.file.Paths) CoreEventType(eu.bcvsolutions.idm.core.api.event.CoreEvent.CoreEventType) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) InputStream(java.io.InputStream) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) Serializable(java.io.Serializable) IntrospectionException(java.beans.IntrospectionException) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) BaseFilter(eu.bcvsolutions.idm.core.api.dto.filter.BaseFilter) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) IdmImportLogDto(eu.bcvsolutions.idm.core.api.dto.IdmImportLogDto) UUID(java.util.UUID) Path(java.nio.file.Path) PropertyDescriptor(java.beans.PropertyDescriptor) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) IOException(java.io.IOException)

Example 7 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class DatabaseTableMonitoringEvaluator method evaluate.

@Override
public IdmMonitoringResultDto evaluate(IdmMonitoringDto monitoring) {
    String serviceName = getParameterConverter().toString(monitoring.getEvaluatorProperties(), PARAMETER_READ_SERVICE_BEAN_NAME);
    Object bean;
    try {
        bean = context.getBean(serviceName);
        if (bean == null || !(bean instanceof ReadDtoService<?, ?>)) {
            throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", serviceName));
        }
    } catch (BeansException ex) {
        throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", serviceName), ex);
    }
    // 
    ReadDtoService<?, ?> readService = (ReadDtoService<?, ?>) bean;
    long treshold = getParameterConverter().toLong(monitoring.getEvaluatorProperties(), PARAMETER_THRESHOLD, DEFAULT_THRESHOLD);
    long count = readService.count(null);
    ResultModel resultModel = new DefaultResultModel(CoreResultCode.MONITORING_DATABASE_TABLE, ImmutableMap.of("tableName", String.valueOf(getTableName(readService)), "dtoName", String.valueOf(getDtoName(readService)), "count", Long.toString(count)));
    IdmMonitoringResultDto result = new IdmMonitoringResultDto();
    result.setValue(Long.toString(count));
    result.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).setModel(resultModel).build());
    if (treshold < count) {
        result.setLevel(NotificationLevel.WARNING);
    }
    // 
    return result;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) ReadDtoService(eu.bcvsolutions.idm.core.api.service.ReadDtoService) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) BeansException(org.springframework.beans.BeansException) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Example 8 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class DemoAdminMonitoringEvaluator method evaluate.

@Override
public IdmMonitoringResultDto evaluate(IdmMonitoringDto monitoring) {
    IdmMonitoringResultDto result = new IdmMonitoringResultDto();
    ResultModel resultModel;
    // 
    IdmIdentityDto adminIdentity = getLookupService().lookupDto(IdmIdentityDto.class, InitAdminIdentityProcessor.ADMIN_USERNAME);
    if (adminIdentity == null) {
        resultModel = new DefaultResultModel(CoreResultCode.MONITORING_DEMO_ADMIN_NOT_FOUND);
    } else {
        result.setOwnerId(getLookupService().getOwnerId(adminIdentity));
        result.setOwnerType(getLookupService().getOwnerType(adminIdentity));
        // 
        LoginDto loginDto = new LoginDto();
        loginDto.setUsername(adminIdentity.getUsername());
        loginDto.setPassword(new GuardedString(InitAdminIdentityProcessor.ADMIN_PASSWORD));
        // 
        if (authenticationManager.validate(loginDto)) {
            resultModel = new DefaultResultModel(CoreResultCode.MONITORING_DEMO_ADMIN_WARNING);
            // 
            if (!applicationConfiguration.isDevelopment()) {
                result.setLevel(NotificationLevel.ERROR);
            }
        } else {
            resultModel = new DefaultResultModel(CoreResultCode.OK);
        }
    }
    // 
    result.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).setModel(resultModel).build());
    // 
    return result;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) LoginDto(eu.bcvsolutions.idm.core.security.api.dto.LoginDto) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Example 9 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class EntityEventMonitoringEvaluator method evaluate.

@Override
public IdmMonitoringResultDto evaluate(IdmMonitoringDto monitoring) {
    IdmMonitoringResultDto result = new IdmMonitoringResultDto();
    ResultModel resultModel;
    // 
    IdmEntityEventFilter filter = new IdmEntityEventFilter();
    filter.setStates(Lists.newArrayList(OperationState.EXCEPTION));
    filter.setMonitoringIgnored(Boolean.FALSE);
    Long givenNumberOfDays = getParameterConverter().toLong(monitoring.getEvaluatorProperties(), PARAMETER_NUMBER_OF_DAYS);
    if (givenNumberOfDays != null) {
        filter.setCreatedFrom(ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(givenNumberOfDays));
    }
    long count = entityEventService.count(filter);
    // 
    if (count > 0) {
        resultModel = new DefaultResultModel(CoreResultCode.MONITORING_ENTITY_EVENT_ERROR, ImmutableMap.of("count", Long.toString(count)));
    } else {
        resultModel = new DefaultResultModel(CoreResultCode.OK);
    }
    // 
    result.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).setModel(resultModel).build());
    result.setValue(String.valueOf(count));
    // 
    return result;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) IdmEntityEventFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmEntityEventFilter) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Example 10 with DefaultResultModel

use of eu.bcvsolutions.idm.core.api.dto.DefaultResultModel in project CzechIdMng by bcvsolutions.

the class LoggingEventMonitoringEvaluator method evaluate.

@Override
public IdmMonitoringResultDto evaluate(IdmMonitoringDto monitoring) {
    IdmMonitoringResultDto result = new IdmMonitoringResultDto();
    ResultModel resultModel;
    // 
    IdmLoggingEventFilter filter = new IdmLoggingEventFilter();
    filter.setLevelString(LogType.ERROR);
    Long givenNumberOfDays = getParameterConverter().toLong(monitoring.getEvaluatorProperties(), PARAMETER_NUMBER_OF_DAYS);
    if (givenNumberOfDays != null) {
        filter.setFrom(ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(givenNumberOfDays));
    }
    long count = loggingEventService.count(filter);
    // 
    if (count > 0) {
        resultModel = new DefaultResultModel(CoreResultCode.MONITORING_LOGGING_EVENT_ERROR, ImmutableMap.of("count", Long.toString(count)));
    } else {
        resultModel = new DefaultResultModel(CoreResultCode.OK);
    }
    // 
    result.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).setModel(resultModel).build());
    result.setValue(String.valueOf(count));
    // 
    return result;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) IdmLoggingEventFilter(eu.bcvsolutions.idm.core.api.audit.dto.filter.IdmLoggingEventFilter) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Aggregations

DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)104 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)52 UUID (java.util.UUID)48 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)40 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)35 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)25 IdmEntityStateDto (eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto)24 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)21 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)19 HashMap (java.util.HashMap)19 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)18 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)16 IdmRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto)16 Autowired (org.springframework.beans.factory.annotation.Autowired)16 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)15 List (java.util.List)15 IdmConceptRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmConceptRoleRequestDto)14 Set (java.util.Set)14 ImmutableMap (com.google.common.collect.ImmutableMap)13 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)13