KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > result > AutoResultMap


1 /*
2  * Copyright 2004 Clinton Begin
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 package com.ibatis.sqlmap.engine.mapping.result;
17
18 import com.ibatis.common.beans.ClassInfo;
19 import com.ibatis.common.exception.NestedRuntimeException;
20 import com.ibatis.sqlmap.client.SqlMapException;
21 import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
22 import com.ibatis.sqlmap.engine.scope.RequestScope;
23 import com.ibatis.sqlmap.engine.type.DomTypeMarker;
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * An automatic result map for simple stuff
35  */

36 public class AutoResultMap extends BasicResultMap {
37
38   /**
39    * Constructor to pass in the SqlMapExecutorDelegate
40    *
41    * @param delegate - the delegate
42    */

43   public AutoResultMap(SqlMapExecutorDelegate delegate, boolean allowRemapping) {
44     super(delegate);
45     this.allowRemapping = allowRemapping;
46   }
47
48   public synchronized Object JavaDoc[] getResults(RequestScope request, ResultSet JavaDoc rs)
49       throws SQLException JavaDoc {
50     if (allowRemapping || getResultMappings() == null) {
51       initialize(rs);
52     }
53     return super.getResults(request, rs);
54   }
55
56   private void initialize(ResultSet JavaDoc rs) {
57     if (getResultClass() == null) {
58       throw new SqlMapException("The automatic ResultMap named " + this.getId() + " had a null result class (not allowed).");
59     } else if (Map JavaDoc.class.isAssignableFrom(getResultClass())) {
60       initializeMapResults(rs);
61     } else if (getDelegate().getTypeHandlerFactory().getTypeHandler(getResultClass()) != null) {
62       initializePrimitiveResults(rs);
63     } else if (DomTypeMarker.class.isAssignableFrom(getResultClass())) {
64       initializeXmlResults(rs);
65     } else {
66       initializeBeanResults(rs);
67     }
68   }
69
70   private void initializeBeanResults(ResultSet JavaDoc rs) {
71     try {
72       ClassInfo classInfo = ClassInfo.getInstance(getResultClass());
73       String JavaDoc[] propertyNames = classInfo.getWriteablePropertyNames();
74
75       Map JavaDoc propertyMap = new HashMap JavaDoc();
76       for (int i = 0; i < propertyNames.length; i++) {
77         propertyMap.put(propertyNames[i].toUpperCase(), propertyNames[i]);
78       }
79
80       List JavaDoc resultMappingList = new ArrayList JavaDoc();
81       ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
82       for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
83         String JavaDoc columnName = rsmd.getColumnLabel(i + 1);
84         String JavaDoc upperColumnName = columnName.toUpperCase();
85         String JavaDoc matchedProp = (String JavaDoc) propertyMap.get(upperColumnName);
86         if (matchedProp != null) {
87           BasicResultMapping resultMapping = new BasicResultMapping();
88           resultMapping.setPropertyName(matchedProp);
89           resultMapping.setColumnName(columnName);
90           resultMapping.setColumnIndex(i + 1);
91           Class JavaDoc type = classInfo.getSetterType(matchedProp);
92           resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(type));
93           resultMappingList.add(resultMapping);
94         }
95       }
96
97       setResultMappingList(resultMappingList);
98
99     } catch (SQLException JavaDoc e) {
100       throw new NestedRuntimeException("Error automapping columns. Cause: " + e);
101     }
102
103   }
104
105   private void initializeXmlResults(ResultSet JavaDoc rs) {
106     try {
107       List JavaDoc resultMappingList = new ArrayList JavaDoc();
108       ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
109       for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
110         String JavaDoc columnName = rsmd.getColumnLabel(i + 1);
111         BasicResultMapping resultMapping = new BasicResultMapping();
112         resultMapping.setPropertyName(columnName);
113         resultMapping.setColumnName(columnName);
114         resultMapping.setColumnIndex(i + 1);
115         resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(String JavaDoc.class));
116         resultMappingList.add(resultMapping);
117       }
118       setResultMappingList(resultMappingList);
119     } catch (SQLException JavaDoc e) {
120       throw new NestedRuntimeException("Error automapping columns. Cause: " + e);
121     }
122   }
123
124   private void initializeMapResults(ResultSet JavaDoc rs) {
125     try {
126       List JavaDoc resultMappingList = new ArrayList JavaDoc();
127       ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
128       for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
129         String JavaDoc columnName = rsmd.getColumnLabel(i + 1);
130         BasicResultMapping resultMapping = new BasicResultMapping();
131         resultMapping.setPropertyName(columnName);
132         resultMapping.setColumnName(columnName);
133         resultMapping.setColumnIndex(i + 1);
134         resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(Object JavaDoc.class));
135         resultMappingList.add(resultMapping);
136       }
137
138       setResultMappingList(resultMappingList);
139
140     } catch (SQLException JavaDoc e) {
141       throw new NestedRuntimeException("Error automapping columns. Cause: " + e);
142     }
143   }
144
145   private void initializePrimitiveResults(ResultSet JavaDoc rs) {
146     try {
147       ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
148       String JavaDoc columnName = rsmd.getColumnLabel(1);
149       BasicResultMapping resultMapping = new BasicResultMapping();
150       resultMapping.setPropertyName(columnName);
151       resultMapping.setColumnName(columnName);
152       resultMapping.setColumnIndex(1);
153       resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(getResultClass()));
154
155       List JavaDoc resultMappingList = new ArrayList JavaDoc();
156       resultMappingList.add(resultMapping);
157
158       setResultMappingList(resultMappingList);
159
160     } catch (SQLException JavaDoc e) {
161       throw new NestedRuntimeException("Error automapping columns. Cause: " + e);
162     }
163   }
164
165 }
166
167
Popular Tags