KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26
27 /**
28  * JDBCTypeComplex provides the mapping between a Java Bean (not an EJB)
29  * and a set of columns. This class has a flattened view of the Java Bean,
30  * which may contain other Java Beans. This class simply treats the bean
31  * as a set of properties, which may be in the a.b.c style. The details
32  * of how this mapping is performed can be found in JDBCTypeFactory.
33  *
34  * This class holds a description of the columns
35  * and the properties that map to the columns. Additionally, this class
36  * knows how to extract a column value from the Java Bean and how to set
37  * a column value info the Java Bean. See JDBCTypeComplexProperty for
38  * details on how this is done.
39  *
40  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
41  * @version $Revision: 58402 $
42  */

43 public final class JDBCTypeComplex implements JDBCType {
44    private final JDBCTypeComplexProperty[] properties;
45    private final String JavaDoc[] columnNames;
46    private final Class JavaDoc[] javaTypes;
47    private final int[] jdbcTypes;
48    private final String JavaDoc[] sqlTypes;
49    private final boolean[] notNull;
50    private final JDBCResultSetReader[] resultSetReaders;
51    private final JDBCParameterSetter[] paramSetters;
52    private final Class JavaDoc fieldType;
53    private final HashMap JavaDoc propertiesByName = new HashMap JavaDoc();
54
55    public JDBCTypeComplex(
56          JDBCTypeComplexProperty[] properties,
57          Class JavaDoc fieldType) {
58
59       this.properties = properties;
60       this.fieldType = fieldType;
61
62       int propNum = properties.length;
63       columnNames = new String JavaDoc[propNum];
64       javaTypes = new Class JavaDoc[propNum];
65       jdbcTypes = new int[propNum];
66       sqlTypes = new String JavaDoc[propNum];
67       notNull = new boolean[propNum];
68       resultSetReaders = new JDBCResultSetReader[propNum];
69       paramSetters = new JDBCParameterSetter[propNum];
70       for(int i=0; i<properties.length; i++)
71       {
72          JDBCTypeComplexProperty property = properties[i];
73          columnNames[i] = property.getColumnName();
74          javaTypes[i] = property.getJavaType();
75          jdbcTypes[i] = property.getJDBCType();
76          sqlTypes[i] = property.getSQLType();
77          notNull[i] = property.isNotNull();
78          resultSetReaders[i] = property.getResulSetReader();
79          paramSetters[i] = property.getParameterSetter();
80          propertiesByName.put(property.getPropertyName(), property);
81       }
82    }
83
84    public String JavaDoc[] getColumnNames() {
85       return columnNames;
86    }
87    
88    public Class JavaDoc[] getJavaTypes() {
89       return javaTypes;
90    }
91    
92    public int[] getJDBCTypes() {
93       return jdbcTypes;
94    }
95    
96    public String JavaDoc[] getSQLTypes() {
97       return sqlTypes;
98    }
99    
100    public boolean[] getNotNull() {
101       return notNull;
102    }
103
104    public boolean[] getAutoIncrement() {
105       return new boolean[] {false};
106    }
107
108    public Object JavaDoc getColumnValue(int index, Object JavaDoc value) {
109       return getColumnValue(properties[index], value);
110    }
111
112    public Object JavaDoc setColumnValue(int index, Object JavaDoc value, Object JavaDoc columnValue) {
113       return setColumnValue(properties[index], value, columnValue);
114    }
115
116    public boolean hasMapper()
117    {
118       return false;
119    }
120
121    public boolean isSearchable()
122    {
123       return false;
124    }
125
126    public JDBCResultSetReader[] getResultSetReaders()
127    {
128       return resultSetReaders;
129    }
130
131    public JDBCParameterSetter[] getParameterSetter()
132    {
133       return paramSetters;
134    }
135
136    public JDBCTypeComplexProperty[] getProperties() {
137       return properties;
138    }
139
140    public JDBCTypeComplexProperty getProperty(String JavaDoc propertyName) {
141       JDBCTypeComplexProperty prop = (JDBCTypeComplexProperty)propertiesByName.get(propertyName);
142       if(prop == null) {
143          throw new EJBException JavaDoc(fieldType.getName() +
144                " does not have a property named " + propertyName);
145       }
146       return prop;
147    }
148
149    private static Object JavaDoc getColumnValue(JDBCTypeComplexProperty property, Object JavaDoc value) {
150       try {
151          return property.getColumnValue(value);
152       } catch(EJBException JavaDoc e) {
153          throw e;
154       } catch(Exception JavaDoc e) {
155          throw new EJBException JavaDoc("Error getting column value", e);
156       }
157    }
158
159    private Object JavaDoc setColumnValue(
160          JDBCTypeComplexProperty property,
161          Object JavaDoc value,
162          Object JavaDoc columnValue) {
163
164       if(value==null && columnValue==null) {
165          // nothing to do
166
return null;
167       }
168          
169       try {
170          if(value == null) {
171             value = fieldType.newInstance();
172          }
173          return property.setColumnValue(value, columnValue);
174       } catch(Exception JavaDoc e) {
175          e.printStackTrace();
176          throw new EJBException JavaDoc("Error setting column value", e);
177       }
178    }
179 }
180
Popular Tags