KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > generator > database > DBElementFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * DBElementFactory.java
26  *
27  * Created on Jan 14, 2003
28  */

29
30
31 package com.sun.jdo.spi.persistence.generator.database;
32
33 import org.netbeans.modules.dbschema.*;
34 import org.netbeans.modules.dbschema.jdbcimpl.*;
35 import org.netbeans.modules.dbschema.util.NameUtil;
36
37 /*
38  * This class assists in creating a database model (dbmodel)
39  * element-by-element.
40  */

41 class DBElementFactory {
42     /**
43      * String which indicates that schema was generated.
44      */

45     private final static String JavaDoc TAGLINE =
46         "generated schema version "; //NOI18N
47

48     /**
49      * Signature which identifies version of database generator. Updated
50      * each time the file is checked in to CVS.
51      */

52     private static final String JavaDoc SIGNATURE =
53         "$RCSfile: DBElementFactory.java,v $ $Revision: 1.3 $"; //NOI18N
54

55     /** Field type used if null is given in getColumnType. */
56     private static final String JavaDoc UNKNOWN_FIELD_TYPE = "java.lang.Long"; // NOI18N
57

58     /** Field type used for user-defined types in getColumnType. */
59     private static final String JavaDoc DEFAULT_FIELD_TYPE = "java.lang.Object"; // NOI18N
60

61     /**
62      * Disallow outside construction.
63      */

64     private DBElementFactory() {
65     }
66
67     /**
68      * Creates and returns a schema from give schema name
69      * @param schemaName A name for schema.
70      * @return Newly created schema element.
71      * @throws DBException
72      */

73     static SchemaElement createSchema(String JavaDoc schemaName) throws DBException {
74         SchemaElementImpl schemaImpl = new SchemaElementImpl();
75         SchemaElement schema = new SchemaElement(schemaImpl);
76         schema.setName(DBIdentifier.create(schemaName));
77         schema.setDatabaseProductVersion(TAGLINE + SIGNATURE);
78         return schema;
79     }
80
81     /**
82      * Create table and add to schema.
83      * @param schema Schema to which the table gets attached.
84      * @param tableName Name of the table without the schema name.
85      * @return TableElement for this table name
86      * @throws DBException
87      */

88     static TableElement createAndAttachTable(SchemaElement schema,
89             String JavaDoc tableName) throws DBException {
90
91         String JavaDoc fullName = NameUtil.getAbsoluteTableName(
92                 schema.getName().getName(), tableName);
93
94         TableElementImpl tableImpl = new TableElementImpl(tableName);
95         TableElement table = new TableElement(tableImpl, schema);
96         table.setName(DBIdentifier.create(fullName));
97         table.setTableOrView(true);
98         schema.addTable(table);
99         return table;
100     }
101
102     /**
103      * Create column and add to the table.
104      * @param columnName Name of the column to create.
105      * @param declaringTbl The declaring table to which column gets added.
106      * @return ColumnElement that represents the newly-added column.
107      * @throws DBException
108      */

109     static ColumnElement createAndAttachColumn(String JavaDoc columnName,
110             TableElement table, JDBCInfo ji) throws DBException {
111
112         // Create column id
113
String JavaDoc fullName = NameUtil.getAbsoluteMemberName(
114                 table.getName().getName(), columnName);
115         DBIdentifier columnId = DBIdentifier.create(columnName);
116
117         ColumnElementImpl columnImpl = new ColumnElementImpl();
118         ColumnElement column = new ColumnElement(columnImpl, table);
119         column.setName(columnId);
120         column.setType(ji.getJdbcType());
121         column.setNullable(ji.getNullable());
122         column.setPrecision(ji.getPrecision());
123         column.setScale(ji.getScale());
124         column.setLength(ji.getLength());
125
126         table.addColumn(column);
127
128         return column;
129     }
130
131     /**
132      * Create column pair from local column and reference column.
133      * @param column The local column.
134      * @param refColumn The reference column.
135      * @param declaringTbl The declaring table.
136      * @return ColumnPairElement that represents the column pair.
137      * @throws DBException
138      */

139     static ColumnPairElement createColumnPair(ColumnElement column,
140             ColumnElement refColumn, TableElement declaringTbl)
141             throws DBException {
142
143         ColumnPairElementImpl pairImpl = new ColumnPairElementImpl();
144         ColumnPairElement pair = new ColumnPairElement(
145                 pairImpl, column, refColumn, declaringTbl);
146         return pair;
147     }
148
149     /**
150      * Create primary key and add to table.
151      * @param table TableElement for adding primary key.
152      * @return UniqueKeyElement that represents the primary key.
153      * @throws DBException
154      */

155     static UniqueKeyElement createAndAttachPrimaryKey(TableElement table,
156             String JavaDoc pKeyName) throws DBException {
157
158         String JavaDoc tableName = table.getName().getName();
159         String JavaDoc fullName = NameUtil.getAbsoluteMemberName(tableName, pKeyName);
160
161         // create index for primary key
162
TableElementImpl tableImpl = (TableElementImpl)table.getElementImpl();
163         IndexElementImpl indexImpl =
164                 new IndexElementImpl(tableImpl, fullName, true);
165         IndexElement index = new IndexElement(indexImpl, table);
166         index.setUnique(true);
167
168         UniqueKeyElementImpl pKeyImpl = new UniqueKeyElementImpl();
169         UniqueKeyElement pKey = new UniqueKeyElement(pKeyImpl, table, index);
170         pKey.setName(DBIdentifier.create(fullName));
171         pKey.setPrimaryKey(true);
172         table.addKey(pKey);
173         table.addIndex(pKey.getAssociatedIndex());
174         return pKey;
175     }
176
177     /**
178      * Create foreign key between declaring table and reference table with
179      * relationship name.
180      * @param declaringTbl The declaring table.
181      * @param refTbl The referencing table.
182      * @param relationName The name for relationship.
183      * @param uniqueId Id that can be appened to relName to distinguish it
184      * from other relNames in the database.
185      * @return The foreign key object.
186      * @throws DBException
187      */

188     static ForeignKeyElement createAndAttachForeignKey(
189                 TableElement declaringTbl, TableElement refTbl, String JavaDoc keyName,
190                 MappingPolicy mappingPolicy, String JavaDoc uniqueId) throws DBException {
191
192         String JavaDoc fkeyName = mappingPolicy.getConstraintName(keyName, uniqueId);
193
194         TableElementImpl tableImpl =
195                 (TableElementImpl) declaringTbl. getElementImpl();
196         ForeignKeyElementImpl fkeyImpl =
197                 new ForeignKeyElementImpl(tableImpl, fkeyName);
198         ForeignKeyElement fkey = new ForeignKeyElement(fkeyImpl, declaringTbl);
199
200         UniqueKeyElement pk = refTbl.getPrimaryKey();
201         ColumnElement [] pkColumns = pk.getColumns();
202         String JavaDoc refTblName = refTbl.getName().getName();
203
204         // Each PK column contributes to the FK.
205
if (pkColumns != null) {
206             for (int i = 0; i < pkColumns.length; i++) {
207                 ColumnElement refColumn = pkColumns[i];
208
209                 // get name from mappingPolicy
210
String JavaDoc columnName =
211                         mappingPolicy.getConstraintColumnName(
212                                 refTblName, refColumn.getName().getName());
213
214                 // create column to ref primary key of ref table
215
JDBCInfo ji = new JDBCInfo(
216                         refColumn.getType(),
217                         refColumn.getPrecision(),
218                         refColumn.getScale(),
219                         refColumn.getLength(),
220                         true);
221
222                 // create column and add to declaring table
223
ColumnElement column = createAndAttachColumn(
224                         columnName, declaringTbl, ji);
225
226                 // create column pairs and add to foreign key
227
ColumnPairElement pair = createColumnPair(
228                         column, refColumn, declaringTbl);
229                 fkey.addColumnPair(pair);
230             }
231         }
232         declaringTbl.addKey(fkey);
233         return fkey;
234     }
235
236     /**
237      * Returns properties of a type of a field.
238      * @param fieldName Full name of a field, including its package and
239      * class. If null, UNKNOWN_FIELD_TYPE is used.
240      * @param fieldType Full name of a type, including its package and
241      * class.
242      * @param mappingPolicy Policy dictating properties.
243      * @return JDBCInfo which indicates properties of a type of field. If
244      * there is information specific to fieldName, that is returned, else
245      * information for fieldType is returned.
246      */

247     static JDBCInfo getColumnType(String JavaDoc fieldName, String JavaDoc fieldType,
248             MappingPolicy mappingPolicy) {
249
250         // fieldType will be null when we are handling an unknown PK
251
// situation. Use a Long in that case.
252
if (fieldType == null) {
253             fieldType = UNKNOWN_FIELD_TYPE;
254         }
255
256         JDBCInfo rc = mappingPolicy.getJDBCInfo(fieldName, fieldType);
257
258         // We won't find a JDBCInfo for user-defined types. Treat them as
259
// Object.
260
if (null == rc) {
261             // Treat as user-defined object type.
262
rc = mappingPolicy.getJDBCInfo(null, DEFAULT_FIELD_TYPE); // NOI18N
263
}
264         return rc;
265     }
266 }
267
Popular Tags