1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package pl.kernelpanic.dbmonster.schema;
48
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.HashMap;
52 import java.util.Iterator;
53 import java.util.List;
54 import java.util.Map;
55
56 import org.apache.commons.logging.Log;
57
58 import pl.kernelpanic.dbmonster.DBMonster;
59 import pl.kernelpanic.dbmonster.DBMonsterContext;
60 import pl.kernelpanic.dbmonster.ProgressMonitor;
61
62 /***
63 * Schema represents the look of the database, it is complete database
64 * structure including tables, primary keys, columns and generators.
65 *
66 * @author Piotr Maj <piotr.maj@kernelpanic.pl>
67 *
68 * @version $Id: Schema.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
69 */
70 public class Schema implements Comparable {
71
72 /***
73 * The name of the schema.
74 */
75 private String name = "default";
76
77 /***
78 * A map which holds tables.
79 */
80 private Map tables = new HashMap();
81
82 /***
83 * The directory in which the schema file is located.
84 */
85 private String home = ".";
86
87 /***
88 * DBMonsterContext.
89 */
90 private DBMonsterContext context = null;
91
92 /***
93 * Progress monitor.
94 */
95 private ProgressMonitor progressMonitor = null;
96
97 /***
98 * Adds a table to the schema.
99 *
100 * @param table a table to add.
101 *
102 * @throws SchemaException if a schema error occures
103 */
104 public void addTable(final Table table) throws SchemaException {
105 if (table == null) {
106 throw new SchemaException("No table.");
107 }
108 String tableName = table.getName();
109 if (tableName == null) {
110 throw new SchemaException("No table name.");
111 }
112 if (tables.containsKey(tableName)) {
113 throw new SchemaException(
114 "Table <" + tableName + "> already exists.");
115 }
116 table.setSchema(this);
117 tables.put(tableName, table);
118 }
119
120 /***
121 * Returns the name of the schema.
122 *
123 * @return the name of the schema.
124 */
125 public String getName() {
126 return name;
127 }
128
129 /***
130 * Sets the name of the schema.
131 *
132 * @param s the name of the schema.
133 */
134 public void setName(final String s) {
135 name = s;
136 }
137
138 /***
139 * Initializes the schema.
140 *
141 * @param ctx dbmonster context.
142 *
143 * @throws Exception if initialization fails
144 */
145 public void initialize(final DBMonsterContext ctx) throws Exception {
146 context = ctx;
147
148 Log log = (Log) context.getProperty(DBMonster.LOGGER_KEY);
149 progressMonitor =
150 (ProgressMonitor) context.getProperty(
151 DBMonster.PROGRESS_MONITOR_KEY);
152 if (log.isDebugEnabled()) {
153 log.debug("Initializing schema <" + name
154 + ">, home: <" + home + ">.");
155 }
156 if (progressMonitor != null) {
157 progressMonitor.setTableCount(tables.size());
158 }
159 Iterator it = tables.values().iterator();
160 while (it.hasNext()) {
161 ((Table) it.next()).initialize(ctx);
162 }
163
164 if (log.isDebugEnabled()) {
165 log.debug("Initialization of schema <" + name + "> finished.");
166 }
167 }
168
169 /***
170 * Generates the schema.
171 *
172 * @throws Exception if this schema could not be generated
173 */
174 public void generate() throws Exception {
175 Log log = (Log) context.getProperty(DBMonster.LOGGER_KEY);
176 if (log.isInfoEnabled()) {
177 log.info("Generating schema <" + name + ">.");
178 }
179 Iterator it = tables.values().iterator();
180 while (it.hasNext()) {
181 ((Table) it.next()).generate();
182 }
183 if (log.isInfoEnabled()) {
184 log.info("Generation of schema <" + name + "> finished.");
185 }
186 }
187
188 /***
189 * Returns the directory where the schema is located.
190 *
191 * @return schema home directory
192 */
193 public String getHome() {
194 return home;
195 }
196
197 /***
198 * Sets the home directory of the schema.
199 *
200 * @param dir the home directory of schema.
201 */
202 public void setHome(final String dir) {
203 home = dir;
204 }
205
206 /***
207 * Returns a table of given name.
208 *
209 * @param tableName the name of the table
210 *
211 * @return the table
212 */
213 public Table findTable(final String tableName) {
214 return (Table) tables.get(tableName);
215 }
216
217 /***
218 * Returns tables.
219 *
220 * @return list of tables;
221 */
222 public List getTables() {
223 Collection coll = tables.values();
224 List list = new ArrayList();
225 Iterator it = coll.iterator();
226 while (it.hasNext()) {
227 list.add(it.next());
228 }
229 return list;
230 }
231
232 /***
233 * Removes the table.
234 *
235 * @param t table to remove
236 */
237 public void removeTable(final Table t) {
238 Table table = (Table) tables.remove(t.getName());
239 table.setSchema(null);
240 }
241
242 /***
243 * @see java.lang.Comparable#compareTo(java.lang.Object)
244 */
245 public int compareTo(final Object o) {
246 Schema s = (Schema) o;
247 return getName().compareTo(s.getName());
248 }
249
250 /***
251 * Resets the schema.
252 */
253 public void reset() {
254 Iterator it = getTables().iterator();
255 while (it.hasNext()) {
256 Table t = (Table) it.next();
257 t.reset();
258 }
259 }
260 }