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 org.jboss.deployment.DeploymentException;
025: import org.jboss.metadata.MetaData;
026:
027: import org.w3c.dom.Element;
028:
029: /**
030: * This immutable class contains information about the an overriden field property.
031: *
032: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
033: * @version $Revision: 57209 $
034: */
035: public final class JDBCCMPFieldPropertyMetaData {
036: /**
037: * the cmp field on which this property is defined
038: */
039: private final JDBCCMPFieldMetaData cmpField;
040:
041: /**
042: * name of this property
043: */
044: private final String propertyName;
045:
046: /**
047: * the column name in the table
048: */
049: private final String columnName;
050:
051: /**
052: * the jdbc type (see java.sql.Types), used in PreparedStatement.setParameter
053: */
054: private final int jdbcType;
055:
056: /**
057: * the sql type, used for table creation.
058: */
059: private final String sqlType;
060:
061: /**
062: * Should null values not be allowed for this property.
063: */
064: private final boolean notNull;
065:
066: /**
067: * Constructs cmp field property meta data with the data contained in the
068: * property xml element from a jbosscmp-jdbc xml file.
069: *
070: * @param cmpField the JDBCCMPFieldMetaData on which this property is defined
071: * @param element the xml Element which contains the metadata about this
072: * field
073: * @throws DeploymentException if the xml element is not semantically correct
074: */
075: public JDBCCMPFieldPropertyMetaData(JDBCCMPFieldMetaData cmpField,
076: Element element) throws DeploymentException {
077:
078: this .cmpField = cmpField;
079:
080: // Property name
081: propertyName = MetaData.getUniqueChildContent(element,
082: "property-name");
083:
084: // Column name
085: String columnStr = MetaData.getOptionalChildContent(element,
086: "column-name");
087: if (columnStr != null) {
088: columnName = columnStr;
089: } else {
090: columnName = null;
091: }
092:
093: // jdbc type
094: String jdbcStr = MetaData.getOptionalChildContent(element,
095: "jdbc-type");
096: if (jdbcStr != null) {
097: jdbcType = JDBCMappingMetaData.getJdbcTypeFromName(jdbcStr);
098: sqlType = MetaData.getUniqueChildContent(element,
099: "sql-type");
100: } else {
101: jdbcType = Integer.MIN_VALUE;
102: sqlType = null;
103: }
104:
105: // notNull
106: notNull = (MetaData.getOptionalChild(element, "not-null") != null);
107: }
108:
109: /**
110: * Constructs cmp field property meta data based on the data contained in
111: * the defaultValues parameter but defined on the specified cmpField. This
112: * is effectly a copy constructory, except it can change the cmpField object
113: * on which the property is defined.
114: *
115: * @param cmpField the JDBCCMPFieldMetaData on which this property is defined
116: * @param defaultValues the defaultValues of this property
117: */
118: public JDBCCMPFieldPropertyMetaData(JDBCCMPFieldMetaData cmpField,
119: JDBCCMPFieldPropertyMetaData defaultValues) {
120:
121: this .cmpField = cmpField;
122:
123: // Property name
124: propertyName = defaultValues.propertyName;
125:
126: // Column name
127: columnName = defaultValues.columnName;
128:
129: // jdbc type
130: jdbcType = defaultValues.jdbcType;
131:
132: // sql type
133: sqlType = defaultValues.sqlType;
134:
135: // not-null
136: notNull = defaultValues.notNull;
137: }
138:
139: /**
140: * Gets the name of the property to be overriden.
141: */
142: public String getPropertyName() {
143: return propertyName;
144: }
145:
146: /**
147: * Gets the column name the property should use or null if the
148: * column name is not overriden.
149: */
150: public String getColumnName() {
151: return columnName;
152: }
153:
154: /**
155: * Gets the JDBC type the property should use or Integer.MIN_VALUE
156: * if not overriden.
157: */
158: public int getJDBCType() {
159: return jdbcType;
160: }
161:
162: /**
163: * Gets the SQL type the property should use or null
164: * if not overriden.
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: * Compares this JDBCCMPFieldPropertyMetaData against the specified object.
180: * Returns true if the objects are the same. Two
181: * JDBCCMPFieldPropertyMetaData are the same if they both have the same name
182: * and are defined on the same cmpField.
183: * @param o the reference object with which to compare
184: * @return true if this object is the same as the object argument; false
185: * otherwise
186: */
187: public boolean equals(Object o) {
188: if (o instanceof JDBCCMPFieldPropertyMetaData) {
189: JDBCCMPFieldPropertyMetaData cmpFieldProperty = (JDBCCMPFieldPropertyMetaData) o;
190: return propertyName.equals(cmpFieldProperty.propertyName)
191: && cmpField.equals(cmpFieldProperty.cmpField);
192: }
193: return false;
194: }
195:
196: /**
197: * Returns a hashcode for this JDBCCMPFieldPropertyMetaData. The hashcode is
198: * computed based on the hashCode of the declaring entity and the hashCode
199: * of the fieldName
200: * @return a hash code value for this object
201: */
202: public int hashCode() {
203: int result = 17;
204: result = 37 * result + cmpField.hashCode();
205: result = 37 * result + propertyName.hashCode();
206: return result;
207: }
208:
209: /**
210: * Returns a string describing this JDBCCMPFieldPropertyMetaData. The exact
211: * details of the representation are unspecified and subject to change, but
212: * the following may be regarded as typical:
213: *
214: * "[JDBCCMPFieldPropertyMetaData: propertyName=line1,
215: * [JDBCCMPFieldMetaData: fieldName=address,
216: * [JDBCEntityMetaData: entityName=UserEJB]]"
217: *
218: * @return a string representation of the object
219: */
220: public String toString() {
221: return "[JDBCCMPFieldPropertyMetaData : propertyName="
222: + propertyName + ", " + cmpField + "]";
223: }
224: }
|