Search in sources :

Example 1 with ICustomFilterPredicateCreator

use of org.jowidgets.cap.service.jpa.api.query.ICustomFilterPredicateCreator in project jo-client-platform by jo-source.

the class CriteriaQueryCreator method createFilterPredicate.

private Predicate createFilterPredicate(final CriteriaBuilder criteriaBuilder, final Root<?> bean, final CriteriaQuery<?> query, final IFilter filter, final PARAM_TYPE parameter) {
    if (filter instanceof IPropertyFilter) {
        final IPropertyFilter propertyFilter = (IPropertyFilter) filter;
        final String propertyName = propertyFilter.getPropertyName();
        final IPropertyFilterPredicateCreator<PARAM_TYPE> predicateCreator = propertyFilterPredicateCreators.get(propertyName);
        if (predicateCreator != null) {
            return predicateCreator.createPredicate(criteriaBuilder, bean, query, propertyFilter, parameter);
        }
    }
    if (filter instanceof IArithmeticFilter) {
        return createArithmeticFilterPredicate(criteriaBuilder, bean, query, (IArithmeticFilter) filter);
    } else if (filter instanceof IArithmeticPropertyFilter) {
        // TODO MG support IArithmeticPropertyFilter
        throw new IllegalArgumentException("unsupported filter type: " + filter.getClass().getName());
    } else if (filter instanceof IBooleanFilter) {
        final Predicate predicate;
        predicate = createBooleanFilterPredicate(criteriaBuilder, bean, query, (IBooleanFilter) filter, parameter);
        return invertPredicateIfNeeded(criteriaBuilder, predicate, filter, true);
    } else if (filter instanceof ICustomFilter) {
        final ICustomFilter customFilter = (ICustomFilter) filter;
        final ICustomFilterPredicateCreator<PARAM_TYPE> customFilterPredicateCreator = getCustomFilterPredicateCreator(customFilter);
        if (customFilterPredicateCreator != null) {
            final Predicate predicate;
            predicate = customFilterPredicateCreator.createPredicate(criteriaBuilder, getPath(bean, customFilter.getPropertyName()), query, customFilter, parameter);
            return invertPredicateIfNeeded(criteriaBuilder, predicate, filter, true);
        } else {
            throw new IllegalArgumentException("unsupported custom filter type: " + customFilter.getFilterType());
        }
    } else {
        throw new IllegalArgumentException("unsupported filter type: " + filter.getClass().getName());
    }
}
Also used : IArithmeticPropertyFilter(org.jowidgets.cap.common.api.filter.IArithmeticPropertyFilter) IArithmeticFilter(org.jowidgets.cap.common.api.filter.IArithmeticFilter) IBooleanFilter(org.jowidgets.cap.common.api.filter.IBooleanFilter) ICustomFilterPredicateCreator(org.jowidgets.cap.service.jpa.api.query.ICustomFilterPredicateCreator) ICustomFilter(org.jowidgets.cap.common.api.filter.ICustomFilter) IPropertyFilter(org.jowidgets.cap.common.api.filter.IPropertyFilter) Predicate(javax.persistence.criteria.Predicate)

Example 2 with ICustomFilterPredicateCreator

use of org.jowidgets.cap.service.jpa.api.query.ICustomFilterPredicateCreator in project jo-client-platform by jo-source.

the class JpaReaderAnnotationPostProcessor method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) {
    try {
        final ReaderBean beanAnnotation = beanFactory.findAnnotationOnBean(beanName, ReaderBean.class);
        if (beanAnnotation != null) {
            final Class<? extends IBean> beanType = beanAnnotation.type();
            final List<String> propertyNames = new BeanTypeUtil(beanType).getPropertyNames();
            final Set<Method> methods = getReaderMethods(bean);
            for (final Method method : methods) {
                final IPredicateCreator<Object> predicateCreator = createPredicateCreator(beanFactory, beanName, method);
                final ICriteriaQueryCreatorBuilder<Object> queryCreatorBuilder = JpaQueryToolkit.criteriaQueryCreatorBuilder(beanType).setCaseSensitve(!beanAnnotation.caseInsensitive());
                // TODO: MvR -> Check genericity...
                if (ParentLinkHelper.hasParent(beanType)) {
                    queryCreatorBuilder.setParentPropertyName(beanAnnotation.parentPropertyName());
                }
                if (predicateCreator != null) {
                    queryCreatorBuilder.addPredicateCreator(predicateCreator);
                }
                for (final Map.Entry<String, ? extends ICustomFilterPredicateCreator<?>> entry : customFilterPredicateCreators.entrySet()) {
                    final String filterName = entry.getKey();
                    @SuppressWarnings("unchecked") final ICustomFilterPredicateCreator<Object> customFilterPredicateCreator = (ICustomFilterPredicateCreator<Object>) entry.getValue();
                    queryCreatorBuilder.addCustomFilterPredicateCreator(filterName, customFilterPredicateCreator);
                }
                final IQueryCreator<Object> queryCreator = queryCreatorBuilder.build();
                final IReaderService<Object> readerService = JpaServiceToolkit.serviceFactory().readerService(beanType, queryCreator, propertyNames);
                final Reader executorAnnotation = method.getAnnotation(Reader.class);
                final IServiceId<IReaderService<Void>> serviceId = new ServiceId<IReaderService<Void>>(executorAnnotation.value(), IReaderService.class);
                SpringServiceProvider.getInstance().addService(serviceId, readerService);
            }
        }
    } catch (final NoSuchBeanDefinitionException e) {
    }
    return bean;
}
Also used : BeanTypeUtil(org.jowidgets.cap.service.spring.BeanTypeUtil) ICustomFilterPredicateCreator(org.jowidgets.cap.service.jpa.api.query.ICustomFilterPredicateCreator) Reader(org.jowidgets.cap.service.api.annotation.Reader) Method(java.lang.reflect.Method) IReaderService(org.jowidgets.cap.common.api.service.IReaderService) IServiceId(org.jowidgets.service.api.IServiceId) ServiceId(org.jowidgets.service.tools.ServiceId) ReaderBean(org.jowidgets.cap.service.api.annotation.ReaderBean) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Map(java.util.Map)

Aggregations

ICustomFilterPredicateCreator (org.jowidgets.cap.service.jpa.api.query.ICustomFilterPredicateCreator)2 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 Predicate (javax.persistence.criteria.Predicate)1 IArithmeticFilter (org.jowidgets.cap.common.api.filter.IArithmeticFilter)1 IArithmeticPropertyFilter (org.jowidgets.cap.common.api.filter.IArithmeticPropertyFilter)1 IBooleanFilter (org.jowidgets.cap.common.api.filter.IBooleanFilter)1 ICustomFilter (org.jowidgets.cap.common.api.filter.ICustomFilter)1 IPropertyFilter (org.jowidgets.cap.common.api.filter.IPropertyFilter)1 IReaderService (org.jowidgets.cap.common.api.service.IReaderService)1 Reader (org.jowidgets.cap.service.api.annotation.Reader)1 ReaderBean (org.jowidgets.cap.service.api.annotation.ReaderBean)1 BeanTypeUtil (org.jowidgets.cap.service.spring.BeanTypeUtil)1 IServiceId (org.jowidgets.service.api.IServiceId)1 ServiceId (org.jowidgets.service.tools.ServiceId)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1