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  
21  import com.healthmarketscience.jackcess.Column;
22  import com.healthmarketscience.jackcess.Row;
23  import com.healthmarketscience.jackcess.Table;
24  import com.healthmarketscience.jackcess.complex.ComplexDataType;
25  import com.healthmarketscience.jackcess.complex.ComplexValue;
26  import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
27  import com.healthmarketscience.jackcess.complex.MultiValueColumnInfo;
28  import com.healthmarketscience.jackcess.complex.SingleValue;
29  
30  /**
31   * Complex column info for a column holding multiple simple values per row.
32   *
33   * @author James Ahlborn
34   */
35  public class MultiValueColumnInfoImpl extends ComplexColumnInfoImpl<SingleValue>
36    implements MultiValueColumnInfo
37  {
38    private final Column _valueCol;
39    
40    public MultiValueColumnInfoImpl(Column column, int complexId,
41                                    Table typeObjTable, Table flatTable) 
42      throws IOException
43    {
44      super(column, complexId, typeObjTable, flatTable);
45  
46      _valueCol = getTypeColumns().get(0);
47    }
48  
49    @Override
50    public ComplexDataType getType()
51    {
52      return ComplexDataType.MULTI_VALUE;
53    }
54  
55    public Column getValueColumn() {
56      return _valueCol;
57    }
58  
59    @Override
60    protected SingleValueImpl toValue(
61        ComplexValueForeignKey complexValueFk,
62        Row rawValue)
63    {
64      ComplexValue.Id id = getValueId(rawValue);
65      Object value = getValueColumn().getRowValue(rawValue);
66  
67      return new SingleValueImpl(id, complexValueFk, value);
68    }
69  
70    @Override
71    protected Object[] asRow(Object[] row, SingleValue value) throws IOException {
72      super.asRow(row, value);
73      getValueColumn().setRowValue(row, value.get());
74      return row;
75    }
76    
77    public static SingleValue newSingleValue(Object value) {
78      return newSingleValue(INVALID_FK, value);
79    }
80  
81    public static SingleValue newSingleValue(
82        ComplexValueForeignKey complexValueFk, Object value) {
83      return new SingleValueImpl(INVALID_ID, complexValueFk, value);
84    }
85  
86  
87    private static class SingleValueImpl extends ComplexValueImpl
88      implements SingleValue
89    {
90      private Object _value;
91  
92      private SingleValueImpl(Id id, ComplexValueForeignKey complexValueFk,
93                              Object value)
94      {
95        super(id, complexValueFk);
96        _value = value;
97      }
98      
99      public Object get() {
100       return _value;
101     }
102 
103     public void set(Object value) {
104       _value = value;
105     }
106 
107     public void update() throws IOException {
108       getComplexValueForeignKey().updateMultiValue(this);
109     }
110     
111     public void delete() throws IOException {
112       getComplexValueForeignKey().deleteMultiValue(this);
113     }
114     
115     @Override
116     public String toString()
117     {
118       return "SingleValue(" + getComplexValueForeignKey() + "," + getId() +
119         ") " + get();
120     } 
121   }
122 }