You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleDataProvider.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tokka.server.communication.data;
  17. import java.util.Collections;
  18. import java.util.HashSet;
  19. import java.util.Set;
  20. import com.vaadin.shared.data.DataRequestRpc;
  21. import com.vaadin.shared.data.typed.DataProviderClientRpc;
  22. import elemental.json.Json;
  23. import elemental.json.JsonArray;
  24. /**
  25. * DataProvider for Collections. This class takes care of sending data objects
  26. * stored in a Collection from the server-side to the client-side.
  27. * <p>
  28. * This is an implementation that does not provide any kind of lazy loading. All
  29. * data is sent to the client-side on the initial client response.
  30. *
  31. * @since
  32. */
  33. public class SimpleDataProvider<T> extends DataProvider<T> {
  34. /**
  35. * Simple implementation of collection data provider communication. All data
  36. * is sent by server automatically and no data is requested by client.
  37. */
  38. protected class SimpleDataRequestRpc implements DataRequestRpc {
  39. @Override
  40. public void requestRows(int firstRowIndex, int numberOfRows,
  41. int firstCachedRowIndex, int cacheSize) {
  42. throw new UnsupportedOperationException(
  43. "Collection data provider sends all data from server."
  44. + " It does not expect client to request anything.");
  45. }
  46. @Override
  47. public void dropRows(JsonArray keys) {
  48. for (int i = 0; i < keys.length(); ++i) {
  49. handler.dropActiveData(keys.getString(i));
  50. }
  51. // Use the whole data as the ones sent to the client.
  52. handler.cleanUp(dataSource);
  53. }
  54. }
  55. private boolean reset = false;
  56. private final Set<T> updatedData = new HashSet<T>();
  57. /**
  58. * Creates a new DataProvider with the given Collection.
  59. *
  60. * @param data
  61. * collection of data to use
  62. */
  63. protected SimpleDataProvider(DataSource<T> data) {
  64. super(data);
  65. }
  66. /**
  67. * Initially and in the case of a reset all data should be pushed to the
  68. * client.
  69. */
  70. @Override
  71. public void beforeClientResponse(boolean initial) {
  72. super.beforeClientResponse(initial);
  73. if (reset) {
  74. getRpcProxy(DataProviderClientRpc.class).reset();
  75. }
  76. if (initial || reset) {
  77. pushData(0, dataSource);
  78. } else if (!updatedData.isEmpty()) {
  79. JsonArray dataArray = Json.createArray();
  80. int i = 0;
  81. for (T data : updatedData) {
  82. dataArray.set(i++, getDataObject(data));
  83. }
  84. rpc.updateData(dataArray);
  85. }
  86. reset = false;
  87. updatedData.clear();
  88. }
  89. /**
  90. * Informs the DataProvider that a data object has been added. It is assumed
  91. * to be the last object in the collection.
  92. *
  93. * @param data
  94. * data object added to collection
  95. */
  96. protected void add(T data) {
  97. rpc.add(getDataObject(data));
  98. handler.addActiveData(Collections.singleton(data));
  99. }
  100. /**
  101. * Informs the DataProvider that a data object has been removed.
  102. *
  103. * @param data
  104. * data object removed from collection
  105. */
  106. protected void remove(T data) {
  107. if (handler.getActiveData().contains(data)) {
  108. rpc.drop(getKeyMapper().key(data));
  109. }
  110. }
  111. /**
  112. * Informs the DataProvider that the collection has changed.
  113. */
  114. protected void reset() {
  115. if (reset) {
  116. return;
  117. }
  118. reset = true;
  119. markAsDirty();
  120. }
  121. /**
  122. * Informs the DataProvider that a data object has been updated.
  123. *
  124. * @param data
  125. * updated data object
  126. */
  127. public void refresh(T data) {
  128. if (updatedData.isEmpty()) {
  129. markAsDirty();
  130. }
  131. updatedData.add(data);
  132. }
  133. @Override
  134. protected DataKeyMapper<T> createKeyMapper() {
  135. return new KeyMapper<T>();
  136. }
  137. @Override
  138. protected DataRequestRpc createRpc() {
  139. return new SimpleDataRequestRpc();
  140. }
  141. @Override
  142. protected DataChangeHandler<T> createDataChangeHandler() {
  143. return new DataChangeHandler<T>() {
  144. @Override
  145. public void onDataChange() {
  146. reset();
  147. }
  148. @Override
  149. public void onDataAppend(T data) {
  150. add(data);
  151. }
  152. @Override
  153. public void onDataRemove(T data) {
  154. remove(data);
  155. }
  156. @Override
  157. public void onDataUpdate(T data) {
  158. refresh(data);
  159. }
  160. };
  161. }
  162. }