From: Aleksi Hietanen Date: Fri, 12 May 2017 06:44:55 +0000 (+0300) Subject: Add BackEndHierarchicalDataProvider and an abstract base class X-Git-Tag: 8.1.0.alpha8~3 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9c58a56ffe102082afcee9c55825612711b2530b;p=vaadin-framework.git Add BackEndHierarchicalDataProvider and an abstract base class --- diff --git a/server/src/main/java/com/vaadin/data/provider/AbstractBackEndHierarchicalDataProvider.java b/server/src/main/java/com/vaadin/data/provider/AbstractBackEndHierarchicalDataProvider.java new file mode 100644 index 0000000000..8cefc8a9c8 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/provider/AbstractBackEndHierarchicalDataProvider.java @@ -0,0 +1,93 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data.provider; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Abstract base class for implementing + * {@link BackEndHierarchicalDataProvider}s. + * + * @author Vaadin Ltd + * @since 8.1 + * + * @param + * data type + * @param + * filter type + */ +public abstract class AbstractBackEndHierarchicalDataProvider + extends AbstractHierarchicalDataProvider + implements BackEndHierarchicalDataProvider { + + private List sortOrders = new ArrayList<>(); + + private HierarchicalQuery mixInSortOrders( + HierarchicalQuery query) { + if (sortOrders.isEmpty()) { + return query; + } + + Set sortedPropertyNames = query.getSortOrders().stream() + .map(SortOrder::getSorted).collect(Collectors.toSet()); + + List combinedSortOrders = Stream + .concat(query.getSortOrders().stream(), + sortOrders.stream() + .filter(order -> !sortedPropertyNames + .contains(order.getSorted()))) + .collect(Collectors.toList()); + + return new HierarchicalQuery<>(query.getOffset(), query.getLimit(), + combinedSortOrders, query.getInMemorySorting(), + query.getFilter().orElse(null), query.getParent()); + } + + @Override + public Stream fetchChildren(HierarchicalQuery query) { + return fetchChildrenFromBackEnd(mixInSortOrders(query)); + } + + @Override + public boolean isInMemory() { + return false; + } + + @Override + public void setSortOrders(List sortOrders) { + this.sortOrders = Objects.requireNonNull(sortOrders, + "Sort orders cannot be null"); + refreshAll(); + } + + /** + * Fetches data from the back end using the given query. + * + * @see HierarchicalQuery + * + * @param query + * the query that defines sorting, filtering, paging and the + * parent item to fetch children from + * @return a stream of items matching the query + */ + protected abstract Stream fetchChildrenFromBackEnd( + HierarchicalQuery query); +} diff --git a/server/src/main/java/com/vaadin/data/provider/BackEndHierarchicalDataProvider.java b/server/src/main/java/com/vaadin/data/provider/BackEndHierarchicalDataProvider.java new file mode 100644 index 0000000000..82900bb790 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/provider/BackEndHierarchicalDataProvider.java @@ -0,0 +1,33 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data.provider; + +/** + * A data provider that lazy loads items from a back end containing hierarchical + * data. + * + * @author Vaadin Ltd + * @since 8.1 + * + * @param + * data provider data type + * @param + * data provider filter type + */ +public interface BackEndHierarchicalDataProvider + extends HierarchicalDataProvider, BackEndDataProvider { + +} diff --git a/server/src/main/java/com/vaadin/ui/Tree.java b/server/src/main/java/com/vaadin/ui/Tree.java index 7b8fae59ca..6ecbaac597 100644 --- a/server/src/main/java/com/vaadin/ui/Tree.java +++ b/server/src/main/java/com/vaadin/ui/Tree.java @@ -38,7 +38,6 @@ import com.vaadin.event.SerializableEventListener; import com.vaadin.event.selection.SelectionListener; import com.vaadin.server.ErrorMessage; import com.vaadin.server.Resource; -import com.vaadin.server.SerializablePredicate; import com.vaadin.shared.Registration; import com.vaadin.shared.ui.ContentMode; import com.vaadin.shared.ui.grid.HeightMode; diff --git a/uitest/src/main/java/com/vaadin/tests/components/treegrid/LazyHierarchicalDataProvider.java b/uitest/src/main/java/com/vaadin/tests/components/treegrid/LazyHierarchicalDataProvider.java index 1fe7202d8a..a468f34d29 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/treegrid/LazyHierarchicalDataProvider.java +++ b/uitest/src/main/java/com/vaadin/tests/components/treegrid/LazyHierarchicalDataProvider.java @@ -5,12 +5,12 @@ import java.util.List; import java.util.Optional; import java.util.stream.Stream; -import com.vaadin.data.provider.AbstractHierarchicalDataProvider; +import com.vaadin.data.provider.AbstractBackEndHierarchicalDataProvider; import com.vaadin.data.provider.HierarchicalQuery; import com.vaadin.tests.data.bean.HierarchicalTestBean; -public class LazyHierarchicalDataProvider - extends AbstractHierarchicalDataProvider { +public class LazyHierarchicalDataProvider extends + AbstractBackEndHierarchicalDataProvider { private final int nodesPerLevel; private final int depth; @@ -32,7 +32,16 @@ public class LazyHierarchicalDataProvider } @Override - public Stream fetchChildren( + public boolean hasChildren(HierarchicalTestBean item) { + return internalHasChildren(item); + } + + private boolean internalHasChildren(HierarchicalTestBean node) { + return node.getDepth() < depth; + } + + @Override + protected Stream fetchChildrenFromBackEnd( HierarchicalQuery query) { final int depth = query.getParentOptional().isPresent() ? query.getParent().getDepth() + 1 : 0; @@ -46,18 +55,4 @@ public class LazyHierarchicalDataProvider } return list.stream(); } - - @Override - public boolean hasChildren(HierarchicalTestBean item) { - return internalHasChildren(item); - } - - private boolean internalHasChildren(HierarchicalTestBean node) { - return node.getDepth() < depth; - } - - @Override - public boolean isInMemory() { - return false; - } }