FileDocCategorySizeDatePackage
EjbConversionHelper.javaAPI DocGlassfish v2 API16691Fri May 04 22:34:48 BST 2007com.sun.jdo.spi.persistence.support.ejb.ejbc

EjbConversionHelper

public class EjbConversionHelper extends Object implements com.sun.jdo.api.persistence.mapping.ejb.ConversionHelper

Fields Summary
private com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper
nameMapper
private com.sun.enterprise.deployment.EjbBundleDescriptor
bundle
private HashMap
ejbDescMap
private HashMap
ejbFieldMap
private HashMap
ejbKeyMap
private HashMap
ejbPerDescMap
private HashMap
ejbRelMap
boolean
generateFields
boolean
ensureValidation
Constructors Summary
public EjbConversionHelper(com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper nameMapper)


       
        this.nameMapper = nameMapper;
        this.bundle = nameMapper.getBundleDescriptor();

        Iterator iter = bundle.getEjbs().iterator();
        while (iter.hasNext()) {
            Object desc = iter.next();
            if (desc instanceof IASEjbCMPEntityDescriptor) {
                IASEjbCMPEntityDescriptor ejbDesc =
                        (IASEjbCMPEntityDescriptor)desc;

                String ejbName = ejbDesc.getName();
                //collect all ejbdesc
                ejbDescMap.put(ejbName, ejbDesc);

                //collect PersistenceDescriptor
                PersistenceDescriptor pers = ejbDesc.getPersistenceDescriptor();
                ejbPerDescMap.put(ejbName, pers);

                //collect pers fields
                Collection pFields = ejbDesc.getPersistentFields();
                HashMap fieldMap = new HashMap();
                Iterator fIter = pFields.iterator();
                while (fIter.hasNext()) {
                    String fieldName = ((PersistentFieldInfo)fIter.next()).name;
                    fieldMap.put(fieldName, fieldName);
                }
                ejbFieldMap.put(ejbName, fieldMap);

                //collect pseudo cmr fields
                List pseudoFields = nameMapper.getGeneratedRelationshipsForEjbName(ejbName);
                Iterator pIter = pseudoFields.iterator();
                while (pIter.hasNext()) {
                    addField(ejbName, (String)pIter.next());
                }

                //collect all keys
                Collection pKeys = ejbDesc.getPrimaryKeyFields();
                HashMap pKeyMap = new HashMap();
                Iterator kIter = pKeys.iterator();
                while (kIter.hasNext()) {
                    String fieldName = ((PersistentFieldInfo)kIter.next()).name;
                    pKeyMap.put(fieldName, fieldName);
                }
                ejbKeyMap.put(ejbName, pKeyMap);
            }
        }

        //collect relationship
        Set rels = bundle.getRelationships();
        Iterator relIter = rels.iterator();
        while (relIter.hasNext()) {
            RelationshipDescriptor rel = (RelationshipDescriptor)relIter.next();
            RelationRoleDescriptor source = rel.getSource();
            RelationRoleDescriptor sink = rel.getSink();

            //collect source RelationshipDescriptor
            String sourceEjbName = source.getOwner().getName();
            ArrayList sourceRels = (ArrayList)ejbRelMap.get(sourceEjbName);
            if (sourceRels == null) {
                sourceRels = new ArrayList();
                ejbRelMap.put(sourceEjbName, sourceRels);
            }
            sourceRels.add(rel);

            //collect source cmr field
            String sourceCMRField = source.getCMRField();
            if (sourceCMRField != null) {
                addField(sourceEjbName, sourceCMRField);
            }

            //collect sink RelationshipDescriptor
            String sinkEjbName = sink.getOwner().getName();
            ArrayList sinkRels = (ArrayList)ejbRelMap.get(sinkEjbName);
            if (sinkRels == null) {
                sinkRels = new ArrayList();
                ejbRelMap.put(sinkEjbName, sinkRels);
            }
            sinkRels.add(rel);

            //collect sink cmr field
            String sinkCMRField = sink.getCMRField();
            if (sinkCMRField != null) {
                addField(sinkEjbName, sinkCMRField);
            }
        }
    
Methods Summary
private voidaddField(java.lang.String ejbName, java.lang.String fieldName)

        HashMap fieldMap = (HashMap)ejbFieldMap.get(ejbName);
        if (fieldMap == null) {
            fieldMap = new HashMap();
            ejbFieldMap.put(ejbName, fieldMap);
        }
        fieldMap.put(fieldName, fieldName);
    
public booleanapplyDefaultUnknownPKClassStrategy(java.lang.String ejbName)
Returns flag whether the mapping conversion should apply the default strategy for dealing with unknown primary key classes. This method will only be called when {@link #generateFields} returns true.

param
ejbName The value of the ejb-name element for a bean.
return
true to apply the default unknown PK Class Strategy, false otherwise

        IASEjbCMPEntityDescriptor ejbDesc =
                (IASEjbCMPEntityDescriptor)ejbDescMap.get(ejbName);
        String keyClassName = ejbDesc.getPrimaryKeyClassName();
        return keyClassName != null &&
                keyClassName.equals(Object.class.getName());
    
public booleanensureValidation()
Returns the flag whether the mapping conversion should validate all fields against schema columns.

return
true to validate all the fields in the dot-mapping file.

        return ensureValidation;
    
public booleangenerateFields()
Returns the flag whether the mapping conversion should generate relationship fields and primary key fields to support run-time. The version field is always created even {@link #generateFields} is false because it holds version column information.

return
true to generate fields in the dot-mapping file (if they are not present).

        return generateFields;
    
public java.lang.Object[]getFields(java.lang.String ejbName)
If {@link #generateFields} is true, then this method will return an array of cmp + cmr + pseudo cmr fields, otherwise the method will return an array of cmp + cmr fields.

param
ejbName The ejb-name element for the bean
param
fieldName The name of a container managed field in the named bean
return
an array of fields in the ejb bean

        HashMap fieldMap = (HashMap)ejbFieldMap.get(ejbName);
        if (fieldMap != null) {
            List fields = new ArrayList(fieldMap.keySet());
            if (!generateFields) {
                fields.removeAll(getGeneratedRelationships(ejbName));
            }
            return fields.toArray();
        }
        return null;
    
public java.lang.StringgetGeneratedPKFieldName()
Returns the name used for generated primary key fields.

return
a string for key field name

        return nameMapper.GENERATED_KEY_FIELD_NAME;
    
public java.util.ListgetGeneratedRelationships(java.lang.String ejbName)
Returns a list of generated relationship field names.

param
ejbName The ejb-name element for the bean
return
a list of generated relationship field names

        return nameMapper.getGeneratedRelationshipsForEjbName(ejbName);
 
    
public java.lang.StringgetGeneratedVersionFieldNamePrefix()
Returns the prefix used for generated version fields.

return
a string for version field name prefix

        return nameMapper.GENERATED_VERSION_FIELD_PREFIX;
    
public java.lang.StringgetInverseFieldName(java.lang.String ejbName, java.lang.String fieldName)
This method return the fieldName of relation role on the other end.

        RelationRoleDescriptor oppRole = getRelationRoleDescriptor(ejbName,
                fieldName, false);
        String inverseName = oppRole.getCMRField();

        // if we are generating relationships, check for a generated inverse
        if ((generateFields) && (inverseName == null))
            inverseName = nameMapper.getGeneratedFieldForEjbField(
                ejbName, fieldName)[1];

        return inverseName;
    
public java.lang.StringgetMappedClassName(java.lang.String ejbName)

        return nameMapper.getPersistenceClassForEjbName(ejbName);
    
public java.lang.StringgetMultiplicity(java.lang.String ejbName, java.lang.String fieldName)
getMultiplicity of the other role on the relationship Please note that multiplicity is JDO style

        RelationRoleDescriptor oppRole = getRelationRoleDescriptor(ejbName,
                fieldName, false);
        return (oppRole.getIsMany()) ? MANY : ONE;
    
private com.sun.enterprise.deployment.RelationRoleDescriptorgetRealRelationRoleDescriptor(java.lang.String ejbName, java.lang.String cmrFieldName, boolean self)

        ArrayList rels = (ArrayList)ejbRelMap.get(ejbName);
        for (int i = 0; i < rels.size(); i++) {
            RelationshipDescriptor rel = (RelationshipDescriptor)rels.get(i);
            RelationRoleDescriptor source = rel.getSource();
            RelationRoleDescriptor sink = rel.getSink();
            if (ejbName.equals(source.getOwner().getName()) &&
                    cmrFieldName.equals(source.getCMRField())) {
                return (self) ? source : sink;
            } else if (ejbName.equals(sink.getOwner().getName()) &&
                    cmrFieldName.equals(sink.getCMRField())) {
                return (self) ? sink : source;
            }
        }
        throw new IllegalArgumentException();
    
private com.sun.enterprise.deployment.RelationRoleDescriptorgetRelationRoleDescriptor(java.lang.String ejbName, java.lang.String cmrFieldName, boolean self)

        String myEjbName = ejbName;
        String myCMRFieldName = cmrFieldName;
        boolean myself = self;
        if (isGeneratedRelationship(ejbName, cmrFieldName)) {
            String[] nfPair = nameMapper.getEjbFieldForGeneratedField(
                    ejbName, cmrFieldName);
            myEjbName = nfPair[0];
            myCMRFieldName = nfPair[1];
            myself = !self;
        }
        return getRealRelationRoleDescriptor(myEjbName, myCMRFieldName, myself);
    
public java.lang.StringgetRelationshipFieldContent(java.lang.String ejbName, java.lang.String fieldName)

        RelationRoleDescriptor oppRole = getRelationRoleDescriptor(ejbName,
                fieldName, false);
        return oppRole.getOwner().getName();
    
public java.lang.StringgetRelationshipFieldType(java.lang.String ejbName, java.lang.String fieldName)
This API will only be called from MappingFile when multiplicity is Many on the other role.

        if (isGeneratedRelationship(ejbName, fieldName)) {
            return java.util.Collection.class.getName();
        } else {
            PersistenceDescriptor pers =
                (PersistenceDescriptor)ejbPerDescMap.get(ejbName);
            return pers.getCMRFieldReturnType(fieldName);
        }
    
public booleanhasField(java.lang.String ejbName, java.lang.String fieldName)
If {@link #generateFields} is true, then this method will check if the field is one of the cmp + cmr + pseudo cmr fields, otherwise the method will check if the field is one of the cmp + cmr fields.

param
ejbName The ejb-name element for the bean
param
fieldName The name of a container managed field in the named bean
return
true if the bean contains the field, otherwise return false

        if (!generateFields && isGeneratedRelationship(ejbName, fieldName))
            return false;
        else {
            HashMap fieldMap = (HashMap)ejbFieldMap.get(ejbName);
            return (fieldMap != null) ? 
                (fieldMap.get(fieldName) != null) : false;
        }
    
public booleanisGeneratedField(java.lang.String ejbName, java.lang.String fieldName)
Returns true if the field is generated. There are three types of generated fields: generated relationships, unknown primary key fields, and version consistency fields.

param
ejbName The ejb-name element for the bean
param
fieldName The name of a container managed field in the named bean
return
true if the field is generated; false otherwise.

        return nameMapper.isGeneratedField(ejbName, fieldName);
    
public booleanisGeneratedRelationship(java.lang.String ejbName, java.lang.String fieldName)

        return nameMapper.isGeneratedEjbRelationship(ejbName, fieldName);
    
public booleanisKey(java.lang.String ejbName, java.lang.String fieldName, boolean candidate)
The boolean argument candidate is ignored in this case.

        HashMap keyMap = (HashMap)ejbKeyMap.get(ejbName);
        return (keyMap != null) ? (keyMap.get(fieldName) != null) : false;
    
public booleanrelatedObjectsAreDeleted(java.lang.String beanName, java.lang.String fieldName)

        RelationRoleDescriptor oppRole = getRelationRoleDescriptor(beanName, fieldName, false);
        return oppRole.getCascadeDelete();
    
public voidsetEnsureValidation(boolean isValidating)
Sets the flag whether the mapping conversion should validate all fields against schema columns.

param
isValidating a boolean of indicating validating fields or not

        ensureValidation = isValidating;
    
public voidsetGenerateFields(boolean generateFields)
Sets the flag whether the mapping conversion should generate relationship fields, primary key fields, and version fields to support run-time.

param
generateFields a flag which indicates whether fields should be generated

        this.generateFields = generateFields;