Search in sources :

Example 16 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project Settler by EmhyrVarEmreis.

the class AbstractService method applyFilters.

private BooleanExpression applyFilters(String filtersJson, QEntity qObject, boolean isAnd) throws NoSuchFieldException, IllegalAccessException {
    BooleanExpressionWrapper pWrapper = new BooleanExpressionWrapper();
    if (isAnd) {
        pWrapper.predicate = qObject.isNotNull();
    } else {
        pWrapper.predicate = qObject.ne(qObject);
    }
    for (BooleanExpression preFilter : getPreFilters()) {
        pWrapper.predicate = pWrapper.predicate.and(preFilter);
    }
    if (filtersJson.length() == 0) {
        return pWrapper.predicate;
    }
    ListFilters filters;
    try {
        filters = new ObjectMapper().readValue(filtersJson, getListFilterClass());
        Stream.concat(Arrays.stream(filters.getClass().getDeclaredFields()), getExtendedFields(filters)).filter(field -> extractValueFromField(field, filters) != null).forEach(field -> {
            AbstractServiceSingleFilter abstractServiceSingleFilter = AbstractServiceSingleFilter.getAbstractServiceSingleFilterClassFromField(field, singleFiltersPack);
            if (abstractServiceSingleFilter == null) {
                getAbstractServiceSingleFilters().stream().filter(filter -> filter.isApplicable(extractValueFromField(field, filters), extractValueFromField(extractFieldFromObject(qObject, field.getName()), qObject))).findFirst().ifPresent(filter -> pWrapper.predicate = apply(pWrapper, filter, field, filters, qObject, isAnd));
            } else {
                pWrapper.predicate = apply(pWrapper, abstractServiceSingleFilter, field, filters, qObject, isAnd);
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return pWrapper.predicate;
}
Also used : Arrays(java.util.Arrays) ComparableExpressionBase(com.querydsl.core.types.dsl.ComparableExpressionBase) QPageRequest(org.springframework.data.querydsl.QPageRequest) QueryDslPredicateExecutor(org.springframework.data.querydsl.QueryDslPredicateExecutor) Autowired(org.springframework.beans.factory.annotation.Autowired) UnaryOperator(java.util.function.UnaryOperator) OrderSpecifier(com.querydsl.core.types.OrderSpecifier) Function(java.util.function.Function) ModelMapper(org.modelmapper.ModelMapper) ListPage(pl.morecraft.dev.settler.web.misc.ListPage) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Page(org.springframework.data.domain.Page) Field(java.lang.reflect.Field) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Stream(java.util.stream.Stream) BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) EntityPathBase(com.querydsl.core.types.dsl.EntityPathBase) ListPageConverter(pl.morecraft.dev.settler.service.converters.ListPageConverter) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) JpaRepository(org.springframework.data.jpa.repository.JpaRepository) BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

BooleanExpression (com.querydsl.core.types.dsl.BooleanExpression)16 Test (org.junit.Test)13 Field (java.lang.reflect.Field)2 LinkedList (java.util.LinkedList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Tuple (com.querydsl.core.Tuple)1 Expression (com.querydsl.core.types.Expression)1 OrderSpecifier (com.querydsl.core.types.OrderSpecifier)1 ComparableExpressionBase (com.querydsl.core.types.dsl.ComparableExpressionBase)1 EntityPathBase (com.querydsl.core.types.dsl.EntityPathBase)1 StringExpression (com.querydsl.core.types.dsl.StringExpression)1 StringPath (com.querydsl.core.types.dsl.StringPath)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Function (java.util.function.Function)1