001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc.metadata;
023:
024: import java.lang.reflect.Method;
025: import org.jboss.deployment.DeploymentException;
026: import org.jboss.metadata.MetaData;
027: import org.w3c.dom.Element;
028:
029: /**
030: * Imutable class which contains information about a single dependent
031: * value object property.
032: *
033: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
034: * @version $Revision: 57209 $
035: */
036: public final class JDBCValuePropertyMetaData {
037: private final String propertyName;
038: private final Class propertyType;
039: private final String columnName;
040: private final String sqlType;
041: private final int jdbcType;
042: private final boolean notNull;
043: private final Method getter;
044: private final Method setter;
045:
046: /**
047: * Constructs a value property metadata class with the data contained in
048: * the property xml element from a jbosscmp-jdbc xml file.
049: *
050: * @param element the xml Element which contains the metadata about
051: * this property
052: * @param classType the java Class type of the value class on which this
053: * property is defined
054: * @throws DeploymentException if the xml element is not semantically correct
055: */
056: public JDBCValuePropertyMetaData(Element element, Class classType)
057: throws DeploymentException {
058:
059: // Property name
060: propertyName = MetaData.getUniqueChildContent(element,
061: "property-name");
062:
063: // Column name
064: String columnNameString = MetaData.getOptionalChildContent(
065: element, "column-name");
066: if (columnNameString != null) {
067: columnName = columnNameString;
068: } else {
069: columnName = propertyName;
070: }
071:
072: // Not null
073: Element notNullElement = MetaData.getOptionalChild(element,
074: "not-null");
075: notNull = (notNullElement != null);
076:
077: // Getter
078: try {
079: getter = classType.getMethod(toGetterName(propertyName),
080: new Class[0]);
081: } catch (Exception e) {
082: throw new DeploymentException(
083: "Unable to find getter for property "
084: + propertyName
085: + " on dependent value class "
086: + classType.getName());
087: }
088:
089: // get property type from getter return type
090: propertyType = getter.getReturnType();
091:
092: // resolve setter
093: try {
094: setter = classType.getMethod(toSetterName(propertyName),
095: new Class[] { propertyType });
096: } catch (Exception e) {
097: throw new DeploymentException(
098: "Unable to find setter for property "
099: + propertyName
100: + " on dependent value class "
101: + classType.getName());
102: }
103:
104: // jdbc type - optional
105: String jdbcString = MetaData.getOptionalChildContent(element,
106: "jdbc-type");
107: if (jdbcString != null) {
108: jdbcType = JDBCMappingMetaData
109: .getJdbcTypeFromName(jdbcString);
110:
111: // sql type - required if jdbc-type specified
112: sqlType = MetaData.getUniqueChildContent(element,
113: "sql-type");
114: } else {
115: jdbcType = Integer.MIN_VALUE;
116: sqlType = null;
117: }
118: }
119:
120: /**
121: * Gets the name of this property. The name will always begin with a lower
122: * case letter and is a Java Beans property name. This is the base name of
123: * the getter and setter property.
124: *
125: * @return the name of this property
126: */
127: public String getPropertyName() {
128: return propertyName;
129: }
130:
131: /**
132: * Gets the java class type of this property. The class the the return type
133: * of the getter and type of the sole argument of the setter.
134: *
135: * @return the java Class type of this property
136: */
137: public Class getPropertyType() {
138: return propertyType;
139: }
140:
141: /**
142: * Gets the column name which this property will be persisted.
143: *
144: * @return the name of the column which this property will be persisted
145: */
146: public String getColumnName() {
147: return columnName;
148: }
149:
150: /**
151: * Gets the jdbc type of this property. The jdbc type is used to retrieve
152: * data from a result set and to set parameters in a prepared statement.
153: *
154: * @return the jdbc type of this property
155: */
156: public int getJDBCType() {
157: return jdbcType;
158: }
159:
160: /**
161: * Gets the sql type of this mapping. The sql type is the sql column data
162: * type, and is used in CREATE TABLE statements.
163: *
164: * @return the sql type String of this mapping
165: */
166: public String getSqlType() {
167: return sqlType;
168: }
169:
170: /**
171: * Should this field allow null values?
172: * @return true if this field will not allow a null value.
173: */
174: public boolean isNotNull() {
175: return notNull;
176: }
177:
178: /**
179: * Gets the getter method of this property. The getter method is used to
180: * retrieve the value of this property from the value class.
181: *
182: * @return the Method which gets the value of this property
183: */
184: public Method getGetter() {
185: return getter;
186: }
187:
188: /**
189: * Gets the setter method of this property. The setter method is used to
190: * set the value of this property in the value class.
191: *
192: * @return the Method which sets the value of this property
193: */
194: public Method getSetter() {
195: return setter;
196: }
197:
198: private static String toGetterName(String propertyName) {
199: return "get" + upCaseFirstCharacter(propertyName);
200: }
201:
202: private static String toSetterName(String propertyName) {
203: return "set" + upCaseFirstCharacter(propertyName);
204: }
205:
206: private static String upCaseFirstCharacter(String propertyName) {
207: return Character.toUpperCase(propertyName.charAt(0))
208: + propertyName.substring(1);
209: }
210: }
|