Search in sources :

Example 1 with ReadDtoService

use of eu.bcvsolutions.idm.core.api.service.ReadDtoService 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 2 with ReadDtoService

use of eu.bcvsolutions.idm.core.api.service.ReadDtoService in project CzechIdMng by bcvsolutions.

the class DefaultLookupService method getDtoLookup.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <I extends BaseDto> DtoLookup<I> getDtoLookup(Class<? extends Identifiable> identifiableType) {
    ReadDtoService service = getDtoService(identifiableType);
    if (service == null) {
        LOG.debug("Service for identifiable type [{}] is not found, lookup not found.", identifiableType);
        return null;
    }
    // 
    DtoLookup<I> lookup = (DtoLookup<I>) dtoLookups.getPluginFor(service.getDtoClass());
    if (lookup == null) {
        if (service instanceof CodeableService) {
            return new CodeableDtoLookup<I>((CodeableService<I>) service);
        }
        return new DefaultDtoLookup<I>(service);
    }
    return lookup;
}
Also used : CodeableDtoLookup(eu.bcvsolutions.idm.core.api.rest.lookup.CodeableDtoLookup) CodeableDtoLookup(eu.bcvsolutions.idm.core.api.rest.lookup.CodeableDtoLookup) DtoLookup(eu.bcvsolutions.idm.core.api.rest.lookup.DtoLookup) DefaultDtoLookup(eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoLookup) ReadDtoService(eu.bcvsolutions.idm.core.api.service.ReadDtoService) DefaultDtoLookup(eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoLookup) CodeableService(eu.bcvsolutions.idm.core.api.service.CodeableService)

Example 3 with ReadDtoService

use of eu.bcvsolutions.idm.core.api.service.ReadDtoService in project CzechIdMng by bcvsolutions.

the class ModelMapperChecker method verify.

/**
 * Check registered services and their conversions to dto provided by model mapper.
 * Throws Exception, if check does not pass.
 *
 * @throws ConfigurationDisabledException if check is disabled by configuration.
 * @throws ResultCodeException if service check failed (referential integrity is broken or other IdM exception occurs).
 * @throws ModelMapperServiceInitException if mapper is wrongly inited.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void verify() {
    if (!configurationService.getBooleanValue(PROPERTY_ENABLED, DEFAULT_ENABLED)) {
        LOG.warn("Init: check registered IdM services is disabled.");
        // 
        throw new ConfigurationDisabledException(ModelMapperChecker.PROPERTY_ENABLED);
    }
    long start = System.currentTimeMillis();
    int modelMapperUsed = 0;
    Map<String, ReadDtoService> services = context.getBeansOfType(ReadDtoService.class);
    for (ReadDtoService service : services.values()) {
        if ((service instanceof DefaultWorkflowHistoricTaskInstanceService) || (service instanceof DefaultWorkflowHistoricProcessInstanceService)) {
            LOG.debug("Workflow history service [{}] will not be checked - find method with pagination is not fully supported and took long time.", service.getClass());
            continue;
        }
        // 
        LOG.trace("Service [{}] will be checked.", service.getClass());
        try {
            BaseFilter filter = null;
            if (service.getFilterClass() != null) {
                // some services could not define filter
                try {
                    if (service.getFilterClass().equals(DataFilter.class)) {
                        filter = new DataFilter(service.getDtoClass());
                    } else {
                        filter = (BaseFilter) mapper.convertValue(new HashMap(), service.getFilterClass());
                    }
                } catch (Exception ex) {
                    LOG.debug("Service [{}] filter [{}] cannot be constructed. Find method will be checked without filter.", service.getClass(), service.getFilterClass());
                }
            }
            if (!service.find(filter, PageRequest.of(0, 1)).getContent().isEmpty()) {
                modelMapperUsed++;
            }
        } catch (UnsupportedOperationException ex) {
            LOG.debug("Service [{}] does not support find method. Check will be skipped.", service.getClass());
        } catch (MappingException ex) {
            throw new ModelMapperServiceInitException(AutowireHelper.getTargetType(service), ex);
        } catch (EntityNotFoundException ex) {
            throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", String.valueOf(ex.getMessage())), ex);
        } catch (ResultCodeException ex) {
            throw ex;
        } catch (Exception ex) {
            if (ex.getCause() instanceof EntityNotFoundException) {
                throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", String.valueOf(ex.getCause().getMessage()), "service", service.getClass().getCanonicalName()), ex);
            }
            LOG.error("Service [{}] cannot be checked. Find method cannot be called.", service.getClass(), ex);
        }
        LOG.trace("Service [{}] was checked.", service.getClass());
    }
    LOG.info("Init: all registered IdM services [{}]. " + "Services usage were checked [{}] (agenda contains some records) [took: {}ms].", services.size(), modelMapperUsed, System.currentTimeMillis() - start);
}
Also used : DataFilter(eu.bcvsolutions.idm.core.api.dto.filter.DataFilter) HashMap(java.util.HashMap) ReadDtoService(eu.bcvsolutions.idm.core.api.service.ReadDtoService) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityNotFoundException(javax.persistence.EntityNotFoundException) ConfigurationDisabledException(eu.bcvsolutions.idm.core.security.api.exception.ConfigurationDisabledException) BaseFilter(eu.bcvsolutions.idm.core.api.dto.filter.BaseFilter) ModelMapperServiceInitException(eu.bcvsolutions.idm.core.exception.ModelMapperServiceInitException) ConfigurationDisabledException(eu.bcvsolutions.idm.core.security.api.exception.ConfigurationDisabledException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityNotFoundException(javax.persistence.EntityNotFoundException) MappingException(org.modelmapper.MappingException) ModelMapperServiceInitException(eu.bcvsolutions.idm.core.exception.ModelMapperServiceInitException) MappingException(org.modelmapper.MappingException) DefaultWorkflowHistoricTaskInstanceService(eu.bcvsolutions.idm.core.workflow.service.impl.DefaultWorkflowHistoricTaskInstanceService) DefaultWorkflowHistoricProcessInstanceService(eu.bcvsolutions.idm.core.workflow.service.impl.DefaultWorkflowHistoricProcessInstanceService)

Example 4 with ReadDtoService

use of eu.bcvsolutions.idm.core.api.service.ReadDtoService in project CzechIdMng by bcvsolutions.

the class DefaultFilterManager method getRegisteredServiceFilters.

/**
 * Returns filters processed directly by service predicates.
 *
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private Map<FilterKey, FilterBuilderDto> getRegisteredServiceFilters() {
    if (registeredServiceFilters == null) {
        registeredServiceFilters = new HashMap<>();
        // find field by recursion from registered services and filter dtos
        Map<String, ReadDtoService> services = context.getBeansOfType(ReadDtoService.class);
        services.forEach((beanName, service) -> {
            Class<? extends BaseEntity> entityClass = service.getEntityClass();
            Class<? extends BaseFilter> filterClass = service.getFilterClass();
            if (entityClass == null || filterClass == null) {
                LOG.trace("Service [{}], [{}] does not define controlled entity [{}] or filter[{}], skip to resolve available filter criteria.", beanName, service.getClass(), entityClass, filterClass);
            } else {
                registeredServiceFilters.putAll(getRegisteredFilters(entityClass, filterClass).entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> {
                    FilterBuilderDto dto = e.getValue();
                    // service and property is unique combination
                    dto.setId(String.format("%s-%s", beanName, dto.getName()));
                    // service - can be overriden - https://wiki.czechidm.com/tutorial/dev/override_service
                    dto.setModule(EntityUtils.getModule(service.getClass()));
                    // 
                    if (service instanceof AbstractFormValueService<?, ?>) {
                        // eav value services are constructed dynamically (prevent to show cglib class)
                        dto.setFilterBuilderClass(AbstractFormValueService.class);
                    } else {
                        dto.setFilterBuilderClass(AutowireHelper.getTargetClass(service));
                    }
                    return dto;
                })));
            }
        });
    }
    // 
    return registeredServiceFilters;
}
Also used : FilterBuilderDto(eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto) ReadDtoService(eu.bcvsolutions.idm.core.api.service.ReadDtoService) AbstractFormValueService(eu.bcvsolutions.idm.core.eav.service.impl.AbstractFormValueService)

Example 5 with ReadDtoService

use of eu.bcvsolutions.idm.core.api.service.ReadDtoService in project CzechIdMng by bcvsolutions.

the class DefaultLongPollingManager method baseCheckDeferredResult.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void baseCheckDeferredResult(DeferredResult<OperationResultDto> deferredResult, LongPollingSubscriber subscriber, ModifiedFromFilter filter, ReadDtoService service, boolean checkCount) {
    Assert.notNull(deferredResult, "Deferred result is required to check.");
    Assert.notNull(subscriber.getEntityId(), "Subscriber is required to check deferred result.");
    LOG.debug("Start baseCheckDeferredResult for deferred-result [{}] and subscriber [{}]", deferredResult, subscriber);
    if (checkCount) {
        long countOfentities = service.count(filter);
        Long lastNumberOfEntities = subscriber.getLastNumberOfEntities();
        subscriber.setLastNumberOfEntities(countOfentities);
        if (lastNumberOfEntities != null && countOfentities != lastNumberOfEntities) {
            // Notify FE -> Some of an entities were changed (refresh must be executed)
            deferredResult.setResult(new OperationResultDto(OperationState.RUNNING));
            return;
        }
    }
    ZonedDateTime timeStamp = subscriber.getLastTimeStamp();
    if (timeStamp == null) {
        List<AbstractDto> entities = service.find(filter, PageRequest.of(0, 1, new Sort(Direction.DESC, AbstractEntity_.created.getName(), AbstractEntity_.modified.getName()))).getContent();
        if (entities.isEmpty()) {
            subscriber.setLastTimeStamp(ZonedDateTime.now());
            return;
        }
        ZonedDateTime lastModified = this.getLastTimeStamp(entities.get(0));
        subscriber.setLastTimeStamp(lastModified);
        return;
    }
    // Try to find, if some from not finished entities were changed
    // TODO: For search SysSyncLogs the filter by modifiedFrom didn't work properly.
    // I didn't found reason why, but I hope using sort by created and modified fields will be works better.
    // filter.setModifiedFrom(timeStamp);
    List<AbstractDto> changedRequestsFromLastChecks = service.find(filter, PageRequest.of(0, 1, Sort.by(Direction.DESC, AbstractEntity_.created.getName(), AbstractEntity_.modified.getName()))).getContent();
    if (!changedRequestsFromLastChecks.isEmpty()) {
        AbstractDto changedRequestsFromLastCheck = changedRequestsFromLastChecks.get(0);
        ZonedDateTime lastModified = this.getLastTimeStamp(changedRequestsFromLastCheck);
        if (lastModified.isAfter(timeStamp)) {
            // Notify FE -> Some of the entity was changed (refresh must be executed).
            // Notified will be all suspended request for same entity ID!
            this.suspendedRequests.stream().filter(request -> request.getEntityId().equals(subscriber.getEntityId())).forEach(request -> {
                request.getResult().setResult(new OperationResultDto(OperationState.RUNNING));
            });
            subscriber.setLastTimeStamp(lastModified);
            return;
        }
    }
// Nothing was changed
}
Also used : ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) DeferredResult(org.springframework.web.context.request.async.DeferredResult) Autowired(org.springframework.beans.factory.annotation.Autowired) Scheduled(org.springframework.scheduling.annotation.Scheduled) ModifiedFromFilter(eu.bcvsolutions.idm.core.api.dto.filter.ModifiedFromFilter) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) ReadDtoService(eu.bcvsolutions.idm.core.api.service.ReadDtoService) Service(org.springframework.stereotype.Service) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) Map(java.util.Map) LongPollingManager(eu.bcvsolutions.idm.core.model.service.api.LongPollingManager) Sort(org.springframework.data.domain.Sort) Direction(org.springframework.data.domain.Sort.Direction) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) Logger(org.slf4j.Logger) AbstractEntity_(eu.bcvsolutions.idm.core.api.entity.AbstractEntity_) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PageRequest(org.springframework.data.domain.PageRequest) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) UUID(java.util.UUID) LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) Lazy(org.springframework.context.annotation.Lazy) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Assert(org.springframework.util.Assert) ZonedDateTime(java.time.ZonedDateTime) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) Sort(org.springframework.data.domain.Sort)

Aggregations

ReadDtoService (eu.bcvsolutions.idm.core.api.service.ReadDtoService)6 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)1 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)1 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)1 FilterBuilderDto (eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto)1 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)1 BaseFilter (eu.bcvsolutions.idm.core.api.dto.filter.BaseFilter)1 DataFilter (eu.bcvsolutions.idm.core.api.dto.filter.DataFilter)1 ModifiedFromFilter (eu.bcvsolutions.idm.core.api.dto.filter.ModifiedFromFilter)1 AbstractEntity_ (eu.bcvsolutions.idm.core.api.entity.AbstractEntity_)1 CodeableDtoLookup (eu.bcvsolutions.idm.core.api.rest.lookup.CodeableDtoLookup)1 DefaultDtoLookup (eu.bcvsolutions.idm.core.api.rest.lookup.DefaultDtoLookup)1 DtoLookup (eu.bcvsolutions.idm.core.api.rest.lookup.DtoLookup)1 DtoLookupByExample (eu.bcvsolutions.idm.core.api.rest.lookup.DtoLookupByExample)1 CodeableService (eu.bcvsolutions.idm.core.api.service.CodeableService)1 ConfigurationService (eu.bcvsolutions.idm.core.api.service.ConfigurationService)1 AbstractFormValueService (eu.bcvsolutions.idm.core.eav.service.impl.AbstractFormValueService)1 ModelMapperServiceInitException (eu.bcvsolutions.idm.core.exception.ModelMapperServiceInitException)1