Search in sources :

Example 1 with FilterOperator

use of org.entando.entando.web.common.model.FilterOperator in project entando-core by entando.

the class FilterUtils method filterBoolean.

public static boolean filterBoolean(Filter filter, boolean value) {
    FilterOperator operator = getFilterOperator(filter);
    boolean result = false;
    for (boolean filterValue : getTypedAllowedValues(filter, v -> Boolean.parseBoolean(v.toLowerCase()))) {
        switch(operator) {
            case EQUAL:
            case LIKE:
                result |= value == filterValue;
                break;
            case NOT_EQUAL:
                result |= value != filterValue;
                break;
            default:
                throw new UnsupportedOperationException(getUnsupportedOperatorMessage(filter));
        }
    }
    return result;
}
Also used : FilterOperator(org.entando.entando.web.common.model.FilterOperator)

Example 2 with FilterOperator

use of org.entando.entando.web.common.model.FilterOperator in project entando-core by entando.

the class FilterUtils method filterDouble.

private static boolean filterDouble(Filter filter, List<Double> filterValues, double value) {
    FilterOperator operator = getFilterOperator(filter);
    boolean result = false;
    for (double filterValue : filterValues) {
        switch(operator) {
            case EQUAL:
            case LIKE:
                result |= value == filterValue;
                break;
            case NOT_EQUAL:
                result |= value != filterValue;
                break;
            case GREATER:
                result |= value >= filterValue;
                break;
            case LOWER:
                result |= value <= filterValue;
                break;
            default:
                throw new UnsupportedOperationException(getUnsupportedOperatorMessage(filter));
        }
    }
    return result;
}
Also used : FilterOperator(org.entando.entando.web.common.model.FilterOperator)

Example 3 with FilterOperator

use of org.entando.entando.web.common.model.FilterOperator in project entando-core by entando.

the class AbstractPaginationValidator method validateRestListRequest.

public void validateRestListRequest(RestListRequest listRequest, Class<?> type) {
    this.checkDefaultSortField(listRequest);
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(listRequest, "listRequest");
    if (listRequest.getPage() < 1) {
        bindingResult.reject(ERRCODE_PAGE_INVALID, new Object[] {}, "pagination.page.invalid");
        throw new ValidationGenericException(bindingResult);
    }
    if (listRequest.getPageSize() < 0) {
        bindingResult.reject(ERRCODE_PAGE_SIZE_INVALID, new Object[] {}, "pagination.page.size.invalid");
    } else if (listRequest.getPage() > 1 && listRequest.getPageSize() == 0) {
        bindingResult.reject(ERRCODE_NO_ITEM_ON_PAGE, new String[] { String.valueOf(listRequest.getPage()) }, "pagination.item.empty");
        throw new ResourceNotFoundException(bindingResult);
    }
    if (null == type) {
        return;
    }
    if (!isValidField(listRequest.getSort(), type)) {
        bindingResult.reject(ERRCODE_SORTING_INVALID, new Object[] { listRequest.getSort() }, "sorting.sort.invalid");
    }
    if (listRequest.getFilters() != null) {
        List<String> attributes = Arrays.asList(listRequest.getFilters()).stream().filter(filter -> filter.getEntityAttr() == null).map(filter -> filter.getAttributeName()).filter(attr -> !isValidField(attr, type)).collect(Collectors.toList());
        if (attributes.size() > 0) {
            bindingResult.reject(ERRCODE_FILTERING_ATTR_INVALID, new Object[] { attributes.get(0) }, "filtering.filter.attr.name.invalid");
        }
        if (Arrays.stream(FilterOperator.values()).map(FilterOperator::getValue).noneMatch(op -> Arrays.stream(listRequest.getFilters()).map(Filter::getOperator).anyMatch(op::equals))) {
            bindingResult.reject(ERRCODE_FILTERING_OP_INVALID, new Object[] {}, "filtering.filter.operation.invalid");
        }
        Arrays.stream(listRequest.getFilters()).filter(f -> getDateFilterKeys().contains(f.getAttribute())).forEach(f -> {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(SystemConstants.API_DATE_FORMAT);
                sdf.parse(f.getValue());
            } catch (ParseException ex) {
                bindingResult.reject(ERRCODE_FILTERING_OP_INVALID, new Object[] { f.getValue(), f.getAttribute() }, "filtering.filter.date.format.invalid");
            }
        });
    }
    if (!Arrays.asList(directions).contains(listRequest.getDirection())) {
        bindingResult.reject(ERRCODE_DIRECTION_INVALID, new Object[] {}, "sorting.direction.invalid");
    }
    if (bindingResult.hasErrors()) {
        throw new ValidationGenericException(bindingResult);
    }
}
Also used : ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Validator(org.springframework.validation.Validator) SystemConstants(com.agiletec.aps.system.SystemConstants) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) FilterOperator(org.entando.entando.web.common.model.FilterOperator) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) RestListRequest(org.entando.entando.web.common.model.RestListRequest) Filter(org.entando.entando.web.common.model.Filter) PagedMetadata(org.entando.entando.web.common.model.PagedMetadata) Map(java.util.Map) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ParseException(java.text.ParseException) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Filter(org.entando.entando.web.common.model.Filter) ParseException(java.text.ParseException) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with FilterOperator

use of org.entando.entando.web.common.model.FilterOperator in project entando-core by entando.

the class FilterUtils method filterString.

public static boolean filterString(Filter filter, String value) {
    FilterOperator operator = getFilterOperator(filter);
    if (value == null) {
        return false;
    }
    boolean result = false;
    for (String filterValue : getFilterValues(filter)) {
        switch(operator) {
            case EQUAL:
                result |= value.equals(filterValue);
                break;
            case NOT_EQUAL:
                result |= !value.equals(filterValue);
                break;
            case LIKE:
                result |= value.toLowerCase().contains(filterValue.toLowerCase());
                break;
            case GREATER:
                result |= value.compareTo(filterValue) >= 0;
                break;
            case LOWER:
                result |= value.compareTo(filterValue) <= 0;
                break;
            default:
                throw new UnsupportedOperationException(getUnsupportedOperatorMessage(filter));
        }
    }
    return result;
}
Also used : FilterOperator(org.entando.entando.web.common.model.FilterOperator)

Aggregations

FilterOperator (org.entando.entando.web.common.model.FilterOperator)4 SystemConstants (com.agiletec.aps.system.SystemConstants)1 Field (java.lang.reflect.Field)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 ResourceNotFoundException (org.entando.entando.aps.system.exception.ResourceNotFoundException)1 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)1 Filter (org.entando.entando.web.common.model.Filter)1 PagedMetadata (org.entando.entando.web.common.model.PagedMetadata)1 RestListRequest (org.entando.entando.web.common.model.RestListRequest)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 Validator (org.springframework.validation.Validator)1