diff options
author | Leif Åstrand <legioth@gmail.com> | 2017-01-17 16:22:55 +0200 |
---|---|---|
committer | Denis <denis@vaadin.com> | 2017-01-17 16:22:55 +0200 |
commit | 83b16a8ee742b676bd8ac8ef48e62b8fd64326e3 (patch) | |
tree | 23321774daa70bf2faf41be546fbae169b5dcf61 /server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java | |
parent | 487cb4ea0c5e51e7a9b85d6bbb6ab9200f6772f7 (diff) | |
download | vaadin-framework-83b16a8ee742b676bd8ac8ef48e62b8fd64326e3.tar.gz vaadin-framework-83b16a8ee742b676bd8ac8ef48e62b8fd64326e3.zip |
Make data providers statefull with regards to default sort orders (#8247)
* Make data providers statefull with regards to default sort orders
This is one of many steps towards #8245
Diffstat (limited to 'server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java')
-rw-r--r-- | server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java | 89 |
1 files changed, 59 insertions, 30 deletions
diff --git a/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java index 5f789e8a55..3606a99ade 100644 --- a/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java +++ b/server/src/main/java/com/vaadin/data/provider/BackEndDataProvider.java @@ -16,13 +16,15 @@ package com.vaadin.data.provider; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; import java.util.stream.Stream; import com.vaadin.server.SerializableFunction; import com.vaadin.server.SerializableToIntFunction; -import com.vaadin.shared.Registration; /** * A {@link DataProvider} for any back end. @@ -34,6 +36,8 @@ import com.vaadin.shared.Registration; */ public class BackEndDataProvider<T, F> extends AbstractDataProvider<T, F> { + private List<SortOrder<String>> sortOrders = new ArrayList<>(); + private final SerializableFunction<Query<T, F>, Stream<T>> request; private final SerializableToIntFunction<Query<T, F>> sizeCallback; @@ -57,46 +61,71 @@ public class BackEndDataProvider<T, F> extends AbstractDataProvider<T, F> { @Override public Stream<T> fetch(Query<T, F> query) { - return request.apply(query); + return request.apply(mixInSortOrders(query)); } @Override public int size(Query<T, F> query) { - return sizeCallback.applyAsInt(query); + return sizeCallback.applyAsInt(mixInSortOrders(query)); + } + + private Query<T, F> mixInSortOrders(Query<T, F> query) { + Set<String> sortedPropertyNames = query.getSortOrders().stream() + .map(SortOrder::getSorted).collect(Collectors.toSet()); + + List<SortOrder<String>> combinedSortOrders = Stream + .concat(query.getSortOrders().stream(), + sortOrders.stream() + .filter(order -> !sortedPropertyNames + .contains(order.getSorted()))) + .collect(Collectors.toList()); + + return new Query<>(query.getOffset(), query.getLimit(), + combinedSortOrders, query.getInMemorySorting(), + query.getFilter().orElse(null)); } /** - * Sets a default sorting order to the data provider. + * Sets a list of sort orders to use as the default sorting for this data + * provider. This overrides the sorting set by any other method that + * manipulates the default sorting of this data provider. + * <p> + * The default sorting is used if the query defines no sorting. The default + * sorting is also used to determine the ordering of items that are + * considered equal by the sorting defined in the query. + * + * @see #setSortOrder(SortOrder) * * @param sortOrders - * a list of sorting information containing field ids and - * directions - * @return new data provider with modified sorting + * a list of sort orders to set, not <code>null</code> */ - @SuppressWarnings("serial") - public BackEndDataProvider<T, F> sortingBy( - List<SortOrder<String>> sortOrders) { - BackEndDataProvider<T, F> parent = this; - return new BackEndDataProvider<T, F>(query -> { - List<SortOrder<String>> queryOrder = new ArrayList<>( - query.getSortOrders()); - queryOrder.addAll(sortOrders); - return parent.fetch(new Query<>(query.getOffset(), query.getLimit(), - queryOrder, query.getInMemorySorting(), - query.getFilter().orElse(null))); - }, sizeCallback) { - - @Override - public Registration addDataProviderListener( - DataProviderListener listener) { - return parent.addDataProviderListener(listener); - } + public void setSortOrders(List<SortOrder<String>> sortOrders) { + this.sortOrders = Objects.requireNonNull(sortOrders, + "Sort orders cannot be null"); + refreshAll(); + } - @Override - public void refreshAll() { - parent.refreshAll(); - } - }; + /** + * Sets a single sort order to use as the default sorting for this data + * provider. This overrides the sorting set by any other method that + * manipulates the default sorting of this data provider. + * <p> + * The default sorting is used if the query defines no sorting. The default + * sorting is also used to determine the ordering of items that are + * considered equal by the sorting defined in the query. + * + * @see #setSortOrders(List) + * + * @param sortOrder + * a sort order to set, or <code>null</code> to clear any + * previously set sort orders + */ + public void setSortOrder(SortOrder<String> sortOrder) { + if (sortOrder == null) { + setSortOrders(Collections.emptyList()); + } else { + setSortOrders(Collections.singletonList(sortOrder)); + } } @Override |