KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > model > TableDef


1 package xdoclet.modules.ojb.model;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.*;
19
20 import org.apache.commons.collections.SequencedHashMap;
21
22 /**
23  * Definition of a table for the torque schema.
24  *
25  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
26  */

27 public class TableDef extends DefBase
28 {
29     /** Map of columns keyed by their column name */
30     private SequencedHashMap _columns = new SequencedHashMap();
31     /** List of indices/uniques */
32     private ArrayList _indices = new ArrayList();
33     /** The foreignkeys to other tables */
34     private ArrayList _foreignkeys = new ArrayList();
35
36     /**
37      * Creates a new table definition object.
38      *
39      * @param name The table name
40      */

41     public TableDef(String JavaDoc name)
42     {
43         super(name);
44     }
45
46     /**
47      * Returns an iterator of all columns of this table.
48      *
49      * @return The iterator
50      */

51     public Iterator getColumns()
52     {
53         return _columns.values().iterator();
54     }
55
56     /**
57      * Returns the column with the given name.
58      *
59      * @param name The name of the desired column
60      * @return The column or <code>null</code> if there is no column with that name
61      */

62     public ColumnDef getColumn(String JavaDoc name)
63     {
64         return (ColumnDef)_columns.get(name);
65     }
66
67     /**
68      * Adds a column to this table definition.
69      *
70      * @param columnDef The new column
71      */

72     public void addColumn(ColumnDef columnDef)
73     {
74         columnDef.setOwner(this);
75         _columns.put(columnDef.getName(), columnDef);
76     }
77
78     /**
79      * Adds an index to this table.
80      *
81      * @param The index def
82      */

83     public void addIndex(IndexDef indexDef)
84     {
85         indexDef.setOwner(this);
86         _indices.add(indexDef);
87     }
88
89     /**
90      * Returns the index of the given name.
91      *
92      * @param name The name of the index (null or empty string for the default index)
93      * @return The index def or <code>null</code> if it does not exist
94      */

95     public IndexDef getIndex(String JavaDoc name)
96     {
97         String JavaDoc realName = (name == null ? "" : name);
98         IndexDef def = null;
99
100         for (Iterator it = getIndices(); it.hasNext();)
101         {
102             def = (IndexDef)it.next();
103             if (def.getName().equals(realName))
104             {
105                 return def;
106             }
107         }
108         return null;
109     }
110
111     /**
112      * Returns an iterator of all indices of this table.
113      *
114      * @return The indices
115      */

116     public Iterator getIndices()
117     {
118         return _indices.iterator();
119     }
120
121     /**
122      * Adds a foreignkey to this table.
123      *
124      * @param relationName The name of the relation represented by the foreignkey
125      * @param remoteTable The referenced table
126      * @param localColumns The local columns
127      * @param remoteColumns The remote columns
128      */

129     public void addForeignkey(String JavaDoc relationName, String JavaDoc remoteTable, List localColumns, List remoteColumns)
130     {
131         ForeignkeyDef foreignkeyDef = new ForeignkeyDef(relationName, remoteTable);
132
133         // the field arrays have the same length if we already checked the constraints
134
for (int idx = 0; idx < localColumns.size(); idx++)
135         {
136             foreignkeyDef.addColumnPair((String JavaDoc)localColumns.get(idx),
137                                         (String JavaDoc)remoteColumns.get(idx));
138         }
139
140         // we got to determine whether this foreignkey is already present
141
ForeignkeyDef def = null;
142
143         for (Iterator it = getForeignkeys(); it.hasNext();)
144         {
145             def = (ForeignkeyDef)it.next();
146             if (foreignkeyDef.equals(def))
147             {
148                 return;
149             }
150         }
151         foreignkeyDef.setOwner(this);
152         _foreignkeys.add(foreignkeyDef);
153     }
154
155     /**
156      * Determines whether this table has a foreignkey of the given name.
157      *
158      * @param name The name of the foreignkey
159      * @return <code>true</code> if there is a foreignkey of that name
160      */

161     public boolean hasForeignkey(String JavaDoc name)
162     {
163         String JavaDoc realName = (name == null ? "" : name);
164         ForeignkeyDef def = null;
165
166         for (Iterator it = getForeignkeys(); it.hasNext();)
167         {
168             def = (ForeignkeyDef)it.next();
169             if (realName.equals(def.getName()))
170             {
171                 return true;
172             }
173         }
174         return false;
175     }
176
177     /**
178      * Returns the foreignkey to the specified table.
179      *
180      * @param name The name of the foreignkey
181      * @param tableName The name of the referenced table
182      * @return The foreignkey def or <code>null</code> if it does not exist
183      */

184     public ForeignkeyDef getForeignkey(String JavaDoc name, String JavaDoc tableName)
185     {
186         String JavaDoc realName = (name == null ? "" : name);
187         ForeignkeyDef def = null;
188
189         for (Iterator it = getForeignkeys(); it.hasNext();)
190         {
191             def = (ForeignkeyDef)it.next();
192             if (realName.equals(def.getName()) &&
193                 def.getTableName().equals(tableName))
194             {
195                 return def;
196             }
197         }
198         return null;
199     }
200
201     /**
202      * Returns an iterator of all foreignkeys of this table.
203      *
204      * @return The foreignkeys
205      */

206     public Iterator getForeignkeys()
207     {
208         return _foreignkeys.iterator();
209     }
210 }
211
Popular Tags