001: /**********************************************************************
002: Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: 2004 Andy Jefferson - added initialise() method
018: ...
019: **********************************************************************/package org.jpox.metadata;
020:
021: import org.jpox.util.JPOXLogger;
022: import org.jpox.util.StringUtils;
023:
024: /**
025: * Representation of a discriminator in an inheritance strategy.
026: *
027: * @since 1.1
028: * @version $Revision: 1.21 $
029: */
030: public class DiscriminatorMetaData extends MetaData {
031: /** strategy tag value. */
032: protected DiscriminatorStrategy strategy = null;
033:
034: /** Column name of discriminator */
035: protected String columnName = null;
036:
037: /** Value for discriminator column */
038: protected String value = null;
039:
040: /** Discriminator column */
041: protected ColumnMetaData columnMetaData = null;
042:
043: /** Whether the discriminator is indexed or not and whether it is unique */
044: protected IndexedValue indexed = null;
045:
046: /** Definition of any indexing of the discriminator column. */
047: protected IndexMetaData indexMetaData;
048:
049: /**
050: * Constructor.
051: * @param parent parent InheritanceMetaData instance
052: * @param columnName Name of the column
053: * @param value Value for discriminator column
054: * @param strategy The strategy
055: * @param indexed The indexed tag
056: */
057: public DiscriminatorMetaData(InheritanceMetaData parent,
058: final String columnName, final String value,
059: final String strategy, final String indexed) {
060: super (parent);
061:
062: this .columnName = (StringUtils.isWhitespace(columnName) ? null
063: : columnName);
064: this .strategy = DiscriminatorStrategy
065: .getDiscriminatorStrategy(strategy);
066: if (value != null && strategy == null) {
067: // Default to value strategy if a value is specified
068: this .strategy = DiscriminatorStrategy.VALUE_MAP;
069: } else if (strategy == null) {
070: // Default to class strategy if no value is specified
071: this .strategy = DiscriminatorStrategy.CLASS_NAME;
072: }
073: this .indexed = IndexedValue.getIndexedValue(indexed);
074: if (StringUtils.isWhitespace(value)) {
075: if (this .strategy == DiscriminatorStrategy.VALUE_MAP) {
076: String className = ((AbstractClassMetaData) parent
077: .getParent()).getFullClassName();
078: JPOXLogger.METADATA.warn(LOCALISER.msg("044103",
079: className));
080: this .value = className;
081: } else {
082: this .value = null;
083: }
084: } else {
085: this .value = value;
086: }
087: }
088:
089: /**
090: * Constructor.
091: * @param parent parent InheritanceMetaData instance
092: * @param dmd DiscriminatorMetaData
093: */
094: public DiscriminatorMetaData(InheritanceMetaData parent,
095: final DiscriminatorMetaData dmd) {
096: super (parent);
097:
098: this .columnName = dmd.columnName;
099: this .value = dmd.value;
100: this .strategy = dmd.strategy;
101: this .indexed = dmd.indexed;
102: columnMetaData = new ColumnMetaData(this , dmd.columnMetaData);
103: if (dmd.indexMetaData != null) {
104: indexMetaData = new IndexMetaData(dmd.indexMetaData);
105: }
106: }
107:
108: /**
109: * Initialisation method. This should be called AFTER using the populate
110: * method if you are going to use populate. It creates the internal
111: * convenience arrays etc needed for normal operation.
112: */
113: public void initialise() {
114: // Interpret the "indexed" value to create our IndexMetaData where it wasn't specified that way
115: if (indexMetaData == null && columnMetaData != null
116: && indexed != null && indexed != IndexedValue.FALSE) {
117: indexMetaData = new IndexMetaData(null, null,
118: (indexed == IndexedValue.UNIQUE) ? "true" : "false");
119: // Note we only support a single column discriminator here
120: indexMetaData.addColumn(columnMetaData);
121: }
122: if (indexMetaData != null) {
123: indexMetaData.initialise();
124: }
125:
126: if (columnMetaData == null && columnName != null) {
127: columnMetaData = new ColumnMetaData(this , columnName);
128: columnMetaData.initialise();
129: }
130:
131: setInitialised();
132: }
133:
134: // --------------------------- Accessors/Mutators ---------------------------
135:
136: /**
137: * Accessor for column MetaData.
138: * @return Returns the column MetaData.
139: */
140: public ColumnMetaData getColumnMetaData() {
141: return columnMetaData;
142: }
143:
144: /**
145: * Mutator for column MetaData.
146: * @param columnMetaData The column MetaData to set.
147: */
148: public void setColumnMetaData(ColumnMetaData columnMetaData) {
149: this .columnMetaData = columnMetaData;
150: this .columnMetaData.parent = this ;
151: }
152:
153: /**
154: * Accessor for indexMetaData
155: * @return Returns the indexMetaData.
156: */
157: public final IndexMetaData getIndexMetaData() {
158: return indexMetaData;
159: }
160:
161: /**
162: * Mutator for the index MetaData
163: * @param indexMetaData The indexMetaData to set.
164: */
165: public final void setIndexMetaData(IndexMetaData indexMetaData) {
166: this .indexMetaData = indexMetaData;
167: }
168:
169: /**
170: * Accessor for value.
171: * @return Returns the value.
172: */
173: public String getValue() {
174: return value;
175: }
176:
177: /**
178: * Accessor for columnName.
179: * @return Returns the columnName.
180: */
181: public String getColumnName() {
182: // Return the column variant if specified, otherwise the name field
183: if (columnMetaData != null && columnMetaData.getName() != null) {
184: return columnMetaData.getName();
185: }
186: return columnName;
187: }
188:
189: /**
190: * Accessor for strategy.
191: * @return Returns the strategy.
192: */
193: public final DiscriminatorStrategy getStrategy() {
194: return strategy;
195: }
196:
197: /**
198: * Accessor for indexed value.
199: * @return Returns the indexed value.
200: */
201: public final IndexedValue getIndexedValue() {
202: return indexed;
203: }
204:
205: /**
206: * Mutator for columnName.
207: * @param columnName The columnName to set.
208: */
209: public void setColumnName(String columnName) {
210: this .columnName = columnName;
211: }
212:
213: // ----------------------------- Utilities ---------------------------------
214:
215: /**
216: * Returns a string representation of the object using a prefix
217: * @param prefix prefix string
218: * @param indent indent string
219: * @return a string representation of the object.
220: */
221: public String toString(String prefix, String indent) {
222: StringBuffer sb = new StringBuffer();
223: sb.append(prefix).append("<discriminator");
224: if (strategy != null) {
225: sb.append(" strategy=\"" + strategy.toString() + "\"");
226: }
227: if (columnName != null && columnMetaData == null) {
228: sb.append(" column=\"" + columnName + "\"");
229: }
230: if (value != null) {
231: sb.append(" value=\"" + value + "\"");
232: }
233: if (indexed != null) {
234: sb.append(" indexed=\"" + indexed.toString() + "\"");
235: }
236: sb.append(">\n");
237:
238: // Column MetaData
239: if (columnMetaData != null) {
240: sb.append(columnMetaData.toString(prefix + indent, indent));
241: }
242:
243: // Add index
244: if (indexMetaData != null) {
245: sb.append(indexMetaData.toString(prefix + indent, indent));
246: }
247:
248: // Add extensions
249: sb.append(super .toString(prefix + indent, indent));
250:
251: sb.append(prefix).append("</discriminator>\n");
252:
253: return sb.toString();
254: }
255: }
|