KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > jdbc > DataRowPostProcessor


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.jdbc;
21
22 import java.sql.ResultSet JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.cayenne.DataRow;
30 import org.apache.cayenne.access.trans.SelectTranslator;
31 import org.apache.cayenne.access.types.ExtendedType;
32 import org.apache.cayenne.dba.TypesMapping;
33 import org.apache.cayenne.map.Entity;
34 import org.apache.cayenne.map.EntityInheritanceTree;
35 import org.apache.cayenne.map.ObjAttribute;
36 import org.apache.cayenne.map.ObjEntity;
37
38 /**
39  * Deals with DataRow type conversion in inheritance situations.
40  *
41  * @since 1.2
42  * @author Andrus Adamchik
43  */

44 class DataRowPostProcessor {
45
46     private EntityInheritanceTree inheritanceTree;
47     private Map JavaDoc columnOverrides;
48     private Collection JavaDoc defaultOverrides;
49
50     // factory method
51
static DataRowPostProcessor createPostProcessor(SelectTranslator translator) {
52         Map JavaDoc attributeOverrides = translator.getAttributeOverrides();
53         if (attributeOverrides.isEmpty()) {
54             return null;
55         }
56
57         ColumnDescriptor[] columns = translator.getResultColumns();
58
59         Map JavaDoc columnOverrides = new HashMap JavaDoc(2);
60
61         Iterator JavaDoc it = attributeOverrides.entrySet().iterator();
62         while (it.hasNext()) {
63             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
64
65             ObjAttribute attribute = (ObjAttribute) entry.getKey();
66             Entity entity = attribute.getEntity();
67
68             String JavaDoc key = null;
69             int jdbcType = TypesMapping.NOT_DEFINED;
70             int index = -1;
71             for (int i = 0; i < columns.length; i++) {
72                 if (columns[i] == entry.getValue()) {
73
74                     // if attribute type is the same as column, there is no conflict
75
if (!attribute.getType().equals(columns[i].getJavaClass())) {
76                         // note that JDBC index is "1" based
77
index = i + 1;
78                         jdbcType = columns[i].getJdbcType();
79                         key = columns[i].getLabel();
80                     }
81
82                     break;
83                 }
84             }
85
86             if (index < 1) {
87                 continue;
88             }
89
90             ExtendedType converter = translator
91                     .getAdapter()
92                     .getExtendedTypes()
93                     .getRegisteredType(attribute.getType());
94
95             Collection JavaDoc overrides = (Collection JavaDoc) columnOverrides.get(entity.getName());
96
97             if (overrides == null) {
98                 overrides = new ArrayList JavaDoc(3);
99                 columnOverrides.put(entity.getName(), overrides);
100             }
101
102             overrides.add(new ColumnOverride(index, key, converter, jdbcType));
103         }
104
105         // inject null post-processor
106
return columnOverrides.isEmpty() ? null : new DataRowPostProcessor(translator
107                 .getRootInheritanceTree(), columnOverrides);
108     }
109
110     private DataRowPostProcessor(EntityInheritanceTree inheritanceTree,
111             Map JavaDoc columnOverrides) {
112
113         if (inheritanceTree != null && inheritanceTree.getChildren().size() > 0) {
114             this.inheritanceTree = inheritanceTree;
115             this.columnOverrides = columnOverrides;
116         }
117         else {
118             if (columnOverrides.size() != 1) {
119                 throw new IllegalArgumentException JavaDoc(
120                         "No inheritance - there must be only one override set");
121             }
122
123             defaultOverrides = (Collection JavaDoc) columnOverrides.values().iterator().next();
124         }
125     }
126
127     void postprocessRow(ResultSet JavaDoc resultSet, DataRow row) throws Exception JavaDoc {
128
129         Collection JavaDoc overrides = getOverrides(row);
130
131         if (overrides != null) {
132             Iterator JavaDoc it = overrides.iterator();
133             while (it.hasNext()) {
134                 ColumnOverride override = (ColumnOverride) it.next();
135
136                 Object JavaDoc newValue = override.converter.materializeObject(
137                         resultSet,
138                         override.index,
139                         override.jdbcType);
140                 row.put(override.key, newValue);
141             }
142         }
143     }
144
145     private final Collection JavaDoc getOverrides(DataRow row) {
146         if (defaultOverrides != null) {
147             return defaultOverrides;
148         }
149         else {
150             ObjEntity entity = inheritanceTree.entityMatchingRow(row);
151             return entity != null
152                     ? (Collection JavaDoc) columnOverrides.get(entity.getName())
153                     : null;
154         }
155     }
156
157     static final class ColumnOverride {
158
159         int index;
160         int jdbcType;
161         String JavaDoc key;
162         ExtendedType converter;
163
164         ColumnOverride(int index, String JavaDoc key, ExtendedType converter, int jdbcType) {
165             this.index = index;
166             this.key = key;
167             this.converter = converter;
168             this.jdbcType = jdbcType;
169         }
170     }
171 }
172
Popular Tags