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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.sqlcontainer;
  5. import java.lang.reflect.Constructor;
  6. import java.sql.Date;
  7. import java.sql.Time;
  8. import java.sql.Timestamp;
  9. import com.vaadin.data.Property;
  10. /**
  11. * ColumnProperty represents the value of one column in a RowItem. In addition
  12. * to the value, ColumnProperty also contains some basic column attributes such
  13. * as nullability status, read-only status and data type.
  14. *
  15. * Note that depending on the QueryDelegate in use this does not necessarily map
  16. * into an actual column in a database table.
  17. */
  18. final public class ColumnProperty implements Property {
  19. private static final long serialVersionUID = -3694463129581802457L;
  20. private RowItem owner;
  21. private String propertyId;
  22. private boolean readOnly;
  23. private boolean allowReadOnlyChange = true;
  24. private boolean nullable = true;
  25. private Object value;
  26. private Object changedValue;
  27. private Class<?> type;
  28. private boolean modified;
  29. private boolean versionColumn;
  30. /**
  31. * Prevent instantiation without required parameters.
  32. */
  33. @SuppressWarnings("unused")
  34. private ColumnProperty() {
  35. }
  36. public ColumnProperty(String propertyId, boolean readOnly,
  37. boolean allowReadOnlyChange, boolean nullable, Object value,
  38. Class<?> type) {
  39. if (propertyId == null) {
  40. throw new IllegalArgumentException("Properties must be named.");
  41. }
  42. if (type == null) {
  43. throw new IllegalArgumentException("Property type must be set.");
  44. }
  45. this.propertyId = propertyId;
  46. this.type = type;
  47. this.value = value;
  48. this.allowReadOnlyChange = allowReadOnlyChange;
  49. this.nullable = nullable;
  50. this.readOnly = readOnly;
  51. }
  52. public Object getValue() {
  53. if (isModified()) {
  54. return changedValue;
  55. }
  56. return value;
  57. }
  58. public void setValue(Object newValue) throws ReadOnlyException,
  59. ConversionException {
  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. /*
  96. * If the type is not correct, try to generate it through a possibly
  97. * existing String constructor.
  98. */
  99. if (!getType().isAssignableFrom(newValue.getClass())) {
  100. try {
  101. final Constructor<?> constr = getType().getConstructor(
  102. new Class[] { String.class });
  103. newValue = constr.newInstance(new Object[] { newValue
  104. .toString() });
  105. } catch (Exception e) {
  106. throw new ConversionException(e);
  107. }
  108. }
  109. /*
  110. * If the value to be set is the same that has already been set, do
  111. * not set it again.
  112. */
  113. if (isValueAlreadySet(newValue)) {
  114. return;
  115. }
  116. }
  117. /* Set the new value and notify container of the change. */
  118. changedValue = newValue;
  119. owner.getContainer().itemChangeNotification(owner);
  120. modified = true;
  121. }
  122. private boolean isValueAlreadySet(Object newValue) {
  123. Object referenceValue = isModified() ? changedValue : value;
  124. return (isNullable() && newValue == null && referenceValue == null)
  125. || newValue.equals(referenceValue);
  126. }
  127. public Class<?> getType() {
  128. return type;
  129. }
  130. public boolean isReadOnly() {
  131. return readOnly;
  132. }
  133. public boolean isReadOnlyChangeAllowed() {
  134. return allowReadOnlyChange;
  135. }
  136. public void setReadOnly(boolean newStatus) {
  137. if (allowReadOnlyChange) {
  138. readOnly = newStatus;
  139. }
  140. }
  141. public String getPropertyId() {
  142. return propertyId;
  143. }
  144. @Override
  145. public String toString() {
  146. Object val = getValue();
  147. if (val == null) {
  148. return null;
  149. }
  150. return val.toString();
  151. }
  152. public void setOwner(RowItem owner) {
  153. if (owner == null) {
  154. throw new IllegalArgumentException("Owner can not be set to null.");
  155. }
  156. if (this.owner != null) {
  157. throw new IllegalStateException(
  158. "ColumnProperties can only be bound once.");
  159. }
  160. this.owner = owner;
  161. }
  162. public boolean isModified() {
  163. return modified;
  164. }
  165. public boolean isVersionColumn() {
  166. return versionColumn;
  167. }
  168. public void setVersionColumn(boolean versionColumn) {
  169. this.versionColumn = versionColumn;
  170. }
  171. public boolean isNullable() {
  172. return nullable;
  173. }
  174. /**
  175. * An exception that signals that a <code>null</code> value was passed to
  176. * the <code>setValue</code> method, but the value of this property can not
  177. * be set to <code>null</code>.
  178. */
  179. @SuppressWarnings("serial")
  180. public class NotNullableException extends RuntimeException {
  181. /**
  182. * Constructs a new <code>NotNullableException</code> without a detail
  183. * message.
  184. */
  185. public NotNullableException() {
  186. }
  187. /**
  188. * Constructs a new <code>NotNullableException</code> with the specified
  189. * detail message.
  190. *
  191. * @param msg
  192. * the detail message
  193. */
  194. public NotNullableException(String msg) {
  195. super(msg);
  196. }
  197. /**
  198. * Constructs a new <code>NotNullableException</code> from another
  199. * exception.
  200. *
  201. * @param cause
  202. * The cause of the failure
  203. */
  204. public NotNullableException(Throwable cause) {
  205. super(cause);
  206. }
  207. }
  208. public void commit() {
  209. if (isModified()) {
  210. modified = false;
  211. value = changedValue;
  212. }
  213. }
  214. }