View Javadoc
1   /*
2   Copyright (c) 2011 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of 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,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl.complex;
18  
19  import java.io.IOException;
20  import java.time.LocalDateTime;
21  import java.util.Date;
22  import java.util.List;
23  import java.util.Map;
24  
25  import com.healthmarketscience.jackcess.Column;
26  import com.healthmarketscience.jackcess.Database;
27  import com.healthmarketscience.jackcess.DateTimeType;
28  import com.healthmarketscience.jackcess.Row;
29  import com.healthmarketscience.jackcess.complex.Attachment;
30  import com.healthmarketscience.jackcess.complex.AttachmentColumnInfo;
31  import com.healthmarketscience.jackcess.complex.ComplexColumnInfo;
32  import com.healthmarketscience.jackcess.complex.ComplexDataType;
33  import com.healthmarketscience.jackcess.complex.ComplexValue;
34  import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
35  import com.healthmarketscience.jackcess.complex.MultiValueColumnInfo;
36  import com.healthmarketscience.jackcess.complex.SingleValue;
37  import com.healthmarketscience.jackcess.complex.UnsupportedColumnInfo;
38  import com.healthmarketscience.jackcess.complex.UnsupportedValue;
39  import com.healthmarketscience.jackcess.complex.Version;
40  import com.healthmarketscience.jackcess.complex.VersionHistoryColumnInfo;
41  
42  /**
43   * Value which is returned for a complex column.  This value corresponds to a
44   * foreign key in a secondary table which contains the actual complex data for
45   * this row (which could be 0 or more complex values for a given row).  This
46   * class contains various convenience methods for interacting with the actual
47   * complex values.
48   * <p>
49   * This class will cache the associated complex values returned from one of
50   * the lookup methods.  The various modification methods will clear this cache
51   * automatically.  The {@link #reset} method may be called manually to clear
52   * this internal cache.
53   *
54   * @author James Ahlborn
55   */
56  @SuppressWarnings("deprecation")
57  public class ComplexValueForeignKeyImpl extends ComplexValueForeignKey
58  {
59    private static final long serialVersionUID = 20110805L;
60  
61    private transient final Column _column;
62    private final int _value;
63    private transient List<? extends ComplexValue> _values;
64  
65    public ComplexValueForeignKeyImpl(Column column, int value) {
66      _column = column;
67      _value = value;
68    }
69  
70    @Override
71    public int get() {
72      return _value;
73    }
74  
75    @Override
76    public Column getColumn() {
77      return _column;
78    }
79  
80    @Override
81    public ComplexDataType getComplexType() {
82      return getComplexInfo().getType();
83    }
84  
85    protected ComplexColumnInfo<? extends ComplexValue> getComplexInfo() {
86      return _column.getComplexInfo();
87    }
88  
89    protected VersionHistoryColumnInfo getVersionInfo() {
90      return (VersionHistoryColumnInfo)getComplexInfo();
91    }
92  
93    protected AttachmentColumnInfo getAttachmentInfo() {
94      return (AttachmentColumnInfo)getComplexInfo();
95    }
96  
97    protected MultiValueColumnInfo getMultiValueInfo() {
98      return (MultiValueColumnInfo)getComplexInfo();
99    }
100 
101   protected UnsupportedColumnInfo getUnsupportedInfo() {
102     return (UnsupportedColumnInfo)getComplexInfo();
103   }
104 
105   @Override
106   public int countValues() throws IOException {
107     return getComplexInfo().countValues(get());
108   }
109 
110   public List<Row> getRawValues() throws IOException {
111     return getComplexInfo().getRawValues(get());
112   }
113 
114   @Override
115   public List<? extends ComplexValue> getValues() throws IOException {
116     if(_values == null) {
117       _values = getComplexInfo().getValues(this);
118     }
119     return _values;
120   }
121 
122   @Override
123   @SuppressWarnings("unchecked")
124   public List<Version> getVersions() throws IOException {
125     if(getComplexType() != ComplexDataType.VERSION_HISTORY) {
126       throw new UnsupportedOperationException();
127     }
128     return (List<Version>)getValues();
129   }
130 
131   @Override
132   @SuppressWarnings("unchecked")
133   public List<Attachment> getAttachments() throws IOException {
134     if(getComplexType() != ComplexDataType.ATTACHMENT) {
135       throw new UnsupportedOperationException();
136     }
137     return (List<Attachment>)getValues();
138   }
139 
140   @Override
141   @SuppressWarnings("unchecked")
142   public List<SingleValue> getMultiValues() throws IOException {
143     if(getComplexType() != ComplexDataType.MULTI_VALUE) {
144       throw new UnsupportedOperationException();
145     }
146     return (List<SingleValue>)getValues();
147   }
148 
149   @Override
150   @SuppressWarnings("unchecked")
151   public List<UnsupportedValue> getUnsupportedValues() throws IOException {
152     if(getComplexType() != ComplexDataType.UNSUPPORTED) {
153       throw new UnsupportedOperationException();
154     }
155     return (List<UnsupportedValue>)getValues();
156   }
157 
158   @Override
159   public void reset() {
160     // discard any cached values
161     _values = null;
162   }
163 
164   @Override
165   public Version addVersion(String value) throws IOException {
166     return addVersionImpl(value, now());
167   }
168 
169   @Override
170   public Version addVersion(String value, Date modifiedDate) throws IOException {
171     return addVersionImpl(value, modifiedDate);
172   }
173 
174   @Override
175   public Version addVersion(String value, LocalDateTime modifiedDate) throws IOException {
176     return addVersionImpl(value, modifiedDate);
177   }
178 
179   private Version addVersionImpl(String value, Object modifiedDate) throws IOException {
180     reset();
181     Version v = VersionHistoryColumnInfoImpl.newVersion(this, value, modifiedDate);
182     getVersionInfo().addValue(v);
183     return v;
184   }
185 
186   @Override
187   public Attachment addAttachment(byte[] data) throws IOException {
188     return addAttachmentImpl(null, null, null, data, null, null);
189   }
190 
191   @Override
192   public Attachment addAttachment(
193       String url, String name, String type, byte[] data,
194       Date timeStamp, Integer flags)
195     throws IOException
196   {
197     return addAttachmentImpl(url, name, type, data, timeStamp, flags);
198   }
199 
200   @Override
201   public Attachment addAttachment(
202       String url, String name, String type, byte[] data,
203       LocalDateTime timeStamp, Integer flags)
204     throws IOException
205   {
206     return addAttachmentImpl(url, name, type, data, timeStamp, flags);
207   }
208 
209   private Attachment addAttachmentImpl(
210       String url, String name, String type, byte[] data,
211       Object timeStamp, Integer flags)
212     throws IOException
213   {
214     reset();
215     Attachment a = AttachmentColumnInfoImpl.newAttachment(
216         this, url, name, type, data, timeStamp, flags);
217     getAttachmentInfo().addValue(a);
218     return a;
219   }
220 
221   @Override
222   public Attachment addEncodedAttachment(byte[] encodedData)
223     throws IOException
224   {
225     return addEncodedAttachmentImpl(null, null, null, encodedData, null, null);
226   }
227 
228   @Override
229   public Attachment addEncodedAttachment(
230       String url, String name, String type, byte[] encodedData,
231       Date timeStamp, Integer flags)
232     throws IOException
233   {
234     return addEncodedAttachmentImpl(url, name, type, encodedData, timeStamp,
235                                     flags);
236   }
237 
238   @Override
239   public Attachment addEncodedAttachment(
240       String url, String name, String type, byte[] encodedData,
241       LocalDateTime timeStamp, Integer flags)
242     throws IOException
243   {
244     return addEncodedAttachmentImpl(url, name, type, encodedData, timeStamp,
245                                     flags);
246   }
247 
248   private Attachment addEncodedAttachmentImpl(
249       String url, String name, String type, byte[] encodedData,
250       Object timeStamp, Integer flags)
251     throws IOException
252   {
253     reset();
254     Attachment a = AttachmentColumnInfoImpl.newEncodedAttachment(
255         this, url, name, type, encodedData, timeStamp, flags);
256     getAttachmentInfo().addValue(a);
257     return a;
258   }
259 
260   @Override
261   public Attachment/../com/healthmarketscience/jackcess/complex/Attachment.html#Attachment">Attachment updateAttachment(Attachment attachment) throws IOException {
262     reset();
263     getAttachmentInfo().updateValue(attachment);
264     return attachment;
265   }
266 
267   @Override
268   public Attachment/../com/healthmarketscience/jackcess/complex/Attachment.html#Attachment">Attachment deleteAttachment(Attachment attachment) throws IOException {
269     reset();
270     getAttachmentInfo().deleteValue(attachment);
271     return attachment;
272   }
273 
274   @Override
275   public SingleValue addMultiValue(Object value) throws IOException {
276     reset();
277     SingleValue v = MultiValueColumnInfoImpl.newSingleValue(this, value);
278     getMultiValueInfo().addValue(v);
279     return v;
280   }
281 
282   @Override
283   public SingleValue../com/healthmarketscience/jackcess/complex/SingleValue.html#SingleValue">SingleValue updateMultiValue(SingleValue value) throws IOException {
284     reset();
285     getMultiValueInfo().updateValue(value);
286     return value;
287   }
288 
289   @Override
290   public SingleValue../com/healthmarketscience/jackcess/complex/SingleValue.html#SingleValue">SingleValue deleteMultiValue(SingleValue value) throws IOException {
291     reset();
292     getMultiValueInfo().deleteValue(value);
293     return value;
294   }
295 
296   @Override
297   public UnsupportedValue addUnsupportedValue(Map<String,?> values)
298     throws IOException
299   {
300     reset();
301     UnsupportedValue v = UnsupportedColumnInfoImpl.newValue(this, values);
302     getUnsupportedInfo().addValue(v);
303     return v;
304   }
305 
306   @Override
307   public UnsupportedValuethmarketscience/jackcess/complex/UnsupportedValue.html#UnsupportedValue">UnsupportedValue updateUnsupportedValue(UnsupportedValue value)
308     throws IOException
309   {
310     reset();
311     getUnsupportedInfo().updateValue(value);
312     return value;
313   }
314 
315   @Override
316   public UnsupportedValuethmarketscience/jackcess/complex/UnsupportedValue.html#UnsupportedValue">UnsupportedValue deleteUnsupportedValue(UnsupportedValue value)
317     throws IOException
318   {
319     reset();
320     getUnsupportedInfo().deleteValue(value);
321     return value;
322   }
323 
324   @Override
325   public void deleteAllValues() throws IOException {
326     reset();
327     getComplexInfo().deleteAllValues(this);
328   }
329 
330   @Override
331   public boolean equals(Object o) {
332     return(super.equals(o) &&
333            (_column == ((ComplexValueForeignKeyImpl)o)._column));
334   }
335 
336   private Object now() {
337     Database db = getColumn().getDatabase();
338     if(db.getDateTimeType() == DateTimeType.DATE) {
339       return new Date();
340     }
341     return LocalDateTime.now(db.getZoneId());
342   }
343 }