Search in sources :

Example 6 with FilterBuilderDto

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

the class FilterBuilderControllerRestTest method testFindByName.

@Test
public void testFindByName() {
    FilterBuilderFilter filter = new FilterBuilderFilter();
    filter.setName(defaultManagersFilter.getName());
    filter.setEntityClass(IdmIdentity.class.getCanonicalName());
    List<FilterBuilderDto> results = find(filter);
    // 
    Assert.assertFalse(results.isEmpty());
    Assert.assertTrue(results.stream().anyMatch(f -> f.getFilterBuilderClass().equals(AutowireHelper.getTargetClass(defaultManagersFilter))));
    Assert.assertTrue(results.stream().anyMatch(f -> f.getFilterBuilderClass().equals(AutowireHelper.getTargetClass(guaranteeManagersFilter))));
    Assert.assertTrue(results.stream().allMatch(f -> f.getEntityClass().equals(IdmIdentity.class)));
    Assert.assertTrue(results.stream().allMatch(f -> f.getName().equals(defaultManagersFilter.getName())));
    // 
    // not-exists
    filter.setName(getHelper().createName());
    filter.setEntityClass(IdmIdentity.class.getCanonicalName());
    results = find(filter);
    Assert.assertTrue(results.isEmpty());
}
Also used : MockMvcRequestBuilders.put(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put) FilterBuilderFilter(eu.bcvsolutions.idm.core.api.dto.filter.FilterBuilderFilter) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) MockMvcResultMatchers.content(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content) GuaranteeManagersFilter(eu.bcvsolutions.idm.core.model.repository.filter.GuaranteeManagersFilter) AutowireHelper(eu.bcvsolutions.idm.core.api.utils.AutowireHelper) List(java.util.List) SecurityMockMvcRequestPostProcessors.authentication(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) DefaultManagersFilter(eu.bcvsolutions.idm.core.model.repository.filter.DefaultManagersFilter) FilterBuilderDto(eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto) TestHelper(eu.bcvsolutions.idm.test.api.TestHelper) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) BaseController(eu.bcvsolutions.idm.core.api.rest.BaseController) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Assert(org.junit.Assert) Transactional(org.springframework.transaction.annotation.Transactional) FilterBuilderFilter(eu.bcvsolutions.idm.core.api.dto.filter.FilterBuilderFilter) FilterBuilderDto(eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) Test(org.junit.Test) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest)

Example 7 with FilterBuilderDto

use of eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto 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 8 with FilterBuilderDto

use of eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto 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 9 with FilterBuilderDto

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

the class DefaultFilterManager method toDto.

/**
 * Convert filterBuilder to dto.
 *
 * @param key
 * @param filterBuilder
 * @return
 */
private FilterBuilderDto toDto(FilterBuilder<? extends BaseEntity, ? extends DataFilter> filterBuilder) {
    FilterBuilderDto dto = new FilterBuilderDto();
    dto.setId(filterBuilder.getId());
    dto.setName(filterBuilder.getName());
    dto.setModule(filterBuilder.getModule());
    dto.setDisabled(filterBuilder.isDisabled());
    dto.setDescription(filterBuilder.getDescription());
    dto.setEntityClass(filterBuilder.getEntityClass());
    dto.setFilterClass(filterBuilder.getFilterClass());
    dto.setFilterBuilderClass(AutowireHelper.getTargetClass(filterBuilder));
    return dto;
}
Also used : FilterBuilderDto(eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto)

Example 10 with FilterBuilderDto

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

the class DefaultFilterManagerUnitTest method testFindByDescription.

@Test
public void testFindByDescription() {
    FilterBuilderDto dto = new FilterBuilderDto();
    dto.setDescription("tesT-x");
    dto.setEntityClass(IdmIdentity.class);
    // 
    FilterBuilderFilter filter = new FilterBuilderFilter();
    filter.setDescription("Test-x");
    Assert.assertTrue(manager.passFilter(dto, filter));
    filter.setDescription("test-");
    Assert.assertTrue(manager.passFilter(dto, filter));
    filter.setDescription("tee");
    Assert.assertFalse(manager.passFilter(dto, filter));
    dto.setDescription(null);
    Assert.assertFalse(manager.passFilter(dto, filter));
}
Also used : FilterBuilderFilter(eu.bcvsolutions.idm.core.api.dto.filter.FilterBuilderFilter) FilterBuilderDto(eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto) Test(org.junit.Test) AbstractUnitTest(eu.bcvsolutions.idm.test.api.AbstractUnitTest)

Aggregations

FilterBuilderDto (eu.bcvsolutions.idm.core.api.dto.FilterBuilderDto)16 Test (org.junit.Test)11 FilterBuilderFilter (eu.bcvsolutions.idm.core.api.dto.filter.FilterBuilderFilter)10 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)8 IdmIdentity (eu.bcvsolutions.idm.core.model.entity.IdmIdentity)4 FilterBuilder (eu.bcvsolutions.idm.core.api.repository.filter.FilterBuilder)2 FilterKey (eu.bcvsolutions.idm.core.api.repository.filter.FilterKey)2 AbstractRestTest (eu.bcvsolutions.idm.test.api.AbstractRestTest)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DataFilter (eu.bcvsolutions.idm.core.api.dto.filter.DataFilter)1 DisabledFilterBuilder (eu.bcvsolutions.idm.core.api.repository.filter.DisabledFilterBuilder)1 BaseController (eu.bcvsolutions.idm.core.api.rest.BaseController)1 IdmIdentityService (eu.bcvsolutions.idm.core.api.service.IdmIdentityService)1 ReadDtoService (eu.bcvsolutions.idm.core.api.service.ReadDtoService)1 AutowireHelper (eu.bcvsolutions.idm.core.api.utils.AutowireHelper)1 AbstractFormValueService (eu.bcvsolutions.idm.core.eav.service.impl.AbstractFormValueService)1 DefaultManagersFilter (eu.bcvsolutions.idm.core.model.repository.filter.DefaultManagersFilter)1 GuaranteeManagersFilter (eu.bcvsolutions.idm.core.model.repository.filter.GuaranteeManagersFilter)1 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)1