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 - added toString(), MetaData docs, javadocs.
018: 2004 Andy Jefferson - added unique, indexed
019: ...
020: **********************************************************************/package org.jpox.metadata;
021:
022: import org.jpox.ClassLoaderResolver;
023:
024: /**
025: * This element specifies the mapping for the element component of arrays and
026: * collections.
027: *
028: * If only one column is mapped, and no additional information is needed for the
029: * column, then the column attribute can be used. Otherwise, the column
030: * element(s) are used.
031: *
032: * The serialized attribute specifies that the key values are to be serialized
033: * into the named column.
034: *
035: * The foreign-key attribute specifies the name of a foreign key to be
036: * generated.
037: *
038: * <H3>MetaData Element</H3>
039: * The MetaData element represented here is as follows
040: * <PRE>
041: * <!ELEMENT element (extension*,embedded?,column*,foreign-key?,index?,unique?,extension*)>
042: * <!ATTLIST element column CDATA #IMPLIED>
043: * <!ATTLIST element delete-action (restrict|cascade|null|default|none) #IMPLIED>
044: * <!ATTLIST element update-action CDATA #IMPLIED>
045: * <!ATTLIST element indexed (true|false|unique) #IMPLIED>
046: * <!ATTLIST element unique (true|false) #IMPLIED>
047: * <!ATTLIST element mapped-by CDATA #IMPLIED>
048: * </PRE>
049: *
050: * @since 1.1
051: * @version $Revision: 1.19 $
052: */
053: public class ElementMetaData extends AbstractElementMetaData {
054: /**
055: * Constructor to create a copy of the passed metadata using the provided parent.
056: * @param parent The parent
057: * @param emd The metadata to copy
058: */
059: public ElementMetaData(MetaData parent, ElementMetaData emd) {
060: super (parent, emd);
061: }
062:
063: /**
064: * Constructor.
065: * @param parent Parent element
066: * @param column The column tag
067: * @param deleteAction attribute delete-action value
068: * @param updateAction attribute update-action value
069: * @param indexed Whether to index this
070: * @param unique Whether to add a unique constraint
071: * @param mappedBy Mapped by field
072: */
073: public ElementMetaData(MetaData parent, String column,
074: String deleteAction, String updateAction, String indexed,
075: String unique, String mappedBy) {
076: super (parent, column, deleteAction, updateAction, indexed,
077: unique, mappedBy);
078: }
079:
080: /**
081: * Populate the MetaData.
082: * @param clr Class loader to use
083: * @param primary the primary ClassLoader to use (or null)
084: */
085: public void populate(ClassLoaderResolver clr, ClassLoader primary) {
086: if (embeddedMetaData == null
087: && ((AbstractMemberMetaData) parent).hasCollection()
088: && ((AbstractMemberMetaData) parent).getCollection()
089: .isEmbeddedElement()
090: && ((AbstractMemberMetaData) parent).getJoinMetaData() != null
091: && ((AbstractMemberMetaData) parent).getCollection()
092: .getElementClassMetaData() != null) {
093: // User has specified that the element is embedded in a join table but not how we embed it
094: // so add a dummy definition
095: embeddedMetaData = new EmbeddedMetaData(this , null, null,
096: null);
097: }
098: super .populate(clr, primary);
099: }
100:
101: // ------------------------------- Utilities -------------------------------
102:
103: /**
104: * Returns a string representation of the object using a prefix
105: * This can be used as part of a facility to output a MetaData file.
106: * @param prefix prefix string
107: * @param indent indent string
108: * @return a string representation of the object.
109: */
110: public String toString(String prefix, String indent) {
111: // Field needs outputting so generate metadata
112: StringBuffer sb = new StringBuffer();
113: sb.append(prefix).append("<element");
114: if (mappedBy != null) {
115: sb.append(" mapped-by=\"" + mappedBy + "\"");
116: }
117: if (columnName != null) {
118: sb.append("\n");
119: sb.append(prefix).append(
120: " column=\"" + columnName + "\"");
121: }
122: sb.append(">\n");
123:
124: // Add columns
125: for (int i = 0; i < columns.size(); i++) {
126: ColumnMetaData colmd = (ColumnMetaData) columns.get(i);
127: sb.append(colmd.toString(prefix + indent, indent));
128: }
129:
130: // Add index metadata
131: if (indexMetaData != null) {
132: sb.append(indexMetaData.toString(prefix + indent, indent));
133: }
134:
135: // Add unique metadata
136: if (uniqueMetaData != null) {
137: sb.append(uniqueMetaData.toString(prefix + indent, indent));
138: }
139:
140: // Add embedded metadata
141: if (embeddedMetaData != null) {
142: sb.append(embeddedMetaData
143: .toString(prefix + indent, indent));
144: }
145:
146: // Add foreign-key metadata
147: if (foreignKeyMetaData != null) {
148: sb.append(foreignKeyMetaData.toString(prefix + indent,
149: indent));
150: }
151:
152: // Add extensions
153: sb.append(super .toString(prefix + indent, indent));
154:
155: sb.append(prefix).append("</element>\n");
156: return sb.toString();
157: }
158: }
|