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.

TransactionalPropertyWrapper.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import com.vaadin.data.Property;
  6. import com.vaadin.data.Property.ValueChangeEvent;
  7. import com.vaadin.data.Property.ValueChangeNotifier;
  8. /**
  9. * Wrapper class that helps implement two-phase commit for a non-transactional
  10. * property.
  11. *
  12. * When accessing the property through the wrapper, getting and setting the
  13. * property value take place immediately. However, the wrapper keeps track of
  14. * the old value of the property so that it can be set for the property in case
  15. * of a roll-back. This can result in the underlying property value changing
  16. * multiple times (first based on modifications made by the application, then
  17. * back upon roll-back).
  18. *
  19. * Value change events on the {@link TransactionalPropertyWrapper} are only
  20. * fired at the end of a successful transaction, whereas listeners attached to
  21. * the underlying property may receive multiple value change events.
  22. *
  23. * @see com.vaadin.data.Property.Transactional
  24. *
  25. * @author Vaadin Ltd
  26. * @version @version@
  27. * @since 7.0
  28. *
  29. * @param <T>
  30. */
  31. public class TransactionalPropertyWrapper<T> extends AbstractProperty<T>
  32. implements ValueChangeNotifier, Property.Transactional<T> {
  33. private Property<T> wrappedProperty;
  34. private boolean inTransaction = false;
  35. private boolean valueChangePending;
  36. private T valueBeforeTransaction;
  37. public TransactionalPropertyWrapper(Property<T> wrappedProperty) {
  38. this.wrappedProperty = wrappedProperty;
  39. if (wrappedProperty instanceof ValueChangeNotifier) {
  40. ((ValueChangeNotifier) wrappedProperty)
  41. .addListener(new ValueChangeListener() {
  42. @Override
  43. public void valueChange(ValueChangeEvent event) {
  44. fireValueChange();
  45. }
  46. });
  47. }
  48. }
  49. @Override
  50. public Class getType() {
  51. return wrappedProperty.getType();
  52. }
  53. @Override
  54. public T getValue() {
  55. return wrappedProperty.getValue();
  56. }
  57. @Override
  58. public void setValue(Object newValue) throws ReadOnlyException {
  59. // Causes a value change to be sent to this listener which in turn fires
  60. // a new value change event for this property
  61. wrappedProperty.setValue(newValue);
  62. }
  63. @Override
  64. public void startTransaction() {
  65. inTransaction = true;
  66. valueBeforeTransaction = getValue();
  67. }
  68. @Override
  69. public void commit() {
  70. endTransaction();
  71. }
  72. @Override
  73. public void rollback() {
  74. try {
  75. wrappedProperty.setValue(valueBeforeTransaction);
  76. } finally {
  77. valueChangePending = false;
  78. endTransaction();
  79. }
  80. }
  81. protected void endTransaction() {
  82. inTransaction = false;
  83. valueBeforeTransaction = null;
  84. if (valueChangePending) {
  85. fireValueChange();
  86. }
  87. }
  88. @Override
  89. protected void fireValueChange() {
  90. if (inTransaction) {
  91. valueChangePending = true;
  92. } else {
  93. super.fireValueChange();
  94. }
  95. }
  96. public Property<T> getWrappedProperty() {
  97. return wrappedProperty;
  98. }
  99. }