Search in sources :

Example 1 with FilterBuilder

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

the class DefaultFilterManager method getBuilder.

@SuppressWarnings("unchecked")
private <E extends BaseEntity> FilterBuilder<E, DataFilter> getBuilder(FilterKey key) {
    if (!builders.hasPluginFor(key)) {
        return null;
    }
    // 
    // default plugin by ordered definition
    FilterBuilder<E, DataFilter> builder = (FilterBuilder<E, DataFilter>) builders.getPluginFor(key);
    if (builder.isDisabled()) {
        return new DisabledFilterBuilder<E>(builder);
    }
    String implName = builder.getConfigurationValue(ConfigurationService.PROPERTY_IMPLEMENTATION);
    if (StringUtils.isEmpty(implName)) {
        // return default builder - configuration is empty
        return 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", key.getName(), "configurationProperty", builder.getConfigurationPropertyName(ConfigurationService.PROPERTY_IMPLEMENTATION)), ex);
    }
}
Also used : DataFilter(eu.bcvsolutions.idm.core.api.dto.filter.DataFilter) DisabledFilterBuilder(eu.bcvsolutions.idm.core.api.repository.filter.DisabledFilterBuilder) FilterBuilder(eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DisabledFilterBuilder(eu.bcvsolutions.idm.core.api.repository.filter.DisabledFilterBuilder) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) FilterNotSupportedException(eu.bcvsolutions.idm.core.api.exception.FilterNotSupportedException) FilterSizeExceededException(eu.bcvsolutions.idm.core.api.exception.FilterSizeExceededException)

Example 2 with FilterBuilder

use of eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder 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 3 with FilterBuilder

use of eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder 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 4 with FilterBuilder

use of eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder 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 FilterBuilder

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

the class DefaultFilterManagerIntegrationTest method testGetNullPredicate.

/**
 * Test all registered filters - filter has to be checked for null values.
 */
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetNullPredicate() throws Exception {
    for (FilterBuilderDto filterBuilderDto : filterManager.find(null)) {
        FilterBuilder filterBuilder = filterManager.getBuilder(filterBuilderDto.getEntityClass(), filterBuilderDto.getName());
        if (filterBuilder == null) {
            // ~ service impl
            continue;
        }
        if (!DataFilter.class.isAssignableFrom(filterBuilderDto.getFilterClass())) {
            // ~ not registrable
            continue;
        }
        if (DataFilter.class.equals(filterBuilderDto.getFilterClass())) {
            // TODO: construct abstract data filter
            continue;
        }
        // 
        Class<? extends DataFilter> filterClass = (Class<? extends DataFilter>) filterBuilderDto.getFilterClass();
        Assert.assertNull(filterBuilder.getPredicate(null, null, null, filterClass.getDeclaredConstructor().newInstance()));
    }
}
Also used : DataFilter(eu.bcvsolutions.idm.core.api.dto.filter.DataFilter) FilterBuilderDto(eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto) FilterBuilder(eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)

Aggregations

FilterBuilder (eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder)5 DisabledFilterBuilder (eu.bcvsolutions.idm.core.api.repository.filter.DisabledFilterBuilder)3 FilterKey (eu.bcvsolutions.idm.core.api.repository.filter.FilterKey)3 FilterBuilderDto (eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto)2 DataFilter (eu.bcvsolutions.idm.core.api.dto.filter.DataFilter)2 FilterNotSupportedException (eu.bcvsolutions.idm.core.api.exception.FilterNotSupportedException)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 ArrayList (java.util.ArrayList)2 FilterSizeExceededException (eu.bcvsolutions.idm.core.api.exception.FilterSizeExceededException)1 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Predicate (javax.persistence.criteria.Predicate)1 Test (org.junit.Test)1