KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > mapping > ejb > AbstractNameMapper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * AbstractNameMapper.java
26  *
27  * Created on October 28, 2004, 2:51 PM
28  */

29
30 package com.sun.jdo.api.persistence.mapping.ejb;
31
32 import java.util.*;
33
34 /** This is a class which helps translate between the various names of the
35  * CMP (ejb name, abstract schema, abstract bean, concrete bean, local
36  * interface, remote interface) and the persistence-capable class name. It
37  * also has methods for translation of field names. The basic entry point
38  * is ejb name or persistence-capable class name.
39  *
40  * @author Rochelle Raccah
41  */

42 abstract public class AbstractNameMapper {
43     public static final int USER_DEFINED_KEY_CLASS = 1;
44     public static final int PRIMARY_KEY_FIELD = 2;
45     public static final int UNKNOWN_KEY_CLASS = 3;
46
47     /** Defines key field name for unknown primary key */
48     public static final String JavaDoc GENERATED_KEY_FIELD_NAME = "generatedPKField"; // NOI18N
49
/** Defines version field name prefix for version consistency */
50     public static final String JavaDoc GENERATED_VERSION_FIELD_PREFIX =
51         "thisVersionFieldWasGeneratedByTheNameMapper"; // NOI18N
52
protected static final String JavaDoc GENERATED_CMR_FIELD_PREFIX =
53         "thisRelationshipFieldWasGeneratedByTheNameMapper"; // NOI18N
54

55     abstract protected Map getGeneratedFieldsMap();
56
57     abstract protected Map getInverseFieldsMap();
58
59     /** Determines if the specified name represents an ejb.
60      * @param name the fully qualified name to be checked
61      * @return <code>true</code> if this name represents an ejb;
62      * <code>false</code> otherwise.
63      */

64     abstract public boolean isEjbName(String JavaDoc name);
65
66     /** Gets the name of the abstract bean class which corresponds to the
67      * specified ejb name.
68      * @param name the name of the ejb
69      * @return the name of the abstract bean for the specified ejb
70      */

71     abstract public String JavaDoc getAbstractBeanClassForEjbName(String JavaDoc name);
72
73     /** Gets the name of the key class which corresponds to the specified
74      * ejb name.
75      * @param name the name of the ejb
76      * @return the name of the key class for the ejb
77      */

78     abstract public String JavaDoc getKeyClassForEjbName(String JavaDoc name);
79
80     /** Gets the name of the key class which corresponds to the specified
81      * persistence-capable key class name. Returns <code>null</code> if the
82      * supplied className is not a persistence-capable key class name.
83      * @param className the name of the persistence-capable key class
84      * @return the name of the key class for the ejb
85      */

86     public String JavaDoc getKeyClassForPersistenceKeyClass(String JavaDoc className) {
87         String JavaDoc ejbName = getEjbNameForPersistenceKeyClass(className);
88
89         return ((ejbName != null) ? getKeyClassForEjbName(ejbName) : null);
90     }
91
92     /** Gets the name of the ejb name which corresponds to the
93      * specified persistence-capable key class name. Returns
94      * <code>null</code> if the supplied className is not a
95      * persistence-capable key class name.
96      * @param className the name of the persistence-capable key class
97      * @return the name of the ejb for the specified persistence-capable
98      * key class
99      */

100     public String JavaDoc getEjbNameForPersistenceKeyClass(String JavaDoc className) {
101         if (className.toUpperCase().endsWith("OID")) { // NOI18N
102
return getEjbNameForPersistenceClass(
103                 className.substring(0, className.length() - 4));
104         }
105
106         return null;
107     }
108
109     /** Get the type of key class of this ejb.
110      * @return the key class type, one of {@link #USER_DEFINED_KEY_CLASS},
111      * {@link #PRIMARY_KEY_FIELD}, or {@link #UNKNOWN_KEY_CLASS}
112      */

113     abstract public int getKeyClassTypeForEjbName (String JavaDoc name);
114
115     /** Gets the name of the abstract schema which corresponds to the
116      * specified ejb.
117      * @param name the name of the ejb
118      * @return the name of the abstract schema for the specified ejb
119      */

120     abstract public String JavaDoc getAbstractSchemaForEjbName(String JavaDoc name);
121
122     /** Gets the name of the ejb name which corresponds to the
123      * specified persistence-capable class name.
124      * @param className the name of the persistence-capable
125      * @return the name of the ejb for the specified persistence-capable
126      */

127     abstract public String JavaDoc getEjbNameForPersistenceClass(String JavaDoc className);
128
129     /** Gets the name of the persistence-capable class which corresponds to
130      * the specified ejb name.
131      * @param name the name of the ejb
132      * @return the name of the persistence-capable for the specified ejb
133      */

134     abstract public String JavaDoc getPersistenceClassForEjbName(String JavaDoc name);
135
136     /** Determines if the specified name represents a local interface.
137      * @param name the fully qualified name to be checked
138      * @return <code>true</code> if this name represents a local interface;
139      * <code>false</code> otherwise.
140      */

141     abstract public boolean isLocalInterface(String JavaDoc name);
142
143     /** Gets the name of the persistence-capable class which corresponds to
144      * the specified local interface name.
145      * @param className the name of the persistence-capable class which
146      * contains fieldName from which to find relationship and therefore the
147      * local interface
148      * @param fieldName the name of the field in the persistence-capable class
149      * @param interfaceName the name of the local interface
150      * @return the name of the persistence-capable for the specified
151      * local interface which is related to the specified class name, field name
152      * pair
153      */

154     public String JavaDoc getPersistenceClassForLocalInterface(String JavaDoc className,
155             String JavaDoc fieldName, String JavaDoc interfaceName) {
156         if (isLocalInterface(interfaceName)) {
157             String JavaDoc ejbName = getEjbNameForPersistenceClass(className);
158             String JavaDoc ejbField =
159                 getEjbFieldForPersistenceField(className, fieldName);
160
161             return getPersistenceClassForEjbName(
162                 getEjbNameForLocalInterface(ejbName, ejbField, interfaceName));
163         }
164
165         return null;
166     }
167
168     /** Gets the name of the ejb which corresponds to the specified
169      * local interface name.
170      * @param ejbName the name of the ejb which contains fieldName
171      * from which to find relationship and therefore the local interface
172      * @param fieldName the name of the field in the ejb
173      * @param interfaceName the name of the local interface
174      * @return the name of the ejb for the specified local interface
175      */

176     abstract public String JavaDoc getEjbNameForLocalInterface(String JavaDoc ejbName,
177         String JavaDoc fieldName, String JavaDoc interfaceName);
178
179     /** Gets the name of the local interface which corresponds to the
180      * specified ejb name.
181      * @param name the name of the ejb
182      * @return the name of the local interface for the specified ejb
183      */

184     abstract public String JavaDoc getLocalInterfaceForEjbName(String JavaDoc name);
185
186     /** Determines if the specified name represents a remote interface.
187      * @param name the fully qualified name to be checked
188      * @return <code>true</code> if this name represents a remote interface;
189      * <code>false</code> otherwise.
190      */

191     abstract public boolean isRemoteInterface(String JavaDoc name);
192
193     /** Gets the name of the persistence-capable class which corresponds to
194      * the specified remote interface name.
195      * @param className the name of the persistence-capable class which
196      * contains fieldName from which to find relationship and therefore the
197      * remote interface
198      * @param fieldName the name of the field in the persistence-capable class
199      * @param interfaceName the name of the remote interface
200      * @return the name of the persistence-capable for the specified
201      * remote interface which is related to the specified class name, field name
202      * pair
203      */

204     public String JavaDoc getPersistenceClassForRemoteInterface(
205             String JavaDoc className, String JavaDoc fieldName, String JavaDoc interfaceName) {
206         if (isRemoteInterface(interfaceName)) {
207             String JavaDoc ejbName = getEjbNameForPersistenceClass(className);
208             String JavaDoc ejbField =
209                 getEjbFieldForPersistenceField(className, fieldName);
210
211             return getPersistenceClassForEjbName(
212                 getEjbNameForRemoteInterface(ejbName, ejbField, interfaceName));
213         }
214
215         return null;
216     }
217
218     /** Gets the name of the ejb which corresponds to the specified
219      * remote interface name.
220      * @param ejbName the name of the ejb which contains fieldName
221      * from which to find relationship and therefore the remote interface
222      * @param fieldName the name of the field in the ejb
223      * @param interfaceName the name of the remote interface
224      * @return the name of the ejb for the specified remote interface
225      */

226     abstract public String JavaDoc getEjbNameForRemoteInterface(String JavaDoc ejbName,
227         String JavaDoc fieldName, String JavaDoc interfaceName);
228
229     /** Gets the name of the remote interface which corresponds to the
230      * specified ejb name.
231      * @param name the name of the ejb
232      * @return the name of the remote interface for the specified ejb
233      */

234     abstract public String JavaDoc getRemoteInterfaceForEjbName(String JavaDoc name);
235
236     /** Gets the name of the field in the ejb which corresponds to the
237      * specified persistence-capable class name and field name pair.
238      * @param className the name of the persistence-capable
239      * @param fieldName the name of the field in the persistence-capable
240      * @return the name of the field in the ejb for the specified
241      * persistence-capable field
242      */

243     abstract public String JavaDoc getEjbFieldForPersistenceField(String JavaDoc className,
244         String JavaDoc fieldName);
245
246     /** Gets the name of the field in the persistence-capable class which
247      * corresponds to the specified ejb name and field name pair.
248      * @param name the name of the ejb
249      * @param fieldName the name of the field in the ejb
250      * @return the name of the field in the persistence-capable for the
251      * specified ejb field
252      */

253     abstract public String JavaDoc getPersistenceFieldForEjbField(String JavaDoc name,
254         String JavaDoc fieldName);
255
256     /** Returns <code>true</code> if the field is a generated field.
257      * That includes: relationships generated for 2 way managed relationships,
258      * key fields generated for use with {@link #UNKNOWN_KEY_CLASS}, or
259      * version fields generated to hold a version consistency column.
260      * @param name the name of the ejb
261      * @param fieldName the name of the field in the ejb
262      * @return <code>true</code> if the field is generated;<code>false</code>
263      * otherwise
264      */

265     public boolean isGeneratedField(String JavaDoc name, String JavaDoc fieldName) {
266         return isGeneratedEjbRelationship(name, fieldName) ||
267             fieldName.equals(GENERATED_KEY_FIELD_NAME) ||
268             fieldName.startsWith(GENERATED_VERSION_FIELD_PREFIX);
269     }
270
271     /** Returns <code>true</code> if the field in the persistence-capable
272      * class which corresponds to the specified ejb name and field name pair
273      * is one which was generated automatically for 2 way managed
274      * relationships in the case that the ejb specifies one way
275      * relationships.
276      * @param className the name of the ejb
277      * @param fieldName the name of the field in the ejb
278      * @return <code>true</code> if the field is generated;<code>false</code>
279      * otherwise
280      */

281     public boolean isGeneratedEjbRelationship(String JavaDoc name, String JavaDoc fieldName) {
282         return getGeneratedFieldsMap().keySet().contains(
283             Arrays.asList(new String JavaDoc[]{name, fieldName}));
284     }
285
286     /** The list contains generated relationship field names.
287      * @param name the name of the ejb
288      * @return a List of generated relationship names
289      */

290     public List getGeneratedRelationshipsForEjbName(String JavaDoc name) {
291         Map generatedFieldsMap = getGeneratedFieldsMap();
292         Iterator iterator = generatedFieldsMap.keySet().iterator();
293         List returnList = new ArrayList();
294
295         while (iterator.hasNext()) {
296             List nextField = (List)iterator.next();
297
298             if (nextField.get(0).equals(name))
299                 returnList.add(nextField.get(1));
300         }
301
302         return returnList;
303     }
304
305     /** Gets the name of the generated field in the ejb which corresponds to
306      * the specified ejb name and field name pair.
307      * @param name the name of the ejb
308      * @param fieldName the name of the field in the ejb
309      * @return a String array of the form {<ejb name>, <field name>} which
310      * represents the generated field for the ejb field
311      */

312     public String JavaDoc[] getGeneratedFieldForEjbField(String JavaDoc name,
313             String JavaDoc fieldName) {
314         List field = (List)getInverseFieldsMap().get(
315             Arrays.asList(new String JavaDoc[]{name, fieldName}));
316
317         return ((field != null) ?
318             (String JavaDoc[])field.toArray(new String JavaDoc[2]) : null);
319     }
320
321     /** Gets the name of the ejb field which corresponds to the specified
322      * generated ejb name and field name pair.
323      * @param name the name of the ejb
324      * @param fieldName the name of the field in the ejb
325      * @return a String array of the form {<ejb name>, <field name>} which
326      * represents the inverse field for the generated field
327      */

328     public String JavaDoc[] getEjbFieldForGeneratedField(String JavaDoc name, String JavaDoc fieldName)
329     {
330         List field = (List)getGeneratedFieldsMap().get(
331             Arrays.asList(new String JavaDoc[]{name, fieldName}));
332
333         return ((field != null) ?
334             (String JavaDoc[])field.toArray(new String JavaDoc[2]) : null);
335     }
336 }
337
Popular Tags