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: /**
021: * MetaData representing a unique constraint.
022: *
023: * @since 1.1
024: * @version $Revision: 1.17 $
025: */
026: public class UniqueMetaData extends AbstractConstraintMetaData
027: implements ColumnMetaDataContainer {
028: /**
029: * Whether the unique is initially deferred.
030: */
031: final boolean deferred;
032:
033: /**
034: * Constructor to create a copy of the passed metadata using the provided parent.
035: * @param umd The metadata to copy
036: */
037: public UniqueMetaData(UniqueMetaData umd) {
038: super (null, umd.name, umd.table);
039:
040: this .deferred = umd.deferred;
041: // TODO Change these to copy rather than reference
042: for (int i = 0; i < umd.members.size(); i++) {
043: addMember((AbstractMemberMetaData) umd.members.get(i));
044: }
045: for (int i = 0; i < umd.columns.size(); i++) {
046: addColumn((ColumnMetaData) umd.columns.get(i));
047: }
048: }
049:
050: /**
051: * Constructor.
052: * @param name Name of unique constraint
053: * @param table Name of the table
054: * @param deferredValue Whether the unique is deferred initially
055: */
056: public UniqueMetaData(final String name, final String table,
057: final String deferredValue) {
058: super (null, name, table); // Ignore parent
059:
060: if (deferredValue != null
061: && deferredValue.equalsIgnoreCase("true")) {
062: this .deferred = true;
063: } else {
064: this .deferred = false;
065: }
066: }
067:
068: /**
069: * Method to initialise the object, creating internal convenience arrays.
070: * Initialise all sub-objects.
071: */
072: public void initialise() {
073: if (isInitialised()) {
074: return;
075: }
076:
077: // Set up the fieldMetaData
078: if (members.size() == 0) {
079: memberMetaData = null;
080: } else {
081: memberMetaData = new AbstractMemberMetaData[members.size()];
082: for (int i = 0; i < memberMetaData.length; i++) {
083: memberMetaData[i] = (AbstractMemberMetaData) members
084: .get(i);
085: memberMetaData[i].initialise();
086: }
087: }
088:
089: // Set up the columnMetaData
090: if (columns.size() == 0) {
091: columnMetaData = null;
092: } else {
093: columnMetaData = new ColumnMetaData[columns.size()];
094: for (int i = 0; i < columnMetaData.length; i++) {
095: columnMetaData[i] = (ColumnMetaData) columns.get(i);
096: columnMetaData[i].initialise();
097: }
098: }
099:
100: // Clear out parsing data
101: members.clear();
102: members = null;
103: columns.clear();
104: columns = null;
105:
106: setInitialised();
107: }
108:
109: // -------------------------------- Mutators -------------------------------
110:
111: /**
112: * Accessor for whether the unique constraint is deferred.
113: * @return Returns whether the unique constraint is deferred
114: */
115: public final boolean isDeferred() {
116: return deferred;
117: }
118:
119: // -------------------------------- Utilities ------------------------------
120:
121: /**
122: * Returns a string representation of the object.
123: * This can be used as part of a facility to output a MetaData file.
124: * @param prefix prefix string
125: * @param indent indent string
126: * @return a string representation of the object.
127: */
128: public String toString(String prefix, String indent) {
129: StringBuffer sb = new StringBuffer();
130: sb.append(prefix).append("<unique");
131: if (table != null) {
132: sb.append(" table=\"" + table + "\"");
133: }
134: if (deferred) {
135: sb.append(" deferred=\"true\"");
136: }
137: sb.append(name != null ? (" name=\"" + name + "\">\n") : ">\n");
138:
139: // Add fields
140: if (memberMetaData != null) {
141: for (int i = 0; i < memberMetaData.length; i++) {
142: sb.append(memberMetaData[i].toString(prefix + indent,
143: indent));
144: }
145: }
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("</unique>\n");
159: return sb.toString();
160: }
161: }
|