View Javadoc

1   package org.apache.torque.map;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache Turbine" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache Turbine", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  
57  import java.util.Iterator;
58  import java.util.HashMap;
59  import java.util.Hashtable;
60  import org.apache.torque.adapter.IDMethod;
61  import org.apache.torque.oid.IDBroker;
62  import org.apache.torque.oid.IdGenerator;
63  
64  /***
65   * DatabaseMap is used to model a database.
66   *
67   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
68   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
69   * @version $Id: DatabaseMap.java,v 1.7 2002/09/13 06:00:49 mpoeschl Exp $
70   */
71  public class DatabaseMap implements java.io.Serializable
72  {
73      /*** Name of the database. */
74      private String name;
75  
76      /*** Name of the tables in the database. */
77      private Hashtable tables;
78  
79      /***
80       * A special table used to generate primary keys for the other
81       * tables.
82       */
83      private TableMap idTable = null;
84  
85      /*** The IDBroker that goes with the idTable. */
86      private IDBroker idBroker = null;
87  
88      /*** The IdGenerators, keyed by type of idMethod. */
89      private HashMap idGenerators;
90  
91      /***
92       * Required by proxy. Not used.
93       */
94      public DatabaseMap()
95      {
96      }
97  
98      /***
99       * Constructor.
100      *
101      * @param name Name of the database.
102      * @param numberOfTables Number of tables in the database.
103      */
104     public DatabaseMap(String name, int numberOfTables)
105     {
106         this.name = name;
107         tables = new Hashtable((int) (1.25 * numberOfTables) + 1);
108         idGenerators = new HashMap(6);
109     }
110 
111     /***
112      * Constructor.
113      *
114      * @param name Name of the database.
115      */
116     public DatabaseMap(String name)
117     {
118         this.name = name;
119         tables = new Hashtable();
120         idGenerators = new HashMap(6);
121     }
122 
123     /***
124      * Does this database contain this specific table?
125      *
126      * @param table The TableMap representation of the table.
127      * @return True if the database contains the table.
128      */
129     public boolean containsTable(TableMap table)
130     {
131         return containsTable(table.getName());
132     }
133 
134     /***
135      * Does this database contain this specific table?
136      *
137      * @param name The String representation of the table.
138      * @return True if the database contains the table.
139      */
140     public boolean containsTable(String name)
141     {
142         if (name.indexOf('.') > 0)
143         {
144             name = name.substring(0, name.indexOf('.'));
145         }
146         return tables.containsKey(name);
147     }
148 
149     /***
150      * Get the ID table for this database.
151      *
152      * @return A TableMap.
153      */
154     public TableMap getIdTable()
155     {
156         return idTable;
157     }
158 
159     /***
160      * Get the IDBroker for this database.
161      *
162      * @return An IDBroker.
163      */
164     public IDBroker getIDBroker()
165     {
166         return idBroker;
167     }
168 
169     /***
170      * Get the name of this database.
171      *
172      * @return A String.
173      */
174     public String getName()
175     {
176         return name;
177     }
178 
179     /***
180      * Get a TableMap for the table by name.
181      *
182      * @param name Name of the table.
183      * @return A TableMap, null if the table was not found.
184      */
185     public TableMap getTable(String name)
186     {
187         return (TableMap) tables.get(name);
188     }
189 
190     /***
191      * Get a TableMap[] of all of the tables in the database.
192      *
193      * @return A TableMap[].
194      */
195     public TableMap[] getTables()
196     {
197         TableMap[] dbTables = new TableMap[tables.size()];
198         Iterator it = tables.values().iterator();
199         int i = 0;
200         while (it.hasNext())
201         {
202             dbTables[i++] = (TableMap) it.next() ;
203         }
204         return dbTables;
205     }
206 
207     /***
208      * Add a new table to the database by name.  It creates an empty
209      * TableMap that you need to populate.
210      *
211      * @param tableName The name of the table.
212      */
213     public void addTable(String tableName)
214     {
215         TableMap tmap = new TableMap(tableName, this);
216         tables.put(tableName, tmap);
217     }
218 
219     /***
220      * Add a new table to the database by name.  It creates an empty
221      * TableMap that you need to populate.
222      *
223      * @param tableName The name of the table.
224      * @param numberOfColumns The number of columns in the table.
225      */
226     public void addTable(String tableName, int numberOfColumns)
227     {
228         TableMap tmap = new TableMap(tableName, numberOfColumns, this);
229         tables.put(tableName, tmap);
230     }
231 
232     /***
233      * Add a new TableMap to the database.
234      *
235      * @param map The TableMap representation.
236      */
237     public void addTable(TableMap map)
238     {
239         tables.put(map.getName(), map);
240     }
241 
242     /***
243      * Set the ID table for this database.
244      *
245      * @param idTable The TableMap representation for the ID table.
246      */
247     public void setIdTable(TableMap idTable)
248     {
249         this.idTable = idTable;
250         addTable(idTable);
251         idBroker = new IDBroker(idTable);
252         addIdGenerator(IDMethod.ID_BROKER, idBroker);
253     }
254 
255     /***
256      * Set the ID table for this database.
257      *
258      * @param tableName The name for the ID table.
259      */
260     public void setIdTable(String tableName)
261     {
262         TableMap tmap = new TableMap(tableName, this);
263         setIdTable(tmap);
264     }
265 
266     /***
267      * Add a type of id generator for access by a TableMap.
268      *
269      * @param type a <code>String</code> value
270      * @param idGen an <code>IdGenerator</code> value
271      */
272     public void addIdGenerator(String type, IdGenerator idGen)
273     {
274         idGenerators.put(type, idGen);
275     }
276 
277     /***
278      * Get a type of id generator.  Valid values are listed in the
279      * {@link org.apache.torque.adapter.IDMethod} interface.
280      *
281      * @param type a <code>String</code> value
282      * @return an <code>IdGenerator</code> value
283      */
284     IdGenerator getIdGenerator(String type)
285     {
286         return (IdGenerator) idGenerators.get(type);
287     }
288 }