KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > metadata > JDBCValuePropertyMetaData


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.metadata;
23
24 import java.lang.reflect.Method JavaDoc;
25 import org.jboss.deployment.DeploymentException;
26 import org.jboss.metadata.MetaData;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * Imutable class which contains information about a single dependent
31  * value object property.
32  *
33  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
34  * @version $Revision: 37459 $
35  */

36 public final class JDBCValuePropertyMetaData {
37    private final String JavaDoc propertyName;
38    private final Class JavaDoc propertyType;
39    private final String JavaDoc columnName;
40    private final String JavaDoc sqlType;
41    private final int jdbcType;
42    private final boolean notNull;
43    private final Method JavaDoc getter;
44    private final Method JavaDoc setter;
45    
46    /**
47     * Constructs a value property metadata class with the data contained in
48     * the property xml element from a jbosscmp-jdbc xml file.
49     *
50     * @param element the xml Element which contains the metadata about
51     * this property
52     * @param classType the java Class type of the value class on which this
53     * property is defined
54     * @throws DeploymentException if the xml element is not semantically correct
55     */

56    public JDBCValuePropertyMetaData(Element JavaDoc element, Class JavaDoc classType)
57          throws DeploymentException {
58
59       // Property name
60
propertyName = MetaData.getUniqueChildContent(element, "property-name");
61
62       // Column name
63
String JavaDoc columnNameString =
64             MetaData.getOptionalChildContent(element, "column-name");
65       if(columnNameString != null) {
66          columnName = columnNameString;
67       } else {
68          columnName = propertyName;
69       }
70
71       // Not null
72
Element JavaDoc notNullElement = MetaData.getOptionalChild(element, "not-null");
73       notNull = (notNullElement != null);
74
75       // Getter
76
try {
77          getter = classType.getMethod(toGetterName(propertyName), new Class JavaDoc[0]);
78       } catch(Exception JavaDoc e) {
79          throw new DeploymentException("Unable to find getter for property " +
80                propertyName + " on dependent value class " +
81                classType.getName());
82       }
83
84       // get property type from getter return type
85
propertyType = getter.getReturnType();
86       
87       // resolve setter
88
try {
89          setter = classType.getMethod(
90                toSetterName(propertyName),
91                new Class JavaDoc[] { propertyType } );
92       } catch(Exception JavaDoc e) {
93          throw new DeploymentException("Unable to find setter for property " +
94                propertyName + " on dependent value class " +
95                classType.getName());
96       }
97
98       // jdbc type - optional
99
String JavaDoc jdbcString =
100             MetaData.getOptionalChildContent(element, "jdbc-type");
101       if(jdbcString != null) {
102          jdbcType = JDBCMappingMetaData.getJdbcTypeFromName(jdbcString);
103          
104          // sql type - required if jdbc-type specified
105
sqlType = MetaData.getUniqueChildContent(element, "sql-type");
106       } else {
107          jdbcType = Integer.MIN_VALUE;
108          sqlType = null;
109       }
110    }
111    
112    /**
113     * Gets the name of this property. The name will always begin with a lower
114     * case letter and is a Java Beans property name. This is the base name of
115     * the getter and setter property.
116     *
117     * @return the name of this property
118     */

119    public String JavaDoc getPropertyName() {
120       return propertyName;
121    }
122
123    /**
124     * Gets the java class type of this property. The class the the return type
125     * of the getter and type of the sole argument of the setter.
126     *
127     * @return the java Class type of this property
128     */

129    public Class JavaDoc getPropertyType() {
130       return propertyType;
131    }
132
133    /**
134     * Gets the column name which this property will be persisted.
135     *
136     * @return the name of the column which this property will be persisted
137     */

138    public String JavaDoc getColumnName() {
139       return columnName;
140    }
141    
142    /**
143     * Gets the jdbc type of this property. The jdbc type is used to retrieve
144     * data from a result set and to set parameters in a prepared statement.
145     *
146     * @return the jdbc type of this property
147     */

148    public int getJDBCType() {
149       return jdbcType;
150    }
151
152    /**
153     * Gets the sql type of this mapping. The sql type is the sql column data
154     * type, and is used in CREATE TABLE statements.
155     *
156     * @return the sql type String of this mapping
157     */

158    public String JavaDoc getSqlType() {
159       return sqlType;
160    }
161
162    /**
163     * Should this field allow null values?
164     * @return true if this field will not allow a null value.
165     */

166    public boolean isNotNull() {
167       return notNull;
168    }
169
170    /**
171     * Gets the getter method of this property. The getter method is used to
172     * retrieve the value of this property from the value class.
173     *
174     * @return the Method which gets the value of this property
175     */

176    public Method JavaDoc getGetter() {
177       return getter;
178    }
179
180    /**
181     * Gets the setter method of this property. The setter method is used to
182     * set the value of this property in the value class.
183     *
184     * @return the Method which sets the value of this property
185     */

186    public Method JavaDoc getSetter() {
187       return setter;
188    }
189    
190    private static String JavaDoc toGetterName(String JavaDoc propertyName) {
191       return "get" + upCaseFirstCharacter(propertyName);
192    }
193    
194    private static String JavaDoc toSetterName(String JavaDoc propertyName) {
195       return "set" + upCaseFirstCharacter(propertyName);
196    }
197    
198    private static String JavaDoc upCaseFirstCharacter(String JavaDoc propertyName) {
199       return Character.toUpperCase(propertyName.charAt(0)) +
200             propertyName.substring(1);
201    }
202 }
203
Popular Tags