FileDocCategorySizeDatePackage
MetadataProject.javaAPI DocGlassfish v2 API27730Wed Jun 06 15:46:16 BST 2007oracle.toplink.essentials.internal.ejb.cmp3.metadata

MetadataProject

public class MetadataProject extends Object
author
Guy Pelletier
since
TopLink EJB 3.0 Reference Implementation

Fields Summary
protected PersistenceUnitInfo
m_PUInfo
protected Collection
m_entityNames
protected Map
m_mappingFiles
protected AbstractSession
m_session
protected boolean
m_enableLazyForOneToOne
protected MetadataPersistenceUnit
m_persistenceUnit
protected HashMap
m_mappedSuperclassNodes
protected HashMap
m_mappedSuperclasses
protected HashMap
m_embeddableNodes
protected HashMap
m_embeddables
protected HashMap
m_allDescriptors
protected HashSet
m_descriptorsWithRelationships
protected HashMap
m_namedQueries
protected HashMap
m_namedNativeQueries
protected HashMap
m_generatedValues
protected HashMap
m_tableGenerators
protected HashMap
m_sequenceGenerators
protected HashMap
m_defaultListeners
Constructors Summary
public MetadataProject(PersistenceUnitInfo puInfo, AbstractSession session, boolean enableLazyForOneToOne)
INTERNAL:


          
           
        m_PUInfo = puInfo;
        m_session = session;
        m_enableLazyForOneToOne = enableLazyForOneToOne;

        m_defaultListeners = new HashMap<XMLHelper, NodeList>();

        m_namedQueries = new HashMap<String, MetadataNamedQuery>();
        m_namedNativeQueries = new HashMap<String, MetadataNamedNativeQuery>();

        m_mappedSuperclassNodes = new HashMap<String, Node>();
        m_mappedSuperclasses = new HashMap<String, XMLHelper>();

        m_embeddableNodes = new HashMap<String, Node>();
        m_embeddables = new HashMap<String, XMLHelper>();

        m_allDescriptors = new HashMap<String, MetadataDescriptor>();
        m_descriptorsWithRelationships = new HashSet<MetadataDescriptor>();

        m_generatedValues = new HashMap<Class, MetadataGeneratedValue>();
        m_tableGenerators = new HashMap<String, MetadataTableGenerator>();
        m_sequenceGenerators = new HashMap<String, MetadataSequenceGenerator>();
    
Methods Summary
public voidaddDefaultListeners(org.w3c.dom.NodeList nodes, oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelper helper)
INTERNAL:

        m_defaultListeners.put(helper, nodes);
    
public voidaddDescriptor(oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor descriptor)
INTERNAL: This method will add the descriptor to the actual TopLink project as well if it has not already been added.

        // Set the persistence unit defaults (if there are any) on the descriptor.

        if (m_persistenceUnit != null) {
            descriptor.setAccess(m_persistenceUnit.getAccess());
            descriptor.setSchema(m_persistenceUnit.getSchema());
            descriptor.setCatalog(m_persistenceUnit.getCatalog());
            descriptor.setIsCascadePersist(m_persistenceUnit.isCascadePersist());
            descriptor.setIgnoreAnnotations(m_persistenceUnit.isMetadataComplete());
        }

        Project project = getSession().getProject();
        ClassDescriptor descriptorOnProject = MetadataHelper.findDescriptor(project, descriptor.getJavaClass());

        if (descriptorOnProject == null) {
            project.addDescriptor(descriptor.getClassDescriptor());
        } else {
            descriptor.setDescriptor(descriptorOnProject);
        }

        m_allDescriptors.put(descriptor.getJavaClassName(), descriptor);
    
public voidaddEmbeddable(java.lang.Class embeddable, org.w3c.dom.Node node, oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelper helper)
INTERNAL: Add an embeddable that we found in an XML document.

        m_embeddables.put(embeddable.getName(), helper);
        m_embeddableNodes.put(embeddable.getName(), node);
    
public voidaddGeneratedValue(oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataGeneratedValue metadatageneratedvalue, java.lang.Class entityClass)
INTERNAL:

        m_generatedValues.put(entityClass, metadatageneratedvalue);
    
public voidaddMappedSuperclass(java.lang.Class mappedSuperclass, org.w3c.dom.Node node, oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelper helper)
INTERNAL: Add a mapped-superclass that we found in an XML document.

        m_mappedSuperclasses.put(mappedSuperclass.getName(), helper);
        m_mappedSuperclassNodes.put(mappedSuperclass.getName(), node);
    
public voidaddNamedNativeQuery(oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataNamedNativeQuery namedNativeQuery)
INTERNAL:

        m_namedNativeQueries.put(namedNativeQuery.getName(), namedNativeQuery);
    
public voidaddNamedQuery(oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataNamedQuery namedQuery)
INTERNAL:

        m_namedQueries.put(namedQuery.getName(), namedQuery);
    
public voidaddRelationshipDescriptor(oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor descriptor)
INTERNAL:

        m_descriptorsWithRelationships.add(descriptor);
    
public voidaddSequenceGenerator(oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataSequenceGenerator sequenceGenerator)
INTERNAL:

        m_sequenceGenerators.put(sequenceGenerator.getName(), sequenceGenerator);
    
public voidaddTableGenerator(oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataTableGenerator tableGenerator)
INTERNAL:

        m_tableGenerators.put(tableGenerator.getName(), tableGenerator);
    
public voidcleanup()
INTERNAL: This method frees up resources acquired by this object.

        // get rid of the DOM trees.
        m_mappingFiles.clear();
    
public booleancontainsDescriptor(java.lang.Class cls)
INTERNAL:

        return m_allDescriptors.containsKey(cls.getName());
    
public booleanenableLazyForOneToOne()
INTERNAL:

        return m_enableLazyForOneToOne;
    
public java.util.HashMapgetDefaultListeners()
INTERNAL:

        return m_defaultListeners;
    
public oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptorgetDescriptor(java.lang.Class cls)
INTERNAL:

        if (cls == null) {
            return null;
        } else {
            MetadataDescriptor descriptor = m_allDescriptors.get(cls.getName());
            if (descriptor == null) {
                throw ValidationException.classNotListedInPersistenceUnit(cls.getName());
            } else {
                return descriptor;
            }
        }
    
public java.util.CollectiongetDescriptors()
INTERNAL:

        return m_allDescriptors.values();
    
public oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelpergetEmbeddableHelper(java.lang.Class cls)
INTERNAL:

        return m_embeddables.get(cls.getName());
    
public org.w3c.dom.NodegetEmbeddableNode(java.lang.Class cls)
INTERNAL:

        return m_embeddableNodes.get(cls.getName());
    
public java.util.CollectiongetEntityNames()
INTERNAL:

        return Collections.unmodifiableCollection(m_entityNames);
    
public oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelpergetMappedSuperclassHelper(java.lang.Class cls)
INTERNAL:

        return m_mappedSuperclasses.get(cls.getName());
    
public org.w3c.dom.NodegetMappedSuperclassNode(java.lang.Class cls)
INTERNAL:

        return m_mappedSuperclassNodes.get(cls.getName());
    
public java.util.MapgetMappingFiles()
INTERNAL:

        return Collections.unmodifiableMap(m_mappingFiles);
    
public oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataNamedNativeQuerygetNamedNativeQuery(java.lang.String name)
INTERNAL:

        return m_namedNativeQueries.get(name);
    
public oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataNamedQuerygetNamedQuery(java.lang.String name)
INTERNAL:

        return m_namedQueries.get(name);
    
public javax.persistence.spi.PersistenceUnitInfogetPUInfo()
INTERNAL:

        return m_PUInfo;
    
public oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataPersistenceUnitgetPersistenceUnit()
INTERNAL: Set the classes for processing.

        return m_persistenceUnit;
    
public java.util.HashSetgetRelationshipDescriptors()
INTERNAL:

        return m_descriptorsWithRelationships;
    
public oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataSequenceGeneratorgetSequenceGenerator(java.lang.String name)
INTERNAL:

        return m_sequenceGenerators.get(name);
    
public java.util.CollectiongetSequenceGenerators()
INTERNAL:

        return m_sequenceGenerators.values();
    
public oracle.toplink.essentials.internal.sessions.AbstractSessiongetSession()
INTERNAL:

        return m_session;
    
public oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataTableGeneratorgetTableGenerator(java.lang.String name)
INTERNAL:

        return m_tableGenerators.get(name);
    
public java.util.CollectiongetTableGenerators()
INTERNAL:

        return m_tableGenerators.values();
    
public booleanhasConflictingSequenceGenerator(oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataSequenceGenerator sequenceGenerator)
INTERNAL:

        if (hasSequenceGenerator(sequenceGenerator.getName())) {
            return ! getSequenceGenerator(sequenceGenerator.getName()).equals(sequenceGenerator);
        } else {
            return false;
        }
    
public booleanhasConflictingTableGenerator(oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataTableGenerator tableGenerator)
INTERNAL:

        if (hasTableGenerator(tableGenerator.getName())) {
            return ! getTableGenerator(tableGenerator.getName()).equals(tableGenerator);
        } else {
            return false;
        }
    
public booleanhasDescriptors()
INTERNAL:

        return ! m_allDescriptors.isEmpty();
    
public booleanhasEmbeddable(java.lang.Class cls)
INTERNAL:

        return m_embeddables.containsKey(cls.getName());
    
public booleanhasMappedSuperclass(java.lang.Class cls)
INTERNAL:

        return m_mappedSuperclasses.containsKey(cls.getName());
    
public booleanhasNamedNativeQuery(java.lang.String name)
INTERNAL:

        return m_namedNativeQueries.containsKey(name);
    
public booleanhasNamedQuery(java.lang.String name)
INTERNAL:

        return m_namedQueries.containsKey(name);
    
public booleanhasPersistenceUnit()
INTERNAL: Set the classes for processing.

        return m_persistenceUnit != null;
    
public booleanhasSequenceGenerator(java.lang.String name)
INTERNAL:

        return getSequenceGenerator(name) != null;
    
public booleanhasTableGenerator(java.lang.String name)
INTERNAL:

        return getTableGenerator(name) != null;
    
public voidprocess()
INTERNAL: Stage 2 processing. That is, it does all the extra processing that couldn't be completed in the original metadata accessor processing.

        processSequencing();
        processRelationshipDescriptors();
    
public voidprocessNamedNativeQueries(java.lang.ClassLoader loader)
INTERNAL: Process the named native queries we found and add them to the given session.

        for (MetadataNamedNativeQuery query : m_namedNativeQueries.values()) {
            HashMap<String, String> hints = processQueryHints(query.getHints(), query.getName());

            Class resultClass = query.getResultClass();

            if (resultClass != void.class) {
                resultClass = MetadataHelper.getClassForName(resultClass.getName(), loader);
                m_session.addQuery(query.getName(), EJBQueryImpl.buildSQLDatabaseQuery(resultClass, query.getEJBQLString(), hints));
            } else { 
                String resultSetMapping = query.getResultSetMapping();

                if (! resultSetMapping.equals("")) {
                    m_session.addQuery(query.getName(), EJBQueryImpl.buildSQLDatabaseQuery(resultSetMapping, query.getEJBQLString(), hints));
                } else {
                    // Neither a resultClass or resultSetMapping is specified so place in a temp query on the session
                    m_session.addQuery(query.getName(), EJBQueryImpl.buildSQLDatabaseQuery(query.getEJBQLString(), hints));
                }
            }
        }
    
public voidprocessNamedQueries(oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataValidator validator)
INTERNAL: Process the named queries we found and add them to the given session.

        for (MetadataNamedQuery query : m_namedQueries.values()) {
            try {
                HashMap<String, String> hints = processQueryHints(query.getHints(), query.getName());
                m_session.addEjbqlPlaceHolderQuery(new EJBQLPlaceHolderQuery(query.getName(), query.getEJBQLString(), hints));
            } catch (Exception exception) {
                validator.throwErrorProcessingNamedQueryAnnotation(query.getClass(), query.getName(), exception);
            }
        }
    
protected java.util.HashMapprocessQueryHints(java.util.List hints, java.lang.String queryName)
INTERNAL: Process a list of MetadataQueryHint.

        HashMap<String, String> hm = new HashMap<String, String>();

        for (MetadataQueryHint hint : hints) {
            QueryHintsHandler.verify(hint.getName(), hint.getValue(), queryName, m_session);
            hm.put(hint.getName(), hint.getValue());
        } 
		
        return hm;
    
protected voidprocessRelationshipDescriptors()
INTERNAL: Process the related descriptors.

        for (MetadataDescriptor descriptor : (HashSet<MetadataDescriptor>) getRelationshipDescriptors()) {
            for (RelationshipAccessor accessor : (Collection<RelationshipAccessor>) descriptor.getRelationshipAccessors()) {
                accessor.processRelationship();
            }
        }
    
protected voidprocessSequencing()
INTERNAL: Process the sequencing information.

        if (! m_generatedValues.isEmpty()) {
            DatasourceLogin login = m_session.getProject().getLogin();
            
            // Generators referenced from Id should have correct type
            for (MetadataGeneratedValue generatedValue : m_generatedValues.values()) {
                String type = generatedValue.getStrategy();
                String generatorName = generatedValue.getGenerator();
                
                if (type.equals(MetadataConstants.TABLE)) {
                    MetadataSequenceGenerator sequenceGenerator = m_sequenceGenerators.get(generatorName);
                    
                    if (sequenceGenerator != null) {
                        // WIP
                    }
                } else if (type.equals(MetadataConstants.SEQUENCE) || type.equals(MetadataConstants.IDENTITY)) {
                    MetadataTableGenerator tableGenerator = m_tableGenerators.get(generatorName);
                    
                    if (tableGenerator != null) {
                        // WIP
                    }
                }
            }
    
            Sequence defaultAutoSequence = null;
            TableSequence defaultTableSequence = new TableSequence(MetadataConstants.DEFAULT_TABLE_GENERATOR);
            NativeSequence defaultNativeSequence = new NativeSequence(MetadataConstants.DEFAULT_SEQUENCE_GENERATOR);
            
            // Sequences keyed on generator names.
            Hashtable<String, Sequence> sequences = new Hashtable<String, Sequence>();
            
            for (MetadataSequenceGenerator sequenceGenerator : m_sequenceGenerators.values()) {
                String sequenceGeneratorName = sequenceGenerator.getName();
                String seqName = (sequenceGenerator.getSequenceName().equals("")) ? sequenceGeneratorName : sequenceGenerator.getSequenceName();
                NativeSequence sequence = new NativeSequence(seqName, sequenceGenerator.getAllocationSize());
                sequences.put(sequenceGeneratorName, sequence);
                
                if (sequenceGeneratorName.equals(MetadataConstants.DEFAULT_AUTO_GENERATOR)) {
                    // SequenceGenerator defined with DEFAULT_AUTO_GENERATOR.
                    // The sequence it defines will be used as a defaultSequence.
                    defaultAutoSequence = sequence;
                } else if (sequenceGeneratorName.equals(MetadataConstants.DEFAULT_SEQUENCE_GENERATOR)) {
                    // SequenceGenerator deinfed with DEFAULT_SEQUENCE_GENERATOR.
                    // All sequences of GeneratorType SEQUENCE and IDENTITY 
                    // referencing non-defined generators will use a clone of 
                    // the sequence defined by this generator.
                    defaultNativeSequence = sequence;
                }
            }

            for (MetadataTableGenerator tableGenerator : m_tableGenerators.values()) {
                String tableGeneratorName = tableGenerator.getName();
                String seqName = (tableGenerator.getPkColumnValue().equals("")) ? tableGeneratorName : tableGenerator.getPkColumnValue();
                TableSequence sequence = new TableSequence(seqName, tableGenerator.getAllocationSize(), tableGenerator.getInitialValue());
                sequences.put(tableGeneratorName, sequence);

                //bug 2647: pull schema and catalog defaults from the persistence Unit if they are not defined.  
                String catalogName = tableGenerator.getCatalog();
                String schemaName = tableGenerator.getSchema();
                if (this.getPersistenceUnit()!=null){
                    catalogName = catalogName.length()>0? catalogName: this.getPersistenceUnit().getCatalog();
                    schemaName = schemaName.length()>0? schemaName: this.getPersistenceUnit().getSchema();
                }

                // Get the database table from the @TableGenerator values.
                // In case tableGenerator.table().equals("") default sequence 
                // table name will be extracted from sequence and used, see 
                // TableSequence class.
                sequence.setTable(new DatabaseTable(MetadataHelper.getFullyQualifiedTableName(tableGenerator.getTable(), sequence.getTableName(), catalogName, schemaName)));
                
                // Process the @UniqueConstraints for this table.
                for (String[] uniqueConstraint : tableGenerator.getUniqueConstraints()) {
                    sequence.getTable().addUniqueConstraints(uniqueConstraint);
                }
                
                if (! tableGenerator.getPkColumnName().equals("")) {
                    sequence.setNameFieldName(tableGenerator.getPkColumnName());
                }
                    
                if (! tableGenerator.getValueColumnName().equals("")) {
                    sequence.setCounterFieldName(tableGenerator.getValueColumnName());
                }

                if (tableGeneratorName.equals(MetadataConstants.DEFAULT_AUTO_GENERATOR)) {
                    // TableGenerator defined with DEFAULT_AUTO_GENERATOR.
                    // The sequence it defines will be used as a defaultSequence.
                    defaultAutoSequence = sequence;
                } else if (tableGeneratorName.equals(MetadataConstants.DEFAULT_TABLE_GENERATOR)) {
                    // SequenceGenerator defined with DEFAULT_TABLE_GENERATOR. 
                    // All sequences of GenerationType TABLE referencing non-
                    // defined generators will use a clone of the sequence 
                    // defined by this generator.
                    defaultTableSequence = sequence;
                }
            }

            // Finally loop through descriptors and set sequences as required into 
            // Descriptors and Login
            for (Class entityClass : m_generatedValues.keySet()) {
                MetadataDescriptor descriptor = m_allDescriptors.get(entityClass.getName());
                MetadataGeneratedValue generatedValue = m_generatedValues.get(entityClass);
                String generatorName = generatedValue.getGenerator();
                Sequence sequence = null;

                if (! generatorName.equals("")) {
                    sequence = sequences.get(generatorName);
                }
                
                if (sequence == null) {
                    if (generatedValue.getStrategy().equals(MetadataConstants.TABLE)) {
                        if (generatorName.equals("")) {
                            sequence = defaultTableSequence;
                        } else {
                            sequence = (Sequence)defaultTableSequence.clone();
                            sequence.setName(generatorName);
                        }
                    } else if (generatedValue.getStrategy().equals(MetadataConstants.SEQUENCE) || generatedValue.getStrategy().equals(MetadataConstants.IDENTITY)) {
                        if (generatorName.equals("")) {
                            sequence = defaultNativeSequence;
                        } else {
                            sequence = (Sequence)defaultNativeSequence.clone();
                            sequence.setName(generatorName);
                        }
                    }
                }

                if (sequence != null) {
                    descriptor.setSequenceNumberName(sequence.getName());
                    login.addSequence(sequence);
                } else if (generatedValue.getStrategy().equals(MetadataConstants.AUTO)) {
                    if (defaultAutoSequence != null) {
                        descriptor.setSequenceNumberName(defaultAutoSequence.getName());
                        login.setDefaultSequence(defaultAutoSequence);
                    } else {
                        descriptor.setSequenceNumberName(MetadataConstants.DEFAULT_AUTO_GENERATOR);
                    }
                }
            }
        }
    
public voidsetEntityNames(java.util.Collection entityNames)
INTERNAL:

        m_entityNames.clear();
        m_entityNames.addAll(entityNames);
    
public voidsetMappingFiles(java.util.Map mappingFiles)
INTERNAL:

        m_mappingFiles.clear();
        m_mappingFiles.putAll(mappingFiles);
    
public voidsetPersistenceUnit(oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataPersistenceUnit persistenceUnit)
INTERNAL: Set the classes for processing.

        m_persistenceUnit = persistenceUnit;