KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > revolt > support > ScriptBuilder


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.revolt.support;
25
26 import java.util.Iterator JavaDoc;
27
28 import org.riotfamily.revolt.Dialect;
29 import org.riotfamily.revolt.Script;
30 import org.riotfamily.revolt.definition.Database;
31 import org.riotfamily.revolt.definition.ForeignKey;
32 import org.riotfamily.revolt.definition.Index;
33 import org.riotfamily.revolt.definition.Table;
34 import org.riotfamily.revolt.definition.UniqueConstraint;
35
36 /**
37  * @author Felix Gnass [fgnass at neteye dot de]
38  * @since 6.4
39  */

40 public class ScriptBuilder {
41
42     private Database model;
43     
44     private Dialect dialect;
45     
46     private Script script;
47     
48     public ScriptBuilder(Database model, Dialect dialect) {
49         this.model = model;
50         this.dialect = dialect;
51     }
52     
53     public Script buildScript() {
54         script = new Script();
55         Iterator JavaDoc it = model.getSequences().iterator();
56         while (it.hasNext()) {
57             String JavaDoc sequence = (String JavaDoc) it.next();
58             script.append(dialect.createAutoIncrementSequence(sequence));
59         }
60         it = model.getTables().iterator();
61         while (it.hasNext()) {
62             Table table = (Table) it.next();
63             script.append(dialect.createTable(table));
64             createIndices(table);
65             createUniqueConstraints(table);
66         }
67         it = model.getTables().iterator();
68         while (it.hasNext()) {
69             Table table = (Table) it.next();
70             createForeignKeys(table);
71         }
72         return script;
73     }
74     
75     private void createIndices(Table table) {
76         Iterator JavaDoc it = table.getIndices().iterator();
77         while (it.hasNext()) {
78             Index index = (Index) it.next();
79             script.append(dialect.createIndex(table.getName(), index));
80         }
81     }
82     
83     private void createUniqueConstraints(Table table) {
84         Iterator JavaDoc it = table.getUniqueConstraints().iterator();
85         while (it.hasNext()) {
86             UniqueConstraint constraint = (UniqueConstraint) it.next();
87             script.append(dialect.addUniqueConstraint(table.getName(), constraint));
88         }
89     }
90     
91     private void createForeignKeys(Table table) {
92         Iterator JavaDoc it = table.getForeignKeys().iterator();
93         while (it.hasNext()) {
94             ForeignKey fk = (ForeignKey) it.next();
95             script.append(dialect.addForeignKey(table.getName(), fk));
96         }
97     }
98 }
99
Popular Tags