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.

RpcDataSourceConnector.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.client.data;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import com.google.gwt.json.client.JSONArray;
  22. import com.google.gwt.json.client.JSONObject;
  23. import com.google.gwt.json.client.JSONParser;
  24. import com.google.gwt.json.client.JSONString;
  25. import com.google.gwt.json.client.JSONValue;
  26. import com.vaadin.client.ServerConnector;
  27. import com.vaadin.client.extensions.AbstractExtensionConnector;
  28. import com.vaadin.client.ui.grid.GridConnector;
  29. import com.vaadin.shared.data.DataProviderRpc;
  30. import com.vaadin.shared.data.DataProviderState;
  31. import com.vaadin.shared.data.DataRequestRpc;
  32. import com.vaadin.shared.ui.Connect;
  33. import com.vaadin.shared.ui.grid.GridState;
  34. import com.vaadin.shared.ui.grid.Range;
  35. /**
  36. * Connects a Vaadin server-side container data source to a Grid. This is
  37. * currently implemented as an Extension hardcoded to support a specific
  38. * connector type. This will be changed once framework support for something
  39. * more flexible has been implemented.
  40. *
  41. * @since
  42. * @author Vaadin Ltd
  43. */
  44. @Connect(com.vaadin.data.RpcDataProviderExtension.class)
  45. public class RpcDataSourceConnector extends AbstractExtensionConnector {
  46. public class RpcDataSource extends AbstractRemoteDataSource<JSONObject> {
  47. private Collection<JSONObject> prevRows = Collections.emptySet();
  48. @Override
  49. protected void requestRows(int firstRowIndex, int numberOfRows) {
  50. Range cached = getCachedRange();
  51. Collection<JSONObject> newRows = new ArrayList<JSONObject>(
  52. temporarilyPinnedRows);
  53. newRows.removeAll(prevRows);
  54. List<String> temporarilyPinnedKeys = new ArrayList<String>(
  55. newRows.size());
  56. for (JSONObject row : newRows) {
  57. temporarilyPinnedKeys.add((String) getRowKey(row));
  58. }
  59. getRpcProxy(DataRequestRpc.class).requestRows(firstRowIndex,
  60. numberOfRows, cached.getStart(), cached.length(),
  61. temporarilyPinnedKeys);
  62. prevRows = temporarilyPinnedRows;
  63. }
  64. @Override
  65. public Object getRowKey(JSONObject row) {
  66. JSONString string = row.get(GridState.JSONKEY_ROWKEY).isString();
  67. if (string != null) {
  68. return string.stringValue();
  69. } else {
  70. return null;
  71. }
  72. }
  73. public RowHandle<JSONObject> getHandleByKey(Object key) {
  74. JSONObject row = new JSONObject();
  75. row.put(GridState.JSONKEY_ROWKEY, new JSONString((String) key));
  76. return new RowHandleImpl(row, key);
  77. }
  78. @Override
  79. @SuppressWarnings("deprecation")
  80. public void transactionPin(Collection<JSONObject> keys) {
  81. super.transactionPin(keys);
  82. if (keys.isEmpty() && !prevRows.isEmpty()) {
  83. prevRows = Collections.emptySet();
  84. getRpcProxy(DataRequestRpc.class)
  85. .releaseTemporarilyPinnedKeys();
  86. }
  87. }
  88. @Override
  89. public int size() {
  90. return getState().containerSize;
  91. }
  92. }
  93. private final RpcDataSource dataSource = new RpcDataSource();
  94. @Override
  95. protected void extend(ServerConnector target) {
  96. ((GridConnector) target).setDataSource(dataSource);
  97. registerRpc(DataProviderRpc.class, new DataProviderRpc() {
  98. @Override
  99. public void setRowData(int firstRow, String rowsJson) {
  100. JSONValue parsedJson = JSONParser.parseStrict(rowsJson);
  101. JSONArray rowArray = parsedJson.isArray();
  102. assert rowArray != null : "Was unable to parse JSON into an array: "
  103. + parsedJson;
  104. ArrayList<JSONObject> rows = new ArrayList<JSONObject>(rowArray
  105. .size());
  106. for (int i = 0; i < rowArray.size(); i++) {
  107. JSONValue rowValue = rowArray.get(i);
  108. JSONObject rowObject = rowValue.isObject();
  109. assert rowObject != null : "Was unable to parse JSON into an object: "
  110. + rowValue;
  111. rows.add(rowObject);
  112. }
  113. dataSource.setRowData(firstRow, rows);
  114. }
  115. @Override
  116. public void removeRowData(int firstRow, int count) {
  117. dataSource.removeRowData(firstRow, count);
  118. }
  119. @Override
  120. public void insertRowData(int firstRow, int count) {
  121. dataSource.insertRowData(firstRow, count);
  122. }
  123. @Override
  124. public void resetDataAndSize(int size) {
  125. dataSource.resetDataAndSize(size);
  126. }
  127. });
  128. }
  129. @Override
  130. public DataProviderState getState() {
  131. return (DataProviderState) super.getState();
  132. }
  133. }