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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data;
  5. import java.io.Serializable;
  6. import com.vaadin.data.Validator.InvalidValueException;
  7. import com.vaadin.terminal.ErrorMessage;
  8. import com.vaadin.terminal.PaintException;
  9. import com.vaadin.terminal.PaintTarget;
  10. import com.vaadin.terminal.SystemError;
  11. /**
  12. * <p>
  13. * Defines the interface to commit and discard changes to an object, supporting
  14. * read-through and write-through modes.
  15. * </p>
  16. *
  17. * <p>
  18. * <i>Read-through mode</i> means that the value read from the buffered object
  19. * is constantly up to date with the data source. <i>Write-through</i> mode
  20. * means that all changes to the object are immediately updated to the data
  21. * source.
  22. * </p>
  23. *
  24. * <p>
  25. * Since these modes are independent, their combinations may result in some
  26. * behaviour that may sound surprising.
  27. * </p>
  28. *
  29. * <p>
  30. * For example, if a <code>Buffered</code> object is in read-through mode but
  31. * not in write-through mode, the result is an object whose value is updated
  32. * directly from the data source only if it's not locally modified. If the value
  33. * is locally modified, retrieving the value from the object would result in a
  34. * value that is different than the one stored in the data source, even though
  35. * the object is in read-through mode.
  36. * </p>
  37. *
  38. * @author IT Mill Ltd.
  39. * @version
  40. * @VERSION@
  41. * @since 3.0
  42. */
  43. public interface Buffered extends Serializable {
  44. /**
  45. * Updates all changes since the previous commit to the data source. The
  46. * value stored in the object will always be updated into the data source
  47. * when <code>commit</code> is called.
  48. *
  49. * @throws SourceException
  50. * if the operation fails because of an exception is thrown by
  51. * the data source. The cause is included in the exception.
  52. * @throws InvalidValueException
  53. * if the operation fails because validation is enabled and the
  54. * values do not validate
  55. */
  56. public void commit() throws SourceException, InvalidValueException;
  57. /**
  58. * Discards all changes since last commit. The object updates its value from
  59. * the data source.
  60. *
  61. * @throws SourceException
  62. * if the operation fails because of an exception is thrown by
  63. * the data source. The cause is included in the exception.
  64. */
  65. public void discard() throws SourceException;
  66. /**
  67. * Tests if the object is in write-through mode. If the object is in
  68. * write-through mode, all modifications to it will result in
  69. * <code>commit</code> being called after the modification.
  70. *
  71. * @return <code>true</code> if the object is in write-through mode,
  72. * <code>false</code> if it's not.
  73. */
  74. public boolean isWriteThrough();
  75. /**
  76. * Sets the object's write-through mode to the specified status. When
  77. * switching the write-through mode on, the <code>commit</code> operation
  78. * will be performed.
  79. *
  80. * @param writeThrough
  81. * Boolean value to indicate if the object should be in
  82. * write-through mode after the call.
  83. * @throws SourceException
  84. * If the operation fails because of an exception is thrown by
  85. * the data source.
  86. * @throws InvalidValueException
  87. * If the implicit commit operation fails because of a
  88. * validation error.
  89. *
  90. */
  91. public void setWriteThrough(boolean writeThrough) throws SourceException,
  92. InvalidValueException;
  93. /**
  94. * Tests if the object is in read-through mode. If the object is in
  95. * read-through mode, retrieving its value will result in the value being
  96. * first updated from the data source to the object.
  97. * <p>
  98. * The only exception to this rule is that when the object is not in
  99. * write-through mode and it's buffer contains a modified value, the value
  100. * retrieved from the object will be the locally modified value in the
  101. * buffer which may differ from the value in the data source.
  102. * </p>
  103. *
  104. * @return <code>true</code> if the object is in read-through mode,
  105. * <code>false</code> if it's not.
  106. */
  107. public boolean isReadThrough();
  108. /**
  109. * Sets the object's read-through mode to the specified status. When
  110. * switching read-through mode on, the object's value is updated from the
  111. * data source.
  112. *
  113. * @param readThrough
  114. * Boolean value to indicate if the object should be in
  115. * read-through mode after the call.
  116. *
  117. * @throws SourceException
  118. * If the operation fails because of an exception is thrown by
  119. * the data source. The cause is included in the exception.
  120. */
  121. public void setReadThrough(boolean readThrough) throws SourceException;
  122. /**
  123. * Tests if the value stored in the object has been modified since it was
  124. * last updated from the data source.
  125. *
  126. * @return <code>true</code> if the value in the object has been modified
  127. * since the last data source update, <code>false</code> if not.
  128. */
  129. public boolean isModified();
  130. /**
  131. * An exception that signals that one or more exceptions occurred while a
  132. * buffered object tried to access its data source or if there is a problem
  133. * in processing a data source.
  134. *
  135. * @author IT Mill Ltd.
  136. * @version
  137. * @VERSION@
  138. * @since 3.0
  139. */
  140. @SuppressWarnings("serial")
  141. public class SourceException extends RuntimeException implements
  142. ErrorMessage, Serializable {
  143. /** Source class implementing the buffered interface */
  144. private final Buffered source;
  145. /** Original cause of the source exception */
  146. private Throwable[] causes = {};
  147. /**
  148. * Creates a source exception that does not include a cause.
  149. *
  150. * @param source
  151. * the source object implementing the Buffered interface.
  152. */
  153. public SourceException(Buffered source) {
  154. this.source = source;
  155. }
  156. /**
  157. * Creates a source exception from a cause exception.
  158. *
  159. * @param source
  160. * the source object implementing the Buffered interface.
  161. * @param cause
  162. * the original cause for this exception.
  163. */
  164. public SourceException(Buffered source, Throwable cause) {
  165. this.source = source;
  166. causes = new Throwable[] { cause };
  167. }
  168. /**
  169. * Creates a source exception from multiple causes.
  170. *
  171. * @param source
  172. * the source object implementing the Buffered interface.
  173. * @param causes
  174. * the original causes for this exception.
  175. */
  176. public SourceException(Buffered source, Throwable[] causes) {
  177. this.source = source;
  178. this.causes = causes;
  179. }
  180. /**
  181. * Gets the cause of the exception.
  182. *
  183. * @return The cause for the exception.
  184. * @throws MoreThanOneCauseException
  185. * if there is more than one cause for the exception. This
  186. * is possible if the commit operation triggers more than
  187. * one error at the same time.
  188. */
  189. @Override
  190. public final Throwable getCause() {
  191. if (causes.length == 0) {
  192. return null;
  193. }
  194. return causes[0];
  195. }
  196. /**
  197. * Gets all the causes for this exception.
  198. *
  199. * @return throwables that caused this exception
  200. */
  201. public final Throwable[] getCauses() {
  202. return causes;
  203. }
  204. /**
  205. * Gets a source of the exception.
  206. *
  207. * @return the Buffered object which generated this exception.
  208. */
  209. public Buffered getSource() {
  210. return source;
  211. }
  212. /**
  213. * Gets the error level of this buffered source exception. The level of
  214. * the exception is maximum error level of all the contained causes.
  215. * <p>
  216. * The causes that do not specify error level default to
  217. * <code>ERROR</code> level. Also source exception without any causes
  218. * are of level <code>ERROR</code>.
  219. * </p>
  220. *
  221. * @see com.vaadin.terminal.ErrorMessage#getErrorLevel()
  222. */
  223. public int getErrorLevel() {
  224. int level = Integer.MIN_VALUE;
  225. for (int i = 0; i < causes.length; i++) {
  226. final int causeLevel = (causes[i] instanceof ErrorMessage) ? ((ErrorMessage) causes[i])
  227. .getErrorLevel()
  228. : ErrorMessage.ERROR;
  229. if (causeLevel > level) {
  230. level = causeLevel;
  231. }
  232. }
  233. return level == Integer.MIN_VALUE ? ErrorMessage.ERROR : level;
  234. }
  235. /* Documented in super interface */
  236. public void paint(PaintTarget target) throws PaintException {
  237. target.startTag("error");
  238. final int level = getErrorLevel();
  239. if (level > 0 && level <= ErrorMessage.INFORMATION) {
  240. target.addAttribute("level", "info");
  241. } else if (level <= ErrorMessage.WARNING) {
  242. target.addAttribute("level", "warning");
  243. } else if (level <= ErrorMessage.ERROR) {
  244. target.addAttribute("level", "error");
  245. } else if (level <= ErrorMessage.CRITICAL) {
  246. target.addAttribute("level", "critical");
  247. } else {
  248. target.addAttribute("level", "system");
  249. }
  250. // Paint all the exceptions
  251. for (int i = 0; i < causes.length; i++) {
  252. if (causes[i] instanceof ErrorMessage) {
  253. ((ErrorMessage) causes[i]).paint(target);
  254. } else {
  255. new SystemError(causes[i]).paint(target);
  256. }
  257. }
  258. target.endTag("error");
  259. }
  260. /* Documented in super interface */
  261. public void addListener(RepaintRequestListener listener) {
  262. }
  263. /* Documented in super interface */
  264. public void removeListener(RepaintRequestListener listener) {
  265. }
  266. /* Documented in super interface */
  267. public void requestRepaint() {
  268. }
  269. /* Documented in super interface */
  270. public void requestRepaintRequests() {
  271. }
  272. public String getDebugId() {
  273. // TODO Auto-generated method stub
  274. return null;
  275. }
  276. public void setDebugId(String id) {
  277. throw new UnsupportedOperationException(
  278. "Setting testing id for this Paintable is not implemented");
  279. }
  280. }
  281. }