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.

RowItem.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.sqlcontainer;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import com.vaadin.data.Item;
  9. import com.vaadin.data.Property;
  10. /**
  11. * RowItem represents one row of a result set obtained from a QueryDelegate.
  12. *
  13. * Note that depending on the QueryDelegate in use this does not necessarily map
  14. * into an actual row in a database table.
  15. */
  16. public final class RowItem implements Item {
  17. private static final long serialVersionUID = -6228966439127951408L;
  18. private SQLContainer container;
  19. private RowId id;
  20. private Collection<ColumnProperty> properties;
  21. /**
  22. * Prevent instantiation without required parameters.
  23. */
  24. @SuppressWarnings("unused")
  25. private RowItem() {
  26. }
  27. public RowItem(SQLContainer container, RowId id,
  28. Collection<ColumnProperty> properties) {
  29. if (container == null) {
  30. throw new IllegalArgumentException("Container cannot be null.");
  31. }
  32. if (id == null) {
  33. throw new IllegalArgumentException("Row ID cannot be null.");
  34. }
  35. this.container = container;
  36. this.properties = properties;
  37. /* Set this RowItem as owner to the properties */
  38. if (properties != null) {
  39. for (ColumnProperty p : properties) {
  40. p.setOwner(this);
  41. }
  42. }
  43. this.id = id;
  44. }
  45. @Override
  46. public Property<?> getItemProperty(Object id) {
  47. if (id instanceof String && id != null) {
  48. for (ColumnProperty cp : properties) {
  49. if (id.equals(cp.getPropertyId())) {
  50. return cp;
  51. }
  52. }
  53. }
  54. return null;
  55. }
  56. @Override
  57. public Collection<?> getItemPropertyIds() {
  58. Collection<String> ids = new ArrayList<String>(properties.size());
  59. for (ColumnProperty cp : properties) {
  60. ids.add(cp.getPropertyId());
  61. }
  62. return Collections.unmodifiableCollection(ids);
  63. }
  64. /**
  65. * Adding properties is not supported. Properties are generated by
  66. * SQLContainer.
  67. */
  68. @Override
  69. public boolean addItemProperty(Object id, Property property)
  70. throws UnsupportedOperationException {
  71. throw new UnsupportedOperationException();
  72. }
  73. /**
  74. * Removing properties is not supported. Properties are generated by
  75. * SQLContainer.
  76. */
  77. @Override
  78. public boolean removeItemProperty(Object id)
  79. throws UnsupportedOperationException {
  80. throw new UnsupportedOperationException();
  81. }
  82. public RowId getId() {
  83. return id;
  84. }
  85. public SQLContainer getContainer() {
  86. return container;
  87. }
  88. public boolean isModified() {
  89. if (properties != null) {
  90. for (ColumnProperty p : properties) {
  91. if (p.isModified()) {
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. @Override
  99. public String toString() {
  100. StringBuffer s = new StringBuffer();
  101. s.append("ID:");
  102. s.append(getId().toString());
  103. for (Object propId : getItemPropertyIds()) {
  104. s.append("|");
  105. s.append(propId.toString());
  106. s.append(":");
  107. Object value = getItemProperty(propId).getValue();
  108. s.append((null != value) ? value.toString() : null);
  109. }
  110. return s.toString();
  111. }
  112. public void commit() {
  113. if (properties != null) {
  114. for (ColumnProperty p : properties) {
  115. p.commit();
  116. }
  117. }
  118. }
  119. }