package org.jboss.ejb.plugins.cmp.jdbc.metadata;
import java.lang.reflect.Method;
import org.jboss.deployment.DeploymentException;
import org.jboss.metadata.MetaData;
import org.w3c.dom.Element;
public final class JDBCValuePropertyMetaData {
private final String propertyName;
private final Class propertyType;
private final String columnName;
private final String sqlType;
private final int jdbcType;
private final boolean notNull;
private final Method getter;
private final Method setter;
public JDBCValuePropertyMetaData(Element element, Class classType)
throws DeploymentException {
propertyName = MetaData.getUniqueChildContent(element, "property-name");
String columnNameString =
MetaData.getOptionalChildContent(element, "column-name");
if(columnNameString != null) {
columnName = columnNameString;
} else {
columnName = propertyName;
}
Element notNullElement = MetaData.getOptionalChild(element, "not-null");
notNull = (notNullElement != null);
try {
getter = classType.getMethod(toGetterName(propertyName), new Class[0]);
} catch(Exception e) {
throw new DeploymentException("Unable to find getter for property " +
propertyName + " on dependent value class " +
classType.getName());
}
propertyType = getter.getReturnType();
try {
setter = classType.getMethod(
toSetterName(propertyName),
new Class[] { propertyType } );
} catch(Exception e) {
throw new DeploymentException("Unable to find setter for property " +
propertyName + " on dependent value class " +
classType.getName());
}
String jdbcString =
MetaData.getOptionalChildContent(element, "jdbc-type");
if(jdbcString != null) {
jdbcType = JDBCMappingMetaData.getJdbcTypeFromName(jdbcString);
sqlType = MetaData.getUniqueChildContent(element, "sql-type");
} else {
jdbcType = Integer.MIN_VALUE;
sqlType = null;
}
}
public String getPropertyName() {
return propertyName;
}
public Class getPropertyType() {
return propertyType;
}
public String getColumnName() {
return columnName;
}
public int getJDBCType() {
return jdbcType;
}
public String getSqlType() {
return sqlType;
}
public boolean isNotNull() {
return notNull;
}
public Method getGetter() {
return getter;
}
public Method getSetter() {
return setter;
}
private static String toGetterName(String propertyName) {
return "get" + upCaseFirstCharacter(propertyName);
}
private static String toSetterName(String propertyName) {
return "set" + upCaseFirstCharacter(propertyName);
}
private static String upCaseFirstCharacter(String propertyName) {
return Character.toUpperCase(propertyName.charAt(0)) +
propertyName.substring(1);
}
}