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.

Buffered.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2000-2016 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.v7.data;
  17. import java.io.Serializable;
  18. import com.vaadin.data.Binder;
  19. import com.vaadin.server.AbstractErrorMessage;
  20. import com.vaadin.server.ErrorMessage;
  21. import com.vaadin.server.ErrorMessageProducer;
  22. import com.vaadin.server.UserError;
  23. import com.vaadin.shared.ui.ErrorLevel;
  24. import com.vaadin.v7.data.Validator.InvalidValueException;
  25. /**
  26. * <p>
  27. * Defines the interface to commit and discard changes to an object, supporting
  28. * buffering.
  29. *
  30. * <p>
  31. * In <i>buffered</i> mode the initial value is read from the data source and
  32. * then buffered. Any subsequential writes or reads will be done on the buffered
  33. * value. Calling {@link #commit()} will write the buffered value to the data
  34. * source while calling {@link #discard()} while discard the buffered value and
  35. * re-read the value from the data source.
  36. *
  37. * <p>
  38. * In <i>non-buffered</i> mode the value is always read directly from the data
  39. * source. Any write is done directly to the data source with no buffering in
  40. * between. Reads are also done directly from the data source. Calling
  41. * {@link #commit()} or {@link #discard()} in this mode is efficiently a no-op.
  42. *
  43. * @author Vaadin Ltd.
  44. * @since 3.0
  45. * @deprecated As of 8.0, no replacement available, see {@link Binder#writeBean(Object)}, {@link Binder#clearFields()}
  46. *
  47. */
  48. @Deprecated
  49. public interface Buffered extends Serializable {
  50. /**
  51. * Updates all changes since the previous commit to the data source. The
  52. * value stored in the object will always be updated into the data source
  53. * when <code>commit</code> is called.
  54. *
  55. * @throws SourceException
  56. * if the operation fails because of an exception is thrown by
  57. * the data source. The cause is included in the exception.
  58. * @throws InvalidValueException
  59. * if the operation fails because validation is enabled and the
  60. * values do not validate
  61. */
  62. public void commit() throws SourceException, InvalidValueException;
  63. /**
  64. * Discards all changes since last commit. The object updates its value from
  65. * the data source.
  66. *
  67. * @throws SourceException
  68. * if the operation fails because of an exception is thrown by
  69. * the data source. The cause is included in the exception.
  70. */
  71. public void discard() throws SourceException;
  72. /**
  73. * Sets the buffered mode to the specified status.
  74. * <p>
  75. * When in buffered mode, an internal buffer will be used to store changes
  76. * until {@link #commit()} is called. Calling {@link #discard()} will revert
  77. * the internal buffer to the value of the data source.
  78. * <p>
  79. * When in non-buffered mode both read and write operations will be done
  80. * directly on the data source. In this mode the {@link #commit()} and
  81. * {@link #discard()} methods serve no purpose.
  82. *
  83. * @param buffered
  84. * true if buffered mode should be turned on, false otherwise
  85. * @since 7.0
  86. */
  87. public void setBuffered(boolean buffered);
  88. /**
  89. * Checks the buffered mode
  90. *
  91. * @return true if buffered mode is on, false otherwise
  92. * @since 7.0
  93. */
  94. public boolean isBuffered();
  95. /**
  96. * Tests if the value stored in the object has been modified since it was
  97. * last updated from the data source.
  98. *
  99. * @return <code>true</code> if the value in the object has been modified
  100. * since the last data source update, <code>false</code> if not.
  101. */
  102. public boolean isModified();
  103. /**
  104. * An exception that signals that one or more exceptions occurred while a
  105. * buffered object tried to access its data source or if there is a problem
  106. * in processing a data source.
  107. *
  108. * @author Vaadin Ltd.
  109. * @since 3.0
  110. */
  111. @SuppressWarnings("serial")
  112. @Deprecated
  113. public class SourceException extends RuntimeException
  114. implements Serializable, ErrorMessageProducer {
  115. /** Source class implementing the buffered interface */
  116. private final Buffered source;
  117. /** Original cause of the source exception */
  118. private Throwable[] causes = {};
  119. /**
  120. * Creates a source exception that does not include a cause.
  121. *
  122. * @param source
  123. * the source object implementing the Buffered interface.
  124. */
  125. public SourceException(Buffered source) {
  126. this.source = source;
  127. }
  128. /**
  129. * Creates a source exception from multiple causes.
  130. *
  131. * @param source
  132. * the source object implementing the Buffered interface.
  133. * @param causes
  134. * the original causes for this exception.
  135. */
  136. public SourceException(Buffered source, Throwable... causes) {
  137. this.source = source;
  138. this.causes = causes;
  139. }
  140. /**
  141. * Gets the cause of the exception.
  142. *
  143. * @return The (first) cause for the exception, null if no cause.
  144. */
  145. @Override
  146. public final Throwable getCause() {
  147. if (causes.length == 0) {
  148. return null;
  149. }
  150. return causes[0];
  151. }
  152. /**
  153. * Gets all the causes for this exception.
  154. *
  155. * @return throwables that caused this exception
  156. */
  157. public final Throwable[] getCauses() {
  158. return causes;
  159. }
  160. /**
  161. * Gets a source of the exception.
  162. *
  163. * @return the Buffered object which generated this exception.
  164. */
  165. public Buffered getSource() {
  166. return source;
  167. }
  168. // Intentional change in compatibility package
  169. @Override
  170. public ErrorMessage getErrorMessage() {
  171. // no message, only the causes to be painted
  172. UserError error = new UserError(null);
  173. // in practice, this was always ERROR in Vaadin 6 unless tweaked in
  174. // custom exceptions implementing ErrorMessage
  175. error.setErrorLevel(ErrorLevel.ERROR);
  176. // causes
  177. for (Throwable nestedException : getCauses()) {
  178. error.addCause(AbstractErrorMessage
  179. .getErrorMessageForException(nestedException));
  180. }
  181. return error;
  182. }
  183. }
  184. }