KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > jdbc > ColumnDescriptor


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.jdbc;
21
22 import java.io.Serializable JavaDoc;
23 import java.sql.ResultSetMetaData JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26 import org.apache.cayenne.dba.TypesMapping;
27 import org.apache.cayenne.map.DbAttribute;
28 import org.apache.cayenne.map.ObjAttribute;
29 import org.apache.cayenne.map.ProcedureParameter;
30 import org.apache.commons.lang.builder.EqualsBuilder;
31 import org.apache.commons.lang.builder.HashCodeBuilder;
32 import org.apache.commons.lang.builder.ToStringBuilder;
33
34 /**
35  * A descriptor of a ResultSet column.
36  *
37  * @since 1.1
38  * @author Andrus Adamchik
39  */

40 public class ColumnDescriptor implements Serializable JavaDoc {
41
42     protected String JavaDoc tableName;
43     protected String JavaDoc procedureName;
44
45     // identifies column in result set
46
protected String JavaDoc name;
47     protected String JavaDoc qualifiedColumnName;
48
49     // identifies column in a DataRow
50
protected String JavaDoc label;
51
52     protected int jdbcType;
53     protected String JavaDoc javaClass;
54
55     /**
56      * Creates a ColumnDescriptor
57      */

58     public ColumnDescriptor() {
59     }
60
61     /**
62      * Creates a column descriptor with user-specified parameters.
63      *
64      * @since 1.2
65      */

66     public ColumnDescriptor(String JavaDoc columnName, int jdbcType, String JavaDoc javaClass) {
67         this.name = columnName;
68         this.qualifiedColumnName = columnName;
69         this.label = columnName;
70         this.jdbcType = jdbcType;
71         this.javaClass = javaClass;
72     }
73
74     /**
75      * Creates a ColumnDescriptor from Cayenne DbAttribute.
76      *
77      * @since 1.2
78      */

79     public ColumnDescriptor(DbAttribute attribute, String JavaDoc columnAlias) {
80         this.name = attribute.getName();
81         this.qualifiedColumnName = attribute.getAliasedName(columnAlias);
82         this.label = name;
83         this.jdbcType = attribute.getType();
84         this.javaClass = getDefaultJavaClass(attribute.getMaxLength(), attribute
85                 .getScale());
86
87         if (attribute.getEntity() != null) {
88             this.tableName = attribute.getEntity().getName();
89         }
90     }
91
92     /**
93      * @since 1.2
94      */

95     public ColumnDescriptor(ObjAttribute objAttribute, DbAttribute dbAttribute,
96             String JavaDoc columnAlias) {
97         this(dbAttribute, columnAlias);
98         this.label = objAttribute.getDbAttributePath();
99         this.javaClass = objAttribute.getType();
100     }
101
102     /**
103      * Creates a ColumnDescriptor from stored procedure parameter.
104      *
105      * @since 1.2
106      */

107     public ColumnDescriptor(ProcedureParameter parameter) {
108         this.name = parameter.getName();
109         this.qualifiedColumnName = name;
110         this.label = name;
111         this.jdbcType = parameter.getType();
112         this.javaClass = getDefaultJavaClass(parameter.getMaxLength(), parameter
113                 .getPrecision());
114
115         if (parameter.getProcedure() != null) {
116             this.procedureName = parameter.getProcedure().getName();
117         }
118     }
119
120     /**
121      * Creates a ColumnDescriptor using ResultSetMetaData.
122      *
123      * @since 1.2
124      */

125     public ColumnDescriptor(ResultSetMetaData JavaDoc metaData, int position) throws SQLException JavaDoc {
126         String JavaDoc name = metaData.getColumnLabel(position);
127         if (name == null || name.length() == 0) {
128             name = metaData.getColumnName(position);
129
130             if (name == null || name.length() == 0) {
131                 name = "column_" + position;
132             }
133         }
134
135         this.name = name;
136         this.qualifiedColumnName = name;
137         this.label = name;
138         this.jdbcType = metaData.getColumnType(position);
139         this.javaClass = metaData.getColumnClassName(position);
140     }
141
142     /**
143      * Returns true if another object is a ColumnDescriptor with the same name, name
144      * prefix, table and procedure names. Other fields are ignored in the equality test.
145      *
146      * @since 1.2
147      */

148     public boolean equals(Object JavaDoc o) {
149         if (!(o instanceof ColumnDescriptor)) {
150             return false;
151         }
152
153         ColumnDescriptor rhs = (ColumnDescriptor) o;
154         return new EqualsBuilder().append(name, rhs.name).append(
155                 qualifiedColumnName,
156                 rhs.qualifiedColumnName).append(procedureName, rhs.procedureName).append(
157                 label,
158                 rhs.label).append(tableName, rhs.tableName).isEquals();
159     }
160
161     /**
162      * @since 1.2
163      */

164     public int hashCode() {
165         return new HashCodeBuilder(23, 43)
166                 .append(name)
167                 .append(qualifiedColumnName)
168                 .append(procedureName)
169                 .append(tableName)
170                 .append(label)
171                 .toHashCode();
172     }
173
174     /**
175      * @since 1.2
176      */

177     public String JavaDoc toString() {
178         ToStringBuilder builder = new ToStringBuilder(this);
179         builder.append("namePrefix", getQualifiedColumnName());
180         builder.append("name", getName());
181         builder.append("label", getLabel());
182         builder.append("tableName", getTableName());
183         builder.append("procedureName", getProcedureName());
184         builder.append("javaClass", getJavaClass());
185         builder.append("jdbcType", getJdbcType());
186         return builder.toString();
187     }
188
189     /**
190      * Returns a default Java class for an internal JDBC type.
191      *
192      * @since 1.2
193      */

194     public String JavaDoc getDefaultJavaClass(int size, int scale) {
195         return TypesMapping.getJavaBySqlType(getJdbcType(), size, scale);
196     }
197
198     /**
199      * Returns "qualifiedColumnName" property.
200      *
201      * @since 1.2
202      */

203     public String JavaDoc getQualifiedColumnName() {
204         return qualifiedColumnName != null ? qualifiedColumnName : name;
205     }
206
207     public int getJdbcType() {
208         return jdbcType;
209     }
210
211     /**
212      * Retunrs column name. Name is an unqualified column name in a query.
213      */

214     public String JavaDoc getName() {
215         return name;
216     }
217
218     public void setJdbcType(int i) {
219         jdbcType = i;
220     }
221
222     public void setName(String JavaDoc name) {
223         this.name = name;
224     }
225
226     public String JavaDoc getJavaClass() {
227         return javaClass;
228     }
229
230     public void setJavaClass(String JavaDoc string) {
231         javaClass = string;
232     }
233
234     /**
235      * Returns the name of the parent table.
236      *
237      * @since 1.2
238      */

239     public String JavaDoc getTableName() {
240         return tableName;
241     }
242
243     /**
244      * @since 1.2
245      */

246     public void setTableName(String JavaDoc tableName) {
247         this.tableName = tableName;
248     }
249
250     /**
251      * Returns the name of the parent stored procedure.
252      *
253      * @since 1.2
254      */

255     public String JavaDoc getProcedureName() {
256         return procedureName;
257     }
258
259     /**
260      * @since 1.2
261      */

262     public void setProcedureName(String JavaDoc procedureName) {
263         this.procedureName = procedureName;
264     }
265
266     /**
267      * @since 1.2
268      */

269     public void setQualifiedColumnName(String JavaDoc namePrefix) {
270         this.qualifiedColumnName = namePrefix;
271     }
272
273     /**
274      * Returns "label" used in a DataRow for column value.
275      *
276      * @since 1.2
277      */

278     public String JavaDoc getLabel() {
279         return (label != null) ? label : getName();
280     }
281
282     /**
283      * @since 1.2
284      */

285     public void setLabel(String JavaDoc columnName) {
286         this.label = columnName;
287     }
288 }
289
Popular Tags