01: package xdoclet.modules.ojb.model;
02:
03: import java.util.Iterator;
04:
05: import xdoclet.modules.ojb.CommaListIterator;
06:
07: /* Copyright 2004-2005 The Apache Software Foundation
08: *
09: * Licensed under the Apache License, Version 2.0 (the "License");
10: * you may not use this file except in compliance with the License.
11: * You may obtain a copy of the License at
12: *
13: * http://www.apache.org/licenses/LICENSE-2.0
14: *
15: * Unless required by applicable law or agreed to in writing, software
16: * distributed under the License is distributed on an "AS IS" BASIS,
17: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18: * See the License for the specific language governing permissions and
19: * limitations under the License.
20: */
21:
22: /**
23: * A collection descriptor for the ojb repository file.
24: *
25: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
26: */
27: public class CollectionDescriptorDef extends FeatureDescriptorDef {
28: /**
29: * Creates a new collection descriptor object.
30: *
31: * @param name The name of the collection field
32: */
33: public CollectionDescriptorDef(String name) {
34: super (name);
35: }
36:
37: /**
38: * Creates copy of the given collection descriptor object. Note that the copy has no owner initially.
39: *
40: * @param src The original collection descriptor
41: * @param prefix A prefix for the name
42: */
43: public CollectionDescriptorDef(CollectionDescriptorDef src,
44: String prefix) {
45: super (src, prefix);
46: }
47:
48: /**
49: * Tries to find the corresponding remote collection for this m:n collection.
50: *
51: * @return The corresponding remote collection
52: */
53: public CollectionDescriptorDef getRemoteCollection() {
54: if (!hasProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE)) {
55: return null;
56: }
57: ModelDef modelDef = (ModelDef) getOwner().getOwner();
58: String elementClassName = getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF);
59: ClassDescriptorDef elementClass = modelDef
60: .getClass(elementClassName);
61: String indirTable = getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE);
62: boolean hasRemoteKey = hasProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY);
63: String remoteKey = getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY);
64: CollectionDescriptorDef remoteCollDef = null;
65:
66: // find the collection in the element class that has the same indirection table
67: for (Iterator it = elementClass.getCollections(); it.hasNext();) {
68: remoteCollDef = (CollectionDescriptorDef) it.next();
69: if (indirTable
70: .equals(remoteCollDef
71: .getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE))
72: && (this != remoteCollDef)
73: && (!hasRemoteKey || CommaListIterator
74: .sameLists(
75: remoteKey,
76: remoteCollDef
77: .getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)))) {
78: return remoteCollDef;
79: }
80: }
81: return null;
82: }
83: }
|