View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
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 }