KickJava   Java API By Example, From Geeks To Geeks.

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


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.map;
21
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.cayenne.dba.TypesMapping;
25 import org.apache.cayenne.map.event.AttributeEvent;
26 import org.apache.cayenne.map.event.DbAttributeListener;
27 import org.apache.cayenne.util.Util;
28 import org.apache.cayenne.util.XMLEncoder;
29
30 /**
31  * A DbAttribute defines a descriptor for a single database table column.
32  *
33  * @author Misha Shengaout
34  * @author Andrus Adamchik
35  */

36 public class DbAttribute extends Attribute {
37
38     /**
39      * Defines JDBC type of the column.
40      */

41     protected int type = TypesMapping.NOT_DEFINED;
42
43     /**
44      * Defines whether the attribute allows nulls.
45      */

46     protected boolean mandatory;
47
48     /**
49      * Defines whether the attribute is a part of the table primary key.
50      */

51     protected boolean primaryKey;
52
53     /**
54      * Defines whether this column value is generated by the database. Other terms for
55      * such columns are "auto-increment" or "identity".
56      *
57      * @since 1.2
58      */

59     protected boolean generated;
60
61     // The length of CHAR or VARCHAr or max num of digits for DECIMAL.
62
protected int maxLength = -1;
63
64     /**
65      * @since 3.0
66      */

67     protected int scale = -1;
68
69     /**
70      * @since 3.0
71      */

72     // must call it 'attributePrecison' as 'precision' in 1.2 really meant 'scale'
73
protected int attributePrecision = -1;
74
75     public DbAttribute() {
76         super();
77     }
78
79     public DbAttribute(String JavaDoc name) {
80         super(name);
81     }
82
83     public DbAttribute(String JavaDoc name, int type, DbEntity entity) {
84         this.setName(name);
85         this.setType(type);
86         this.setEntity(entity);
87     }
88
89     /**
90      * Prints itself as XML to the provided XMLEncoder.
91      *
92      * @since 1.1
93      */

94     public void encodeAsXML(XMLEncoder encoder) {
95
96         encoder.print("<db-attribute name=\"");
97         encoder.print(Util.encodeXmlAttribute(getName()));
98         encoder.print('\"');
99
100         String JavaDoc type = TypesMapping.getSqlNameByType(getType());
101         if (type != null) {
102             encoder.print(" type=\"" + type + '\"');
103         }
104
105         if (isPrimaryKey()) {
106             encoder.print(" isPrimaryKey=\"true\"");
107
108             // only allow generated if an attribute is a PK.
109
if (isGenerated()) {
110                 encoder.print(" isGenerated=\"true\"");
111             }
112         }
113
114         if (isMandatory()) {
115             encoder.print(" isMandatory=\"true\"");
116         }
117
118         if (getMaxLength() > 0) {
119             encoder.print(" length=\"");
120             encoder.print(getMaxLength());
121             encoder.print('\"');
122         }
123
124         if (getScale() > 0) {
125             encoder.print(" scale=\"");
126             encoder.print(getScale());
127             encoder.print('\"');
128         }
129
130         if (getAttributePrecision() > 0) {
131             encoder.print(" attributePrecision=\"");
132             encoder.print(getAttributePrecision());
133             encoder.print('\"');
134         }
135
136         encoder.println("/>");
137     }
138
139     public String JavaDoc getAliasedName(String JavaDoc alias) {
140         return (alias != null) ? alias + '.' + this.getName() : this.getName();
141     }
142
143     /**
144      * Returns the SQL type of the column.
145      *
146      * @see java.sql.Types
147      */

148     public int getType() {
149         return type;
150     }
151
152     /**
153      * Sets the SQL type for the column.
154      *
155      * @see java.sql.Types
156      */

157     public void setType(int type) {
158         this.type = type;
159     }
160
161     public boolean isPrimaryKey() {
162         return primaryKey;
163     }
164
165     /**
166      * Returns <code>true</code> if the DB column represented by this attribute is a
167      * foreign key, referencing another table.
168      *
169      * @since 1.1
170      */

171     public boolean isForeignKey() {
172         String JavaDoc name = getName();
173         if (name == null) {
174             // won't be able to match joins...
175
return false;
176         }
177
178         Iterator JavaDoc relationships = getEntity().getRelationships().iterator();
179         while (relationships.hasNext()) {
180             DbRelationship relationship = (DbRelationship) relationships.next();
181             Iterator JavaDoc joins = relationship.getJoins().iterator();
182
183             while (joins.hasNext()) {
184                 DbJoin join = (DbJoin) joins.next();
185                 if (name.equals(join.getSourceName())) {
186                     DbAttribute target = join.getTarget();
187                     if (target != null && target.isPrimaryKey()) {
188                         return true;
189                     }
190                 }
191             }
192         }
193
194         return false;
195     }
196
197     /**
198      * Updates attribute "primaryKey" property.
199      */

200     public void setPrimaryKey(boolean primaryKey) {
201         if (this.primaryKey != primaryKey) {
202             this.primaryKey = primaryKey;
203
204             Entity e = this.getEntity();
205             if (e instanceof DbAttributeListener) {
206                 ((DbAttributeListener) e).dbAttributeChanged(new AttributeEvent(
207                         this,
208                         this,
209                         e));
210             }
211         }
212     }
213
214     public boolean isMandatory() {
215         return mandatory;
216     }
217
218     public void setMandatory(boolean mandatory) {
219         this.mandatory = mandatory;
220     }
221
222     /**
223      * Returns the length of database column described by this attribute.
224      */

225     public int getMaxLength() {
226         return maxLength;
227     }
228
229     /**
230      * Sets the length of character or binary type or max num of digits for DECIMAL.
231      */

232     public void setMaxLength(int maxLength) {
233         this.maxLength = maxLength;
234     }
235
236     /**
237      * Returns the number of digits after period for DECIMAL.
238      *
239      * @deprecated since 3.0 as this property really referred to 'scale'. Use
240      * {@link #getScale()} instead.
241      */

242     public int getPrecision() {
243         return getScale();
244     }
245
246     /**
247      * @deprecated since 3.0 as this property really referred to 'scale'. Use
248      * {@link #setScale(int)} instead.
249      */

250     public void setPrecision(int i) {
251         setScale(i);
252     }
253
254     /**
255      * Returns true if this column value is generated by the database. Other terms for
256      * such columns are "auto-increment" or "identity".
257      *
258      * @since 1.2
259      */

260     public boolean isGenerated() {
261         return generated;
262     }
263
264     /**
265      * Updates attribute "generated" property.
266      *
267      * @since 1.2
268      */

269     public void setGenerated(boolean generated) {
270         if (this.generated != generated) {
271             this.generated = generated;
272
273             Entity e = this.getEntity();
274             if (e instanceof DbAttributeListener) {
275                 ((DbAttributeListener) e).dbAttributeChanged(new AttributeEvent(
276                         this,
277                         this,
278                         e));
279             }
280         }
281     }
282
283     /**
284      * @since 3.0
285      */

286     public int getAttributePrecision() {
287         return attributePrecision;
288     }
289
290     /**
291      * @since 3.0
292      */

293     public void setAttributePrecision(int attributePrecision) {
294         this.attributePrecision = attributePrecision;
295     }
296
297     /**
298      * Returns the number of digits after period for decimal attributes. Returns "-1" if
299      * not set.
300      *
301      * @since 3.0
302      */

303     public int getScale() {
304         return scale;
305     }
306
307     /**
308      * @since 3.0
309      */

310     public void setScale(int scale) {
311         this.scale = scale;
312     }
313 }
314
Popular Tags