aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2016-02-02 12:46:56 +0200
committerTeemu Suo-Anttila <teemusa@vaadin.com>2016-02-03 12:23:25 +0200
commit4b7b21d35cbf80ddfa6e16f62bc4ff65008a4cd5 (patch)
tree8a16cd09fd01eeb8e2f9be2ff63af66c7eeca302
parente25dd72efe36de7e59780cda0de472c1183be028 (diff)
downloadvaadin-framework-4b7b21d35cbf80ddfa6e16f62bc4ff65008a4cd5.tar.gz
vaadin-framework-4b7b21d35cbf80ddfa6e16f62bc4ff65008a4cd5.zip
Add simple data update support to DataProvider
This patch adds the option of updating an existing data object. Update is done on the next client response to catch any possible changes before and after the update is initiated. Change-Id: I30ca4803dd7c47befcb8e5ae761860c09cbdb8bd
-rw-r--r--client/src/com/vaadin/client/connectors/data/typed/DataSourceConnector.java19
-rw-r--r--server/src/com/vaadin/server/communication/data/typed/DataProvider.java26
-rw-r--r--shared/src/com/vaadin/shared/data/DataProviderClientRpc.java8
-rw-r--r--uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderTest.java29
-rw-r--r--uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java16
-rw-r--r--uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java9
6 files changed, 105 insertions, 2 deletions
diff --git a/client/src/com/vaadin/client/connectors/data/typed/DataSourceConnector.java b/client/src/com/vaadin/client/connectors/data/typed/DataSourceConnector.java
index 1d4521c5b3..59d7f38811 100644
--- a/client/src/com/vaadin/client/connectors/data/typed/DataSourceConnector.java
+++ b/client/src/com/vaadin/client/connectors/data/typed/DataSourceConnector.java
@@ -98,6 +98,23 @@ public class DataSourceConnector extends AbstractExtensionConnector {
sendDroppedKeys();
}
}
+
+ @Override
+ public void updateData(JsonArray data) {
+ List<JsonObject> list = ds.asList();
+ for (int i = 0; i < data.length(); ++i) {
+ JsonObject json = data.getObject(i);
+ String key = getKey(json);
+
+ if (keyToJson.containsKey(key)) {
+ int index = list.indexOf(keyToJson.get(key));
+ list.set(index, json);
+ } else {
+ dropKey(key);
+ }
+ }
+ sendDroppedKeys();
+ }
});
ServerConnector parent = getParent();
@@ -117,8 +134,8 @@ public class DataSourceConnector extends AbstractExtensionConnector {
* dropped key
*/
private void dropKey(String key) {
+ droppedKeys.add(key);
if (keyToJson.containsKey(key)) {
- droppedKeys.add(key);
keyToJson.remove(key);
}
}
diff --git a/server/src/com/vaadin/server/communication/data/typed/DataProvider.java b/server/src/com/vaadin/server/communication/data/typed/DataProvider.java
index 9417d0a1f0..66489fe627 100644
--- a/server/src/com/vaadin/server/communication/data/typed/DataProvider.java
+++ b/server/src/com/vaadin/server/communication/data/typed/DataProvider.java
@@ -199,6 +199,7 @@ public class DataProvider<T> extends AbstractExtension {
private Collection<TypedDataGenerator<T>> generators = new LinkedHashSet<TypedDataGenerator<T>>();
private boolean reset = false;
+ private final Set<T> updatedData = new HashSet<T>();
private Collection<T> data;
private DataProviderClientRpc rpc;
@@ -233,7 +234,19 @@ public class DataProvider<T> extends AbstractExtension {
getRpcProxy(DataProviderClientRpc.class).resetSize(data.size());
pushData(0, data);
reset = false;
+ updatedData.clear();
}
+
+ if (updatedData.isEmpty()) {
+ return;
+ }
+
+ JsonArray dataArray = Json.createArray();
+ int i = 0;
+ for (T data : updatedData) {
+ dataArray.set(i++, getDataObject(data));
+ }
+ rpc.updateData(dataArray);
}
/**
@@ -369,4 +382,17 @@ public class DataProvider<T> extends AbstractExtension {
markAsDirty();
}
+ /**
+ * Informs the DataProvider that a data object has been updated.
+ *
+ * @param data
+ * updated data object
+ */
+ public void refresh(T data) {
+ if (updatedData.isEmpty()) {
+ markAsDirty();
+ }
+
+ updatedData.add(data);
+ }
}
diff --git a/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java b/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java
index c96a2a64fa..315df89c9b 100644
--- a/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java
+++ b/shared/src/com/vaadin/shared/data/DataProviderClientRpc.java
@@ -66,4 +66,12 @@ public interface DataProviderClientRpc extends ClientRpc {
*/
void drop(String key);
+ /**
+ * Updates an array of objects based on their identifying key.
+ *
+ * @param data
+ * array of updated data
+ */
+ void updateData(JsonArray data);
+
}
diff --git a/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderTest.java b/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderTest.java
index e82f25ba04..2a92743215 100644
--- a/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderTest.java
+++ b/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderTest.java
@@ -16,8 +16,10 @@
package com.vaadin.tests.dataprovider;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -93,6 +95,8 @@ public class DummyDataProviderTest extends SingleBrowserTest {
}
private void createPersonObjects() {
+ personObjects.clear();
+
for (ComplexPerson p : persons) {
JsonObject j = Json.createObject();
j.put(DataProviderConstants.KEY, personToKeyMap.get(p));
@@ -153,4 +157,29 @@ public class DummyDataProviderTest extends SingleBrowserTest {
assertNoErrorNotifications();
}
+
+ @Test
+ public void testEditFirstItem() {
+ persons.retainAll(Arrays.asList(persons.get(0)));
+ createPersonObjects();
+
+ openTestURL();
+
+ String json = personObjects.get(0).toJson();
+ String text = findElements(By.className("v-label")).get(1).getText();
+ assertEquals("Initial data did not match", json, text);
+
+ $(ButtonElement.class).id("edit").click();
+
+ persons.get(0).setFirstName("Foo");
+ createPersonObjects();
+
+ json = personObjects.get(0).toJson();
+
+ assertFalse("JsonObject of edited person was not updated",
+ json.equals(text));
+
+ text = findElements(By.className("v-label")).get(1).getText();
+ assertEquals("Modified data did not match", json, text);
+ }
}
diff --git a/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java b/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java
index 095f4d694c..b86037c89e 100644
--- a/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java
+++ b/uitest/src/com/vaadin/tests/dataprovider/DummyDataProviderUI.java
@@ -85,6 +85,10 @@ public class DummyDataProviderUI extends AbstractTestUI {
Collections.sort(data, comparator);
dataProvider.reset();
}
+
+ public void update(ComplexPerson p) {
+ dataProvider.refresh(p);
+ }
}
public static final int RANDOM_SEED = 1337;
@@ -128,13 +132,23 @@ public class DummyDataProviderUI extends AbstractTestUI {
dummy.sort(nameComparator);
}
});
+ Button edit = new Button("Edit first", new ClickListener() {
+
+ @Override
+ public void buttonClick(ClickEvent event) {
+ ComplexPerson p = persons.get(0);
+ p.setFirstName("Foo");
+ dummy.update(p);
+ }
+ });
// Button Ids
remove.setId("remove");
add.setId("add");
sort.setId("sort");
+ edit.setId("edit");
- addComponent(new HorizontalLayout(add, remove, sort));
+ addComponent(new HorizontalLayout(add, remove, sort, edit));
addComponent(dummy);
}
diff --git a/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java b/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java
index c64f30ab57..cc01faceb0 100644
--- a/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java
+++ b/uitest/src/com/vaadin/tests/widgetset/client/dataprovider/DummyDataConnector.java
@@ -62,6 +62,15 @@ public class DummyDataConnector extends AbstractComponentConnector implements
.toJson()));
}
}
+
+ @Override
+ public void dataUpdated(int firstRowIndex, int numberOfRows) {
+ for (int i = 0; i < numberOfRows; ++i) {
+ VLabel label = (VLabel) getWidget().getWidget(
+ i + firstRowIndex);
+ label.setText(dataSource.getRow(i + firstRowIndex).toJson());
+ }
+ }
});
}
}