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: ...
018: **********************************************************************/package org.jpox.metadata;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.jpox.util.StringUtils;
024:
025: /**
026: * Meta-Data for the datastore-identity of a class.
027: * When used in ORM systems defines the column(s) that the identity is mapped to.
028: * Also defines the generation strategy for the identity values.
029: *
030: * @since 1.1
031: * @version $Revision: 1.18 $
032: */
033: public class IdentityMetaData extends MetaData implements
034: ColumnMetaDataContainer {
035: /** column name value. */
036: protected String columnName;
037:
038: /** strategy tag value. */
039: protected IdentityStrategy strategy = null;
040:
041: /** sequence tag value. */
042: protected String sequence;
043:
044: /** the columns */
045: final List columns = new ArrayList();
046:
047: /** Name of a value generator if the user wants to override the default JPOX generator. */
048: protected String valueGeneratorName;
049:
050: // -------------------------------------------------------------------------
051: // Fields below here are not represented in the output MetaData. They are
052: // for use internally in the operation of the JDO system. The majority are
053: // for convenience to save iterating through the fields since the fields
054: // are fixed once initialised.
055:
056: /** Metadata for columns. */
057: protected ColumnMetaData[] columnMetaData;
058:
059: /**
060: * Constructor
061: * @param parent parent AbstractClassMetaData instance
062: * @param columnName Name of the column
063: * @param strategy strategy
064: * @param sequence sequence name
065: */
066: public IdentityMetaData(AbstractClassMetaData parent,
067: final String columnName, final String strategy,
068: final String sequence) {
069: super (parent);
070:
071: if (strategy != null
072: && IdentityStrategy.getIdentityStrategy(strategy) == null) {
073: throw new RuntimeException(LOCALISER.msg("044157"));
074: }
075:
076: this .columnName = (StringUtils.isWhitespace(columnName) ? null
077: : columnName);
078: this .sequence = (StringUtils.isWhitespace(sequence) ? null
079: : sequence);
080: if (this .sequence != null && strategy == null) {
081: // JDO2 Section 18.6.1. No strategy, but sequence defined means use "sequence"
082: this .strategy = IdentityStrategy.SEQUENCE;
083: } else {
084: this .strategy = IdentityStrategy
085: .getIdentityStrategy(strategy);
086: }
087: }
088:
089: /**
090: * Method to initialise all internal convenience arrays needed.
091: */
092: public void initialise() {
093: // Cater for user specifying column name, or columns
094: if (columns.size() == 0 && columnName != null) {
095: columnMetaData = new ColumnMetaData[1];
096: columnMetaData[0] = new ColumnMetaData(this , columnName);
097: } else {
098: columnMetaData = new ColumnMetaData[columns.size()];
099: for (int i = 0; i < columnMetaData.length; i++) {
100: columnMetaData[i] = (ColumnMetaData) columns.get(i);
101: columnMetaData[i].initialise();
102: }
103: }
104:
105: setInitialised();
106: }
107:
108: // -------------------------------- Mutators -------------------------------
109:
110: /**
111: * Add a new ColumnMetaData element
112: * @param colmd The ColumnMetaData to add
113: */
114: public void addColumn(ColumnMetaData colmd) {
115: columns.add(colmd);
116: colmd.parent = this ;
117: columnMetaData = new ColumnMetaData[columns.size()];
118: for (int i = 0; i < columnMetaData.length; i++) {
119: columnMetaData[i] = (ColumnMetaData) columns.get(i);
120: }
121: }
122:
123: /**
124: * Mutator for the name of the value generator to use for this strategy.
125: * @param generator Name of value generator
126: */
127: public void setValueGeneratorName(String generator) {
128: if (StringUtils.isWhitespace(generator)) {
129: valueGeneratorName = null;
130: } else {
131: this .valueGeneratorName = generator;
132: }
133: }
134:
135: // -------------------------------- Accessors ------------------------------
136:
137: /**
138: * Accessor for columnMetaData
139: * @return Returns the columnMetaData.
140: */
141: public final ColumnMetaData[] getColumnMetaData() {
142: return columnMetaData;
143: }
144:
145: /**
146: * Accessor for the column name.
147: * @return column name
148: */
149: public String getColumnName() {
150: return columnName;
151: }
152:
153: /**
154: * Accessor for the strategy tag value
155: * @return strategy tag value
156: */
157: public IdentityStrategy getValueStrategy() {
158: return strategy;
159: }
160:
161: /**
162: * Accessor for the sequence name
163: * @return sequence name
164: */
165: public String getSequence() {
166: return sequence;
167: }
168:
169: /**
170: * Name of a (user-provided) value generator to override the default JPOX generator for this strategy.
171: * @return Name of user provided value generator
172: */
173: public String getValueGeneratorName() {
174: return valueGeneratorName;
175: }
176:
177: // ------------------------------ Utilities --------------------------------
178:
179: /**
180: * Returns a string representation of the object using a prefix
181: * @param prefix prefix string
182: * @param indent indent string
183: * @return a string representation of the object.
184: */
185: public String toString(String prefix, String indent) {
186: StringBuffer sb = new StringBuffer();
187: if (strategy != null) {
188: sb.append(prefix)
189: .append(
190: "<datastore-identity strategy=\""
191: + strategy + "\"");
192: } else {
193: sb.append(prefix).append("<datastore-identity");
194: }
195:
196: if (columnName != null) {
197: sb.append("\n").append(prefix).append(
198: " column=\"" + columnName + "\"");
199: }
200: if (sequence != null) {
201: sb.append("\n").append(prefix).append(
202: " sequence=\"" + sequence + "\"");
203: }
204:
205: if ((columnMetaData != null && columnMetaData.length > 0)
206: || getNoOfExtensions() > 0) {
207: sb.append(">\n");
208:
209: // Add columns
210: if (columnMetaData != null) {
211: for (int i = 0; i < columnMetaData.length; i++) {
212: sb.append(columnMetaData[i].toString(prefix
213: + indent, indent));
214: }
215: }
216:
217: // Add extensions
218: sb.append(super .toString(prefix + indent, indent));
219:
220: sb.append(prefix).append("</datastore-identity>\n");
221: } else {
222: sb.append("/>\n");
223: }
224:
225: return sb.toString();
226: }
227: }
|