001: /**********************************************************************
002: Copyright (c) 2005 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: * Representation of a primary key constraint.
027: * Can also be used for specifying surrogate keys, but JPOX doesn't support this.
028: *
029: * @since 1.1
030: * @version $Revision$
031: */
032: public class PrimaryKeyMetaData extends MetaData implements
033: ColumnMetaDataContainer {
034: /** PK constraint name. */
035: protected String name = null;
036:
037: /** Column name of PK. */
038: protected String columnName = null;
039:
040: /** MetaData for columns to be used in PK. */
041: protected ColumnMetaData[] columnMetaData = null;
042:
043: // -------------------------------------------------------------------------
044: // Fields below here are used in the metadata parse process where the parser
045: // dynamically adds fields/columns as it encounters them in the MetaData files.
046: // They are typically cleared at the point of initialise() and not used thereafter.
047:
048: /**
049: * the columns elements to be included in the index. Suitable to be empty
050: * when this metadata is contained within a field, element, key, value, or join elements
051: */
052: protected List columns = new ArrayList();
053:
054: /**
055: * Constructor.
056: * @param parent The parent metadata object
057: * @param name Name of the PK constraint
058: * @param columnName Name of the column (optional)
059: */
060: public PrimaryKeyMetaData(MetaData parent, final String name,
061: final String columnName) {
062: super (parent);
063:
064: this .name = (StringUtils.isWhitespace(name) ? null : name);
065: this .columnName = (StringUtils.isWhitespace(columnName) ? null
066: : columnName);
067: }
068:
069: /**
070: * Initialisation method. This should be called AFTER using the populate
071: * method if you are going to use populate. It creates the internal
072: * convenience arrays etc needed for normal operation.
073: */
074: public void initialise() {
075: // Set up the columnMetaData
076: if (columns.size() == 0 && columnName != null) {
077: columnMetaData = new ColumnMetaData[1];
078: columnMetaData[0] = new ColumnMetaData(this , columnName);
079: columnMetaData[0].initialise();
080: } else {
081: columnMetaData = new ColumnMetaData[columns.size()];
082: for (int i = 0; i < columnMetaData.length; i++) {
083: columnMetaData[i] = (ColumnMetaData) columns.get(i);
084: columnMetaData[i].initialise();
085: }
086: }
087:
088: // Clean out parsing data
089: columns.clear();
090: columns = null;
091:
092: setInitialised();
093: }
094:
095: // --------------------------- Accessors/Mutators ---------------------------
096:
097: /**
098: * Accessor for PK constraint name.
099: * @return Returns the constraint name
100: */
101: public String getName() {
102: return name;
103: }
104:
105: /**
106: * Mutator for the name of the PK constraint.
107: * @param name The name to use
108: */
109: public void setName(String name) {
110: this .name = name;
111: }
112:
113: /**
114: * Add a new ColumnMetaData element
115: * @param colmd The ColumnMetaData to add
116: */
117: public void addColumn(ColumnMetaData colmd) {
118: columns.add(colmd);
119: colmd.parent = this ;
120: }
121:
122: /**
123: * Accessor for columnMetaData
124: * @return Returns the columnMetaData.
125: */
126: public final ColumnMetaData[] getColumnMetaData() {
127: return columnMetaData;
128: }
129:
130: // ----------------------------- Utilities ---------------------------------
131:
132: /**
133: * Returns a string representation of the object using a prefix
134: * @param prefix prefix string
135: * @param indent indent string
136: * @return a string representation of the object.
137: */
138: public String toString(String prefix, String indent) {
139: StringBuffer sb = new StringBuffer();
140: sb.append(prefix).append(
141: "<primary-key"
142: + (name != null ? (" name=\"" + name + "\"")
143: : "")
144: + (columnName != null ? (" column=\""
145: + columnName + "\"") : "") + ">\n");
146:
147: // Add columns
148: if (columnMetaData != null) {
149: for (int i = 0; i < columnMetaData.length; i++) {
150: sb.append(columnMetaData[i].toString(prefix + indent,
151: indent));
152: }
153: }
154:
155: // Add extensions
156: sb.append(super .toString(prefix + indent, indent));
157:
158: sb.append(prefix).append("</primary-key>\n");
159:
160: return sb.toString();
161: }
162: }
|