001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: MapMetaData.java,v 1.6 2004/01/25 20:44:32 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.model;
012:
013: import java.util.List;
014: import java.util.Set;
015: import javax.jdo.JDOFatalInternalException;
016: import org.w3c.dom.Element;
017:
018: public class MapMetaData extends MetaData {
019: protected final Class keyType;
020: protected final Class valueType;
021: protected final boolean embeddedKey;
022: protected final boolean embeddedValue;
023: protected final boolean allowsNullValues;
024:
025: public MapMetaData(FieldMetaData fmd, Element elem) {
026: super (fmd.getClassMetaData().getSourceURL(), elem);
027:
028: String typeAttr = elem.getAttribute("key-type");
029: String embeddedAttr = elem.getAttribute("embedded-key");
030:
031: keyType = getReferencedType(typeAttr, fmd.getClassMetaData());
032:
033: if (embeddedAttr.length() > 0)
034: embeddedKey = Boolean.valueOf(embeddedAttr).booleanValue();
035: else
036: embeddedKey = Types.isDefaultEmbeddedType(keyType);
037:
038: typeAttr = elem.getAttribute("value-type");
039: embeddedAttr = elem.getAttribute("embedded-value");
040:
041: valueType = getReferencedType(typeAttr, fmd.getClassMetaData());
042:
043: if (embeddedAttr.length() > 0)
044: embeddedValue = Boolean.valueOf(embeddedAttr)
045: .booleanValue();
046: else
047: embeddedValue = Types.isDefaultEmbeddedType(valueType);
048:
049: if (fmd.getType() == java.util.Hashtable.class)
050: allowsNullValues = false;
051: else
052: allowsNullValues = true;
053: }
054:
055: public String getJavaName() {
056: throw new JDOFatalInternalException(
057: "MapMetaData has no equivalent Java identifier");
058: }
059:
060: public Class getKeyType() {
061: return keyType;
062: }
063:
064: public Class getValueType() {
065: return valueType;
066: }
067:
068: public boolean isEmbeddedKey() {
069: return embeddedKey;
070: }
071:
072: public boolean isEmbeddedValue() {
073: return embeddedValue;
074: }
075:
076: public boolean allowsNullValues() {
077: return allowsNullValues;
078: }
079:
080: public ColumnOptions getKeyColumnOptions() {
081: return new ColumnOptions() {
082: public String getLength() {
083: String length = getVendorExtension(MY_VENDOR,
084: "key-length");
085:
086: if (length == null)
087: length = getVendorExtension(MY_VENDOR, "length");
088:
089: return length;
090: }
091:
092: public String getPrecision() {
093: String precision = getVendorExtension(MY_VENDOR,
094: "key-precision");
095:
096: if (precision == null)
097: precision = getVendorExtension(MY_VENDOR,
098: "precision");
099:
100: return precision;
101: }
102:
103: public String getScale() {
104: String scale = getVendorExtension(MY_VENDOR,
105: "key-scale");
106:
107: if (scale == null)
108: scale = getVendorExtension(MY_VENDOR, "scale");
109:
110: return scale;
111: }
112: };
113: }
114:
115: public ColumnOptions getValueColumnOptions() {
116: return new ColumnOptions() {
117: public String getLength() {
118: return getVendorExtension(MY_VENDOR, "value-length");
119: }
120:
121: public String getPrecision() {
122: return getVendorExtension(MY_VENDOR, "value-precision");
123: }
124:
125: public String getScale() {
126: return getVendorExtension(MY_VENDOR, "value-scale");
127: }
128: };
129: }
130:
131: public boolean isInverseMap() {
132: return getOwnerField() != null;
133: }
134:
135: public boolean clearOnDelete() {
136: String codAttr = getVendorExtension(MY_VENDOR,
137: "clear-on-delete");
138:
139: return codAttr == null ? true : Boolean.valueOf(codAttr)
140: .booleanValue();
141: }
142:
143: public FieldMetaData getOwnerField() {
144: String ownerFieldName = getVendorExtension(MY_VENDOR,
145: "owner-field");
146:
147: return ownerFieldName == null ? null : FieldMetaData.forField(
148: valueType, ownerFieldName);
149: }
150:
151: public FieldMetaData getKeyField() {
152: String keyFieldName = getVendorExtension(MY_VENDOR, "key-field");
153:
154: return keyFieldName == null ? null : FieldMetaData.forField(
155: valueType, keyFieldName);
156: }
157:
158: void getReferencedClasses(final String vendorID,
159: final List ordered, final Set referenced) {
160: ClassMetaData cmd = ClassMetaData.forClass(keyType);
161:
162: if (cmd != null)
163: cmd.getReferencedClasses(vendorID, ordered, referenced);
164:
165: cmd = ClassMetaData.forClass(valueType);
166:
167: if (cmd != null)
168: cmd.getReferencedClasses(vendorID, ordered, referenced);
169: }
170: }
|