KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > tools > schemaframework > TableCreator


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
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
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 in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.tools.schemaframework;
23
24 import java.util.*;
25
26 /**
27  * <b>Purpose</b>: This class is reponsible for creating the tables defined in the project.
28  * A specific subclass of this class is created for each project. The specific table information
29  * is defined in the subclass.
30  *
31  * @since TopLink 2.0
32  * @author Peter Krogh
33  */

34 public class TableCreator {
35     protected Vector tableDefinitions;
36     protected String JavaDoc name;
37
38     public TableCreator() {
39         this(new Vector());
40     }
41
42     public TableCreator(Vector tableDefinitions) {
43         super();
44         this.tableDefinitions = tableDefinitions;
45     }
46
47     /**
48      * Add the table.
49      */

50     public void addTableDefinition(TableDefinition tableDefinition) {
51         tableDefinitions.addElement(tableDefinition);
52     }
53     
54     /**
55      * Add a set of tables.
56      */

57     public void addTableDefinitions(Collection tableDefs) {
58         tableDefinitions.addAll(tableDefs);
59     }
60
61
62     /**
63      * Create constraints.
64      */

65     public void createConstraints(oracle.toplink.essentials.sessions.DatabaseSession session) {
66         //CR2612669
67
createConstraints(session, new SchemaManager(session));
68     }
69
70     /**
71      * Create constraints.
72      */

73     public void createConstraints(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
74         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
75             schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
76         }
77
78         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
79             schemaManager.createConstraints((TableDefinition)enumtr.nextElement());
80         }
81     }
82
83     /**
84      * This creates the tables on the database.
85      * If the table already exists this will fail.
86      */

87     public void createTables(oracle.toplink.essentials.sessions.DatabaseSession session) {
88         //CR2612669
89
createTables(session, new SchemaManager(session));
90     }
91
92     /**
93      * This creates the tables on the database.
94      * If the table already exists this will fail.
95      */

96     public void createTables(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
97         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
98             schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
99         }
100
101         String JavaDoc sequenceTableName = getSequenceTableName(session);
102         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
103             // Must not create sequence table as done in createSequences.
104
TableDefinition table = (TableDefinition)enumtr.nextElement();
105             if (!table.getName().equals(sequenceTableName)) {
106                 schemaManager.createObject(table);
107             }
108         }
109
110         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
111             schemaManager.createConstraints((TableDefinition)enumtr.nextElement());
112         }
113
114         schemaManager.createSequences();
115     }
116
117     /**
118      * Drop the table constraints from the database.
119      */

120     public void dropConstraints(oracle.toplink.essentials.sessions.DatabaseSession session) {
121         //CR2612669
122
dropConstraints(session, new SchemaManager(session));
123     }
124
125     /**
126      * Drop the table constraints from the database.
127      */

128     public void dropConstraints(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
129         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
130             schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
131         }
132
133         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
134             try {
135                 schemaManager.dropConstraints((TableDefinition)enumtr.nextElement());
136             } catch (oracle.toplink.essentials.exceptions.DatabaseException dbE) {
137                 //ignore
138
}
139         }
140     }
141
142     /**
143      * Drop the tables from the database.
144      */

145     public void dropTables(oracle.toplink.essentials.sessions.DatabaseSession session) {
146         //CR2612669
147
dropTables(session, new SchemaManager(session));
148     }
149
150     /**
151      * Drop the tables from the database.
152      */

153     public void dropTables(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
154         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
155             schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
156         }
157
158         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
159             try {
160                 schemaManager.dropConstraints((TableDefinition)enumtr.nextElement());
161             } catch (oracle.toplink.essentials.exceptions.DatabaseException dbE) {
162                 //ignore
163
}
164         }
165
166         String JavaDoc sequenceTableName = getSequenceTableName(session);
167         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
168             // Must not create sequence table as done in createSequences.
169
TableDefinition table = (TableDefinition)enumtr.nextElement();
170             if (!table.getName().equals(sequenceTableName)) {
171                 schemaManager.dropObject(table);
172             }
173         }
174     }
175
176     /**
177      * Return the name.
178      */

179     public String JavaDoc getName() {
180         return name;
181     }
182
183     /**
184      * Return the tables.
185      */

186     public Vector getTableDefinitions() {
187         return tableDefinitions;
188     }
189
190     /**
191      * Recreate the tables on the database.
192      * This will drop the tables if they exist and recreate them.
193      */

194     public void replaceTables(oracle.toplink.essentials.sessions.DatabaseSession session) {
195         replaceTables(session, new SchemaManager(session));
196     }
197
198     /**
199      * Recreate the tables on the database.
200      * This will drop the tables if they exist and recreate them.
201      */

202     public void replaceTables(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
203         replaceTablesAndConstraints(schemaManager, session);
204
205         schemaManager.createSequences();
206
207     }
208     
209     /**
210      * Recreate the tables on the database.
211      * This will drop the tables if they exist and recreate them.
212      */

213     public void replaceTables(oracle.toplink.essentials.sessions.DatabaseSession session,
214             SchemaManager schemaManager, boolean keepSequenceTable) {
215         replaceTablesAndConstraints(schemaManager, session);
216
217         schemaManager.createOrReplaceSequences(keepSequenceTable, false);
218     }
219     
220
221     private void replaceTablesAndConstraints(final SchemaManager schemaManager,
222             final oracle.toplink.essentials.sessions.DatabaseSession session) {
223         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
224             schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
225         }
226         
227         // CR 3870467, do not log stack
228
boolean shouldLogExceptionStackTrace = session.getSessionLog().shouldLogExceptionStackTrace();
229         if (shouldLogExceptionStackTrace) {
230             session.getSessionLog().setShouldLogExceptionStackTrace(false);
231         }
232         try {
233             for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
234                 try {
235                     schemaManager.dropConstraints((TableDefinition)enumtr.nextElement());
236                 } catch (oracle.toplink.essentials.exceptions.DatabaseException dbE) {
237                     //ignore
238
}
239             }
240         } finally {
241             if (shouldLogExceptionStackTrace) {
242                 session.getSessionLog().setShouldLogExceptionStackTrace(true);
243             }
244         }
245
246         String JavaDoc sequenceTableName = getSequenceTableName(session);
247         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
248             // Must not create sequence table as done in createSequences.
249
TableDefinition table = (TableDefinition)enumtr.nextElement();
250             if (!table.getName().equals(sequenceTableName)) {
251                 schemaManager.replaceObject(table);
252             }
253         }
254
255         for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
256             schemaManager.createConstraints((TableDefinition)enumtr.nextElement());
257         }
258     }
259
260     /**
261      * Set the name.
262      */

263     public void setName(String JavaDoc name) {
264         this.name = name;
265     }
266
267     /**
268      * Set the tables.
269      */

270     public void setTableDefinitions(Vector tableDefinitions) {
271         this.tableDefinitions = tableDefinitions;
272     }
273
274     protected String JavaDoc getSequenceTableName(oracle.toplink.essentials.sessions.Session session) {
275         String JavaDoc sequenceTableName = null;
276         if (session.getProject().usesSequencing()) {
277             oracle.toplink.essentials.sequencing.Sequence sequence = session.getLogin().getDefaultSequence();
278             if (sequence instanceof oracle.toplink.essentials.sequencing.TableSequence) {
279                 sequenceTableName = ((oracle.toplink.essentials.sequencing.TableSequence)sequence).getTableName();
280             }
281         }
282         return sequenceTableName;
283     }
284 }
285
Popular Tags