選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VersionHistoryColumnInfo.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Copyright (c) 2011 James Ahlborn
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. */
  16. package com.healthmarketscience.jackcess.complex;
  17. import java.io.IOException;
  18. import java.util.Collections;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.Map;
  22. import com.healthmarketscience.jackcess.Column;
  23. import com.healthmarketscience.jackcess.Table;
  24. /**
  25. * Complex column info for a column which tracking the version history of an
  26. * "append only" memo column.
  27. * <p>
  28. * Note, the strongly typed update/delete methods are <i>not</i> supported for
  29. * version history columns (the data is supposed to be immutable). That said,
  30. * the "raw" update/delete methods are supported for those that <i>really</i>
  31. * want to muck with the version history data.
  32. *
  33. * @author James Ahlborn
  34. */
  35. public class VersionHistoryColumnInfo extends ComplexColumnInfo<Version>
  36. {
  37. private final Column _valueCol;
  38. private final Column _modifiedCol;
  39. public VersionHistoryColumnInfo(Column column, int complexId,
  40. Table typeObjTable, Table flatTable)
  41. throws IOException
  42. {
  43. super(column, complexId, typeObjTable, flatTable);
  44. Column valueCol = null;
  45. Column modifiedCol = null;
  46. for(Column col : getTypeColumns()) {
  47. switch(col.getType()) {
  48. case SHORT_DATE_TIME:
  49. modifiedCol = col;
  50. break;
  51. case MEMO:
  52. valueCol = col;
  53. break;
  54. default:
  55. // ignore
  56. }
  57. }
  58. _valueCol = valueCol;
  59. _modifiedCol = modifiedCol;
  60. }
  61. @Override
  62. public void postTableLoadInit() throws IOException {
  63. super.postTableLoadInit();
  64. // link up with the actual versioned column. it should have the same name
  65. // as the "value" column in the type table.
  66. Column versionedCol = getColumn().getTable().getColumn(
  67. getValueColumn().getName());
  68. versionedCol.setVersionHistoryColumn(getColumn());
  69. }
  70. public Column getValueColumn() {
  71. return _valueCol;
  72. }
  73. public Column getModifiedDateColumn() {
  74. return _modifiedCol;
  75. }
  76. @Override
  77. public ComplexDataType getType() {
  78. return ComplexDataType.VERSION_HISTORY;
  79. }
  80. @Override
  81. public int updateValue(Version value) throws IOException {
  82. throw new UnsupportedOperationException(
  83. "This column does not support value updates");
  84. }
  85. @Override
  86. public void deleteValue(Version value) throws IOException {
  87. throw new UnsupportedOperationException(
  88. "This column does not support value deletes");
  89. }
  90. @Override
  91. public void deleteAllValues(int complexValueFk) throws IOException {
  92. throw new UnsupportedOperationException(
  93. "This column does not support value deletes");
  94. }
  95. @Override
  96. protected List<Version> toValues(ComplexValueForeignKey complexValueFk,
  97. List<Map<String,Object>> rawValues)
  98. throws IOException
  99. {
  100. List<Version> versions = super.toValues(complexValueFk, rawValues);
  101. // order versions newest to oldest
  102. Collections.sort(versions);
  103. return versions;
  104. }
  105. @Override
  106. protected VersionImpl toValue(ComplexValueForeignKey complexValueFk,
  107. Map<String,Object> rawValue) {
  108. int id = (Integer)getPrimaryKeyColumn().getRowValue(rawValue);
  109. String value = (String)getValueColumn().getRowValue(rawValue);
  110. Date modifiedDate = (Date)getModifiedDateColumn().getRowValue(rawValue);
  111. return new VersionImpl(id, complexValueFk, value, modifiedDate);
  112. }
  113. @Override
  114. protected Object[] asRow(Object[] row, Version version) throws IOException {
  115. super.asRow(row, version);
  116. getValueColumn().setRowValue(row, version.getValue());
  117. getModifiedDateColumn().setRowValue(row, version.getModifiedDate());
  118. return row;
  119. }
  120. public static Version newVersion(String value, Date modifiedDate) {
  121. return newVersion(INVALID_COMPLEX_VALUE_ID, value, modifiedDate);
  122. }
  123. public static Version newVersion(ComplexValueForeignKey complexValueFk,
  124. String value, Date modifiedDate) {
  125. return new VersionImpl(INVALID_ID, complexValueFk, value, modifiedDate);
  126. }
  127. public static boolean isVersionHistoryColumn(Table typeObjTable) {
  128. // version history data has these columns <value>(MEMO),
  129. // <modified>(SHORT_DATE_TIME)
  130. List<Column> typeCols = typeObjTable.getColumns();
  131. if(typeCols.size() < 2) {
  132. return false;
  133. }
  134. int numMemo = 0;
  135. int numDate = 0;
  136. for(Column col : typeCols) {
  137. switch(col.getType()) {
  138. case SHORT_DATE_TIME:
  139. ++numDate;
  140. break;
  141. case MEMO:
  142. ++numMemo;
  143. break;
  144. default:
  145. // ignore
  146. }
  147. }
  148. // be flexible, allow for extra columns...
  149. return((numMemo >= 1) && (numDate >= 1));
  150. }
  151. private static class VersionImpl extends ComplexValueImpl implements Version
  152. {
  153. private final String _value;
  154. private final Date _modifiedDate;
  155. private VersionImpl(int id, ComplexValueForeignKey complexValueFk,
  156. String value, Date modifiedDate)
  157. {
  158. super(id, complexValueFk);
  159. _value = value;
  160. _modifiedDate = modifiedDate;
  161. }
  162. public String getValue() {
  163. return _value;
  164. }
  165. public Date getModifiedDate() {
  166. return _modifiedDate;
  167. }
  168. public int compareTo(Version o) {
  169. Date d1 = getModifiedDate();
  170. Date d2 = o.getModifiedDate();
  171. // sort by descending date (newest/greatest first)
  172. int cmp = d2.compareTo(d1);
  173. if(cmp != 0) {
  174. return cmp;
  175. }
  176. // use id, then complexValueFk to break ties (although we really
  177. // shouldn't be comparing across different columns)
  178. int id1 = getId();
  179. int id2 = o.getId();
  180. if(id1 != id2) {
  181. return ((id1 > id2) ? -1 : 1);
  182. }
  183. id1 = getComplexValueForeignKey().get();
  184. id2 = o.getComplexValueForeignKey().get();
  185. return ((id1 > id2) ? -1 :
  186. ((id1 < id2) ? 1 : 0));
  187. }
  188. public void update() throws IOException {
  189. throw new UnsupportedOperationException(
  190. "This column does not support value updates");
  191. }
  192. public void delete() throws IOException {
  193. throw new UnsupportedOperationException(
  194. "This column does not support value deletes");
  195. }
  196. @Override
  197. public String toString()
  198. {
  199. return "Version(" + getComplexValueForeignKey() + "," + getId() + ") " +
  200. getModifiedDate() + ", " + getValue();
  201. }
  202. }
  203. }