Search in sources :

Example 16 with Filter

use of org.entando.entando.web.common.model.Filter 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 17 with Filter

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

the class AbstractEntityService method filterObjects.

protected boolean filterObjects(Object bean, Filter[] filters, Map<String, String> mapping) {
    try {
        if (null == filters) {
            return true;
        }
        Map<String, Object> properties = BeanUtils.describe(bean);
        for (Filter filter : filters) {
            String fieldName = this.getFieldName(filter.getAttribute(), mapping);
            String value = (null != properties.get(fieldName)) ? properties.get(fieldName).toString() : null;
            if (null == value) {
                continue;
            }
            if (!value.toLowerCase().contains(filter.getValue().toLowerCase())) {
                return false;
            }
        }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        logger.error("error filtering bean " + bean.getClass().getName(), e);
        throw new RestServerError("error filtering bean " + bean.getClass().getName(), e);
    }
    return true;
}
Also used : Filter(org.entando.entando.web.common.model.Filter) RestServerError(org.entando.entando.aps.system.exception.RestServerError) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 18 with Filter

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

the class RestListRequestTest method should_exclude_pagination_when_pagesize_0.

@Test
public void should_exclude_pagination_when_pagesize_0() {
    RestListRequest request = new RestListRequest();
    request.setPage(1);
    request.setPageSize(0);
    request.setSort("name");
    request.setDirection(FieldSearchFilter.Order.ASC.name());
    request.addFilter(new Filter("name", "jack"));
    request.addFilter(new Filter("city", "rome"));
    request.addFilter(new Filter("mobile", "+39"));
    // filters
    List<FieldSearchFilter> filters = request.buildFieldSearchFilters();
    assertThat(filters.size(), is(4));
    assertThat(filters.get(0).getKey(), is("name"));
    assertThat(filters.get(1).getKey(), is("city"));
    assertThat(filters.get(2).getKey(), is("mobile"));
    // sort
    assertThat(filters.get(3).getKey(), is("name"));
    assertThat(filters.get(3).getOrder(), is(FieldSearchFilter.Order.ASC));
}
Also used : FieldSearchFilter(com.agiletec.aps.system.common.FieldSearchFilter) Filter(org.entando.entando.web.common.model.Filter) RestListRequest(org.entando.entando.web.common.model.RestListRequest) FieldSearchFilter(com.agiletec.aps.system.common.FieldSearchFilter) Test(org.junit.Test)

Example 19 with Filter

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

the class RestListRequestTest method shuold_create_filters.

@Test
public void shuold_create_filters() {
    RestListRequest request = new RestListRequest();
    request.setPage(1);
    request.setPageSize(25);
    request.setSort("name");
    request.setDirection(FieldSearchFilter.Order.ASC.name());
    request.addFilter(new Filter("name", "jack"));
    request.addFilter(new Filter("city", "rome"));
    request.addFilter(new Filter("mobile", "+39"));
    // filters
    List<FieldSearchFilter> filters = request.buildFieldSearchFilters();
    assertThat(filters.size(), is(5));
    assertThat(filters.get(0).getKey(), is("name"));
    assertThat(filters.get(1).getKey(), is("city"));
    assertThat(filters.get(2).getKey(), is("mobile"));
    // pagination
    assertThat(filters.get(3).getKey(), is(nullValue()));
    assertThat(filters.get(3).getLimit(), is(not(nullValue())));
    assertThat(filters.get(3).getOffset(), is(not(nullValue())));
    // sort
    assertThat(filters.get(4).getKey(), is("name"));
    assertThat(filters.get(4).getOrder(), is(FieldSearchFilter.Order.ASC));
}
Also used : FieldSearchFilter(com.agiletec.aps.system.common.FieldSearchFilter) Filter(org.entando.entando.web.common.model.Filter) RestListRequest(org.entando.entando.web.common.model.RestListRequest) FieldSearchFilter(com.agiletec.aps.system.common.FieldSearchFilter) Test(org.junit.Test)

Example 20 with Filter

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

the class LabelService method getLabelGroups.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public PagedMetadata<LabelDto> getLabelGroups(RestListRequest restRequest) {
    Map<String, ApsProperties> result = this.i18nManager.getLabelGroups();
    List<LabelDto> dtoList = this.getDtoBuilder().convert(result);
    if (restRequest.getDirection().equals(FieldSearchFilter.DESC_ORDER)) {
        dtoList = dtoList.stream().sorted(Comparator.comparing(LabelDto::getKey).reversed()).collect(Collectors.toList());
    } else {
        dtoList = dtoList.stream().sorted(Comparator.comparing(LabelDto::getKey)).collect(Collectors.toList());
    }
    if (null != restRequest.getFilters()) {
        for (Filter f : restRequest.getFilters()) {
            if (f.getAttributeName().equals(LABEL_KEY_FILTER_KEY)) {
                dtoList = dtoList.stream().filter(i -> i.getKey().toLowerCase().contains(f.getValue().toLowerCase())).collect(Collectors.toList());
            }
            if (f.getAttributeName().equals(LABEL_KEY_FILTER_VALUE)) {
                dtoList = dtoList.stream().filter(i -> i.getTitles().values().stream().filter(k -> k.contains(f.getValue())).collect(Collectors.toList()).size() > 0).collect(Collectors.toList());
            }
        }
    }
    List<?> subList = restRequest.getSublist(dtoList);
    SearcherDaoPaginatedResult<LabelDto> resultx = new SearcherDaoPaginatedResult(dtoList.size(), subList);
    PagedMetadata<LabelDto> pagedMetadata = new PagedMetadata<>(restRequest, resultx);
    pagedMetadata.setBody((List<LabelDto>) subList);
    return pagedMetadata;
}
Also used : FieldSearchFilter(com.agiletec.aps.system.common.FieldSearchFilter) Filter(org.entando.entando.web.common.model.Filter) PagedMetadata(org.entando.entando.web.common.model.PagedMetadata) LabelDto(org.entando.entando.aps.system.services.label.model.LabelDto) SearcherDaoPaginatedResult(com.agiletec.aps.system.common.model.dao.SearcherDaoPaginatedResult) ApsProperties(com.agiletec.aps.util.ApsProperties)

Aggregations

Filter (org.entando.entando.web.common.model.Filter)38 RestListRequest (org.entando.entando.web.common.model.RestListRequest)30 Test (org.junit.Test)26 FieldSearchFilter (com.agiletec.aps.system.common.FieldSearchFilter)16 PagedMetadata (org.entando.entando.web.common.model.PagedMetadata)9 AbstractControllerTest (org.entando.entando.web.AbstractControllerTest)8 SearcherDaoPaginatedResult (com.agiletec.aps.system.common.model.dao.SearcherDaoPaginatedResult)5 UserDetails (com.agiletec.aps.system.services.user.UserDetails)4 ResultActions (org.springframework.test.web.servlet.ResultActions)4 Permission (com.agiletec.aps.system.services.role.Permission)3 Role (com.agiletec.aps.system.services.role.Role)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 ResourceNotFoundException (org.entando.entando.aps.system.exception.ResourceNotFoundException)3 RestServerError (org.entando.entando.aps.system.exception.RestServerError)3 GroupDto (org.entando.entando.aps.system.services.group.model.GroupDto)3 LabelDto (org.entando.entando.aps.system.services.label.model.LabelDto)3 RoleDto (org.entando.entando.aps.system.services.role.model.RoleDto)3 WidgetDto (org.entando.entando.aps.system.services.widgettype.model.WidgetDto)3 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)3