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.

ComplexValueForeignKeyImpl.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.Date;
  16. import java.util.List;
  17. import java.util.Map;
  18. import com.healthmarketscience.jackcess.Column;
  19. import com.healthmarketscience.jackcess.Row;
  20. import com.healthmarketscience.jackcess.complex.Attachment;
  21. import com.healthmarketscience.jackcess.complex.AttachmentColumnInfo;
  22. import com.healthmarketscience.jackcess.complex.ComplexColumnInfo;
  23. import com.healthmarketscience.jackcess.complex.ComplexDataType;
  24. import com.healthmarketscience.jackcess.complex.ComplexValue;
  25. import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
  26. import com.healthmarketscience.jackcess.complex.MultiValueColumnInfo;
  27. import com.healthmarketscience.jackcess.complex.SingleValue;
  28. import com.healthmarketscience.jackcess.complex.UnsupportedColumnInfo;
  29. import com.healthmarketscience.jackcess.complex.UnsupportedValue;
  30. import com.healthmarketscience.jackcess.complex.Version;
  31. import com.healthmarketscience.jackcess.complex.VersionHistoryColumnInfo;
  32. /**
  33. * Value which is returned for a complex column. This value corresponds to a
  34. * foreign key in a secondary table which contains the actual complex data for
  35. * this row (which could be 0 or more complex values for a given row). This
  36. * class contains various convenience methods for interacting with the actual
  37. * complex values.
  38. * <p>
  39. * This class will cache the associated complex values returned from one of
  40. * the lookup methods. The various modification methods will clear this cache
  41. * automatically. The {@link #reset} method may be called manually to clear
  42. * this internal cache.
  43. *
  44. * @author James Ahlborn
  45. */
  46. public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey
  47. {
  48. private static final long serialVersionUID = 20110805L;
  49. private transient final Column _column;
  50. private final int _value;
  51. private transient List<? extends ComplexValue> _values;
  52. public ComplexValueForeignKeyImpl(Column column, int value) {
  53. _column = column;
  54. _value = value;
  55. }
  56. @Override
  57. public int get() {
  58. return _value;
  59. }
  60. @Override
  61. public Column getColumn() {
  62. return _column;
  63. }
  64. @Override
  65. public ComplexDataType getComplexType() {
  66. return getComplexInfo().getType();
  67. }
  68. protected ComplexColumnInfo<? extends ComplexValue> getComplexInfo() {
  69. return _column.getComplexInfo();
  70. }
  71. protected VersionHistoryColumnInfo getVersionInfo() {
  72. return (VersionHistoryColumnInfo)getComplexInfo();
  73. }
  74. protected AttachmentColumnInfo getAttachmentInfo() {
  75. return (AttachmentColumnInfo)getComplexInfo();
  76. }
  77. protected MultiValueColumnInfo getMultiValueInfo() {
  78. return (MultiValueColumnInfo)getComplexInfo();
  79. }
  80. protected UnsupportedColumnInfo getUnsupportedInfo() {
  81. return (UnsupportedColumnInfo)getComplexInfo();
  82. }
  83. @Override
  84. public int countValues() throws IOException {
  85. return getComplexInfo().countValues(get());
  86. }
  87. public List<Row> getRawValues() throws IOException {
  88. return getComplexInfo().getRawValues(get());
  89. }
  90. @Override
  91. public List<? extends ComplexValue> getValues() throws IOException {
  92. if(_values == null) {
  93. _values = getComplexInfo().getValues(this);
  94. }
  95. return _values;
  96. }
  97. @Override
  98. @SuppressWarnings("unchecked")
  99. public List<Version> getVersions() throws IOException {
  100. if(getComplexType() != ComplexDataType.VERSION_HISTORY) {
  101. throw new UnsupportedOperationException();
  102. }
  103. return (List<Version>)getValues();
  104. }
  105. @Override
  106. @SuppressWarnings("unchecked")
  107. public List<Attachment> getAttachments() throws IOException {
  108. if(getComplexType() != ComplexDataType.ATTACHMENT) {
  109. throw new UnsupportedOperationException();
  110. }
  111. return (List<Attachment>)getValues();
  112. }
  113. @Override
  114. @SuppressWarnings("unchecked")
  115. public List<SingleValue> getMultiValues() throws IOException {
  116. if(getComplexType() != ComplexDataType.MULTI_VALUE) {
  117. throw new UnsupportedOperationException();
  118. }
  119. return (List<SingleValue>)getValues();
  120. }
  121. @Override
  122. @SuppressWarnings("unchecked")
  123. public List<UnsupportedValue> getUnsupportedValues() throws IOException {
  124. if(getComplexType() != ComplexDataType.UNSUPPORTED) {
  125. throw new UnsupportedOperationException();
  126. }
  127. return (List<UnsupportedValue>)getValues();
  128. }
  129. @Override
  130. public void reset() {
  131. // discard any cached values
  132. _values = null;
  133. }
  134. @Override
  135. public Version addVersion(String value) throws IOException {
  136. return addVersion(value, new Date());
  137. }
  138. @Override
  139. public Version addVersion(String value, Date modifiedDate) throws IOException {
  140. reset();
  141. Version v = VersionHistoryColumnInfoImpl.newVersion(this, value, modifiedDate);
  142. getVersionInfo().addValue(v);
  143. return v;
  144. }
  145. @Override
  146. public Attachment addAttachment(byte[] data) throws IOException {
  147. return addAttachment(null, null, null, data, null, null);
  148. }
  149. @Override
  150. public Attachment addAttachment(
  151. String url, String name, String type, byte[] data,
  152. Date timeStamp, Integer flags)
  153. throws IOException
  154. {
  155. reset();
  156. Attachment a = AttachmentColumnInfoImpl.newAttachment(
  157. this, url, name, type, data, timeStamp, flags);
  158. getAttachmentInfo().addValue(a);
  159. return a;
  160. }
  161. @Override
  162. public Attachment addEncodedAttachment(byte[] encodedData)
  163. throws IOException
  164. {
  165. return addEncodedAttachment(null, null, null, encodedData, null, null);
  166. }
  167. @Override
  168. public Attachment addEncodedAttachment(
  169. String url, String name, String type, byte[] encodedData,
  170. Date timeStamp, Integer flags)
  171. throws IOException
  172. {
  173. reset();
  174. Attachment a = AttachmentColumnInfoImpl.newEncodedAttachment(
  175. this, url, name, type, encodedData, timeStamp, flags);
  176. getAttachmentInfo().addValue(a);
  177. return a;
  178. }
  179. @Override
  180. public Attachment updateAttachment(Attachment attachment) throws IOException {
  181. reset();
  182. getAttachmentInfo().updateValue(attachment);
  183. return attachment;
  184. }
  185. @Override
  186. public Attachment deleteAttachment(Attachment attachment) throws IOException {
  187. reset();
  188. getAttachmentInfo().deleteValue(attachment);
  189. return attachment;
  190. }
  191. @Override
  192. public SingleValue addMultiValue(Object value) throws IOException {
  193. reset();
  194. SingleValue v = MultiValueColumnInfoImpl.newSingleValue(this, value);
  195. getMultiValueInfo().addValue(v);
  196. return v;
  197. }
  198. @Override
  199. public SingleValue updateMultiValue(SingleValue value) throws IOException {
  200. reset();
  201. getMultiValueInfo().updateValue(value);
  202. return value;
  203. }
  204. @Override
  205. public SingleValue deleteMultiValue(SingleValue value) throws IOException {
  206. reset();
  207. getMultiValueInfo().deleteValue(value);
  208. return value;
  209. }
  210. @Override
  211. public UnsupportedValue addUnsupportedValue(Map<String,?> values)
  212. throws IOException
  213. {
  214. reset();
  215. UnsupportedValue v = UnsupportedColumnInfoImpl.newValue(this, values);
  216. getUnsupportedInfo().addValue(v);
  217. return v;
  218. }
  219. @Override
  220. public UnsupportedValue updateUnsupportedValue(UnsupportedValue value)
  221. throws IOException
  222. {
  223. reset();
  224. getUnsupportedInfo().updateValue(value);
  225. return value;
  226. }
  227. @Override
  228. public UnsupportedValue deleteUnsupportedValue(UnsupportedValue value)
  229. throws IOException
  230. {
  231. reset();
  232. getUnsupportedInfo().deleteValue(value);
  233. return value;
  234. }
  235. @Override
  236. public void deleteAllValues() throws IOException {
  237. reset();
  238. getComplexInfo().deleteAllValues(this);
  239. }
  240. @Override
  241. public boolean equals(Object o) {
  242. return(super.equals(o) &&
  243. (_column == ((ComplexValueForeignKeyImpl)o)._column));
  244. }
  245. }