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