From e21098304736e32a74f359e8dfb80e587337085d Mon Sep 17 00:00:00 2001 From: Jonatan Kronqvist Date: Thu, 8 Sep 2011 10:53:55 +0000 Subject: Applied patch fixing #7391 svn changeset:20926/svn branch:6.7 --- .../data/util/sqlcontainer/SQLContainer.java | 31 +++++++++++++++++++++- .../sqlcontainer/SQLContainerTableQueryTest.java | 31 ++++++++++++++++++++-- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java b/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java index f25252b8c0..f6b22bfecb 100644 --- a/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java +++ b/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java @@ -1199,7 +1199,18 @@ public class SQLContainer implements Container, Container.Filterable, } /* Cache item */ itemIndexes.put(rowCount, id); - cachedItems.put(id, new RowItem(this, id, itemProperties)); + + // if an item with the id is contained in the modified + // cache, then use this record and add it to the cached + // items. Otherwise create a new item + int modifiedIndex = indexInModifiedCache(id); + if (modifiedIndex != -1) { + cachedItems.put(id, modifiedItems.get(modifiedIndex)); + } else { + cachedItems.put(id, new RowItem(this, id, + itemProperties)); + } + rowCount++; } } @@ -1229,6 +1240,24 @@ public class SQLContainer implements Container, Container.Filterable, } } + /** + * Returns the index of the item with the given itemId for the modified + * cache. + * + * @param itemId + * @return the index of the item with the itemId in the modified cache. Or + * -1 if not found. + */ + private int indexInModifiedCache(Object itemId) { + for (int ix = 0; ix < modifiedItems.size(); ix++) { + RowItem item = modifiedItems.get(ix); + if (item.getId().equals(itemId)) { + return ix; + } + } + return -1; + } + private int sizeOfAddedItems() { return getFilteredAddedItems().size(); } diff --git a/tests/src/com/vaadin/tests/server/container/sqlcontainer/SQLContainerTableQueryTest.java b/tests/src/com/vaadin/tests/server/container/sqlcontainer/SQLContainerTableQueryTest.java index af9cdbf306..0451607e64 100644 --- a/tests/src/com/vaadin/tests/server/container/sqlcontainer/SQLContainerTableQueryTest.java +++ b/tests/src/com/vaadin/tests/server/container/sqlcontainer/SQLContainerTableQueryTest.java @@ -19,14 +19,12 @@ import com.vaadin.data.Container.ItemSetChangeListener; import com.vaadin.data.Item; import com.vaadin.data.util.filter.Like; import com.vaadin.data.util.sqlcontainer.RowId; -import com.vaadin.data.util.sqlcontainer.RowItem; import com.vaadin.data.util.sqlcontainer.SQLContainer; import com.vaadin.data.util.sqlcontainer.TemporaryRowId; import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; import com.vaadin.data.util.sqlcontainer.query.OrderBy; import com.vaadin.data.util.sqlcontainer.query.TableQuery; -import com.vaadin.terminal.gwt.client.Util; import com.vaadin.tests.server.container.sqlcontainer.AllTests.DB; public class SQLContainerTableQueryTest { @@ -145,6 +143,35 @@ public class SQLContainerTableQueryTest { Assert.assertEquals("Ville", item.getItemProperty("NAME").getValue()); } + @Test + public void getItem_commitedModifiedAndRefreshed() throws SQLException { + String OLD_VALUE = "SomeValue"; //$NON-NLS-1$ + String NEW_VALUE = "OtherValue"; //$NON-NLS-1$ + + SQLContainer container = new SQLContainer(new TableQuery("people", //$NON-NLS-1$ + connectionPool, AllTests.sqlGen)); + Object itemID = container.addItem(); + Item item = container.getItem(itemID); + item.getItemProperty("NAME").setValue(OLD_VALUE); //$NON-NLS-1$ + container.commit(); + + itemID = container.getIdByIndex(container.size() - 1); + item = container.getItem(itemID); + Assert.assertEquals(OLD_VALUE, item.getItemProperty("NAME") //$NON-NLS-1$ + .getValue()); + item.getItemProperty("NAME").setValue(NEW_VALUE); //$NON-NLS-1$ + + // refresh the container which free's the caches + // and the modified cache keeps untouched which is a really powerful + // feature + container.refresh(); + + // access the item again will use the item from the modified cache. + item = container.getItem(itemID); + Assert.assertEquals(NEW_VALUE, item.getItemProperty("NAME") //$NON-NLS-1$ + .getValue()); + } + @Test public void getItem_table5000RowsWithParameter1337_returnsItemWithId1337() throws SQLException { -- cgit v1.2.3