001: /*
002: * Copyright 2006 Le Duc Bao, Ralf Joachim
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.castor.ddlgen.schemaobject;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.castor.ddlgen.DDLGenConfiguration;
026: import org.castor.ddlgen.DDLWriter;
027: import org.castor.ddlgen.GeneratorException;
028:
029: /**
030: * Abstract base class of all table implementations.
031: *
032: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
033: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
034: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
035: * @since 1.1
036: */
037: public abstract class Table extends AbstractSchemaObject {
038: //--------------------------------------------------------------------------
039:
040: /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons
041: * Logging </a> instance used for all logging. */
042: private static final Log LOG = LogFactory.getLog(Table.class);
043:
044: //--------------------------------------------------------------------------
045:
046: /** List of indices for this table. */
047: private List _indices = new ArrayList();
048:
049: /** List of foreign keys for this table. */
050: private List _foreignKeys = new ArrayList();
051:
052: /** List of field for this table. */
053: private List _fields = new ArrayList();
054:
055: /** Map of fields assoizated with their name. */
056: private Map _fieldMap = new HashMap();
057:
058: /** Key generator used for identities of this table. */
059: private KeyGenerator _keyGenerator;
060:
061: /** Primary key with identities of this table. */
062: private PrimaryKey _primaryKey;
063:
064: /** Schema this table belongs to. */
065: private Schema _schema;
066:
067: //--------------------------------------------------------------------------
068:
069: /**
070: * Add given index to list of indices.
071: *
072: * @param index Index to add to list of indices.
073: */
074: public final void addIndex(final Index index) {
075: _indices.add(index);
076: }
077:
078: /**
079: * Get number of indices.
080: *
081: * @return Number of indices.
082: */
083: public final int getIndexCount() {
084: return _indices.size();
085: }
086:
087: /**
088: * Get index at given index.
089: *
090: * @param index Index of index to return.
091: * @return Index at given index.
092: */
093: public final Index getIndex(final int index) {
094: return (Index) _indices.get(index);
095: }
096:
097: /**
098: * Add given foreign key to list of foreign keys.
099: *
100: * @param foreignKey Foreign key to add to list of foreign keys.
101: */
102: public final void addForeignKey(final ForeignKey foreignKey) {
103: _foreignKeys.add(foreignKey);
104: }
105:
106: /**
107: * Get number of foreign keys.
108: *
109: * @return Number of foreign keys.
110: */
111: public final int getForeignKeyCount() {
112: return _foreignKeys.size();
113: }
114:
115: /**
116: * Get foreign key at given index.
117: *
118: * @param index Index of foreign key to return.
119: * @return Foreign key at given index.
120: */
121: public final ForeignKey getForeignKey(final int index) {
122: return (ForeignKey) _foreignKeys.get(index);
123: }
124:
125: /**
126: * Add given field to list of fields.
127: *
128: * @param field Field to add to list of fields.
129: */
130: public final void addField(final Field field) {
131: _fields.add(field);
132: _fieldMap.put(field.getName(), field);
133: }
134:
135: /**
136: * Get number of fields.
137: *
138: * @return Number of fields.
139: */
140: public final int getFieldCount() {
141: return _fields.size();
142: }
143:
144: /**
145: * Get field at given index.
146: *
147: * @param index Index of field to return.
148: * @return Field at given index.
149: */
150: public final Field getField(final int index) {
151: return (Field) _fields.get(index);
152: }
153:
154: /**
155: * Get field with given name.
156: *
157: * @param name Name of field to return.
158: * @return Field with given name.
159: */
160: public final Field getField(final String name) {
161: return (Field) _fieldMap.get(name);
162: }
163:
164: /**
165: * Set key generator used for identities of this table.
166: *
167: * @param keyGenerator Key generator used for identities of this table.
168: */
169: public final void setKeyGenerator(final KeyGenerator keyGenerator) {
170: _keyGenerator = keyGenerator;
171: }
172:
173: /**
174: * Get key generator used for identities of this table.
175: *
176: * @return Key generator used for identities of this table.
177: */
178: public final KeyGenerator getKeyGenerator() {
179: return _keyGenerator;
180: }
181:
182: /**
183: * Set primary key with identities of this table.
184: *
185: * @param primaryKey Primary key with identities of this table.
186: */
187: public final void setPrimaryKey(final PrimaryKey primaryKey) {
188: _primaryKey = primaryKey;
189: }
190:
191: /**
192: * Get primary key with identities of this table.
193: *
194: * @return Primary key with identities of this table.
195: */
196: public final PrimaryKey getPrimaryKey() {
197: return _primaryKey;
198: }
199:
200: /**
201: * Set schema this table belongs to.
202: *
203: * @param schema Schema this table belongs to.
204: */
205: public final void setSchema(final Schema schema) {
206: _schema = schema;
207: }
208:
209: /**
210: * Get schema this table belongs to.
211: *
212: * @return Schema this table belongs to.
213: */
214: public final Schema getSchema() {
215: return _schema;
216: }
217:
218: //--------------------------------------------------------------------------
219:
220: /**
221: * Concatenate all fields names delimited by line separator.
222: *
223: * @param writer DDLWriter to write schema objects to.
224: * @throws GeneratorException If generation of the script failed or is not supported.
225: */
226: protected final void fields(final DDLWriter writer)
227: throws GeneratorException {
228: String delimiter = DDLGenConfiguration.DEFAULT_FIELD_DELIMITER;
229:
230: writer.indent();
231: for (int i = 0; i < getFieldCount(); i++) {
232: if (i > 0) {
233: writer.println(delimiter);
234: }
235: getField(i).toCreateDDL(writer);
236: }
237: writer.unindent();
238: }
239:
240: //--------------------------------------------------------------------------
241:
242: /**
243: * Check if given table can be merged with this one.
244: *
245: * @param table Table to check if it is able to be merged.
246: * @throws GeneratorException If tables cannot be merged.
247: */
248: public final void merge(final Table table)
249: throws GeneratorException {
250: if (table == null) {
251: String msg = "Table to merge is missing.";
252: LOG.error(msg);
253: throw new GeneratorException(msg);
254: }
255:
256: if (!equals(getName(), table.getName())) {
257: String msg = "Name of table differs from: " + getName();
258: LOG.error(msg);
259: throw new GeneratorException(msg);
260: }
261:
262: if (getFieldCount() != table.getFieldCount()) {
263: String msg = "Field count of table differs from: "
264: + getFieldCount();
265: LOG.error(msg);
266: throw new GeneratorException(msg);
267: }
268:
269: for (int i = 0; i < getFieldCount(); i++) {
270: Field field1 = getField(i);
271: Field field2 = null;
272: for (int j = 0; j < table.getFieldCount(); j++) {
273: field2 = table.getField(j);
274: if (!equals(field1.getName(), field2.getName())) {
275: break;
276: }
277: }
278: field1.merge(field2);
279: }
280:
281: for (int i = 0; i < getForeignKeyCount(); i++) {
282: ForeignKey fk1 = getForeignKey(i);
283: ForeignKey fk2 = null;
284: for (int j = 0; j < table.getForeignKeyCount(); j++) {
285: fk2 = table.getForeignKey(j);
286: if (!equals(fk1.getName(), fk2.getName())) {
287: break;
288: }
289: }
290: fk1.merge(fk2);
291: }
292:
293: if (_keyGenerator != null) {
294: _keyGenerator.merge(table.getKeyGenerator());
295: }
296: }
297:
298: //--------------------------------------------------------------------------
299:
300: /**
301: * {@inheritDoc}
302: */
303: public final boolean equals(final Object other) {
304: if (other == this ) {
305: return true;
306: }
307: if (other == null) {
308: return false;
309: }
310: if (other.getClass() != this .getClass()) {
311: return false;
312: }
313:
314: Table table = (Table) other;
315: return equals(getName(), table.getName())
316: && equals(_schema, table._schema)
317: && equals(_primaryKey, table._primaryKey)
318: && equals(_keyGenerator, table._keyGenerator)
319: && equals(_fields, table._fields)
320: && equals(_foreignKeys, table._foreignKeys)
321: && equals(_indices, table._indices);
322: }
323:
324: /**
325: * {@inheritDoc}
326: */
327: public final int hashCode() {
328: int hashCode = 0;
329: if (getName() != null) {
330: hashCode += getName().hashCode();
331: }
332: hashCode *= HASHFACTOR;
333: if (_schema != null) {
334: hashCode += _schema.hashCode();
335: }
336: hashCode *= HASHFACTOR;
337: if (_primaryKey != null) {
338: hashCode += _primaryKey.hashCode();
339: }
340: hashCode *= HASHFACTOR;
341: if (_keyGenerator != null) {
342: hashCode += _keyGenerator.hashCode();
343: }
344: hashCode *= HASHFACTOR;
345: hashCode += _fields.hashCode();
346: hashCode *= HASHFACTOR;
347: hashCode += _foreignKeys.hashCode();
348: hashCode *= HASHFACTOR;
349: hashCode += _indices.hashCode();
350: return hashCode;
351: }
352:
353: //--------------------------------------------------------------------------
354: }
|