001: package xdoclet.modules.ojb.model;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.*;
019:
020: import org.apache.commons.collections.SequencedHashMap;
021:
022: /**
023: * Definition of a table for the torque schema.
024: *
025: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
026: */
027: public class TableDef extends DefBase {
028: /** Map of columns keyed by their column name */
029: private SequencedHashMap _columns = new SequencedHashMap();
030: /** List of indices/uniques */
031: private ArrayList _indices = new ArrayList();
032: /** The foreignkeys to other tables */
033: private ArrayList _foreignkeys = new ArrayList();
034:
035: /**
036: * Creates a new table definition object.
037: *
038: * @param name The table name
039: */
040: public TableDef(String name) {
041: super (name);
042: }
043:
044: /**
045: * Returns an iterator of all columns of this table.
046: *
047: * @return The iterator
048: */
049: public Iterator getColumns() {
050: return _columns.values().iterator();
051: }
052:
053: /**
054: * Returns the column with the given name.
055: *
056: * @param name The name of the desired column
057: * @return The column or <code>null</code> if there is no column with that name
058: */
059: public ColumnDef getColumn(String name) {
060: return (ColumnDef) _columns.get(name);
061: }
062:
063: /**
064: * Adds a column to this table definition.
065: *
066: * @param columnDef The new column
067: */
068: public void addColumn(ColumnDef columnDef) {
069: columnDef.setOwner(this );
070: _columns.put(columnDef.getName(), columnDef);
071: }
072:
073: /**
074: * Adds an index to this table.
075: *
076: * @param The index def
077: */
078: public void addIndex(IndexDef indexDef) {
079: indexDef.setOwner(this );
080: _indices.add(indexDef);
081: }
082:
083: /**
084: * Returns the index of the given name.
085: *
086: * @param name The name of the index (null or empty string for the default index)
087: * @return The index def or <code>null</code> if it does not exist
088: */
089: public IndexDef getIndex(String name) {
090: String realName = (name == null ? "" : name);
091: IndexDef def = null;
092:
093: for (Iterator it = getIndices(); it.hasNext();) {
094: def = (IndexDef) it.next();
095: if (def.getName().equals(realName)) {
096: return def;
097: }
098: }
099: return null;
100: }
101:
102: /**
103: * Returns an iterator of all indices of this table.
104: *
105: * @return The indices
106: */
107: public Iterator getIndices() {
108: return _indices.iterator();
109: }
110:
111: /**
112: * Adds a foreignkey to this table.
113: *
114: * @param relationName The name of the relation represented by the foreignkey
115: * @param remoteTable The referenced table
116: * @param localColumns The local columns
117: * @param remoteColumns The remote columns
118: */
119: public void addForeignkey(String relationName, String remoteTable,
120: List localColumns, List remoteColumns) {
121: ForeignkeyDef foreignkeyDef = new ForeignkeyDef(relationName,
122: remoteTable);
123:
124: // the field arrays have the same length if we already checked the constraints
125: for (int idx = 0; idx < localColumns.size(); idx++) {
126: foreignkeyDef.addColumnPair((String) localColumns.get(idx),
127: (String) remoteColumns.get(idx));
128: }
129:
130: // we got to determine whether this foreignkey is already present
131: ForeignkeyDef def = null;
132:
133: for (Iterator it = getForeignkeys(); it.hasNext();) {
134: def = (ForeignkeyDef) it.next();
135: if (foreignkeyDef.equals(def)) {
136: return;
137: }
138: }
139: foreignkeyDef.setOwner(this );
140: _foreignkeys.add(foreignkeyDef);
141: }
142:
143: /**
144: * Determines whether this table has a foreignkey of the given name.
145: *
146: * @param name The name of the foreignkey
147: * @return <code>true</code> if there is a foreignkey of that name
148: */
149: public boolean hasForeignkey(String name) {
150: String realName = (name == null ? "" : name);
151: ForeignkeyDef def = null;
152:
153: for (Iterator it = getForeignkeys(); it.hasNext();) {
154: def = (ForeignkeyDef) it.next();
155: if (realName.equals(def.getName())) {
156: return true;
157: }
158: }
159: return false;
160: }
161:
162: /**
163: * Returns the foreignkey to the specified table.
164: *
165: * @param name The name of the foreignkey
166: * @param tableName The name of the referenced table
167: * @return The foreignkey def or <code>null</code> if it does not exist
168: */
169: public ForeignkeyDef getForeignkey(String name, String tableName) {
170: String realName = (name == null ? "" : name);
171: ForeignkeyDef def = null;
172:
173: for (Iterator it = getForeignkeys(); it.hasNext();) {
174: def = (ForeignkeyDef) it.next();
175: if (realName.equals(def.getName())
176: && def.getTableName().equals(tableName)) {
177: return def;
178: }
179: }
180: return null;
181: }
182:
183: /**
184: * Returns an iterator of all foreignkeys of this table.
185: *
186: * @return The foreignkeys
187: */
188: public Iterator getForeignkeys() {
189: return _foreignkeys.iterator();
190: }
191: }
|