KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > map > DbAttribute


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.map;
58
59 import java.util.Iterator JavaDoc;
60
61 import org.objectstyle.cayenne.dba.TypesMapping;
62 import org.objectstyle.cayenne.map.event.AttributeEvent;
63 import org.objectstyle.cayenne.map.event.DbAttributeListener;
64 import org.objectstyle.cayenne.util.Util;
65 import org.objectstyle.cayenne.util.XMLEncoder;
66
67 /**
68  * A DbAttribute defines a descriptor for a single database table column.
69  *
70  * @author Misha Shengaout
71  * @author Andrei Adamchik
72  */

73 public class DbAttribute extends Attribute {
74     /**
75      * Defines JDBC type of the column.
76      */

77     protected int type = TypesMapping.NOT_DEFINED;
78
79     /**
80      * Defines whether the attribute allows nulls.
81      */

82     protected boolean mandatory;
83
84     /**
85      * Defines whether the attribute is a part of the table primary key.
86      */

87     protected boolean primaryKey;
88     
89     /**
90      * Defines whether this column value is generated by the database. Other
91      * terms for such columns are "auto-increment" or "identity".
92      *
93      * @since 1.2
94      */

95     protected boolean generated;
96
97     // The length of CHAR or VARCHAr or max num of digits for DECIMAL.
98
protected int maxLength = -1;
99
100     // The number of digits after period for DECIMAL.
101
protected int precision = -1;
102
103     public DbAttribute() {
104         super();
105     }
106
107     public DbAttribute(String JavaDoc name) {
108         super(name);
109     }
110
111     public DbAttribute(String JavaDoc name, int type, DbEntity entity) {
112         this.setName(name);
113         this.setType(type);
114         this.setEntity(entity);
115     }
116
117     /**
118      * Prints itself as XML to the provided XMLEncoder.
119      *
120      * @since 1.1
121      */

122     public void encodeAsXML(XMLEncoder encoder) {
123
124         encoder.print("<db-attribute name=\"");
125         encoder.print(Util.encodeXmlAttribute(getName()));
126         encoder.print('\"');
127
128         String JavaDoc type = TypesMapping.getSqlNameByType(getType());
129         if (type != null) {
130             encoder.print(" type=\"" + type + '\"');
131         }
132
133         // If attribute is part of primary key
134
if (isPrimaryKey()) {
135             encoder.print(" isPrimaryKey=\"true\"");
136         }
137
138         if (isMandatory()) {
139             encoder.print(" isMandatory=\"true\"");
140         }
141         
142         if (isGenerated()) {
143             encoder.print(" isGenerated=\"true\"");
144         }
145
146         if (getMaxLength() > 0) {
147             encoder.print(" length=\"");
148             encoder.print(getMaxLength());
149             encoder.print('\"');
150         }
151
152         if (getPrecision() > 0) {
153             encoder.print(" precision=\"");
154             encoder.print(getPrecision());
155             encoder.print('\"');
156         }
157
158         encoder.println("/>");
159     }
160
161     public String JavaDoc getAliasedName(String JavaDoc alias) {
162         return (alias != null) ? alias + '.' + this.getName() : this.getName();
163     }
164
165     /**
166      * Returns the SQL type of the column.
167      *
168      * @see java.sql.Types
169      */

170     public int getType() {
171         return type;
172     }
173
174     /**
175      * Sets the SQL type for the column.
176      *
177      * @see java.sql.Types
178      */

179     public void setType(int type) {
180         this.type = type;
181     }
182
183     public boolean isPrimaryKey() {
184         return primaryKey;
185     }
186
187     /**
188      * Returns <code>true</code> if the DB column represented by this
189      * attribute is a foreign key, referencing another table.
190      *
191      * @since 1.1
192      */

193     public boolean isForeignKey() {
194         String JavaDoc name = getName();
195         if (name == null) {
196             // won't be able to match joins...
197
return false;
198         }
199
200         Iterator JavaDoc relationships = getEntity().getRelationships().iterator();
201         while (relationships.hasNext()) {
202             DbRelationship relationship = (DbRelationship) relationships.next();
203             Iterator JavaDoc joins = relationship.getJoins().iterator();
204
205             while (joins.hasNext()) {
206                 DbJoin join = (DbJoin) joins.next();
207                 if (name.equals(join.getSourceName())) {
208                     DbAttribute target = join.getTarget();
209                     if (target != null && target.isPrimaryKey()) {
210                         return true;
211                     }
212                 }
213             }
214         }
215
216         return false;
217     }
218
219     /**
220      * Updates attribute "primaryKey" property.
221      */

222     public void setPrimaryKey(boolean primaryKey) {
223         if (this.primaryKey != primaryKey) {
224             this.primaryKey = primaryKey;
225
226             Entity e = this.getEntity();
227             if (e instanceof DbAttributeListener) {
228                 ((DbAttributeListener) e).dbAttributeChanged(new AttributeEvent(
229                         this,
230                         this,
231                         e));
232             }
233         }
234     }
235
236     public boolean isMandatory() {
237         return mandatory;
238     }
239
240     public void setMandatory(boolean mandatory) {
241         this.mandatory = mandatory;
242     }
243
244     /**
245      * Returns the length of database column described by this attribute.
246      */

247     public int getMaxLength() {
248         return maxLength;
249     }
250
251     /**
252      * Sets the length of character or binary type or max num of digits for DECIMAL.
253      */

254     public void setMaxLength(int maxLength) {
255         this.maxLength = maxLength;
256     }
257
258     /**
259      * Returns the number of digits after period for DECIMAL.
260      */

261     public int getPrecision() {
262         return precision;
263     }
264
265     /** Sets the number of digits after period for DECIMAL.*/
266     public void setPrecision(int precision) {
267         this.precision = precision;
268     }
269     
270     /**
271      * Returns true if this column value is generated by the database. Other
272      * terms for such columns are "auto-increment" or "identity".
273      *
274      * @since 1.2
275      */

276     public boolean isGenerated() {
277         return generated;
278     }
279     
280     /**
281      * Updates attribute "generated" property.
282      *
283      * @since 1.2
284      */

285     public void setGenerated(boolean generated) {
286         if (this.generated != generated) {
287             this.generated = generated;
288
289             Entity e = this.getEntity();
290             if (e instanceof DbAttributeListener) {
291                 ((DbAttributeListener) e).dbAttributeChanged(new AttributeEvent(
292                         this,
293                         this,
294                         e));
295             }
296         }
297     }
298 }
299
Popular Tags