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