KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > model > util > NameMapper


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  * NameMapper.java
26  *
27  * Created on December 11, 2001, 9:51 AM
28  */

29
30 package com.sun.jdo.spi.persistence.support.ejb.model.util;
31
32 import java.util.*;
33
34 import com.sun.jdo.api.persistence.mapping.ejb.AbstractNameMapper;
35
36 import com.sun.enterprise.deployment.*;
37
38 /** This is a class which helps translate between the various names of the
39  * CMP (ejb name, abstract schema, abstract bean, concrete bean, local
40  * interface, remote interface) and the persistence-capable class name. It
41  * also has methods for translation of field names. The basic entry point
42  * is ejb name or persistence-capable class name. This is a subclass of
43  * the AbstractNameMapper and implements the methods based on DOL. It
44  * also adds methods which are used during deployment time but not needed
45  * during development time and therefore, not in the abstract superclass.
46  *
47  * @author Rochelle Raccah
48  */

49 public abstract class NameMapper extends AbstractNameMapper
50 {
51     private EjbBundleDescriptor _bundleDescriptor;
52     private Map _generatedRelToInverseRelMap;
53     private Map _relToInverseGeneratedRelMap;
54
55     /** Creates a new instance of NameMapper
56      * @param bundleDescriptor the EjbBundleDescriptor which defines the
57      * universe of names for this application.
58      */

59     protected NameMapper (EjbBundleDescriptor bundleDescriptor)
60     {
61         _bundleDescriptor = bundleDescriptor;
62         initGeneratedRelationshipMaps();
63     }
64
65     private void initGeneratedRelationshipMaps ()
66     {
67         EjbBundleDescriptor bundleDescriptor = getBundleDescriptor();
68         Set relationships = bundleDescriptor.getRelationships();
69
70         _generatedRelToInverseRelMap = new HashMap();
71         _relToInverseGeneratedRelMap = new HashMap();
72
73         // during development time this code may attempt to get the
74
// iterator even with no relationships, so protect it by a
75
// null check
76
if (relationships != null)
77         {
78             Iterator iterator = relationships.iterator();
79             List generatedRels = new ArrayList();
80             int counter = 0;
81
82             // gather list of generated cmr fields by examining source and sink
83
while (iterator.hasNext())
84             {
85                 RelationshipDescriptor relationship =
86                     (RelationshipDescriptor)iterator.next();
87
88                 if (relationship.getSource().getCMRField() == null)
89                     generatedRels.add(relationship);
90
91                 if (relationship.getSink().getCMRField() == null)
92                     generatedRels.add(relationship);
93             }
94
95             // now update the maps to contain this info
96
iterator = generatedRels.iterator();
97             while (iterator.hasNext())
98             {
99                 RelationshipDescriptor relationship =
100                     (RelationshipDescriptor)iterator.next();
101                 RelationRoleDescriptor source = relationship.getSource();
102                 String JavaDoc sourceEjbName = source.getOwner().getName();
103                 String JavaDoc sourceCMRField = source.getCMRField();
104                 boolean sourceIsNull = (sourceCMRField == null);
105                 RelationRoleDescriptor sink = relationship.getSink();
106                 String JavaDoc sinkEjbName = sink.getOwner().getName();
107                 String JavaDoc ejbName = (sourceIsNull ? sourceEjbName : sinkEjbName);
108                 String JavaDoc otherEjbName =
109                     (sourceIsNull ? sinkEjbName : sourceEjbName);
110                 List ejbField = Arrays.asList(new String JavaDoc[]{otherEjbName,
111                     (sourceIsNull ? sink.getCMRField() : sourceCMRField)});
112                 PersistenceDescriptor pDescriptor = ((EjbCMPEntityDescriptor)
113                     bundleDescriptor.getEjbByName(ejbName)).
114                     getPersistenceDescriptor();
115                 List generatedField = null;
116                 String JavaDoc uniqueName = null;
117
118                 // make sure the user doesn't already have a field
119
// with this name
120
do
121                 {
122                     counter++;
123                     uniqueName = GENERATED_CMR_FIELD_PREFIX + counter;
124                 } while (hasField(pDescriptor, uniqueName));
125
126                 generatedField =
127                     Arrays.asList(new String JavaDoc[]{ejbName, uniqueName});
128                 _generatedRelToInverseRelMap.put(generatedField, ejbField);
129                 _relToInverseGeneratedRelMap.put(ejbField, generatedField);
130             }
131         }
132     }
133
134     protected Map getGeneratedFieldsMap ()
135     {
136         return _generatedRelToInverseRelMap;
137     }
138     protected Map getInverseFieldsMap () { return _relToInverseGeneratedRelMap; }
139
140     // isCMPField does not return true for relationships, so we use getTypeFor
141
private boolean hasField (PersistenceDescriptor persistenceDescriptor,
142         String JavaDoc fieldName)
143     {
144         Class JavaDoc fieldType = null;
145
146         try
147         {
148             fieldType = persistenceDescriptor.getTypeFor(fieldName);
149         }
150         catch (RuntimeException JavaDoc e)
151         {
152             // fieldType will be null - there is no such field
153
}
154
155         return (fieldType != null);
156     }
157
158     /** Gets the EjbBundleDescriptor which defines the universe of
159      * names for this application.
160      * @return the EjbBundleDescriptor which defines the universe of
161      * names for this application.
162      */

163     public EjbBundleDescriptor getBundleDescriptor ()
164     {
165         return _bundleDescriptor;
166     }
167
168     /** Gets the EjbCMPEntityDescriptor which represents the ejb
169      * with the specified name.
170      * @param name the name of the ejb
171      * @return the EjbCMPEntityDescriptor which represents the ejb.
172      */

173     abstract public EjbCMPEntityDescriptor getDescriptorForEjbName (
174         String JavaDoc name);
175
176     /** Get the type of key class of this ejb.
177      * @return the key class type, one of {@link #USER_DEFINED_KEY_CLASS},
178      * {@link #PRIMARY_KEY_FIELD}, or {@link #UNKNOWN_KEY_CLASS}
179      */

180     public int getKeyClassTypeForEjbName (String JavaDoc name)
181     {
182         String JavaDoc keyClass = getKeyClassForEjbName(name);
183
184         if (!"java.lang.Object".equals(keyClass)) // NOI18N
185
{
186             EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name);
187
188             return ((descriptor.getPrimaryKeyFieldDesc() != null) ?
189                 PRIMARY_KEY_FIELD : USER_DEFINED_KEY_CLASS);
190         }
191
192         return UNKNOWN_KEY_CLASS;
193     }
194
195     /** Gets the name of the ejb which corresponds to the specified abstract
196      * schema name.
197      * @param schemaName the name of the abstract schema
198      * @return the name of the ejb for the specified abstract schema
199      */

200     abstract public String JavaDoc getEjbNameForAbstractSchema (String JavaDoc schemaName);
201
202     /** Gets the name of the concrete bean class which corresponds to the
203      * specified ejb.
204      * @param name the name of the ejb
205      * @return the name of the concrete bean for the specified ejb
206      */

207     abstract public String JavaDoc getConcreteBeanClassForEjbName (String JavaDoc name);
208 }
209
Popular Tags