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);
}
}
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;
}
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;
}
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);
}
}
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()));
}
}
Aggregations