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 org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.castor.ddlgen.DDLWriter;
021: import org.castor.ddlgen.GeneratorException;
022: import org.castor.ddlgen.typeinfo.TypeInfo;
023:
024: /**
025: * Abstract base class of all field implementations.
026: *
027: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
028: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
029: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
030: * @since 1.1
031: */
032: public abstract class Field extends AbstractSchemaObject {
033: //--------------------------------------------------------------------------
034:
035: /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons
036: * Logging </a> instance used for all logging. */
037: private static final Log LOG = LogFactory.getLog(Field.class);
038:
039: //--------------------------------------------------------------------------
040:
041: /** Key generator for this field. */
042: private KeyGenerator _keyGenerator;
043:
044: /** Is this field part of the identity? */
045: private boolean _isIdentity;
046:
047: /** Is this field a required one? */
048: private boolean _isRequired;
049:
050: /** Type information of this field. */
051: private TypeInfo _type;
052:
053: /** Table which contains this field. */
054: private Table _table;
055:
056: //--------------------------------------------------------------------------
057:
058: /**
059: * Set key generator for this field. <code>null</code> if the field has no key
060: * generator.
061: *
062: * @param keyGenerator Key generator for this field.
063: */
064: public final void setKeyGenerator(final KeyGenerator keyGenerator) {
065: _keyGenerator = keyGenerator;
066: }
067:
068: /**
069: * Get key generator for this field.
070: *
071: * @return Key generator for this field.
072: */
073: public final KeyGenerator getKeyGenerator() {
074: return _keyGenerator;
075: }
076:
077: /**
078: * Set if this field is part of the identity?
079: *
080: * @param isIdentity <code>true</code> if the field is part of the identity,
081: * <code>true</code> otherwise.
082: */
083: public final void setIdentity(final boolean isIdentity) {
084: _isIdentity = isIdentity;
085: }
086:
087: /**
088: * Get if this field is part of the identity?
089: *
090: * @return <code>true</code> if the field is part of the identity,
091: * <code>true</code> otherwise.
092: */
093: public final boolean isIdentity() {
094: return _isIdentity;
095: }
096:
097: /**
098: * Set if this field is a required one?
099: *
100: * @param isRequired <code>true</code> if the field is required, <code>true</code>
101: * otherwise.
102: */
103: public final void setRequired(final boolean isRequired) {
104: _isRequired = isRequired;
105: }
106:
107: /**
108: * Get if this field is a required one?
109: *
110: * @return <code>true</code> if the field is required, <code>true</code>
111: * otherwise.
112: */
113: public final boolean isRequired() {
114: return _isRequired;
115: }
116:
117: /**
118: * Set type information of this field.
119: *
120: * @param type Type information of this field.
121: */
122: public final void setType(final TypeInfo type) {
123: _type = type;
124: }
125:
126: /**
127: * Get type information of this field.
128: *
129: * @return Type information of this field.
130: */
131: public final TypeInfo getType() {
132: return _type;
133: }
134:
135: /**
136: * Set table which contains this field.
137: *
138: * @param table Table which contains this field.
139: */
140: public final void setTable(final Table table) {
141: _table = table;
142: }
143:
144: /**
145: * Get table which contains this field.
146: *
147: * @return Table which contains this field.
148: */
149: public final Table getTable() {
150: return _table;
151: }
152:
153: //--------------------------------------------------------------------------
154:
155: /**
156: * Get length parameter from mapping of sql field.
157: * <br/>
158: * Returns <code>null</code> as it is not supported yet.
159: *
160: * @return length Length parameter from mapping of sql field.
161: */
162: public final Integer getLength() {
163: return null;
164: }
165:
166: /**
167: * Get precision parameter from mapping of sql field.
168: * <br/>
169: * Returns <code>null</code> as it is not supported yet.
170: *
171: * @return precision Precision parameter from mapping of sql field.
172: */
173: public final Integer getPrecision() {
174: return null;
175: }
176:
177: /**
178: * Get decimals parameter from mapping of sql field.
179: * <br/>
180: * Returns <code>null</code> as it is not supported yet.
181: *
182: * @return decimals Decimals parameter from mapping of sql field.
183: */
184: public final Integer getDecimals() {
185: return null;
186: }
187:
188: //--------------------------------------------------------------------------
189:
190: /**
191: * {@inheritDoc}
192: */
193: public final void toDropDDL(final DDLWriter writer) {
194: }
195:
196: //--------------------------------------------------------------------------
197:
198: /**
199: * Check if given field can be merged with this one.
200: *
201: * @param field Field to check if it is able to be merged.
202: * @throws GeneratorException If fields cannot be merged.
203: */
204: public final void merge(final Field field)
205: throws GeneratorException {
206: if (field == null) {
207: String msg = "Field to merge is missing.";
208: LOG.error(msg);
209: throw new GeneratorException(msg);
210: }
211:
212: if (!equals(getName(), field.getName())) {
213: String msg = "Name of field differs from: " + getName();
214: LOG.error(msg);
215: throw new GeneratorException(msg);
216: }
217:
218: if (!equals(getTable(), field.getTable())) {
219: String msg = "Table of field differs from: "
220: + getTable().getName();
221: LOG.error(msg);
222: throw new GeneratorException(msg);
223: }
224:
225: if (_isIdentity != field._isIdentity) {
226: LOG
227: .warn("Merge table: Field 'identity' attributes are different");
228: }
229:
230: if (_isRequired != field._isRequired) {
231: LOG
232: .warn("Merge table: Field 'required' attributes are different");
233: }
234:
235: _type.merge(field._type);
236: }
237:
238: //--------------------------------------------------------------------------
239:
240: /**
241: * {@inheritDoc}
242: */
243: public final boolean equals(final Object other) {
244: if (other == this ) {
245: return true;
246: }
247: if (other == null) {
248: return false;
249: }
250: if (other.getClass() != this .getClass()) {
251: return false;
252: }
253:
254: Field field = (Field) other;
255: return equals(getName(), field.getName())
256: && equals(_table, field._table)
257: && equals(_type, field._type)
258: && (_isRequired == field._isRequired)
259: && (_isIdentity == field._isIdentity)
260: && equals(_keyGenerator, field._keyGenerator);
261: }
262:
263: /**
264: * {@inheritDoc}
265: */
266: public final int hashCode() {
267: int hashCode = 0;
268: if (getName() != null) {
269: hashCode += getName().hashCode();
270: }
271: hashCode *= HASHFACTOR;
272: if (_table != null) {
273: hashCode += _table.hashCode();
274: }
275: hashCode *= HASHFACTOR;
276: if (_type != null) {
277: hashCode += _type.hashCode();
278: }
279: hashCode *= HASHFACTOR;
280: hashCode += Boolean.valueOf(_isRequired).hashCode();
281: hashCode *= HASHFACTOR;
282: hashCode += Boolean.valueOf(_isIdentity).hashCode();
283: hashCode *= HASHFACTOR;
284: if (_keyGenerator != null) {
285: hashCode += _keyGenerator.hashCode();
286: }
287: return hashCode;
288: }
289:
290: //--------------------------------------------------------------------------
291: }
|