]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add Query object for backend data requests
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Tue, 12 Jul 2016 11:57:52 +0000 (14:57 +0300)
committerTeemu Suo-Anttila <teemusa@vaadin.com>
Wed, 13 Jul 2016 09:52:30 +0000 (09:52 +0000)
Change-Id: Ia27923f0a5d178e8b79b4715c93bb8064bbebae0

server/src/main/java/com/vaadin/tokka/server/communication/data/DataCommunicator.java
server/src/main/java/com/vaadin/tokka/server/communication/data/DataSource.java
server/src/main/java/com/vaadin/tokka/server/communication/data/InMemoryDataSource.java
server/src/main/java/com/vaadin/tokka/server/communication/data/Query.java [new file with mode: 0644]
server/src/test/java/com/vaadin/tokka/data/datasource/InMemoryDataSourceTest.java

index d840193bd11072522790bcfaad633e4f5b015b8d..b31e0bbc98524c8fa15f4e78b786a8b1afe988bd 100644 (file)
@@ -17,6 +17,7 @@ package com.vaadin.tokka.server.communication.data;
 
 import java.io.Serializable;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -194,16 +195,35 @@ public class DataCommunicator<T> extends AbstractExtension {
     public void beforeClientResponse(boolean initial) {
         super.beforeClientResponse(initial);
 
+        // FIXME: Sorting and Filtering with Backend
+        List<Object> sortOrders = Collections.emptyList();
+        Set<Object> filters = Collections.emptySet();
+
         if (initial || reset) {
             // FIXME: Rethink the size question.
-            rpc.reset((int) dataSource.apply(null).count());
+            int dataSourceSize = (int) dataSource.apply(
+                    new Query(0, Integer.MAX_VALUE, sortOrders, filters))
+                    .count();
+            rpc.reset(dataSourceSize);
         }
 
         if (!pushRows.isEmpty()) {
-            // FIXME: Query object
-            Stream<T> rowsToPush = dataSource.apply(null)
-                    .skip(pushRows.getStart()).limit(pushRows.length());
-            pushData(pushRows.getStart(), rowsToPush);
+            int offset = pushRows.getStart();
+            int limit = pushRows.length();
+
+            Stream<T> rowsToPush;
+
+            if (dataSource instanceof InMemoryDataSource) {
+                // We can safely request all the data when in memory
+                // FIXME: sorted and filter.
+                rowsToPush = dataSource.apply(new Query()).skip(offset)
+                        .limit(limit);
+            } else {
+                Query query = new Query(offset, limit, sortOrders, filters);
+                rowsToPush = dataSource.apply(query);
+            }
+
+            pushData(offset, rowsToPush);
         }
 
         if (!updatedData.isEmpty()) {
@@ -381,4 +401,4 @@ public class DataCommunicator<T> extends AbstractExtension {
     protected DataRequestRpc createRpc() {
         return new SimpleDataRequestRpc();
     }
-}
\ No newline at end of file
+}
index 1587200c91dc039869f5c32feb94ce86ee8a95a6..38a195e953c72fa7ea355d09b561283ae7643f5b 100644 (file)
@@ -33,7 +33,7 @@ import com.vaadin.tokka.event.Registration;
  * @param <T>
  *            data type
  */
-public interface DataSource<T, SORT> extends Function<Object, Stream<T>>,
+public interface DataSource<T, SORT> extends Function<Query, Stream<T>>,
         Serializable {
 
     /**
index a23d337652a3e40a5bd6eb793fe6e9daca86f065..49ca85d403ce844cd54836d2049c5b6f30a6739a 100644 (file)
@@ -23,14 +23,15 @@ import java.util.function.Function;
 import java.util.stream.Stream;
 
 /**
- * {@link DataSource} wrapper for {@link Collection}s.
+ * {@link DataSource} wrapper for {@link Collection}s. This class does not
+ * actually handle the {@link Query} parameters.
  *
  * @param <T>
  *            data type
  */
 public class InMemoryDataSource<T> implements DataSource<T, Comparator<T>> {
 
-    private Function<Object, Stream<T>> request;
+    private Function<Query, Stream<T>> request;
 
     /**
      * Constructs a new ListDataSource. This method makes a protective copy of
@@ -52,12 +53,12 @@ public class InMemoryDataSource<T> implements DataSource<T, Comparator<T>> {
      * @param request
      *            request for the new data source
      */
-    protected InMemoryDataSource(Function<Object, Stream<T>> request) {
+    protected InMemoryDataSource(Function<Query, Stream<T>> request) {
         this.request = request;
     }
 
     @Override
-    public Stream<T> apply(Object query) {
+    public Stream<T> apply(Query query) {
         return request.apply(query);
     }
 
diff --git a/server/src/main/java/com/vaadin/tokka/server/communication/data/Query.java b/server/src/main/java/com/vaadin/tokka/server/communication/data/Query.java
new file mode 100644 (file)
index 0000000..03f7ef3
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2000-2014 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.tokka.server.communication.data;
+
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Query object used to request data from a backend. Contains index limits,
+ * sorting and filtering information.
+ * 
+ * @since
+ */
+public class Query implements Serializable {
+
+    private final int offset;
+    private final int limit;
+    private final List<Object> sortOrders;
+    private final Set<Object> filters;
+
+    /**
+     * Constructs a Query for all rows from 0 to {@link Integer#MAX_VALUE}
+     * without sorting and filtering.
+     */
+    public Query() {
+        offset = 0;
+        limit = Integer.MAX_VALUE;
+        sortOrders = Collections.emptyList();
+        filters = Collections.emptySet();
+    }
+
+    /**
+     * Constructs a new Query object with given offset, limit, sorting and
+     * filtering.
+     * 
+     * @param offset
+     *            first index to fetch
+     * @param limit
+     *            fetched item count
+     * @param sortOrders
+     *            sorting order for fetching
+     * @param filters
+     *            filtering for fetching
+     */
+    public Query(int offset, int limit, List<Object> sortOrders,
+            Set<Object> filters) {
+        this.offset = offset;
+        this.limit = limit;
+        this.sortOrders = sortOrders;
+        this.filters = filters;
+    }
+
+    /**
+     * Gets the first index of items to fetch.
+     * 
+     * @return offset for data request
+     */
+    public int getOffset() {
+        return offset;
+    }
+
+    /**
+     * Gets the limit of items to fetch.
+     * <p>
+     * <strong>Note: </strong>It is possible that
+     * {@code offset + limit > item count}
+     * 
+     * @return number of items to fetch
+     */
+    public int getLimit() {
+        return limit;
+    }
+
+    /**
+     * Gets the sorting for items to fetch.
+     * 
+     * @return list of sort orders
+     */
+    public List<Object> getSortOrders() {
+        return sortOrders;
+    }
+
+    /**
+     * Gets the filters for items to fetch.
+     * 
+     * @return set of filters
+     */
+    public Set<Object> getFilters() {
+        return filters;
+    }
+}
index 545c4f834ba987092ab706be97b9697f892e4b48..6572fcc5397cf452254735761c13fa3199a3ada9 100644 (file)
@@ -12,6 +12,7 @@ import org.junit.Test;
 
 import com.vaadin.tokka.server.communication.data.DataSource;
 import com.vaadin.tokka.server.communication.data.InMemoryDataSource;
+import com.vaadin.tokka.server.communication.data.Query;
 
 public class InMemoryDataSourceTest {
 
@@ -26,7 +27,7 @@ public class InMemoryDataSourceTest {
 
     @Test
     public void testListContainsAllData() {
-        dataSource.apply(null).forEach(
+        dataSource.apply(new Query()).forEach(
                 str -> assertTrue(
                         "Data source contained values not in original data",
                         data.remove(str)));
@@ -39,7 +40,7 @@ public class InMemoryDataSourceTest {
         Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue)
                 .thenComparing(StrBean::getRandomNumber)
                 .thenComparing(StrBean::getId);
-        List<StrBean> list = dataSource.sortingBy(comp).apply(null)
+        List<StrBean> list = dataSource.sortingBy(comp).apply(new Query())
                 .collect(Collectors.toList());
 
         // First value in data is { Xyz, 10, 100 } which should be last in list
@@ -60,7 +61,7 @@ public class InMemoryDataSourceTest {
     public void testDefatulSortWithSpecifiedPostSort() {
         Comparator<StrBean> comp = Comparator.comparing(StrBean::getValue)
                 .thenComparing(Comparator.comparing(StrBean::getId).reversed());
-        List<StrBean> list = dataSource.sortingBy(comp).apply(null)
+        List<StrBean> list = dataSource.sortingBy(comp).apply(new Query())
                 // The sort here should come e.g from a Component
                 .sorted(Comparator.comparing(StrBean::getRandomNumber))
                 .collect(Collectors.toList());
@@ -87,7 +88,7 @@ public class InMemoryDataSourceTest {
     @Test
     public void testDefatulSortWithFunction() {
         List<StrBean> list = dataSource.sortingBy(StrBean::getValue)
-                .apply(null).collect(Collectors.toList());
+                .apply(new Query()).collect(Collectors.toList());
 
         Assert.assertEquals("Sorted data and original data sizes don't match",
                 data.size(), list.size());