FileDocCategorySizeDatePackage
NameMapper.javaAPI DocGlassfish v2 API7981Fri May 04 22:34:54 BST 2007com.sun.jdo.spi.persistence.support.ejb.model.util

NameMapper

public abstract class NameMapper extends com.sun.jdo.api.persistence.mapping.ejb.AbstractNameMapper
This is a class which helps translate between the various names of the CMP (ejb name, abstract schema, abstract bean, concrete bean, local interface, remote interface) and the persistence-capable class name. It also has methods for translation of field names. The basic entry point is ejb name or persistence-capable class name. This is a subclass of the AbstractNameMapper and implements the methods based on DOL. It also adds methods which are used during deployment time but not needed during development time and therefore, not in the abstract superclass.
author
Rochelle Raccah

Fields Summary
private EjbBundleDescriptor
_bundleDescriptor
private Map
_generatedRelToInverseRelMap
private Map
_relToInverseGeneratedRelMap
Constructors Summary
protected NameMapper(EjbBundleDescriptor bundleDescriptor)
Creates a new instance of NameMapper

param
bundleDescriptor the EjbBundleDescriptor which defines the universe of names for this application.

		_bundleDescriptor = bundleDescriptor;
		initGeneratedRelationshipMaps();
	
Methods Summary
public EjbBundleDescriptorgetBundleDescriptor()
Gets the EjbBundleDescriptor which defines the universe of names for this application.

return
the EjbBundleDescriptor which defines the universe of names for this application.

		return _bundleDescriptor;
	
public abstract java.lang.StringgetConcreteBeanClassForEjbName(java.lang.String name)
Gets the name of the concrete bean class which corresponds to the specified ejb.

param
name the name of the ejb
return
the name of the concrete bean for the specified ejb

public abstract EjbCMPEntityDescriptorgetDescriptorForEjbName(java.lang.String name)
Gets the EjbCMPEntityDescriptor which represents the ejb with the specified name.

param
name the name of the ejb
return
the EjbCMPEntityDescriptor which represents the ejb.

public abstract java.lang.StringgetEjbNameForAbstractSchema(java.lang.String schemaName)
Gets the name of the ejb which corresponds to the specified abstract schema name.

param
schemaName the name of the abstract schema
return
the name of the ejb for the specified abstract schema

protected java.util.MapgetGeneratedFieldsMap()

		return _generatedRelToInverseRelMap;
	
protected java.util.MapgetInverseFieldsMap()

 return _relToInverseGeneratedRelMap; 
public intgetKeyClassTypeForEjbName(java.lang.String name)
Get the type of key class of this ejb.

return
the key class type, one of {@link #USER_DEFINED_KEY_CLASS}, {@link #PRIMARY_KEY_FIELD}, or {@link #UNKNOWN_KEY_CLASS}

		String keyClass = getKeyClassForEjbName(name);

		if (!"java.lang.Object".equals(keyClass))		// NOI18N
		{
			EjbCMPEntityDescriptor descriptor = getDescriptorForEjbName(name);

			return ((descriptor.getPrimaryKeyFieldDesc() != null) ?
				PRIMARY_KEY_FIELD : USER_DEFINED_KEY_CLASS);
		}

		return UNKNOWN_KEY_CLASS;
	
private booleanhasField(PersistenceDescriptor persistenceDescriptor, java.lang.String fieldName)

		Class fieldType = null;

		try
		{
			fieldType = persistenceDescriptor.getTypeFor(fieldName);
		}
		catch (RuntimeException e)
		{
			// fieldType will be null - there is no such field
		}

		return (fieldType != null);
	
private voidinitGeneratedRelationshipMaps()

		EjbBundleDescriptor bundleDescriptor = getBundleDescriptor();
		Set relationships = bundleDescriptor.getRelationships();

		_generatedRelToInverseRelMap = new HashMap();
		_relToInverseGeneratedRelMap = new HashMap();

		// during development time this code may attempt to get the 
		// iterator even with no relationships, so protect it by a 
		// null check
		if (relationships != null)
		{
			Iterator iterator = relationships.iterator();
			List generatedRels = new ArrayList();
			int counter = 0;

			// gather list of generated cmr fields by examining source and sink
			while (iterator.hasNext())
			{
				RelationshipDescriptor relationship = 
					(RelationshipDescriptor)iterator.next();

				if (relationship.getSource().getCMRField() == null)
					generatedRels.add(relationship);

				if (relationship.getSink().getCMRField() == null)
					generatedRels.add(relationship);
			}

			// now update the maps to contain this info
			iterator = generatedRels.iterator();
			while (iterator.hasNext())
			{
				RelationshipDescriptor relationship = 
					(RelationshipDescriptor)iterator.next();
				RelationRoleDescriptor source = relationship.getSource();
				String sourceEjbName = source.getOwner().getName();
				String sourceCMRField = source.getCMRField();
				boolean sourceIsNull = (sourceCMRField == null);
				RelationRoleDescriptor sink = relationship.getSink();
				String sinkEjbName = sink.getOwner().getName();
				String ejbName = (sourceIsNull ? sourceEjbName : sinkEjbName);
				String otherEjbName = 
					(sourceIsNull ? sinkEjbName : sourceEjbName);
				List ejbField = Arrays.asList(new String[]{otherEjbName, 
					(sourceIsNull ? sink.getCMRField() : sourceCMRField)});
				PersistenceDescriptor pDescriptor = ((EjbCMPEntityDescriptor)
					bundleDescriptor.getEjbByName(ejbName)).
					getPersistenceDescriptor();
				List generatedField = null;
				String uniqueName = null;

				// make sure the user doesn't already have a field
				// with this name
				do
				{
					counter++;
					uniqueName = GENERATED_CMR_FIELD_PREFIX + counter;
				} while (hasField(pDescriptor, uniqueName));

				generatedField = 
					Arrays.asList(new String[]{ejbName, uniqueName});
				_generatedRelToInverseRelMap.put(generatedField, ejbField);
				_relToInverseGeneratedRelMap.put(ejbField, generatedField);
			}
		}