Search in sources :

Example 6 with FilterKey

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

the class DefaultFilterManager method getRegisteredFilters.

private Map<FilterKey, FilterBuilderDto> getRegisteredFilters(Class<? extends BaseEntity> entityClass, Class<? extends BaseFilter> filterClass) {
    Assert.notNull(entityClass, "Entity class is required to resolve registered filters.");
    Assert.notNull(filterClass, "Filter class is required to resolve filter properties.");
    // 
    Map<FilterKey, FilterBuilderDto> registeredServiceFilters = new HashMap<>();
    Class<?> processFilterClass = filterClass;
    while (!processFilterClass.equals(Object.class)) {
        Stream.of(processFilterClass.getDeclaredFields(), // both static and private field from interfaces and super classes
        processFilterClass.getFields()).flatMap(Stream::of).forEach(declaredField -> {
            String propertyName = declaredField.getName();
            if (Modifier.isStatic(declaredField.getModifiers())) {
                try {
                    Object propertyValue = declaredField.get(null);
                    if (propertyValue instanceof String) {
                        propertyName = (String) propertyValue;
                    } else {
                        LOG.trace("Value of static property [{}] for class [{}] is not string, skip to resolve available filter criteria.", propertyName, filterClass);
                        return;
                    }
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    LOG.warn("Get value of static property [{}] for class [{}] failed, skip to resolve available filter criteria.", propertyName, filterClass);
                    return;
                }
            }
            if (ignoredFilterProperties.contains(propertyName)) {
                LOG.trace("Pageable or internal property [{}] will be ignored by filters.", propertyName);
                return;
            }
            FilterKey filterKey = new FilterKey(entityClass, propertyName);
            if (registeredServiceFilters.containsKey(filterKey)) {
                // already resolved e.g. by interface
                return;
            }
            // 
            FilterBuilderDto dto = new FilterBuilderDto();
            dto.setName(propertyName);
            dto.setDescription("Internal service implementation (toPredicates).");
            dto.setEntityClass(entityClass);
            dto.setFilterClass(filterClass);
            // service is always effective filter
            dto.setDisabled(false);
            // 
            registeredServiceFilters.put(filterKey, dto);
        });
        // 
        processFilterClass = processFilterClass.getSuperclass();
    }
    // 
    return registeredServiceFilters;
}
Also used : HashMap(java.util.HashMap) FilterBuilderDto(eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto) FilterKey(eu.bcvsolutions.idm.core.api.repository.filter.FilterKey)

Example 7 with FilterKey

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

the class DefaultFilterManagerUnitTest method testCheckFilterSizeExceeded.

@Test(expected = FilterSizeExceededException.class)
public void testCheckFilterSizeExceeded() {
    Mockito.when(configurationService.getIntegerValue(FilterManager.PROPERTY_CHECK_FILTER_SIZE_MAXIMUM, FilterManager.DEFAULT_CHECK_FILTER_SIZE_MAXIMUM)).thenReturn(10);
    // 
    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 8 with FilterKey

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

the class DefaultFilterManagerUnitTest method testCheckFilterSize.

@Test
public void testCheckFilterSize() {
    Mockito.when(configurationService.getIntegerValue(FilterManager.PROPERTY_CHECK_FILTER_SIZE_MAXIMUM, FilterManager.DEFAULT_CHECK_FILTER_SIZE_MAXIMUM)).thenReturn(10);
    // 
    List<UUID> values = Stream.generate(UUID::randomUUID).limit(10).collect(Collectors.toList());
    // 
    Assert.assertEquals(values.size(), manager.checkFilterSizeExceeded(new FilterKey(IdmIdentity.class, "mockProperty"), values).size());
    // 
    values = Stream.generate(UUID::randomUUID).limit(9).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)

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