KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > metainfo > lib > BasicClassMapping


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.metainfo.lib;
25
26 import org.objectweb.jorm.metainfo.api.*;
27 import org.objectweb.jorm.metainfo.api.Class;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * BasicClassMapping is THE basic implementation of the ClassMapping interface.
36  * It defines basic methods, and must be extended to different mappers.
37  * This class is abstract since it does not implement the
38  * createImplicitParentClassMapping of ClassMapping which is specific to mappers
39  *
40  * @author X. Spengler, O. Lobry
41  */

42 public abstract class BasicClassMapping extends BasicCommonClassMapping
43         implements ClassMapping {
44     /**
45      * A vector of ReferenceMapping objects.
46      *
47      * Each element of the vector encapsulates a NameDef object that defines a
48      * reference to a class instance or a generic class instance.
49      */

50     private List JavaDoc fieldNames;
51     private List JavaDoc referenceMappings;
52
53     /**
54      * A vector of ParentClassMapping objects.
55      */

56     protected List JavaDoc parentClassMappings;
57
58     /**
59      * Builds a new BasicClassMapping.
60      * @param ruleName the name of the rule used to map the class,
61      * linkedMO the object referenced by the current object,
62      * parent the parent of the current object.
63      */

64     public BasicClassMapping(String JavaDoc ruleName, MetaObject linkedMO,
65                              MetaObject parent) {
66         super(ruleName, linkedMO, parent);
67         referenceMappings = new ArrayList JavaDoc();
68         parentClassMappings = new ArrayList JavaDoc();
69         fieldNames = new ArrayList JavaDoc();
70     }
71
72     ///////////////////////////////////////////////////////////////////
73
// from ClassMapping interface
74
///////////////////////////////////////////////////////////////////
75

76     /**
77      *
78      * @return the JORM Class linked to the ClassMapping.
79      * This JORM class is also the linkedMO.
80      * The getLinkedMO() method returns a MetaObject and not a Class object.
81      */

82     public Class JavaDoc getJormClass() {
83         return (Class JavaDoc) linkedMO;
84     }
85     
86     /**
87      * Adds a ReferenceMapping object.
88      *
89      * @param refMapping the ReferenceMapping object to add to the list.
90      */

91     public void addReferenceMapping(ReferenceMapping refMapping) {
92         Reference r = (Reference) refMapping.getLinkedMO().getParent();
93         referenceMappings.add(refMapping);
94         fieldNames.add(r.getName());
95         // TODO : optimize the generation of dependencies using the rule name
96
/*
97         if (r instanceof ClassRef) {
98             this.addDependency(((ClassRef) r).getMOClass().getFQName());
99         }
100         */

101
102     }
103
104     public ReferenceMapping createReferenceMapping(String JavaDoc ruleName, NameDef nd) {
105         ReferenceMapping res = super.createReferenceMapping(ruleName, nd);
106         addReferenceMapping(res);
107         return res;
108     }
109
110     public ReferenceMapping getReferenceMapping(String JavaDoc referenceName) {
111         int idx = fieldNames.indexOf(referenceName);
112         ReferenceMapping res = null;
113         if (idx == -1) {
114             for (Iterator JavaDoc it = iterateParentClassMappings(); it.hasNext();) {
115                 ParentClassMapping pcm = (ParentClassMapping) it.next();
116                 Class JavaDoc pc = pcm.getMOClass();
117                 ClassMapping cm = pc.getClassMapping(getProjectName(), getMapperName());
118                 if ((res = cm.getReferenceMapping(referenceName)) != null) {
119                     return res;
120                 }
121             }
122             return res;
123         } else {
124           return (ReferenceMapping) referenceMappings.get(idx);
125         }
126     }
127
128     /**
129      * Returns an Iterator over ReferenceMapping objects.
130      *
131      * @return an Iterator.
132      */

133     public Iterator JavaDoc iterateReferenceMappings() {
134         return referenceMappings.iterator();
135     }
136
137     /**
138      * Returns a collection of ReferenceMapping objects.
139      *
140      * @return a collection.
141      */

142     public Collection JavaDoc getReferenceMappings() {
143         return referenceMappings;
144     }
145
146     /**
147      * Adds a ParentClassMapping object.
148      *
149      * @param pcm the ParentClassMapping object to add to the list.
150      */

151     public void addParentClassMapping(ParentClassMapping pcm) {
152         parentClassMappings.add(pcm);
153     }
154
155     /**
156      * Creates a new ParentClassMapping object.
157      * @param ruleName the name of the rule used to map the reference,
158      * parentClass the name of a parent class.
159      * @return a ParentClassMapping object.
160      */

161     public ParentClassMapping createParentClassMapping(String JavaDoc ruleName, Class JavaDoc superClass) {
162         ParentClassMapping pcm = new BasicParentClassMapping(ruleName, superClass, this);
163         addParentClassMapping(pcm);
164         return pcm;
165     }
166
167     /**
168      * Returns an Iterator over ParentClassMapping objects.
169      *
170      * @return an Iterator.
171      */

172     public Iterator JavaDoc iterateParentClassMappings() {
173         return parentClassMappings.iterator();
174     }
175
176     /**
177      * Returns a collection of ParentClassMapping objects.
178      *
179      * @return a collection.
180      */

181     public Collection JavaDoc getParentClassMappings() {
182         return parentClassMappings;
183     }
184     /**
185      * Returns the parent class mapping using the Fully-Qualified name of its parent
186      * @param classFQName the Fully-Qualified name of the parent
187      * @return the parent class mapping using the Fully-Qualified name of its parent
188      */

189     public ParentClassMapping getParentClassMapping(String JavaDoc classFQName) {
190         for (Iterator JavaDoc pcmIt = parentClassMappings.iterator(); pcmIt.hasNext();) {
191             ParentClassMapping pcm = (ParentClassMapping) pcmIt.next();
192             if (pcm.getFQName().equals(classFQName)) {
193                 return pcm;
194             }
195         }
196         return null;
197     }
198     protected Collection JavaDoc getChildren() {
199         Collection JavaDoc al = super.getChildren();
200         al.addAll(parentClassMappings);
201         al.addAll(referenceMappings);
202         return al;
203     }
204
205
206 }
207
Popular Tags