KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Collections JavaDoc;
37
38 /**
39  * Description
40  */

41 public class BasicCommonClassMapping
42     extends BasicMappingStructure
43     implements CommonClassMapping {
44
45     /**
46      * The name of the rule used to map the class.
47      */

48     private String JavaDoc ruleName;
49
50     /**
51      * A Map of PrimitiveElementMapping objects.
52      * key = field name
53      * value = the PrimitiveElementMapping
54      */

55     private Map JavaDoc primitiveElementMappings;
56
57     /**
58      * A NameDef object.
59      * This is the NameDef object of the class.
60      */

61     private IdentifierMapping identifierMapping;
62
63     private Set JavaDoc dependencies;
64
65     /**
66      * Builds a new BasicCommonClassMapping object.
67      *
68      * @param ruleName the name of the rule used to map the class,
69      * linkedMO the object referenced by the current object,
70      * parent the parent of the current object.
71      */

72     public BasicCommonClassMapping(String JavaDoc ruleName, MetaObject linkedMO,
73                                    MetaObject parent) {
74         super(parent, linkedMO);
75         this.ruleName = ruleName;
76         primitiveElementMappings = new HashMap JavaDoc();
77         identifierMapping = null;
78         dependencies = new HashSet JavaDoc();
79     }
80
81     ///////////////////////////////////////////////////////////////////
82
// from CommonClassMapping interface
83
///////////////////////////////////////////////////////////////////
84

85     /**
86      * Returns the name of the rule used to map the class.
87      * @return the name of the rule.
88      */

89     public String JavaDoc getRuleName() {
90         return this.ruleName;
91     }
92
93     /**
94      * Sets the name of the rule used to map the class.
95      * @param rulename the name of the rule.
96      */

97     public void setRuleName(String JavaDoc rulename) {
98
99         this.ruleName = rulename;
100     }
101
102     /**
103      * Returns the IdentifierMapping object.
104      * @return an IdentifierMapping object.
105      */

106     public IdentifierMapping getIdentifierMapping() {
107         return this.identifierMapping;
108     }
109
110     /**
111      * Sets the IdentifierMapping object.
112      * @param idmapping the IdentifierMapping object.
113      */

114     public void setIdentifierMapping(IdentifierMapping idmapping) {
115         this.identifierMapping = idmapping;
116     }
117
118     /**
119      * Returns a collection of PrimitiveElementMapping objects.
120      * @return a collection.
121      */

122     public Collection JavaDoc getPrimitiveElementMappings() {
123         List JavaDoc list = null;
124         try {
125             list = new ArrayList JavaDoc(primitiveElementMappings.values());
126             Collections.sort(list, FieldComparator.instance);
127         } catch (Exception JavaDoc e) {
128             e.printStackTrace(); //To change body of catch statement use Options | File Templates.
129
}
130         return list;
131     }
132
133
134     /**
135      * Important: This method returns only the PrimitiveElementMapping objects of the class.
136      */

137     public List JavaDoc getAllPrimitiveElementMappings() {
138         return (List JavaDoc) getPrimitiveElementMappings();
139     }
140     
141     public PrimitiveElementMapping getPrimitiveElementMapping(String JavaDoc fieldName) {
142         return (PrimitiveElementMapping) primitiveElementMappings.get(fieldName);
143     }
144
145
146     /**
147      * Adds a PrimitiveElementMapping object.
148      * @param peMapping the PrimitiveElementMapping object to add to the list.
149      */

150     public void addPrimitiveElementMapping(PrimitiveElementMapping peMapping) {
151         addPrimitiveElementMapping(
152                 ((PrimitiveElement) peMapping.getLinkedMO()).getName(), peMapping);
153     }
154
155     /**
156      * Adds a PrimitiveElementMapping object.
157      * @param peMapping the PrimitiveElementMapping object to add to the list.
158      */

159     public void addPrimitiveElementMapping(String JavaDoc fieldName, PrimitiveElementMapping peMapping) {
160         primitiveElementMappings.put(fieldName, peMapping);
161         //System.out.println("fieldname -> PEM=" + fieldName);
162
}
163
164     /**
165      * Returns an Iterator over PrimitiveElementMapping objects.
166      * @return an Iterator.
167      */

168     public Iterator JavaDoc primitiveElementMappingsIterator() {
169         return primitiveElementMappings.values().iterator();
170     }
171
172
173
174     /**
175      * Creates a new IdentifierMapping object.
176      * @param nd the NameDef object that defines an object identifier.
177      * @return an IdentifierMapping object.
178      */

179     public IdentifierMapping createIdentifierMapping(NameDef nd) {
180         IdentifierMapping res = getIdentifierMapping();
181         if (res == null) {
182             res = new BasicIdentifierMapping(nd, this);
183             setIdentifierMapping(res);
184         }
185         return res;
186     }
187
188     /**
189      * Creates a new ReferenceMapping object.
190      * @param ruleName the name of the rule used to map the reference,
191      * nd the NameDef object that defines an object reference.
192      * @return a ReferenceMapping object.
193      */

194     public ReferenceMapping createReferenceMapping(String JavaDoc ruleName, NameDef nd) {
195         return new BasicReferenceMapping(ruleName, nd, this);
196     }
197
198     public void addDependency(String JavaDoc jormClassName) {
199         dependencies.add(jormClassName);
200     }
201
202     public void removeDependency(String JavaDoc jormClassName) {
203         dependencies.remove(jormClassName);
204     }
205
206     public Collection JavaDoc getDependencies() {
207         return dependencies;
208     }
209
210     protected Collection JavaDoc getChildren() {
211         Collection JavaDoc al = new ArrayList JavaDoc();
212         al.add(identifierMapping);
213         al.addAll(primitiveElementMappings.values());
214         return al;
215     }
216
217 }
218
Popular Tags