001: /**********************************************************************
002: Copyright (c) 2004 Erik Bengtson 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 - toString(), "column", javadocs, initialise()
018: ...
019: **********************************************************************/package org.jpox.metadata;
020:
021: /**
022: * For schema generation, it might be useful to specify that a column or columns
023: * be indexed, and to provide the name of the index. For this purpose, an index
024: * element can be contained within a field, element, key, value, or join
025: * element, and this indicates that the column(s) associated with the referenced
026: * element should be indexed. Indexes can also be specified at the class level,
027: * by including index elements containing column elements. In this case, the
028: * column elements are mapped elsewhere, and the column elements contain only
029: * the column name.
030: *
031: * @since 1.1
032: * @version $Revision: 1.19 $
033: */
034: public class IndexMetaData extends AbstractConstraintMetaData implements
035: ColumnMetaDataContainer {
036: /**
037: * You can use UNIQUE constraints to ensure that no duplicate values are
038: * entered in specific columns that do not participate in a primary key.
039: * Although both a UNIQUE constraint and a PRIMARY KEY constraint enforce
040: * uniqueness, use a UNIQUE constraint instead of a PRIMARY KEY constraint
041: * when you want to enforce the uniqueness of:
042: * <ul>
043: * <li>
044: * A column, or combination of columns, that is not the primary key.
045: * Multiple UNIQUE constraints can be defined on a table, whereas only one
046: * PRIMARY KEY constraint can be defined on a table.
047: * </li>
048: * <li>
049: * A column that allows null values. UNIQUE constraints can be defined on
050: * columns that allow null values, whereas PRIMARY KEY constraints can be
051: * defined only on columns that do not allow null values.
052: * </li>
053: * </ul>
054: * A UNIQUE constraint can also be referenced by a FOREIGN KEY constraint.
055: */
056: Boolean unique;
057:
058: /**
059: * Constructor to create a copy of the passed metadata.
060: * @param imd The metadata to copy
061: */
062: public IndexMetaData(IndexMetaData imd) {
063: super (null, imd.name, imd.table); // Ignore parent
064:
065: this .unique = imd.unique;
066: AbstractMemberMetaData[] apmd = null;
067: if (imd.members != null) {
068: apmd = (AbstractMemberMetaData[]) imd.members
069: .toArray(new AbstractMemberMetaData[imd.members
070: .size()]);
071: } else {
072: apmd = imd.memberMetaData;
073: }
074: if (apmd != null) {
075: for (int i = 0; i < apmd.length; i++) {
076: if (apmd[i] instanceof FieldMetaData) {
077: addMember(new FieldMetaData(this , apmd[i]));
078: } else {
079: addMember(new PropertyMetaData(this ,
080: (PropertyMetaData) apmd[i]));
081: }
082: }
083: }
084: ColumnMetaData[] colmd = null;
085: if (imd.columns != null) {
086: colmd = (ColumnMetaData[]) imd.columns
087: .toArray(new ColumnMetaData[imd.columns.size()]);
088: } else {
089: colmd = imd.columnMetaData;
090: }
091: if (colmd != null) {
092: for (int i = 0; i < colmd.length; i++) {
093: addColumn(new ColumnMetaData(this , colmd[i]));
094: }
095: }
096: }
097:
098: /**
099: * Constructor.
100: * @param name Name of index
101: * @param table Name of the table
102: * @param unique Whether it is unique.
103: */
104: public IndexMetaData(final String name, String table, String unique) {
105: super (null, name, table); // Ignore parent
106:
107: if (unique != null) {
108: if (unique.equalsIgnoreCase("true")) {
109: this .unique = Boolean.TRUE;
110: } else if (unique.equalsIgnoreCase("false")) {
111: this .unique = Boolean.FALSE;
112: }
113: }
114: }
115:
116: /**
117: * Method to initialise the object, creating internal convenience arrays.
118: * Initialise all sub-objects.
119: */
120: public void initialise() {
121: if (isInitialised()) {
122: return;
123: }
124:
125: // Set up the fieldMetaData
126: if (members.size() == 0) {
127: memberMetaData = null;
128: } else {
129: memberMetaData = new AbstractMemberMetaData[members.size()];
130: for (int i = 0; i < memberMetaData.length; i++) {
131: memberMetaData[i] = (AbstractMemberMetaData) members
132: .get(i);
133: memberMetaData[i].initialise();
134: }
135: }
136:
137: // Set up the columnMetaData
138: if (columns.size() == 0) {
139: columnMetaData = null;
140: } else {
141: columnMetaData = new ColumnMetaData[columns.size()];
142: for (int i = 0; i < columnMetaData.length; i++) {
143: columnMetaData[i] = (ColumnMetaData) columns.get(i);
144: columnMetaData[i].initialise();
145: }
146: }
147:
148: // Clean out parsing data
149: members.clear();
150: members = null;
151: columns.clear();
152: columns = null;
153:
154: setInitialised();
155: }
156:
157: // -------------------------------- Mutators -------------------------------
158:
159: /**
160: * Accessor for whether the index is unique
161: * @return Returns whether the index is unique.
162: */
163: public final Boolean isUnique() {
164: return unique;
165: }
166:
167: // -------------------------------- Utilities ------------------------------
168:
169: /**
170: * Returns a string representation of the object.
171: * This can be used as part of a facility to output a MetaData file.
172: * @param prefix prefix string
173: * @param indent indent string
174: * @return a string representation of the object.
175: */
176: public String toString(String prefix, String indent) {
177: StringBuffer sb = new StringBuffer();
178: sb.append(prefix).append("<index unique=\"" + unique + "\"");
179: if (table != null) {
180: sb.append(" table=\"" + table + "\"");
181: }
182: sb.append(name != null ? (" name=\"" + name + "\">\n") : ">\n");
183:
184: // Add fields
185: if (memberMetaData != null) {
186: for (int i = 0; i < memberMetaData.length; i++) {
187: sb.append(memberMetaData[i].toString(prefix + indent,
188: indent));
189: }
190: }
191:
192: // Add columns
193: if (columnMetaData != null) {
194: for (int i = 0; i < columnMetaData.length; i++) {
195: sb.append(columnMetaData[i].toString(prefix + indent,
196: indent));
197: }
198: }
199:
200: // Add extensions
201: sb.append(super .toString(prefix + indent, indent));
202:
203: sb.append(prefix).append("</index>\n");
204: return sb.toString();
205: }
206: }
|