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.

VersionHistoryColumnInfoImpl.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright (c) 2011 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl.complex;
  14. import java.io.IOException;
  15. import java.util.Collections;
  16. import java.util.Date;
  17. import java.util.List;
  18. import com.healthmarketscience.jackcess.Column;
  19. import com.healthmarketscience.jackcess.Row;
  20. import com.healthmarketscience.jackcess.Table;
  21. import com.healthmarketscience.jackcess.complex.ComplexDataType;
  22. import com.healthmarketscience.jackcess.complex.ComplexValue;
  23. import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
  24. import com.healthmarketscience.jackcess.complex.Version;
  25. import com.healthmarketscience.jackcess.complex.VersionHistoryColumnInfo;
  26. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  27. /**
  28. * Complex column info for a column which tracking the version history of an
  29. * "append only" memo column.
  30. * <p>
  31. * Note, the strongly typed update/delete methods are <i>not</i> supported for
  32. * version history columns (the data is supposed to be immutable). That said,
  33. * the "raw" update/delete methods are supported for those that <i>really</i>
  34. * want to muck with the version history data.
  35. *
  36. * @author James Ahlborn
  37. */
  38. public class VersionHistoryColumnInfoImpl extends ComplexColumnInfoImpl<Version>
  39. implements VersionHistoryColumnInfo
  40. {
  41. private final Column _valueCol;
  42. private final Column _modifiedCol;
  43. public VersionHistoryColumnInfoImpl(Column column, int complexId,
  44. Table typeObjTable, Table flatTable)
  45. throws IOException
  46. {
  47. super(column, complexId, typeObjTable, flatTable);
  48. Column valueCol = null;
  49. Column modifiedCol = null;
  50. for(Column col : getTypeColumns()) {
  51. switch(col.getType()) {
  52. case SHORT_DATE_TIME:
  53. modifiedCol = col;
  54. break;
  55. case MEMO:
  56. valueCol = col;
  57. break;
  58. default:
  59. // ignore
  60. }
  61. }
  62. _valueCol = valueCol;
  63. _modifiedCol = modifiedCol;
  64. }
  65. @Override
  66. public void postTableLoadInit() throws IOException {
  67. super.postTableLoadInit();
  68. // link up with the actual versioned column. it should have the same name
  69. // as the "value" column in the type table.
  70. Column versionedCol = getColumn().getTable().getColumn(
  71. getValueColumn().getName());
  72. ((ColumnImpl)versionedCol).setVersionHistoryColumn((ColumnImpl)getColumn());
  73. }
  74. public Column getValueColumn() {
  75. return _valueCol;
  76. }
  77. public Column getModifiedDateColumn() {
  78. return _modifiedCol;
  79. }
  80. @Override
  81. public ComplexDataType getType() {
  82. return ComplexDataType.VERSION_HISTORY;
  83. }
  84. @Override
  85. public ComplexValue.Id updateValue(Version value) throws IOException {
  86. throw new UnsupportedOperationException(
  87. "This column does not support value updates");
  88. }
  89. @Override
  90. public void deleteValue(Version value) throws IOException {
  91. throw new UnsupportedOperationException(
  92. "This column does not support value deletes");
  93. }
  94. @Override
  95. public void deleteAllValues(int complexValueFk) throws IOException {
  96. throw new UnsupportedOperationException(
  97. "This column does not support value deletes");
  98. }
  99. @Override
  100. protected List<Version> toValues(ComplexValueForeignKey complexValueFk,
  101. List<Row> rawValues)
  102. throws IOException
  103. {
  104. List<Version> versions = super.toValues(complexValueFk, rawValues);
  105. // order versions newest to oldest
  106. Collections.sort(versions);
  107. return versions;
  108. }
  109. @Override
  110. protected VersionImpl toValue(ComplexValueForeignKey complexValueFk,
  111. Row rawValue) {
  112. ComplexValue.Id id = getValueId(rawValue);
  113. String value = (String)getValueColumn().getRowValue(rawValue);
  114. Date modifiedDate = (Date)getModifiedDateColumn().getRowValue(rawValue);
  115. return new VersionImpl(id, complexValueFk, value, modifiedDate);
  116. }
  117. @Override
  118. protected Object[] asRow(Object[] row, Version version) throws IOException {
  119. super.asRow(row, version);
  120. getValueColumn().setRowValue(row, version.getValue());
  121. getModifiedDateColumn().setRowValue(row, version.getModifiedDate());
  122. return row;
  123. }
  124. public static Version newVersion(String value, Date modifiedDate) {
  125. return newVersion(INVALID_FK, value, modifiedDate);
  126. }
  127. public static Version newVersion(ComplexValueForeignKey complexValueFk,
  128. String value, Date modifiedDate) {
  129. return new VersionImpl(INVALID_ID, complexValueFk, value, modifiedDate);
  130. }
  131. private static class VersionImpl extends ComplexValueImpl implements Version
  132. {
  133. private final String _value;
  134. private final Date _modifiedDate;
  135. private VersionImpl(Id id, ComplexValueForeignKey complexValueFk,
  136. String value, Date modifiedDate)
  137. {
  138. super(id, complexValueFk);
  139. _value = value;
  140. _modifiedDate = modifiedDate;
  141. }
  142. public String getValue() {
  143. return _value;
  144. }
  145. public Date getModifiedDate() {
  146. return _modifiedDate;
  147. }
  148. public int compareTo(Version o) {
  149. Date d1 = getModifiedDate();
  150. Date d2 = o.getModifiedDate();
  151. // sort by descending date (newest/greatest first)
  152. int cmp = d2.compareTo(d1);
  153. if(cmp != 0) {
  154. return cmp;
  155. }
  156. // use id, then complexValueFk to break ties (although we really
  157. // shouldn't be comparing across different columns)
  158. int id1 = getId().get();
  159. int id2 = o.getId().get();
  160. if(id1 != id2) {
  161. return ((id1 > id2) ? -1 : 1);
  162. }
  163. id1 = getComplexValueForeignKey().get();
  164. id2 = o.getComplexValueForeignKey().get();
  165. return ((id1 > id2) ? -1 :
  166. ((id1 < id2) ? 1 : 0));
  167. }
  168. public void update() throws IOException {
  169. throw new UnsupportedOperationException(
  170. "This column does not support value updates");
  171. }
  172. public void delete() throws IOException {
  173. throw new UnsupportedOperationException(
  174. "This column does not support value deletes");
  175. }
  176. @Override
  177. public String toString()
  178. {
  179. return "Version(" + getComplexValueForeignKey() + "," + getId() + ") " +
  180. getModifiedDate() + ", " + getValue();
  181. }
  182. }
  183. }