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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.data.util.sqlcontainer;
  17. import java.sql.Date;
  18. import java.sql.Time;
  19. import java.sql.Timestamp;
  20. import java.util.logging.Logger;
  21. import com.vaadin.v7.data.Property;
  22. import com.vaadin.v7.data.util.converter.Converter.ConversionException;
  23. /**
  24. * ColumnProperty represents the value of one column in a RowItem. In addition
  25. * to the value, ColumnProperty also contains some basic column attributes such
  26. * as nullability status, read-only status and data type.
  27. *
  28. * Note that depending on the QueryDelegate in use this does not necessarily map
  29. * into an actual column in a database table.
  30. * @deprecated As of 8.0, no replacement available.
  31. */
  32. @Deprecated
  33. public final class ColumnProperty implements Property {
  34. private static final long serialVersionUID = -3694463129581802457L;
  35. private RowItem owner;
  36. private String propertyId;
  37. private boolean readOnly;
  38. private boolean allowReadOnlyChange = true;
  39. private boolean nullable = true;
  40. private Object value;
  41. private Object changedValue;
  42. private Class<?> type;
  43. private boolean modified;
  44. private boolean versionColumn;
  45. private boolean primaryKey = false;
  46. /**
  47. * Prevent instantiation without required parameters.
  48. */
  49. @SuppressWarnings("unused")
  50. private ColumnProperty() {
  51. }
  52. /**
  53. * Deprecated constructor for ColumnProperty. If this is used the primary
  54. * keys are not identified correctly in some cases for some databases (i.e.
  55. * Oracle). See http://dev.vaadin.com/ticket/9145.
  56. *
  57. * @param propertyId
  58. * @param readOnly
  59. * @param allowReadOnlyChange
  60. * @param nullable
  61. * @param value
  62. * @param type
  63. *
  64. * @deprecated As of 7.0. Use
  65. * {@link #ColumnProperty(String, boolean, boolean, boolean, boolean, Object, Class)
  66. * instead
  67. */
  68. @Deprecated
  69. public ColumnProperty(String propertyId, boolean readOnly,
  70. boolean allowReadOnlyChange, boolean nullable, Object value,
  71. Class<?> type) {
  72. this(propertyId, readOnly, allowReadOnlyChange, nullable, false, value,
  73. type);
  74. }
  75. /**
  76. * Creates a new ColumnProperty instance.
  77. *
  78. * @param propertyId
  79. * The ID of this property.
  80. * @param readOnly
  81. * Whether this property is read-only.
  82. * @param allowReadOnlyChange
  83. * Whether the read-only status of this property can be changed.
  84. * @param nullable
  85. * Whether this property accepts null values.
  86. * @param primaryKey
  87. * Whether this property corresponds to a database primary key.
  88. * @param value
  89. * The value of this property.
  90. * @param type
  91. * The type of this property.
  92. */
  93. public ColumnProperty(String propertyId, boolean readOnly,
  94. boolean allowReadOnlyChange, boolean nullable, boolean primaryKey,
  95. Object value, Class<?> type) {
  96. if (propertyId == null) {
  97. throw new IllegalArgumentException("Properties must be named.");
  98. }
  99. if (type == null) {
  100. throw new IllegalArgumentException("Property type must be set.");
  101. }
  102. this.propertyId = propertyId;
  103. this.type = type;
  104. this.value = value;
  105. this.allowReadOnlyChange = allowReadOnlyChange;
  106. this.nullable = nullable;
  107. this.readOnly = readOnly;
  108. this.primaryKey = primaryKey;
  109. }
  110. /**
  111. * Returns the current value for this property. To get the previous value
  112. * (if one exists) for a modified property use {@link #getOldValue()}.
  113. *
  114. * @return
  115. */
  116. @Override
  117. public Object getValue() {
  118. if (isModified()) {
  119. return changedValue;
  120. }
  121. return value;
  122. }
  123. /**
  124. * Returns the original non-modified value of this property if it has been
  125. * modified.
  126. *
  127. * @return The original value if <code>isModified()</code> is true,
  128. * <code>getValue()</code> otherwise.
  129. */
  130. public Object getOldValue() {
  131. return value;
  132. }
  133. @Override
  134. public void setValue(Object newValue)
  135. throws ReadOnlyException, ConversionException {
  136. if (newValue == null && !nullable) {
  137. throw new NotNullableException(
  138. "Null values are not allowed for this property.");
  139. }
  140. if (readOnly) {
  141. throw new ReadOnlyException(
  142. "Cannot set value for read-only property.");
  143. }
  144. /* Check if this property is a date property. */
  145. boolean isDateProperty = Time.class.equals(getType())
  146. || Date.class.equals(getType())
  147. || Timestamp.class.equals(getType());
  148. if (newValue != null) {
  149. /* Handle SQL dates, times and Timestamps given as java.util.Date */
  150. if (isDateProperty) {
  151. /*
  152. * Try to get the millisecond value from the new value of this
  153. * property. Possible type to convert from is java.util.Date.
  154. */
  155. long millis = 0;
  156. if (newValue instanceof java.util.Date) {
  157. millis = ((java.util.Date) newValue).getTime();
  158. /*
  159. * Create the new object based on the millisecond value,
  160. * according to the type of this property.
  161. */
  162. if (Time.class.equals(getType())) {
  163. newValue = new Time(millis);
  164. } else if (Date.class.equals(getType())) {
  165. newValue = new Date(millis);
  166. } else if (Timestamp.class.equals(getType())) {
  167. newValue = new Timestamp(millis);
  168. }
  169. }
  170. }
  171. if (!getType().isAssignableFrom(newValue.getClass())) {
  172. throw new IllegalArgumentException(
  173. "Illegal value type for ColumnProperty");
  174. }
  175. /*
  176. * If the value to be set is the same that has already been set, do
  177. * not set it again.
  178. */
  179. if (isValueAlreadySet(newValue)) {
  180. return;
  181. }
  182. }
  183. /* Set the new value and notify container of the change. */
  184. changedValue = newValue;
  185. modified = true;
  186. owner.getContainer().itemChangeNotification(owner);
  187. }
  188. private boolean isValueAlreadySet(Object newValue) {
  189. Object referenceValue = isModified() ? changedValue : value;
  190. return (isNullable() && newValue == null && referenceValue == null)
  191. || newValue.equals(referenceValue);
  192. }
  193. @Override
  194. public Class<?> getType() {
  195. return type;
  196. }
  197. @Override
  198. public boolean isReadOnly() {
  199. return readOnly;
  200. }
  201. /**
  202. * Returns whether the read-only status of this property can be changed
  203. * using {@link #setReadOnly(boolean)}.
  204. * <p>
  205. * Used to prevent setting to read/write mode a property that is not allowed
  206. * to be written by the underlying database. Also used for values like
  207. * VERSION and AUTO_INCREMENT fields that might be set to read-only by the
  208. * container but the database still allows writes.
  209. *
  210. * @return true if the read-only status can be changed, false otherwise.
  211. */
  212. public boolean isReadOnlyChangeAllowed() {
  213. return allowReadOnlyChange;
  214. }
  215. @Override
  216. public void setReadOnly(boolean newStatus) {
  217. if (allowReadOnlyChange) {
  218. readOnly = newStatus;
  219. }
  220. }
  221. public boolean isPrimaryKey() {
  222. return primaryKey;
  223. }
  224. public String getPropertyId() {
  225. return propertyId;
  226. }
  227. // LegacyPropertyHelper has been removed in Vaadin 8
  228. private static Logger getLogger() {
  229. return Logger.getLogger(ColumnProperty.class.getName());
  230. }
  231. public void setOwner(RowItem owner) {
  232. if (owner == null) {
  233. throw new IllegalArgumentException("Owner can not be set to null.");
  234. }
  235. if (this.owner != null) {
  236. throw new IllegalStateException(
  237. "ColumnProperties can only be bound once.");
  238. }
  239. this.owner = owner;
  240. }
  241. public boolean isModified() {
  242. return modified;
  243. }
  244. public boolean isVersionColumn() {
  245. return versionColumn;
  246. }
  247. public void setVersionColumn(boolean versionColumn) {
  248. this.versionColumn = versionColumn;
  249. }
  250. public boolean isNullable() {
  251. return nullable;
  252. }
  253. /**
  254. * Return whether the value of this property should be persisted to the
  255. * database.
  256. *
  257. * @return true if the value should be written to the database, false
  258. * otherwise.
  259. */
  260. public boolean isPersistent() {
  261. if (isVersionColumn()) {
  262. return false;
  263. } else if (isReadOnlyChangeAllowed() && !isReadOnly()) {
  264. return true;
  265. } else {
  266. return false;
  267. }
  268. }
  269. /**
  270. * Returns whether or not this property is used as a row identifier.
  271. *
  272. * @return true if the property is a row identifier, false otherwise.
  273. */
  274. public boolean isRowIdentifier() {
  275. return isPrimaryKey() || isVersionColumn();
  276. }
  277. /**
  278. * An exception that signals that a <code>null</code> value was passed to
  279. * the <code>setValue</code> method, but the value of this property can not
  280. * be set to <code>null</code>.
  281. */
  282. @SuppressWarnings("serial")
  283. @Deprecated
  284. public class NotNullableException extends RuntimeException {
  285. /**
  286. * Constructs a new <code>NotNullableException</code> without a detail
  287. * message.
  288. */
  289. public NotNullableException() {
  290. }
  291. /**
  292. * Constructs a new <code>NotNullableException</code> with the specified
  293. * detail message.
  294. *
  295. * @param msg
  296. * the detail message
  297. */
  298. public NotNullableException(String msg) {
  299. super(msg);
  300. }
  301. /**
  302. * Constructs a new <code>NotNullableException</code> from another
  303. * exception.
  304. *
  305. * @param cause
  306. * The cause of the failure
  307. */
  308. public NotNullableException(Throwable cause) {
  309. super(cause);
  310. }
  311. }
  312. public void commit() {
  313. if (isModified()) {
  314. modified = false;
  315. value = changedValue;
  316. }
  317. }
  318. }