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