KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCTypeComplexProperty


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import java.lang.reflect.Method JavaDoc;
25
26 /**
27  * Immutable class which contins the mapping between a single Java Bean
28  * (not an EJB) property and a column. This class has a flattened view of
29  * the Java Bean property, which may be several properties deep in the
30  * base Java Bean. The details of how a property is mapped to a column
31  * can be found in JDBCTypeFactory.
32  *
33  * This class holds a description of the column and, knows how to extract
34  * the column value from the Java Bean and how to set a column value info
35  * the Java Bean.
36  *
37  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
38  * @version $Revision: 37459 $
39  */

40 public final class JDBCTypeComplexProperty
41 {
42    private final String JavaDoc propertyName;
43    private final String JavaDoc columnName;
44    private final Class JavaDoc javaType;
45    private final int jdbcType;
46    private final String JavaDoc sqlType;
47    private final boolean notNull;
48    private final JDBCResultSetReader resulSetReader;
49    private final JDBCParameterSetter paramSetter;
50
51    private final Method JavaDoc[] getters;
52    private final Method JavaDoc[] setters;
53
54    public JDBCTypeComplexProperty(
55       String JavaDoc propertyName,
56       String JavaDoc columnName,
57       Class JavaDoc javaType,
58       int jdbcType,
59       String JavaDoc sqlType,
60       boolean notNull,
61       Method JavaDoc[] getters,
62       Method JavaDoc[] setters)
63    {
64
65       this.propertyName = propertyName;
66       this.columnName = columnName;
67       this.javaType = javaType;
68       this.jdbcType = jdbcType;
69       this.sqlType = sqlType;
70       this.notNull = notNull;
71       this.getters = getters;
72       this.setters = setters;
73       this.resulSetReader = JDBCUtil.getResultSetReader(jdbcType, javaType);
74       this.paramSetter = JDBCUtil.getParameterSetter(jdbcType, javaType);
75    }
76
77    public JDBCTypeComplexProperty(
78       JDBCTypeComplexProperty defaultProperty,
79       String JavaDoc columnName,
80       int jdbcType,
81       String JavaDoc sqlType,
82       boolean notNull)
83    {
84
85       this.propertyName = defaultProperty.propertyName;
86       this.columnName = columnName;
87       this.javaType = defaultProperty.javaType;
88       this.jdbcType = jdbcType;
89       this.sqlType = sqlType;
90       this.notNull = notNull;
91       this.getters = defaultProperty.getters;
92       this.setters = defaultProperty.setters;
93       this.resulSetReader = JDBCUtil.getResultSetReader(jdbcType, javaType);
94       this.paramSetter = JDBCUtil.getParameterSetter(jdbcType, javaType);
95    }
96
97    public String JavaDoc getPropertyName()
98    {
99       return propertyName;
100    }
101
102    public String JavaDoc getColumnName()
103    {
104       return columnName;
105    }
106
107    public Class JavaDoc getJavaType()
108    {
109       return javaType;
110    }
111
112    public int getJDBCType()
113    {
114       return jdbcType;
115    }
116
117    public String JavaDoc getSQLType()
118    {
119       return sqlType;
120    }
121
122    public boolean isNotNull()
123    {
124       return notNull;
125    }
126
127    public JDBCResultSetReader getResulSetReader()
128    {
129       return resulSetReader;
130    }
131
132    public JDBCParameterSetter getParameterSetter()
133    {
134       return paramSetter;
135    }
136
137    public Object JavaDoc getColumnValue(Object JavaDoc value) throws Exception JavaDoc
138    {
139       Object JavaDoc[] noArgs = new Object JavaDoc[0];
140
141       for(int i = 0; i < getters.length; i++)
142       {
143          if(value == null)
144          {
145             return null;
146          }
147          value = getters[i].invoke(value, noArgs);
148       }
149       return value;
150    }
151
152    public Object JavaDoc setColumnValue(
153       Object JavaDoc value,
154       Object JavaDoc columnValue) throws Exception JavaDoc
155    {
156
157       // Used for invocation of get and set
158
Object JavaDoc[] noArgs = new Object JavaDoc[0];
159       Object JavaDoc[] singleArg = new Object JavaDoc[1];
160
161       // save the first value to return
162
Object JavaDoc returnValue = value;
163
164       // get the second to last object in the chain
165
for(int i = 0; i < getters.length - 1; i++)
166       {
167          // get the next object in chain
168
Object JavaDoc next = getters[i].invoke(value, noArgs);
169
170          // the next object is null creat it
171
if(next == null)
172          {
173             // new type based on getter
174
next = getters[i].getReturnType().newInstance();
175
176             // and set it into the current value
177
singleArg[0] = next;
178
179             setters[i].invoke(value, singleArg);
180          }
181
182          // update value to the next in chain
183
value = next;
184       }
185
186       // value is now the object on which we need to set the column value
187
singleArg[0] = columnValue;
188       setters[setters.length - 1].invoke(value, singleArg);
189
190       // return the first object in call chain
191
return returnValue;
192    }
193 }
194
Popular Tags