001 // <editor-fold defaultstate="collapsed" desc=" License Header ">
002 /*
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
004 *
005 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
006 *
007 * The contents of this file are subject to the terms of either the GNU
008 * General Public License Version 2 only ("GPL") or the Common
009 * Development and Distribution License("CDDL") (collectively, the
010 * "License"). You may not use this file except in compliance with the
011 * License. You can obtain a copy of the License at
012 * http://www.netbeans.org/cddl-gplv2.html
013 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
014 * specific language governing permissions and limitations under the
015 * License. When distributing the software, include this License Header
016 * Notice in each file and include the License file at
017 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
018 * particular file as subject to the "Classpath" exception as provided
019 * by Sun in the GPL Version 2 section of the License file that
020 * accompanied this code. If applicable, add the following below the
021 * License Header, with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * Contributor(s):
026 *
027 * The Original Software is NetBeans. The Initial Developer of the Original
028 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
029 * Microsystems, Inc. All Rights Reserved.
030 *
031 * If you wish your version of this file to be governed by only the CDDL
032 * or only the GPL Version 2, indicate your decision by adding
033 * "[Contributor] elects to include this software in this distribution
034 * under the [CDDL or GPL Version 2] license." If you do not indicate a
035 * single choice of license, a recipient has the option to distribute
036 * your version of this file under either the CDDL, the GPL Version 2 or
037 * to extend the choice of license to its licensees as provided above.
038 * However, if you add GPL Version 2 code and therefore, elected the GPL
039 * Version 2 license, then the option applies only if the new code is
040 * made subject to such option by the copyright holder.
041 */
042 // </editor-fold>
043 /*
044 * AbstractNameMapper.java
045 *
046 * Created on October 28, 2004, 2:51 PM
047 * NOTE: This is a copy of the file in glassfish which is copied
048 * here so as to make the classes available to the plugin for
049 * appservers pre 9.0/glassfish.
050 */
051
052 package com.sun.jdo.api.persistence.mapping.ejb;
053
054 import java.util.*;
055
056 /** This is a class which helps translate between the various names of the
057 * CMP (ejb name, abstract schema, abstract bean, concrete bean, local
058 * interface, remote interface) and the persistence-capable class name. It
059 * also has methods for translation of field names. The basic entry point
060 * is ejb name or persistence-capable class name.
061 *
062 * @author Rochelle Raccah
063 */
064 abstract public class AbstractNameMapper {
065 public static final int USER_DEFINED_KEY_CLASS = 1;
066 public static final int PRIMARY_KEY_FIELD = 2;
067 public static final int UNKNOWN_KEY_CLASS = 3;
068
069 /** Defines key field name for unknown primary key */
070 public static final String GENERATED_KEY_FIELD_NAME = "generatedPKField"; // NOI18N
071 /** Defines version field name prefix for version consistency */
072 public static final String GENERATED_VERSION_FIELD_PREFIX = "thisVersionFieldWasGeneratedByTheNameMapper"; // NOI18N
073 protected static final String GENERATED_CMR_FIELD_PREFIX = "thisRelationshipFieldWasGeneratedByTheNameMapper"; // NOI18N
074
075 abstract protected Map getGeneratedFieldsMap();
076
077 abstract protected Map getInverseFieldsMap();
078
079 /** Determines if the specified name represents an ejb.
080 * @param name the fully qualified name to be checked
081 * @return <code>true</code> if this name represents an ejb;
082 * <code>false</code> otherwise.
083 */
084 abstract public boolean isEjbName(String name);
085
086 /** Gets the name of the abstract bean class which corresponds to the
087 * specified ejb name.
088 * @param name the name of the ejb
089 * @return the name of the abstract bean for the specified ejb
090 */
091 abstract public String getAbstractBeanClassForEjbName(String name);
092
093 /** Gets the name of the key class which corresponds to the specified
094 * ejb name.
095 * @param name the name of the ejb
096 * @return the name of the key class for the ejb
097 */
098 abstract public String getKeyClassForEjbName(String name);
099
100 /** Gets the name of the key class which corresponds to the specified
101 * persistence-capable key class name. Returns <code>null</code> if the
102 * supplied className is not a persistence-capable key class name.
103 * @param className the name of the persistence-capable key class
104 * @return the name of the key class for the ejb
105 */
106 public String getKeyClassForPersistenceKeyClass(String className) {
107 String ejbName = getEjbNameForPersistenceKeyClass(className);
108
109 return ((ejbName != null) ? getKeyClassForEjbName(ejbName)
110 : null);
111 }
112
113 /** Gets the name of the ejb name which corresponds to the
114 * specified persistence-capable key class name. Returns
115 * <code>null</code> if the supplied className is not a
116 * persistence-capable key class name.
117 * @param className the name of the persistence-capable key class
118 * @return the name of the ejb for the specified persistence-capable
119 * key class
120 */
121 public String getEjbNameForPersistenceKeyClass(String className) {
122 if (className.toUpperCase(Locale.ENGLISH).endsWith("OID")) { // NOI18N
123 return getEjbNameForPersistenceClass(className.substring(0,
124 className.length() - 4));
125 }
126
127 return null;
128 }
129
130 /** Get the type of key class of this ejb.
131 * @return the key class type, one of {@link #USER_DEFINED_KEY_CLASS},
132 * {@link #PRIMARY_KEY_FIELD}, or {@link #UNKNOWN_KEY_CLASS}
133 */
134 abstract public int getKeyClassTypeForEjbName(String name);
135
136 /** Gets the name of the abstract schema which corresponds to the
137 * specified ejb.
138 * @param name the name of the ejb
139 * @return the name of the abstract schema for the specified ejb
140 */
141 abstract public String getAbstractSchemaForEjbName(String name);
142
143 /** Gets the name of the ejb name which corresponds to the
144 * specified persistence-capable class name.
145 * @param className the name of the persistence-capable
146 * @return the name of the ejb for the specified persistence-capable
147 */
148 abstract public String getEjbNameForPersistenceClass(
149 String className);
150
151 /** Gets the name of the persistence-capable class which corresponds to
152 * the specified ejb name.
153 * @param name the name of the ejb
154 * @return the name of the persistence-capable for the specified ejb
155 */
156 abstract public String getPersistenceClassForEjbName(String name);
157
158 /** Determines if the specified name represents a local interface.
159 * @param name the fully qualified name to be checked
160 * @return <code>true</code> if this name represents a local interface;
161 * <code>false</code> otherwise.
162 */
163 abstract public boolean isLocalInterface(String name);
164
165 /** Gets the name of the persistence-capable class which corresponds to
166 * the specified local interface name.
167 * @param className the name of the persistence-capable class which
168 * contains fieldName from which to find relationship and therefore the
169 * local interface
170 * @param fieldName the name of the field in the persistence-capable class
171 * @param interfaceName the name of the local interface
172 * @return the name of the persistence-capable for the specified
173 * local interface which is related to the specified class name, field name
174 * pair
175 */
176 public String getPersistenceClassForLocalInterface(
177 String className, String fieldName, String interfaceName) {
178 if (isLocalInterface(interfaceName)) {
179 String ejbName = getEjbNameForPersistenceClass(className);
180 String ejbField = getEjbFieldForPersistenceField(className,
181 fieldName);
182
183 return getPersistenceClassForEjbName(getEjbNameForLocalInterface(
184 ejbName, ejbField, interfaceName));
185 }
186
187 return null;
188 }
189
190 /** Gets the name of the ejb which corresponds to the specified
191 * local interface name.
192 * @param ejbName the name of the ejb which contains fieldName
193 * from which to find relationship and therefore the local interface
194 * @param fieldName the name of the field in the ejb
195 * @param interfaceName the name of the local interface
196 * @return the name of the ejb for the specified local interface
197 */
198 abstract public String getEjbNameForLocalInterface(String ejbName,
199 String fieldName, String interfaceName);
200
201 /** Gets the name of the local interface which corresponds to the
202 * specified ejb name.
203 * @param name the name of the ejb
204 * @return the name of the local interface for the specified ejb
205 */
206 abstract public String getLocalInterfaceForEjbName(String name);
207
208 /** Determines if the specified name represents a remote interface.
209 * @param name the fully qualified name to be checked
210 * @return <code>true</code> if this name represents a remote interface;
211 * <code>false</code> otherwise.
212 */
213 abstract public boolean isRemoteInterface(String name);
214
215 /** Gets the name of the persistence-capable class which corresponds to
216 * the specified remote interface name.
217 * @param className the name of the persistence-capable class which
218 * contains fieldName from which to find relationship and therefore the
219 * remote interface
220 * @param fieldName the name of the field in the persistence-capable class
221 * @param interfaceName the name of the remote interface
222 * @return the name of the persistence-capable for the specified
223 * remote interface which is related to the specified class name, field name
224 * pair
225 */
226 public String getPersistenceClassForRemoteInterface(
227 String className, String fieldName, String interfaceName) {
228 if (isRemoteInterface(interfaceName)) {
229 String ejbName = getEjbNameForPersistenceClass(className);
230 String ejbField = getEjbFieldForPersistenceField(className,
231 fieldName);
232
233 return getPersistenceClassForEjbName(getEjbNameForRemoteInterface(
234 ejbName, ejbField, interfaceName));
235 }
236
237 return null;
238 }
239
240 /** Gets the name of the ejb which corresponds to the specified
241 * remote interface name.
242 * @param ejbName the name of the ejb which contains fieldName
243 * from which to find relationship and therefore the remote interface
244 * @param fieldName the name of the field in the ejb
245 * @param interfaceName the name of the remote interface
246 * @return the name of the ejb for the specified remote interface
247 */
248 abstract public String getEjbNameForRemoteInterface(String ejbName,
249 String fieldName, String interfaceName);
250
251 /** Gets the name of the remote interface which corresponds to the
252 * specified ejb name.
253 * @param name the name of the ejb
254 * @return the name of the remote interface for the specified ejb
255 */
256 abstract public String getRemoteInterfaceForEjbName(String name);
257
258 /** Gets the name of the field in the ejb which corresponds to the
259 * specified persistence-capable class name and field name pair.
260 * @param className the name of the persistence-capable
261 * @param fieldName the name of the field in the persistence-capable
262 * @return the name of the field in the ejb for the specified
263 * persistence-capable field
264 */
265 abstract public String getEjbFieldForPersistenceField(
266 String className, String fieldName);
267
268 /** Gets the name of the field in the persistence-capable class which
269 * corresponds to the specified ejb name and field name pair.
270 * @param name the name of the ejb
271 * @param fieldName the name of the field in the ejb
272 * @return the name of the field in the persistence-capable for the
273 * specified ejb field
274 */
275 abstract public String getPersistenceFieldForEjbField(String name,
276 String fieldName);
277
278 /** Returns <code>true</code> if the field is a generated field.
279 * That includes: relationships generated for 2 way managed relationships,
280 * key fields generated for use with {@link #UNKNOWN_KEY_CLASS}, or
281 * version fields generated to hold a version consistency column.
282 * @param name the name of the ejb
283 * @param fieldName the name of the field in the ejb
284 * @return <code>true</code> if the field is generated;<code>false</code>
285 * otherwise
286 */
287 public boolean isGeneratedField(String name, String fieldName) {
288 return isGeneratedEjbRelationship(name, fieldName)
289 || fieldName.equals(GENERATED_KEY_FIELD_NAME)
290 || fieldName.startsWith(GENERATED_VERSION_FIELD_PREFIX);
291 }
292
293 /** Returns <code>true</code> if the field in the persistence-capable
294 * class which corresponds to the specified ejb name and field name pair
295 * is one which was generated automatically for 2 way managed
296 * relationships in the case that the ejb specifies one way
297 * relationships.
298 * @param className the name of the ejb
299 * @param fieldName the name of the field in the ejb
300 * @return <code>true</code> if the field is generated;<code>false</code>
301 * otherwise
302 */
303 public boolean isGeneratedEjbRelationship(String name,
304 String fieldName) {
305 return getGeneratedFieldsMap().keySet().contains(
306 Arrays.asList(new String[] { name, fieldName }));
307 }
308
309 /** The list contains generated relationship field names.
310 * @param name the name of the ejb
311 * @return a List of generated relationship names
312 */
313 public List getGeneratedRelationshipsForEjbName(String name) {
314 Map generatedFieldsMap = getGeneratedFieldsMap();
315 Iterator iterator = generatedFieldsMap.keySet().iterator();
316 List returnList = new ArrayList();
317
318 while (iterator.hasNext()) {
319 List nextField = (List) iterator.next();
320
321 if (nextField.get(0).equals(name))
322 returnList.add(nextField.get(1));
323 }
324
325 return returnList;
326 }
327
328 /** Gets the name of the generated field in the ejb which corresponds to
329 * the specified ejb name and field name pair.
330 * @param name the name of the ejb
331 * @param fieldName the name of the field in the ejb
332 * @return a String array of the form {<ejb name>, <field name>} which
333 * represents the generated field for the ejb field
334 */
335 public String[] getGeneratedFieldForEjbField(String name,
336 String fieldName) {
337 List field = (List) getInverseFieldsMap().get(
338 Arrays.asList(new String[] { name, fieldName }));
339
340 return ((field != null) ? (String[]) field
341 .toArray(new String[2]) : null);
342 }
343
344 /** Gets the name of the ejb field which corresponds to the specified
345 * generated ejb name and field name pair.
346 * @param name the name of the ejb
347 * @param fieldName the name of the field in the ejb
348 * @return a String array of the form {<ejb name>, <field name>} which
349 * represents the inverse field for the generated field
350 */
351 public String[] getEjbFieldForGeneratedField(String name,
352 String fieldName) {
353 List field = (List) getGeneratedFieldsMap().get(
354 Arrays.asList(new String[] { name, fieldName }));
355
356 return ((field != null) ? (String[]) field
357 .toArray(new String[2]) : null);
358 }
359 }
|