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.

ColumnProperty.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.sqlcontainer;
  5. import java.sql.Date;
  6. import java.sql.Time;
  7. import java.sql.Timestamp;
  8. import com.vaadin.data.Property;
  9. /**
  10. * ColumnProperty represents the value of one column in a RowItem. In addition
  11. * to the value, ColumnProperty also contains some basic column attributes such
  12. * as nullability status, read-only status and data type.
  13. *
  14. * Note that depending on the QueryDelegate in use this does not necessarily map
  15. * into an actual column in a database table.
  16. */
  17. final public class ColumnProperty implements Property {
  18. private static final long serialVersionUID = -3694463129581802457L;
  19. private RowItem owner;
  20. private String propertyId;
  21. private boolean readOnly;
  22. private boolean allowReadOnlyChange = true;
  23. private boolean nullable = true;
  24. private Object value;
  25. private Object changedValue;
  26. private Class<?> type;
  27. private boolean modified;
  28. private boolean versionColumn;
  29. /**
  30. * Prevent instantiation without required parameters.
  31. */
  32. @SuppressWarnings("unused")
  33. private ColumnProperty() {
  34. }
  35. public ColumnProperty(String propertyId, boolean readOnly,
  36. boolean allowReadOnlyChange, boolean nullable, Object value,
  37. Class<?> type) {
  38. if (propertyId == null) {
  39. throw new IllegalArgumentException("Properties must be named.");
  40. }
  41. if (type == null) {
  42. throw new IllegalArgumentException("Property type must be set.");
  43. }
  44. this.propertyId = propertyId;
  45. this.type = type;
  46. this.value = value;
  47. this.allowReadOnlyChange = allowReadOnlyChange;
  48. this.nullable = nullable;
  49. this.readOnly = readOnly;
  50. }
  51. @Override
  52. public Object getValue() {
  53. if (isModified()) {
  54. return changedValue;
  55. }
  56. return value;
  57. }
  58. @Override
  59. public void setValue(Object newValue) throws ReadOnlyException {
  60. if (newValue == null && !nullable) {
  61. throw new NotNullableException(
  62. "Null values are not allowed for this property.");
  63. }
  64. if (readOnly) {
  65. throw new ReadOnlyException(
  66. "Cannot set value for read-only property.");
  67. }
  68. /* Check if this property is a date property. */
  69. boolean isDateProperty = Time.class.equals(getType())
  70. || Date.class.equals(getType())
  71. || Timestamp.class.equals(getType());
  72. if (newValue != null) {
  73. /* Handle SQL dates, times and Timestamps given as java.util.Date */
  74. if (isDateProperty) {
  75. /*
  76. * Try to get the millisecond value from the new value of this
  77. * property. Possible type to convert from is java.util.Date.
  78. */
  79. long millis = 0;
  80. if (newValue instanceof java.util.Date) {
  81. millis = ((java.util.Date) newValue).getTime();
  82. /*
  83. * Create the new object based on the millisecond value,
  84. * according to the type of this property.
  85. */
  86. if (Time.class.equals(getType())) {
  87. newValue = new Time(millis);
  88. } else if (Date.class.equals(getType())) {
  89. newValue = new Date(millis);
  90. } else if (Timestamp.class.equals(getType())) {
  91. newValue = new Timestamp(millis);
  92. }
  93. }
  94. }
  95. if (!getType().isAssignableFrom(newValue.getClass())) {
  96. throw new IllegalArgumentException(
  97. "Illegal value type for ColumnProperty");
  98. }
  99. /*
  100. * If the value to be set is the same that has already been set, do
  101. * not set it again.
  102. */
  103. if (isValueAlreadySet(newValue)) {
  104. return;
  105. }
  106. }
  107. /* Set the new value and notify container of the change. */
  108. changedValue = newValue;
  109. modified = true;
  110. owner.getContainer().itemChangeNotification(owner);
  111. }
  112. private boolean isValueAlreadySet(Object newValue) {
  113. Object referenceValue = isModified() ? changedValue : value;
  114. return (isNullable() && newValue == null && referenceValue == null)
  115. || newValue.equals(referenceValue);
  116. }
  117. @Override
  118. public Class<?> getType() {
  119. return type;
  120. }
  121. @Override
  122. public boolean isReadOnly() {
  123. return readOnly;
  124. }
  125. public boolean isReadOnlyChangeAllowed() {
  126. return allowReadOnlyChange;
  127. }
  128. @Override
  129. public void setReadOnly(boolean newStatus) {
  130. if (allowReadOnlyChange) {
  131. readOnly = newStatus;
  132. }
  133. }
  134. public String getPropertyId() {
  135. return propertyId;
  136. }
  137. /**
  138. * Returns the value of the Property in human readable textual format.
  139. *
  140. * @see java.lang.Object#toString()
  141. * @deprecated get the string representation from the value
  142. */
  143. @Deprecated
  144. @Override
  145. public String toString() {
  146. throw new UnsupportedOperationException(
  147. "Use ColumnProperty.getValue() instead of ColumnProperty.toString()");
  148. }
  149. public void setOwner(RowItem owner) {
  150. if (owner == null) {
  151. throw new IllegalArgumentException("Owner can not be set to null.");
  152. }
  153. if (this.owner != null) {
  154. throw new IllegalStateException(
  155. "ColumnProperties can only be bound once.");
  156. }
  157. this.owner = owner;
  158. }
  159. public boolean isModified() {
  160. return modified;
  161. }
  162. public boolean isVersionColumn() {
  163. return versionColumn;
  164. }
  165. public void setVersionColumn(boolean versionColumn) {
  166. this.versionColumn = versionColumn;
  167. }
  168. public boolean isNullable() {
  169. return nullable;
  170. }
  171. /**
  172. * An exception that signals that a <code>null</code> value was passed to
  173. * the <code>setValue</code> method, but the value of this property can not
  174. * be set to <code>null</code>.
  175. */
  176. @SuppressWarnings("serial")
  177. public class NotNullableException extends RuntimeException {
  178. /**
  179. * Constructs a new <code>NotNullableException</code> without a detail
  180. * message.
  181. */
  182. public NotNullableException() {
  183. }
  184. /**
  185. * Constructs a new <code>NotNullableException</code> with the specified
  186. * detail message.
  187. *
  188. * @param msg
  189. * the detail message
  190. */
  191. public NotNullableException(String msg) {
  192. super(msg);
  193. }
  194. /**
  195. * Constructs a new <code>NotNullableException</code> from another
  196. * exception.
  197. *
  198. * @param cause
  199. * The cause of the failure
  200. */
  201. public NotNullableException(Throwable cause) {
  202. super(cause);
  203. }
  204. }
  205. public void commit() {
  206. if (isModified()) {
  207. modified = false;
  208. value = changedValue;
  209. }
  210. }
  211. }