Search in sources :

Example 1 with FilterKey

use of eu.bcvsolutions.idm.core.api.repository.filter.FilterKey in project CzechIdMng by bcvsolutions.

the class DefaultFilterManager method findFilters.

/**
 * Registered filters - by service and filter builders.
 *
 * @param filter search filters
 * @return registered filters by given key
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Map<FilterKey, List<FilterBuilderDto>> findFilters(FilterBuilderFilter filter) {
    Map<FilterKey, List<FilterBuilderDto>> registeredFilters = new HashMap<>();
    // 
    // registered filter builders
    // TODO: not possible to cache now => cache has to be evicted, when module is enabled / disabled.
    Map<String, FilterBuilder> filterBuilders = context.getBeansOfType(FilterBuilder.class);
    filterBuilders.forEach((beanName, filterBuilder) -> {
        if (enabledEvaluator.isEnabled(filterBuilder)) {
            FilterBuilderDto dto = toDto(filterBuilder);
            if (passFilter(dto, filter)) {
                FilterKey filterKey = new FilterKey(dto.getEntityClass(), dto.getName());
                // evaluate effective filter
                if (getBuilder(filterKey) != filterBuilder) {
                    // check reference to instance
                    dto.setDisabled(true);
                }
                if (!registeredFilters.containsKey(filterKey)) {
                    registeredFilters.put(filterKey, new ArrayList<>());
                }
                registeredFilters.get(filterKey).add(dto);
            }
        }
    });
    // 
    // find field by recursion from registered services and filter dtos
    getRegisteredServiceFilters().forEach((filterKey, filterBuilder) -> {
        if (registeredFilters.containsKey(filterKey)) {
            LOG.trace("Property [{}] for class [{}] has their own filter builder implemented.", filterKey.getName(), filterKey.getEntityClass());
        } else if (passFilter(filterBuilder, filter)) {
            // just one => not registrable, not overridable
            registeredFilters.put(filterKey, Lists.newArrayList(filterBuilder));
        }
    });
    // 
    LOG.debug("Returning [{}] registered filterBuilders", registeredFilters.size());
    // 
    return registeredFilters;
}
Also used : HashMap(java.util.HashMap) DisabledFilterBuilder(eu.bcvsolutions.idm.core.api.repository.filter.DisabledFilterBuilder) FilterBuilder(eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder) FilterBuilderDto(eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto) ArrayList(java.util.ArrayList) List(java.util.List) FilterKey(eu.bcvsolutions.idm.core.api.repository.filter.FilterKey)

Example 2 with FilterKey

use of eu.bcvsolutions.idm.core.api.repository.filter.FilterKey in project CzechIdMng by bcvsolutions.

the class DefaultFilterManager method toPredicates.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Predicate> toPredicates(Root<? extends BaseEntity> root, CriteriaQuery<?> query, CriteriaBuilder builder, DataFilter filter) {
    List<Predicate> predicates = new ArrayList<>();
    if (filter == null) {
        // empty filter - builders cannot be found
        return predicates;
    }
    // 
    Class<? extends BaseEntity> entityClass = root.getJavaType();
    Set<String> filterProperties = filter.getData().keySet();
    for (String filterProperty : filterProperties) {
        FilterKey key = new FilterKey(entityClass, filterProperty);
        FilterBuilder filterBuilder = getBuilder(key);
        // 
        if (filterBuilder == null) {
            if (ignoredFilterProperties.contains(filterProperty) || filterProperty.startsWith(String.format("%s:", CoreModule.MODULE_ID))) {
                LOG.trace("Pageable or internal property [{}] will be ignored by filters.", filterProperty);
                continue;
            }
            if (CollectionUtils.isEmpty(filter.getData().get(filterProperty))) {
                LOG.trace("Filter property [{}] is empty and will be ignored by filters.", filterProperty);
                continue;
            }
            // check property is processed by service
            if (// by service definition
            !getRegisteredServiceFilters().containsKey(key) && !getRegisteredFilters(entityClass, filter.getClass()).containsKey(key)) {
                // by filter instance
                FilterNotSupportedException ex = new FilterNotSupportedException(key);
                // 
                if (configurationService.getBooleanValue(PROPERTY_CHECK_SUPPORTED_FILTER_ENABLED, DEFAULT_CHECK_SUPPORTED_FILTER_ENABLED)) {
                    // throw exception otherwise => unrecognized filter is not supported
                    throw ex;
                } else {
                    // log exception only
                    ExceptionUtils.log(LOG, ex);
                }
            }
            LOG.trace("Filter property [{}] for entity [{}] will by processed directly by service predicates.", filterProperty, entityClass.getSimpleName());
            continue;
        }
        Predicate predicate = filterBuilder.getPredicate(root, query, builder, filter);
        if (predicate != null) {
            predicates.add(predicate);
        }
    }
    // 
    return predicates;
}
Also used : FilterNotSupportedException(eu.bcvsolutions.idm.core.api.exception.FilterNotSupportedException) DisabledFilterBuilder(eu.bcvsolutions.idm.core.api.repository.filter.DisabledFilterBuilder) FilterBuilder(eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder) ArrayList(java.util.ArrayList) FilterKey(eu.bcvsolutions.idm.core.api.repository.filter.FilterKey) Predicate(javax.persistence.criteria.Predicate)

Example 3 with FilterKey

use of eu.bcvsolutions.idm.core.api.repository.filter.FilterKey in project CzechIdMng by bcvsolutions.

the class DefaultFilterManagerUnitTest method testCheckFilterSizeExceededDisabled.

@Test
public void testCheckFilterSizeExceededDisabled() {
    Mockito.when(configurationService.getIntegerValue(FilterManager.PROPERTY_CHECK_FILTER_SIZE_MAXIMUM, FilterManager.DEFAULT_CHECK_FILTER_SIZE_MAXIMUM)).thenReturn(-1);
    // 
    List<UUID> values = Stream.generate(UUID::randomUUID).limit(11).collect(Collectors.toList());
    // 
    Assert.assertEquals(values.size(), manager.checkFilterSizeExceeded(new FilterKey(IdmIdentity.class, "mockProperty"), values).size());
}
Also used : UUID(java.util.UUID) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) FilterKey(eu.bcvsolutions.idm.core.api.repository.filter.FilterKey) Test(org.junit.Test) AbstractUnitTest(eu.bcvsolutions.idm.test.api.AbstractUnitTest)

Example 4 with FilterKey

use of eu.bcvsolutions.idm.core.api.repository.filter.FilterKey in project CzechIdMng by bcvsolutions.

the class DefaultFilterManager method getBuilder.

@SuppressWarnings("unchecked")
public <E extends BaseEntity> FilterBuilder<E, DataFilter> getBuilder(Class<E> entityClass, String propertyName) {
    FilterKey key = new FilterKey(entityClass, propertyName);
    if (!builders.hasPluginFor(key)) {
        return null;
    }
    // 
    // default plugin by ordered definition
    FilterBuilder<?, ?> builder = builders.getPluginFor(new FilterKey(entityClass, propertyName));
    String implName = builder.getConfigurationValue(ConfigurationService.PROPERTY_IMPLEMENTATION);
    if (!StringUtils.hasLength(implName)) {
        // return default builder - configuration is empty
        return (FilterBuilder<E, DataFilter>) builder;
    }
    // 
    try {
        // returns bean by name from filter configuration
        return (FilterBuilder<E, DataFilter>) context.getBean(implName);
    } catch (Exception ex) {
        throw new ResultCodeException(CoreResultCode.FILTER_IMPLEMENTATION_NOT_FOUND, ImmutableMap.of("implementation", implName, "propertyName", propertyName, "configurationProperty", builder.getConfigurationPropertyName(ConfigurationService.PROPERTY_IMPLEMENTATION)), ex);
    }
}
Also used : FilterBuilder(eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) FilterKey(eu.bcvsolutions.idm.core.api.repository.filter.FilterKey) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException)

Example 5 with FilterKey

use of eu.bcvsolutions.idm.core.api.repository.filter.FilterKey in project CzechIdMng by bcvsolutions.

the class AbstractReadDtoService method checkFilterSizeExceeded.

/**
 * Check limit of values given for IN predicates.
 *
 * @param predicate start predicate
 * @since 10.6.0
 */
private void checkFilterSizeExceeded(Predicate predicate) {
    if (predicate instanceof InPredicate) {
        InPredicate<?> in = (InPredicate<?>) predicate;
        // common property name => attribute is optional
        String propertyName = "filter";
        if (in.getExpression() instanceof PathImplementor) {
            // TODO: remove root path as 'null', but it is not fit to filter property name anyway => hidden in UI message
            propertyName = ((PathImplementor<?>) in.getExpression()).getPathIdentifier();
        }
        getFilterManager().checkFilterSizeExceeded(new FilterKey(getEntityClass(), propertyName), in.getValues());
    }
    if (predicate instanceof ExistsPredicate) {
        checkFilterSizeExceeded(((ExistsPredicate) predicate).getSubquery().getRestriction());
    }
    predicate.getExpressions().forEach(expression -> {
        if (expression instanceof Predicate) {
            checkFilterSizeExceeded((Predicate) expression);
        }
    });
}
Also used : ExistsPredicate(org.hibernate.query.criteria.internal.predicate.ExistsPredicate) PathImplementor(org.hibernate.query.criteria.internal.PathImplementor) InPredicate(org.hibernate.query.criteria.internal.predicate.InPredicate) FilterKey(eu.bcvsolutions.idm.core.api.repository.filter.FilterKey) Predicate(javax.persistence.criteria.Predicate) InPredicate(org.hibernate.query.criteria.internal.predicate.InPredicate) ExistsPredicate(org.hibernate.query.criteria.internal.predicate.ExistsPredicate)

Aggregations

FilterKey (eu.bcvsolutions.idm.core.api.repository.filter.FilterKey)8 FilterBuilder (eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder)3 IdmIdentity (eu.bcvsolutions.idm.core.model.entity.IdmIdentity)3 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)3 UUID (java.util.UUID)3 Test (org.junit.Test)3 FilterBuilderDto (eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto)2 DisabledFilterBuilder (eu.bcvsolutions.idm.core.api.repository.filter.DisabledFilterBuilder)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Predicate (javax.persistence.criteria.Predicate)2 FilterNotSupportedException (eu.bcvsolutions.idm.core.api.exception.FilterNotSupportedException)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 List (java.util.List)1 PathImplementor (org.hibernate.query.criteria.internal.PathImplementor)1 ExistsPredicate (org.hibernate.query.criteria.internal.predicate.ExistsPredicate)1 InPredicate (org.hibernate.query.criteria.internal.predicate.InPredicate)1