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.LinkedHashMap;
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.Table;
27  import com.healthmarketscience.jackcess.complex.ComplexDataType;
28  import com.healthmarketscience.jackcess.complex.ComplexValue;
29  import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
30  import com.healthmarketscience.jackcess.complex.UnsupportedColumnInfo;
31  import com.healthmarketscience.jackcess.complex.UnsupportedValue;
32  
33  /**
34   * Complex column info for an unsupported complex type.
35   *
36   * @author James Ahlborn
37   */
38  public class UnsupportedColumnInfoImpl 
39    extends ComplexColumnInfoImpl<UnsupportedValue> 
40    implements UnsupportedColumnInfo
41  {
42  
43    public UnsupportedColumnInfoImpl(Column column, int complexId, 
44                                     Table typeObjTable, Table flatTable)
45      throws IOException
46    {
47      super(column, complexId, typeObjTable, flatTable);
48    }
49  
50    public List<Column> getValueColumns() {
51      return getTypeColumns();
52    }
53  
54    @Override
55    public ComplexDataType getType()
56    {
57      return ComplexDataType.UNSUPPORTED;
58    }
59  
60    @Override
61    protected UnsupportedValueImpl toValue(
62        ComplexValueForeignKey complexValueFk,
63        Row rawValue)
64    {
65      ComplexValue.Id id = getValueId(rawValue);
66  
67      Map<String,Object> values = new LinkedHashMap<String,Object>();
68      for(Column col : getValueColumns()) {
69        col.setRowValue(values, col.getRowValue(rawValue));
70      }
71  
72      return new UnsupportedValueImpl(id, complexValueFk, values);
73    }
74  
75    @Override
76    protected Object[] asRow(Object[] row, UnsupportedValue value) 
77      throws IOException
78    {
79      super.asRow(row, value);
80  
81      Map<String,Object> values = value.getValues();
82      for(Column col : getValueColumns()) {
83        col.setRowValue(row, col.getRowValue(values));
84      }
85  
86      return row;
87    }
88  
89    public static UnsupportedValue newValue(Map<String,?> values) {
90      return newValue(INVALID_FK, values);
91    }
92  
93    public static UnsupportedValue newValue(
94        ComplexValueForeignKey complexValueFk, Map<String,?> values) {
95      return new UnsupportedValueImpl(INVALID_ID, complexValueFk, 
96                                      new LinkedHashMap<String,Object>(values));
97    }
98    
99    private static class UnsupportedValueImpl extends ComplexValueImpl
100     implements UnsupportedValue
101   {
102     private Map<String,Object> _values;
103 
104     private UnsupportedValueImpl(Id id, ComplexValueForeignKey complexValueFk,
105                                  Map<String,Object> values)
106     {
107       super(id, complexValueFk);
108       _values = values;
109     }
110 
111     public Map<String,Object> getValues() {
112       return _values;
113     }
114     
115     public Object get(String columnName) {
116       return getValues().get(columnName);
117     }
118 
119     public void set(String columnName, Object value) {
120       getValues().put(columnName, value);
121     }
122 
123     public void update() throws IOException {
124       getComplexValueForeignKey().updateUnsupportedValue(this);
125     }
126     
127     public void delete() throws IOException {
128       getComplexValueForeignKey().deleteUnsupportedValue(this);
129     }
130     
131     @Override
132     public String toString()
133     {
134       return "UnsupportedValue(" + getComplexValueForeignKey() + "," + getId() +
135         ") " + getValues();
136     } 
137   }
138 }