KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.access.jdbc;
58
59 import java.io.Serializable JavaDoc;
60 import java.sql.ResultSetMetaData JavaDoc;
61 import java.sql.SQLException JavaDoc;
62
63 import org.apache.commons.lang.builder.EqualsBuilder;
64 import org.apache.commons.lang.builder.HashCodeBuilder;
65 import org.apache.commons.lang.builder.ToStringBuilder;
66 import org.objectstyle.cayenne.dba.TypesMapping;
67 import org.objectstyle.cayenne.map.DbAttribute;
68 import org.objectstyle.cayenne.map.ObjAttribute;
69 import org.objectstyle.cayenne.map.ProcedureParameter;
70
71 /**
72  * A descriptor of a ResultSet column.
73  *
74  * @since 1.1
75  * @author Andrei Adamchik
76  */

77 public class ColumnDescriptor implements Serializable JavaDoc {
78
79     protected String JavaDoc tableName;
80     protected String JavaDoc procedureName;
81
82     // identifies column in result set
83
protected String JavaDoc name;
84     protected String JavaDoc qualifiedColumnName;
85
86     // identifies column in a DataRow
87
protected String JavaDoc label;
88
89     /**
90      * @deprecated since 1.2
91      */

92     protected boolean primaryKey;
93
94     protected int jdbcType;
95     protected String JavaDoc javaClass;
96
97     /**
98      * Creates a ColumnDescriptor
99      */

100     public ColumnDescriptor() {
101     }
102     
103     /**
104      * Creates a column descriptor with user-specified parameters.
105      *
106      * @since 1.2
107      */

108     public ColumnDescriptor(String JavaDoc columnName, int jdbcType, String JavaDoc javaClass) {
109         this.name = columnName;
110         this.qualifiedColumnName = columnName;
111         this.label = columnName;
112         this.jdbcType = jdbcType;
113         this.javaClass = javaClass;
114     }
115
116
117     /**
118      * Creates a ColumnDescriptor from Cayenne ObjAttribute and DbAttribute.
119      *
120      * @deprecated since 1.2 use constructor with column alias parameter.
121      */

122     public ColumnDescriptor(ObjAttribute objAttribute, DbAttribute dbAttribute) {
123         this(objAttribute, dbAttribute, null);
124     }
125
126     /**
127      * Creates a ColumnDescriptor from Cayenne DbAttribute.
128      *
129      * @since 1.2
130      */

131     public ColumnDescriptor(DbAttribute attribute, String JavaDoc columnAlias) {
132         this.name = attribute.getName();
133         this.qualifiedColumnName = attribute.getAliasedName(columnAlias);
134         this.label = name;
135         this.jdbcType = attribute.getType();
136         this.primaryKey = attribute.isPrimaryKey();
137         this.javaClass = getDefaultJavaClass(attribute.getMaxLength(), attribute
138                 .getPrecision());
139
140         if (attribute.getEntity() != null) {
141             this.tableName = attribute.getEntity().getName();
142         }
143     }
144
145     /**
146      * @since 1.2
147      */

148     public ColumnDescriptor(ObjAttribute objAttribute, DbAttribute dbAttribute,
149             String JavaDoc columnAlias) {
150         this(dbAttribute, columnAlias);
151         this.label = objAttribute.getDbAttributePath();
152         this.javaClass = objAttribute.getType();
153     }
154
155     /**
156      * Creates a ColumnDescriptor from stored procedure parameter.
157      *
158      * @since 1.2
159      */

160     public ColumnDescriptor(ProcedureParameter parameter) {
161         this.name = parameter.getName();
162         this.qualifiedColumnName = name;
163         this.label = name;
164         this.jdbcType = parameter.getType();
165         this.javaClass = getDefaultJavaClass(parameter.getMaxLength(), parameter
166                 .getPrecision());
167
168         if (parameter.getProcedure() != null) {
169             this.procedureName = parameter.getProcedure().getName();
170         }
171     }
172
173     /**
174      * Creates a ColumnDescriptor using ResultSetMetaData.
175      *
176      * @since 1.2
177      */

178     public ColumnDescriptor(ResultSetMetaData JavaDoc metaData, int position) throws SQLException JavaDoc {
179         String JavaDoc name = metaData.getColumnLabel(position);
180         if (name == null || name.length() == 0) {
181             name = metaData.getColumnName(position);
182
183             if (name == null || name.length() == 0) {
184                 name = "column_" + position;
185             }
186         }
187
188         this.name = name;
189         this.qualifiedColumnName = name;
190         this.label = name;
191         this.jdbcType = metaData.getColumnType(position);
192         this.javaClass = getDefaultJavaClass(metaData.getColumnDisplaySize(position),
193                 metaData.getScale(position));
194     }
195
196     /**
197      * Returns true if another object is a ColumnDescriptor with the same name, name
198      * prefix, table and procedure names. Other fields are ignored in the equality test.
199      *
200      * @since 1.2
201      */

202     public boolean equals(Object JavaDoc o) {
203         if (!(o instanceof ColumnDescriptor)) {
204             return false;
205         }
206
207         ColumnDescriptor rhs = (ColumnDescriptor) o;
208         return new EqualsBuilder()
209                 .append(name, rhs.name)
210                 .append(qualifiedColumnName, rhs.qualifiedColumnName)
211                 .append(procedureName, rhs.procedureName)
212                 .append(label, rhs.label)
213                 .append(tableName, rhs.tableName)
214                 .isEquals();
215     }
216
217     /**
218      * @since 1.2
219      */

220     public int hashCode() {
221         return new HashCodeBuilder(23, 43)
222                 .append(name)
223                 .append(qualifiedColumnName)
224                 .append(procedureName)
225                 .append(tableName)
226                 .append(label)
227                 .toHashCode();
228     }
229
230     /**
231      * @since 1.2
232      */

233     public String JavaDoc toString() {
234         ToStringBuilder builder = new ToStringBuilder(this);
235         builder.append("namePrefix", getQualifiedColumnName());
236         builder.append("name", getName());
237         builder.append("label", getLabel());
238         builder.append("tableName", getTableName());
239         builder.append("procedureName", getProcedureName());
240         builder.append("javaClass", getJavaClass());
241         builder.append("jdbcType", getJdbcType());
242         return builder.toString();
243     }
244
245     /**
246      * Returns a default Java class for an internal JDBC type.
247      *
248      * @since 1.2
249      */

250     public String JavaDoc getDefaultJavaClass(int size, int scale) {
251         return TypesMapping.getJavaBySqlType(getJdbcType(), size, scale);
252     }
253
254     /**
255      * Returns "qualifiedColumnName" property.
256      *
257      * @since 1.2
258      */

259     public String JavaDoc getQualifiedColumnName() {
260         return qualifiedColumnName != null ? qualifiedColumnName : name;
261     }
262
263     public int getJdbcType() {
264         return jdbcType;
265     }
266
267     /**
268      * Retunrs column name. Name is an unqualified column name in a query.
269      */

270     public String JavaDoc getName() {
271         return name;
272     }
273
274     public void setJdbcType(int i) {
275         jdbcType = i;
276     }
277
278     public void setName(String JavaDoc name) {
279         this.name = name;
280     }
281
282     /**
283      * @deprecated since 1.2
284      */

285     public boolean isPrimaryKey() {
286         return primaryKey;
287     }
288
289     public String JavaDoc getJavaClass() {
290         return javaClass;
291     }
292
293     /**
294      * @deprecated since 1.2
295      */

296     public void setPrimaryKey(boolean b) {
297         primaryKey = b;
298     }
299
300     public void setJavaClass(String JavaDoc string) {
301         javaClass = string;
302     }
303
304     /**
305      * Returns the name of the parent table.
306      *
307      * @since 1.2
308      */

309     public String JavaDoc getTableName() {
310         return tableName;
311     }
312
313     /**
314      * @since 1.2
315      */

316     public void setTableName(String JavaDoc tableName) {
317         this.tableName = tableName;
318     }
319
320     /**
321      * Returns the name of the parent stored procedure.
322      *
323      * @since 1.2
324      */

325     public String JavaDoc getProcedureName() {
326         return procedureName;
327     }
328
329     /**
330      * @since 1.2
331      */

332     public void setProcedureName(String JavaDoc procedureName) {
333         this.procedureName = procedureName;
334     }
335
336     /**
337      * @since 1.2
338      */

339     public void setQualifiedColumnName(String JavaDoc namePrefix) {
340         this.qualifiedColumnName = namePrefix;
341     }
342
343     /**
344      * Returns "label" used in a DataRow for column value.
345      *
346      * @since 1.2
347      */

348     public String JavaDoc getLabel() {
349         return (label != null) ? label : getName();
350     }
351
352     /**
353      * @since 1.2
354      */

355     public void setLabel(String JavaDoc columnName) {
356         this.label = columnName;
357     }
358 }
Popular Tags