Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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