All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.metadata.AbstractClassMetaData Maven / Gradle / Ivy

Go to download

DataNucleus Core provides the primary components of a heterogenous Java persistence solution. It supports persistence API's being layered on top of the core functionality. Also includes the JDO API.

There is a newer version: 5.2.0-release
Show newest version
/**********************************************************************
Copyright (c) 2005 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Contributors:
    ...
**********************************************************************/
package org.datanucleus.metadata;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ClassNameConstants;
import org.datanucleus.PropertyNames;
import org.datanucleus.api.ApiAdapter;
import org.datanucleus.exceptions.ClassNotResolvedException;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.store.StoreManager;
import org.datanucleus.util.ClassUtils;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.MacroString;
import org.datanucleus.util.StringUtils;
import org.datanucleus.util.ViewUtils;

/**
 * Abstract representation of the MetaData of a class/interface.
 * Has a parent PackageMetaData that can contain the metadata for several classes/interfaces. 
 * Is extended by ClassMetaData and InterfaceMetaData.
 * Of the things that it contains the main one are the "members" which are the MetaData for
 * all fields and properties that are persistable.
 */
public abstract class AbstractClassMetaData extends MetaData
{
    private static final long serialVersionUID = -2433561862769017940L;

    /** Suffix to add on to the class name for any generated primary key class. */
    public static final String GENERATED_PK_SUFFIX = "_PK";

    /** Class name */
    protected final String name;

    /** Entity name. Required by JPA $4.3.1 for accessing this class in queries. */
    protected String entityName;

    /** Whether this class is explicitly marked as MappedSuperclass. Will be false when using JDO. */
    protected boolean mappedSuperclass = false;

    /** 
     * Whether the class is fully defined, and hence instantiable. This is false when it is a JPA MappedSuperclass
     * and has no PK fields defined (will be defined in the derived Entity). 
     * This is different to whether the class is abstract - use isAbstract() for that.
     */
    protected boolean instantiable = true;

    protected Boolean accessViaField = null;

    /** Identity-type tag value. */
    protected IdentityType identityType = IdentityType.DATASTORE;

    /** persistence-modifier tag value. */
    protected ClassPersistenceModifier persistenceModifier = ClassPersistenceModifier.PERSISTENCE_CAPABLE;

    /** persistable-superclass tag value (deprecated). */
    protected String persistableSuperclass;

    /** objectid-class tag value. */
    protected String objectidClass;

    /** requires-extent tag value. */
    protected boolean requiresExtent = true;

    /** detachable tag value. */
    protected boolean detachable = false;

    /** embedded-only tag value. */
    protected boolean embeddedOnly = false;

    /** Catalog name (O/R mapping). */
    protected String catalog;

    /** Schema name (O/R mapping). */
    protected String schema;

    /** Table name (O/R mapping). This may be of the form "[database].[catalog].[schema].table" */
    protected String table;

    /** cacheable tag value. */
    protected Boolean cacheable = null;

    /** Full name (e.g org.datanucleus.MyClass) */
    protected final String fullName;

    /** Version MetaData */
    protected VersionMetaData versionMetaData;

    /** Identity MetaData */
    protected IdentityMetaData identityMetaData;

    /** Flag whether the identity was specified by the user. */
    protected boolean identitySpecified = false;

    /** Inheritance MetaData */
    protected InheritanceMetaData inheritanceMetaData;

    /** PrimaryKey MetaData */
    protected PrimaryKeyMetaData primaryKeyMetaData;

    /** EventListeners. Use a list to preserve ordering. */
    protected List listeners = null;

    /** Flag to exclude superclass listeners. */
    protected Boolean excludeSuperClassListeners = null;

    /** Flag to exclude default listeners. */
    protected Boolean excludeDefaultListeners = null;

    /** Convenience lookup map of fetch group by the name. */
    protected Map fetchGroupMetaDataByName;

    /** Class MetaData for the persistable superclass (if any) */
    protected AbstractClassMetaData pcSuperclassMetaData = null;

    /** Flag for whether the MetaData here is complete without annotations. Used by JPA. */
    protected boolean metaDataComplete = false;

    /** Whether to lock objects of this type on read operations. */
    protected boolean serializeRead = false;

    /** Named queries */
    protected Collection queries = null;

    /** Named stored procs */
    protected Collection storedProcQueries = null;

    /** List of query result MetaData defined for this file. */
    protected Collection queryResultMetaData = null;

    /** JoinMetaData */
    protected JoinMetaData[] joinMetaData;

    /** IndexMetaData */
    protected IndexMetaData[] indexMetaData;

    /** ForeignKeyMetaData */
    protected ForeignKeyMetaData[] foreignKeyMetaData;

    /** UniqueMetaData */
    protected UniqueMetaData[] uniqueMetaData;

    /** List of members (fields/properties). */
    protected List members = new ArrayList();

    /** The columns that are present in the datastore yet not mapped to fields in this class. */
    protected List unmappedColumns = null;

    protected Set fetchGroups = new HashSet();

    // These fields are only used when the MetaData is read by the parser and elements are dynamically added to 
    // the other elements. At initialise() they are cleared and nulled so shouldn't be used internally.

    /** List of joins */
    protected List joins = new ArrayList();

    /** List of foreign-key */
    protected List foreignKeys = new ArrayList();

    /** List of indexes */
    protected List indexes = new ArrayList();

    /** List of uniqueConstraints */
    protected List uniqueConstraints = new ArrayList();

    // Fields below here are not represented in the output MetaData. They are for use internally in the operation of the system.
    // The majority are for convenience to save iterating through the fields since the fields are fixed once initialised.

    /**
     * Managed fields/properties of this class. Contains the same AbstractMemberMetaData objects as are in "members".
     * Doesnt include any overridden members which are stored separately. All fields will return true to
     * "fmd.isJdoField()".
     */
    protected AbstractMemberMetaData[] managedMembers;

    /** Fields/properties for superclasses that are overridden in this class. */
    protected AbstractMemberMetaData[] overriddenMembers;

    /** Position numbers of members mapped by the name of the field/property. */
    protected Map memberPositionsByName;

    /** Positions of all fields/properties (inc superclasses). */
    protected int[] allMemberPositions;

    /** Positions of the primary-key fields/properties (inc superclasses). */
    protected int[] pkMemberPositions;

    /** Positions of the non-primary-key fields/properties (inc superclasses). */
    protected int[] nonPkMemberPositions;

    /** Flags of the non-primary key fields/properties (inc superclasses). */
    protected boolean[] nonPkMemberFlags;

    /** Positions of the default-fetch-group fields/properties (inc superclasses). */
    protected int[] dfgMemberPositions;

    /** Flags of the default-fetch-group state for all fields/properties. */
    protected boolean[] dfgMemberFlags;

    /** Positions of the SCO mutable fields/properties (inc superclasses). */
    protected int[] scoMutableMemberPositions;

    /** Flags of the SCO mutable state for all fields/properties. */
    protected boolean[] scoMutableMemberFlags;

    /** Absolute positions of all SCO fields/properties that aren't containers. */
    protected int[] scoNonContainerMemberPositions = null;

    /** Absolute positions of the fields/properties that have relations. */
    protected int[] relationPositions = null;

    /** No of managed fields/properties in superclasses, that are inherited by this class. */
    protected int noOfInheritedManagedMembers = 0;

    /** if this persistable class uses SingleFieldIdentity */
    protected boolean usesSingleFieldIdentityClass;

    /** number of managed fields/properties from this class plus inherited classes. */
    protected int memberCount;

    protected boolean implementationOfPersistentDefinition = false;

    /** whether the populate method is running **/
    boolean populating = false;

    /** whether the initialise method is running **/
    boolean initialising = false;

    /** Cached result of {@link #hasFetchGroupWithPostLoad()} */
    protected Boolean fetchGroupMetaWithPostLoad;

    /** Cached result of {@link #pkIsDatastoreAttributed(StoreManager)} */
    protected Boolean pkIsDatastoreAttributed = null;

    /** Cached result of {@link #hasRelations(ClassLoaderResolver, MetaDataManager)} */
    protected Boolean hasRelations = null;

    /** Implementation of "persistent-interface" needing table setting from superclass. */
    protected transient boolean persistentInterfaceImplNeedingTableFromSuperclass = false;

    /** Implementation of "persistent-interface" needing table setting from subclass. */
    protected transient boolean persistentInterfaceImplNeedingTableFromSubclass = false;

    /**
     * Constructor. Set fields using setters, before populate().
     * @param parent The package to which this class/interface belongs
     * @param name (Simple) name of class (omitting the package name)
     */
    protected AbstractClassMetaData(final PackageMetaData parent, final String name)
    {
        super(parent);

        if (StringUtils.isWhitespace(name))
        {
            throw new InvalidMetaDataException("044061", parent.name);
        }

        this.name = name;
        this.fullName = ClassUtils.createFullClassName(parent.name, name);
    }

    /**
     * Constructor for creating the ClassMetaData for an implementation of a "persistent-interface".
     * @param imd MetaData for the "persistent-interface"
     * @param implClassName Name of the implementation class
     * @param copyMembers Whether to copy the fields/properties of the interface too
     */
    public AbstractClassMetaData(InterfaceMetaData imd, String implClassName, boolean copyMembers)
    {
        this((PackageMetaData)imd.parent, implClassName);
        setMappedSuperclass(imd.mappedSuperclass);
        setRequiresExtent(imd.requiresExtent);
        setDetachable(imd.detachable);
        setTable(imd.table);
        setCatalog(imd.catalog);
        setSchema(imd.schema);
        setEntityName(imd.entityName);
        setObjectIdClass(imd.objectidClass);
        setPersistenceModifier(ClassPersistenceModifier.PERSISTENCE_CAPABLE);
        setEmbeddedOnly(imd.embeddedOnly);
        setIdentityType(imd.identityType);

        implementationOfPersistentDefinition = true;

        if (copyMembers)
        {
            copyMembersFromInterface(imd);
        }

        setVersionMetaData(imd.versionMetaData);
        setIdentityMetaData(imd.identityMetaData);
        setPrimaryKeyMetaData(imd.primaryKeyMetaData);

        if (imd.inheritanceMetaData != null)
        {
            if (imd.inheritanceMetaData.getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE)
            {
                // Flag the table as requiring setting based on the next superclass
                persistentInterfaceImplNeedingTableFromSuperclass = true;
            }
            else if (imd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE)
            {
                // Flag the table as requiring setting based on the next subclass
                persistentInterfaceImplNeedingTableFromSubclass = true;
            }

            InheritanceMetaData inhmd = new InheritanceMetaData();
            inhmd.setStrategy(InheritanceStrategy.NEW_TABLE);
            if (imd.inheritanceMetaData.getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE)
            {
                AbstractClassMetaData acmd = imd.getSuperAbstractClassMetaData();
                while (acmd != null)
                {
                    if (acmd.getInheritanceMetaData() != null)
                    {
                        if (acmd.getInheritanceMetaData().getStrategy()==InheritanceStrategy.NEW_TABLE)
                        {
                            if (acmd.getInheritanceMetaData().getDiscriminatorMetaData() != null)
                            {
                                inhmd.setDiscriminatorMetaData(
                                    new DiscriminatorMetaData(acmd.getInheritanceMetaData().getDiscriminatorMetaData()));
                            }
                            inhmd.setJoinMetaData(acmd.getInheritanceMetaData().getJoinMetaData());
                            break;
                        }
                    }
                    acmd = acmd.getSuperAbstractClassMetaData();
                }
            }
            else if (imd.inheritanceMetaData.getStrategy() == InheritanceStrategy.NEW_TABLE)
            {
                if (imd.getInheritanceMetaData().getDiscriminatorMetaData() != null)
                {
                    inhmd.setDiscriminatorMetaData(
                        new DiscriminatorMetaData(imd.getInheritanceMetaData().getDiscriminatorMetaData()));
                }
                inhmd.setJoinMetaData(imd.getInheritanceMetaData().getJoinMetaData());
            }
            setInheritanceMetaData(inhmd);
        }
        
        if (imd.joinMetaData != null)
        {
            for (int i=0;i memberIter = members.iterator();
                while (memberIter.hasNext())
                {
                    AbstractMemberMetaData mmd = memberIter.next();
                    if (mmd.isPrimaryKey())
                    {
                        noOfPkKeys++;
                    }
                }
                if (noOfPkKeys > 0)
                {
                    // Use application-identity since there are primary-key field(s) defined
                    identityType = IdentityType.APPLICATION; // SingleFieldIdentity
                }
                else
                {
                    // Use datastore-identity
                    identityType = IdentityType.DATASTORE;
                }
            }
        }
    }

    /**
     * Determine the nearest superclass that is persistable (if any).
     * @param clr The ClassLoaderResolver
     * @param cls This class
     * @param mmgr MetaData manager
     * @throws InvalidMetaDataException if the super class cannot be loaded by the clr. 
     * @throws InvalidMetaDataException if the declared persistence-capable-superclass is not actually assignable from cls 
     * @throws InvalidMetaDataException if any of the super classes is persistable, but the MetaData says that class is not persistent. 
     */
    protected void determineSuperClassName(ClassLoaderResolver clr, Class cls, MetaDataManager mmgr)
    {
        // Find the true superclass name (using reflection)
        String realPcSuperclassName = null;
        Collection> superclasses;
        if (cls.isInterface())
        {
            superclasses = ClassUtils.getSuperinterfaces(cls);
        }
        else
        {
            superclasses = ClassUtils.getSuperclasses(cls);
        }

        for (Class superclass : superclasses)
        {
            AbstractClassMetaData superCmd = mmgr.getMetaDataForClassInternal(superclass, clr);
            if (superCmd != null && superCmd.getPersistenceModifier() == ClassPersistenceModifier.PERSISTENCE_CAPABLE)
            {
                realPcSuperclassName = superclass.getName();
                break;
            }
        }

        persistableSuperclass = realPcSuperclassName;
        if (persistableSuperclass != null)
        {
            Class pcsc = null;
            try
            {
                // Load using same class loader resolver as this class
                pcsc = clr.classForName(persistableSuperclass);
            }
            catch (ClassNotResolvedException cnre)
            {
                throw new InvalidClassMetaDataException("044081", fullName, persistableSuperclass);
            }

            if (persistableSuperclass.equals(fullName) || !(pcsc.isAssignableFrom(cls)))
            {
                throw new InvalidClassMetaDataException("044082", fullName, persistableSuperclass);
            }

            // Retrieve the Meta-Data for the superclass
            if (mmgr != null)
            {
                // Normal operation will have a MetaDataManager and so ask that for MetaData of superclass.
                pcSuperclassMetaData = mmgr.getMetaDataForClassInternal(pcsc, clr);
                if (pcSuperclassMetaData == null)
                {
                    // Superclass isn't persistence capable since no MetaData could be found.
                    throw new InvalidClassMetaDataException("044083", fullName, persistableSuperclass);
                }
            }
            else
            {
                // The enhancer doesn't need MetaDataManager so just navigate to FileMetaData and find it.
                // NOTE : assumes that the class is specified in the same file 
                String superclass_pkg_name = persistableSuperclass.substring(0,persistableSuperclass.lastIndexOf('.'));
                PackageMetaData pmd = getPackageMetaData().getFileMetaData().getPackage(superclass_pkg_name);
                if (pmd != null)
                {
                    String superclass_class_name = persistableSuperclass.substring(persistableSuperclass.lastIndexOf('.')+1);
                    pcSuperclassMetaData = pmd.getClass(superclass_class_name);
                }
            }
            if (pcSuperclassMetaData == null)
            {
                throw new InvalidClassMetaDataException("044084", fullName, persistableSuperclass);
            }
            if (!pcSuperclassMetaData.isPopulated() && !pcSuperclassMetaData.isInitialised())
            {
                // Populate the superclass since we will be needing it
                pcSuperclassMetaData.populate(clr, cls.getClassLoader(), mmgr);
            }
        }

        if (persistableSuperclass != null)
        {
            // Inherit detachable flag from superclass
            if (!isDetachable() && pcSuperclassMetaData.isDetachable())
            {
                detachable = true;
            }
        }
    }

    /**
     * Check for conflicts on super class and this class MetaData identity
     * @throws InvalidMetaDataException if the user tries to overwrite a superclass identity / strategy
     */
    protected void validateUserInputForIdentity()
    {
        // Check that the user isn't trying to override the datastore-identity strategy!
        if (pcSuperclassMetaData != null)
        {
            AbstractClassMetaData baseCmd = getBaseAbstractClassMetaData();
            IdentityMetaData baseImd = baseCmd.getIdentityMetaData();
            if (baseCmd.identitySpecified && identitySpecified &&
                baseImd != null && baseImd.getValueStrategy() != null &&
                identityMetaData != null && identityMetaData.getValueStrategy() != null &&
                identityMetaData.getValueStrategy() != baseImd.getValueStrategy() &&
                identityMetaData.getValueStrategy() != null && 
                identityMetaData.getValueStrategy() != IdentityStrategy.NATIVE)
            {
                // User made deliberate attempt to change strategy in this subclass
                throw new InvalidClassMetaDataException("044094", fullName, identityMetaData.getValueStrategy(), baseImd.getValueStrategy());
            }

            if (baseCmd.identitySpecified && identityMetaData != null && 
                baseImd.getValueStrategy() != identityMetaData.getValueStrategy())
            {
                // Make sure the strategy matches the parent (likely just took the default "native" schema)
                identityMetaData.setValueStrategy(baseImd.getValueStrategy());
            }
        }
    }

    /**
     * Convenience accessor for the AbstractClassMetaData of the base object in this hierarchy.
     * @return The AbstractClassMetaData for the base object.
     */
    public AbstractClassMetaData getBaseAbstractClassMetaData()
    {
        if (pcSuperclassMetaData != null)
        {
            return pcSuperclassMetaData.getBaseAbstractClassMetaData();
        }
        return this;
    }

    /**
     * Convenience method to return if this class is a descendant of the supplied class metadata.
     * @param cmd The class to check against
     * @return Whether the supplied metadata is an ancestor of this
     */
    public boolean isDescendantOf(AbstractClassMetaData cmd)
    {
        if (pcSuperclassMetaData == null)
        {
            return false;
        }
        if (pcSuperclassMetaData == cmd)
        {
            return true;
        }
        return pcSuperclassMetaData.isDescendantOf(cmd);
    }

    protected String getBaseInheritanceStrategy()
    {
        if (inheritanceMetaData != null && inheritanceMetaData.getStrategyForTree() != null)
        {
            return inheritanceMetaData.getStrategyForTree();
        }
        else if (pcSuperclassMetaData != null)
        {
            return pcSuperclassMetaData.getBaseInheritanceStrategy();
        }
        return null;
    }

    /**
     * Inherit the identity definition from super classes. 
     * @throws InvalidMetaDataException if the MetaData of this class conflicts with super classes definition
     */
    protected void inheritIdentity()
    {
        if (objectidClass != null)
        {
            // Make sure the objectid-class is fully-qualified (may have been specified in simple form)
            this.objectidClass = ClassUtils.createFullClassName(((PackageMetaData)parent).name, objectidClass);
        }

        // "persistence-capable-superclass"
        // Check that the class can be loaded, and is a true superclass 
        if (persistableSuperclass != null)
        {
            // Class has superclass, yet has objectid-class defined! this might result in user errors
            if (objectidClass != null)
            {
                String superObjectIdClass = pcSuperclassMetaData.getObjectidClass();
                if (superObjectIdClass == null || !objectidClass.equals(superObjectIdClass))
                {
                    throw new InvalidClassMetaDataException("044085", fullName, persistableSuperclass);
                }

                // by default users should only specify the object-id class in the root persistent class
                NucleusLogger.METADATA.info(Localiser.msg("044086", name, persistableSuperclass));
            }
            else
            {
                // get the objectid class from superclass
                this.objectidClass = pcSuperclassMetaData.getObjectidClass();
            }

            if (this.identityType == null)
            {
                this.identityType = pcSuperclassMetaData.getIdentityType();
            }

            if (this.identityType != null) 
            {
                // Identity of parent set (some situations exist where due to complicated relations it hasn't yet been)
                if (!this.identityType.equals(pcSuperclassMetaData.getIdentityType())) 
                {
                    // We can't change the identity type from what was specified in the base class
                    throw new InvalidClassMetaDataException("044093", fullName);
                }
            }

            if (pcSuperclassMetaData.getIdentityType() == IdentityType.APPLICATION && pcSuperclassMetaData.getNoOfPopulatedPKMembers() > 0)
            {
                // TODO Allow for overriding superclass members
                // Check whether the superclass defines PK fields and this class defines some more
                int noOfPkKeys = 0;
                Iterator memberIter = members.iterator();
                while (memberIter.hasNext())
                {
                    AbstractMemberMetaData mmd = memberIter.next();
                    if (mmd.isPrimaryKey())
                    {
                        if (mmd.fieldBelongsToClass())
                        {
                            noOfPkKeys++;
                        }
                        else
                        {
                            // TODO Check any overriding doesn't make something PK when it wasn't before
                        }
                    }
                }
                if (noOfPkKeys > 0)
                {
                    throw new InvalidClassMetaDataException("044034", getFullClassName(), noOfPkKeys, pcSuperclassMetaData.getNoOfPopulatedPKMembers());
                }
            }
        }
    }

    /**
     * Utility to add a defaulted PropertyMetaData to the class. 
     * Provided as a method since then any derived classes can override it.
     * @param name name of property
     * @return the new PropertyMetaData
     */
    protected AbstractMemberMetaData newDefaultedProperty(String name)
    {
        return new PropertyMetaData(this, name);
    }

    /**
     * Check if the inheritance MetaData is credible.
     * @param isAbstract Whether this class is abstract
     * @throws InvalidMetaDataException if the strategy is superclass-table, yet there are no super class
     * @throws InvalidMetaDataException if the strategy is superclass-table, yet the super class has not
     *                                  specified a discriminator
     * @throws InvalidMetaDataException if the strategy is superclass-table and discriminator is "value-map",
     *                                  yet no value for the discriminator has been specified
     */
    protected void validateUserInputForInheritanceMetaData(boolean isAbstract)
    {
        if (mappedSuperclass)
        {
            String baseInhStrategy = getBaseInheritanceStrategy();
            if (baseInhStrategy != null && baseInhStrategy.equalsIgnoreCase("SINGLE_TABLE") &&
                getSuperclassManagingTable() != null)
            {
                // We have a mapped-superclass part way down an inheritance tree but with a class with table above it
                // and the tree is defined to use single-table strategy, so change the inheritance strategy to persist
                // to the superclass (why anyone would define such a model is their problem and they get what they deserve)
                if (inheritanceMetaData != null)
                {
                    inheritanceMetaData.setStrategy(InheritanceStrategy.SUPERCLASS_TABLE);
                }
            }
        }

        // Check that the inheritance strategy is credible
        if (inheritanceMetaData != null)
        {
            // Check validity of inheritance strategy and discriminator
            if (inheritanceMetaData.getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE)
            {
                AbstractClassMetaData superCmd = getClassManagingTable();
                if (superCmd == null)
                {
                    // We need a superclass table yet there is no superclass with its own table!
                    throw new InvalidClassMetaDataException("044099", fullName);
                }

                DiscriminatorMetaData superDismd = superCmd.getInheritanceMetaData().getDiscriminatorMetaData();
                if (superDismd == null)
                {
                    // If we are using "superclass-table" then the superclass should have specified the discriminator.
                    throw new InvalidClassMetaDataException("044100",  fullName, superCmd.fullName);
                }

                DiscriminatorMetaData dismd = inheritanceMetaData.getDiscriminatorMetaData();
                if (superDismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP &&
                    (dismd == null || dismd.getValue() == null) && !mappedSuperclass && !isAbstract)
                {
                    // If we are using "superclass-table" and the discriminator uses "value-map" then we must specify a value
                    throw new InvalidClassMetaDataException("044102", fullName, superCmd.fullName, superDismd.getColumnName());
                }
            }

            if (isAbstract)
            {
                // Class is abstract but user has defined a discriminator value (that will NEVER be used)
                DiscriminatorMetaData dismd = inheritanceMetaData.getDiscriminatorMetaData();
                if (dismd != null && !StringUtils.isWhitespace(dismd.getValue()))
                {
                    NucleusLogger.METADATA.info(Localiser.msg("044105", fullName));
                }
            }
            else
            {
                DiscriminatorMetaData dismd = inheritanceMetaData.getDiscriminatorMetaData();
                if (dismd != null && dismd.getColumnMetaData() != null)
                {
                    if (pcSuperclassMetaData != null)
                    {
                        // Check whether the user has tried to redefine the discriminator column down the inheritance tree
                        ColumnMetaData superDiscrimColmd = pcSuperclassMetaData.getDiscriminatorColumnMetaData();
                        if (superDiscrimColmd != null)
                        {
                            NucleusLogger.GENERAL.debug(Localiser.msg("044126", fullName));
                        }
                    }
                }
            }
        }
    }

    /**
     * Impose a default inheritance strategy when one is not already specified.
     * Uses the persistence property for defaultInheritanceStrategy and works to the JDO2 spec etc.
     * @param mmgr MetaData manager
     */
    protected void determineInheritanceMetaData(MetaDataManager mmgr)
    {
        if (inheritanceMetaData == null)
        {
            // User hasn't specified the  for the class
            if (pcSuperclassMetaData != null)
            {
                AbstractClassMetaData baseCmd = getBaseAbstractClassMetaData();
                if (getBaseInheritanceStrategy() != null)
                {
                    // A strategy for the full inheritance tree is defined (like in JPA) so use that
                    String treeStrategy = getBaseInheritanceStrategy();
                    if (treeStrategy.equals("JOINED"))
                    {
                        inheritanceMetaData = new InheritanceMetaData();
                        inheritanceMetaData.setStrategy(InheritanceStrategy.NEW_TABLE);
                        return;
                    }
                    else if (treeStrategy.equals("SINGLE_TABLE"))
                    {
                        inheritanceMetaData = new InheritanceMetaData();
                        inheritanceMetaData.setStrategy(InheritanceStrategy.SUPERCLASS_TABLE);
                        return;
                    }
                    else if (treeStrategy.equals("TABLE_PER_CLASS"))
                    {
                        inheritanceMetaData = new InheritanceMetaData();
                        inheritanceMetaData.setStrategy(InheritanceStrategy.COMPLETE_TABLE);
                        return;
                    }
                }

                if (baseCmd.getInheritanceMetaData() != null &&
                    baseCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE)
                {
                    // Root class in tree is set to use COMPLETE_TABLE so all subclasses have own table
                    inheritanceMetaData = new InheritanceMetaData();
                    inheritanceMetaData.setStrategy(InheritanceStrategy.COMPLETE_TABLE);
                }
                else if (pcSuperclassMetaData.getInheritanceMetaData() != null &&
                    pcSuperclassMetaData.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE)
                {
                    // Superclass exists but needs our table so have own table
                    inheritanceMetaData = new InheritanceMetaData();
                    inheritanceMetaData.setStrategy(InheritanceStrategy.NEW_TABLE);
                }
                else
                {
                    // Superclass exists and has a table or uses its superclass, so default based on that
                    if (mmgr.getNucleusContext().getConfiguration().getStringProperty(PropertyNames.PROPERTY_METADATA_DEFAULT_INHERITANCE_STRATEGY).equalsIgnoreCase("TABLE_PER_CLASS"))
                    {
                        // Each class has its own table
                        inheritanceMetaData = new InheritanceMetaData();
                        inheritanceMetaData.setStrategy(InheritanceStrategy.NEW_TABLE);
                    }
                    else
                    {
                        // JDO2 behaviour (root class has table, and others share it)
                        inheritanceMetaData = new InheritanceMetaData();
                        inheritanceMetaData.setStrategy(InheritanceStrategy.SUPERCLASS_TABLE);
                        if ((baseCmd.getInheritanceMetaData() == null || baseCmd.getInheritanceMetaData().getDiscriminatorMetaData() == null) && !mmgr.isEnhancing())
                        {
                            NucleusLogger.METADATA.warn("Class " + getFullClassName() + " is defined to use the same table as " + baseCmd.getFullClassName() +
                                " yet this root class has not been defined with a discriminator!!!");
                        }
                    }
                }
            }
            else
            {
                inheritanceMetaData = new InheritanceMetaData();
                inheritanceMetaData.setStrategy(InheritanceStrategy.NEW_TABLE);
            }
            return;
        }

        if (inheritanceMetaData.getStrategy() == null)
        {
            // User has included  but not set the strategy, so populate it for them
            if (getBaseInheritanceStrategy() != null)
            {
                // They set a tree strategy for this level (applying to this and all levels below)
                String treeStrategy = getBaseInheritanceStrategy();
                if (treeStrategy.equalsIgnoreCase("SINGLE_TABLE"))
                {
                    if (pcSuperclassMetaData != null)
                    {
                        if (pcSuperclassMetaData.getInheritanceMetaData() != null && 
                            pcSuperclassMetaData.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE)
                        {
                            // Check if superclass is wanting to persist into table of this class
                            // Can happen if superclass is mapped-superclass, and we want this to have the table
                            inheritanceMetaData.strategy = InheritanceStrategy.NEW_TABLE;
                        }
                        else
                        {
                            inheritanceMetaData.strategy = InheritanceStrategy.SUPERCLASS_TABLE;
                        }
                    }
                    else
                    {
                        inheritanceMetaData.strategy = InheritanceStrategy.NEW_TABLE;
                    }
                }
                else if (treeStrategy.equalsIgnoreCase("TABLE_PER_CLASS"))
                {
                    inheritanceMetaData.strategy = InheritanceStrategy.COMPLETE_TABLE;
                }
                else if (treeStrategy.equalsIgnoreCase("JOINED"))
                {
                    inheritanceMetaData.strategy = InheritanceStrategy.NEW_TABLE;
                }
                return;
            }

            if (pcSuperclassMetaData != null)
            {
                String treeStrategy = getBaseInheritanceStrategy();
                InheritanceStrategy baseStrategy = null;
                if (treeStrategy != null && treeStrategy.equalsIgnoreCase("SINGLE_TABLE"))
                {
                    baseStrategy = InheritanceStrategy.SUPERCLASS_TABLE;
                }
                else if (treeStrategy != null && treeStrategy.equalsIgnoreCase("TABLE_PER_CLASS"))
                {
                    baseStrategy = InheritanceStrategy.COMPLETE_TABLE;
                }
                else if (treeStrategy != null && treeStrategy.equalsIgnoreCase("JOINED"))
                {
                    baseStrategy = InheritanceStrategy.NEW_TABLE;
                }
                else
                {
                    AbstractClassMetaData baseCmd = getBaseAbstractClassMetaData();
                    if (baseCmd.getInheritanceMetaData() != null)
                    {
                        baseStrategy = baseCmd.getInheritanceMetaData().getStrategy();
                    }
                }

                if (baseStrategy == InheritanceStrategy.COMPLETE_TABLE)
                {
                    // Base class in tree is set to use COMPLETE_TABLE so all subclasses have own table
                    inheritanceMetaData.strategy = InheritanceStrategy.COMPLETE_TABLE;
                }
                else if (pcSuperclassMetaData.getInheritanceMetaData() != null && 
                    pcSuperclassMetaData.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE)
                {
                    // Superclass exists but needs our table so have own table
                    inheritanceMetaData.strategy = InheritanceStrategy.NEW_TABLE;
                }
                else
                {
                    // Superclass exists and has a table or uses its superclass, so default based on that
                    if (mmgr.getNucleusContext().getConfiguration().getStringProperty(PropertyNames.PROPERTY_METADATA_DEFAULT_INHERITANCE_STRATEGY).equalsIgnoreCase("TABLE_PER_CLASS"))
                    {
                        // Each class has its own table
                        inheritanceMetaData.strategy = InheritanceStrategy.NEW_TABLE;
                    }
                    else
                    {
                        // JDO2 behaviour (root class has table, and others share it)
                        inheritanceMetaData.strategy = InheritanceStrategy.SUPERCLASS_TABLE;
                    }
                }
            }
            else
            {
                inheritanceMetaData.strategy = InheritanceStrategy.NEW_TABLE;
            }
        }
    }

    protected void applyDefaultDiscriminatorValueWhenNotSpecified(MetaDataManager mmgr)
    {
        if (inheritanceMetaData != null &&
            inheritanceMetaData.getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE)
        {
            AbstractClassMetaData superCmd = getClassManagingTable();
            if (superCmd == null)
            {
                throw new InvalidClassMetaDataException("044064", getFullClassName());
            }

            if (superCmd.getInheritanceMetaData() != null)
            {
                DiscriminatorMetaData superDismd = superCmd.getInheritanceMetaData().getDiscriminatorMetaData();
                DiscriminatorMetaData dismd = inheritanceMetaData.getDiscriminatorMetaData();
                if (superDismd != null && superDismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP &&
                    (dismd == null || dismd.getValue() == null))
                {
                    // Impose the full class name as the discriminator value since not set
                    if (dismd == null)
                    {
                        dismd = inheritanceMetaData.newDiscriminatorMetadata();
                    }
                    if (NucleusLogger.METADATA.isDebugEnabled())
                    {
                        NucleusLogger.METADATA.debug("No discriminator value specified for " + getFullClassName() +
                        " so using fully-qualified class name");
                    }
                    dismd.setValue(getFullClassName());
                }
            }
        }

        if (inheritanceMetaData != null)
        {
            // Register the discriminator value if using VALUE_MAP and a value is defined
            DiscriminatorMetaData dismd = inheritanceMetaData.getDiscriminatorMetaData();
            if (dismd != null && getDiscriminatorStrategy() == DiscriminatorStrategy.VALUE_MAP &&
                dismd.getValue() != null)
            {
                mmgr.registerDiscriminatorValueForClass(this, dismd.getValue());
            }
        }
    }

    /**
     * Convenience method to validate the specified "unmapped" columns.
     * @throws InvalidMetaDataException if a column is specified without its name.
     */
    protected void validateUnmappedColumns()
    {
        // Validate any unmapped columns
        if (unmappedColumns != null && unmappedColumns.size() > 0)
        {
            Iterator unmappedIter = unmappedColumns.iterator();
            while (unmappedIter.hasNext())
            {
                ColumnMetaData colmd = (ColumnMetaData)unmappedIter.next();
                if (colmd.getName() == null)
                {
                    throw new InvalidClassMetaDataException("044119", fullName);
                }
                if (colmd.getJdbcType() == null)
                {
                    throw new InvalidClassMetaDataException("044120", fullName, colmd.getName());
                }
            }
        }
    }

    /**
     * Utility to find the immediate superclass that manages its own table.
     * Checks up the inheritance tree for one that uses "new-table" inheritance strategy.
     * @return Metadata for the superclass that uses "NEW-TABLE" (or null, if none found).
     */
    private AbstractClassMetaData getSuperclassManagingTable()
    {
        if (pcSuperclassMetaData != null)
        {
            if (pcSuperclassMetaData.getInheritanceMetaData().getStrategy() == InheritanceStrategy.NEW_TABLE)
            {
                return pcSuperclassMetaData;
            }
            return pcSuperclassMetaData.getSuperclassManagingTable();
        }
        return null;
    }

    /**
     * Utility to navigate up to superclasses to find the next class with its own table.
     * @return The AbstractClassMetaData of the class managing its own table
     */
    private AbstractClassMetaData getClassManagingTable()
    {
        if (inheritanceMetaData == null)
        {
            return this;
        }
        else if (inheritanceMetaData.getStrategy() == InheritanceStrategy.NEW_TABLE)
        {
            return this;
        }
        else if (inheritanceMetaData.getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE)
        {
            if (pcSuperclassMetaData == null)
            {
                return null;
            }
            return pcSuperclassMetaData.getClassManagingTable();
        }
        return null;
    }    

    /**
     * Accessor for the Meta-Data for the superclass of this class.
     * @return MetaData of the superclass
     */
    public final AbstractClassMetaData getSuperAbstractClassMetaData()
    {
        checkPopulated();
        return pcSuperclassMetaData;
    }

    /**
     * Convenience method to calculate and return if the pk has some component that is generated in the datastore.
     * @param storeMgr The storeManager
     * @return Whether the PK is datastore generated
     */
    public boolean pkIsDatastoreAttributed(StoreManager storeMgr)
    {
        if (pkIsDatastoreAttributed == null)
        {
            pkIsDatastoreAttributed = Boolean.FALSE;
            if (identityType == IdentityType.APPLICATION)
            {
                for (int i=0;iobjectid-class
     *                                  has been declared in the MetaData
     * @throws InvalidMetaDataException if the objectid-class has not been set and the primary key field does
     *                                  not match a supported SingleFieldIdentity
     * @throws InvalidMetaDataException if the identity type is APPLICATION but not primary key fields have been set
     * @throws InvalidMetaDataException if the objectid-class cannot be loaded by the clr                                                                     
     */
    protected void determineObjectIdClass(MetaDataManager mmgr)
    {
        if (identityType != IdentityType.APPLICATION || objectidClass != null)
        {
            return;
        }

        int no_of_pk_fields = 0;
        AbstractMemberMetaData mmd_pk = null;
        Iterator memberIter = members.iterator();
        while (memberIter.hasNext())
        {
            AbstractMemberMetaData mmd = memberIter.next();
            if (mmd.isPrimaryKey())
            {
                mmd_pk = mmd;
                no_of_pk_fields++;
            }
        }

        if (no_of_pk_fields == 0 && inheritanceMetaData.getStrategy() == InheritanceStrategy.SUBCLASS_TABLE && getSuperclassManagingTable() == null)
        {
            // Case where we have no table for this class (abstract) and no superclass with a table, and no pk fields
            NucleusLogger.METADATA.debug(Localiser.msg("044163", getFullClassName()));
            instantiable = false;
            return;
        }

        boolean needsObjectidClass = false;
        if (persistableSuperclass == null)
        {
            needsObjectidClass = true;
        }
        else if (getSuperclassManagingTable() == null)
        {
            needsObjectidClass = true;
        }

        if (needsObjectidClass)
        {
            // Update "objectid-class" if required yet not specified
            if (no_of_pk_fields == 0)
            {
                NucleusLogger.METADATA.error(Localiser.msg("044065", fullName, "" + no_of_pk_fields));
                throw new InvalidClassMetaDataException("044065", fullName, "" + no_of_pk_fields);
            }
            else if (no_of_pk_fields > 1)
            {
                // More than 1 PK yet no objectidClass - maybe added by enhancer later, so log warning
                NucleusLogger.METADATA.warn(Localiser.msg("044065", fullName, "" + no_of_pk_fields));
                if (!mmgr.isEnhancing())
                {
                    objectidClass = fullName + GENERATED_PK_SUFFIX;
                    NucleusLogger.METADATA.debug(Localiser.msg("044164", fullName, "" + getNoOfPrimaryKeyMembers(), objectidClass));
                }
            }
            else
            {
                // Assign associated SingleField identity class
                Class pk_type = mmd_pk.getType();
                if (Byte.class.isAssignableFrom(pk_type) || byte.class.isAssignableFrom(pk_type))
                {
                    objectidClass = ClassNameConstants.IDENTITY_SINGLEFIELD_BYTE;
                }
                else if (Character.class.isAssignableFrom(pk_type) || char.class.isAssignableFrom(pk_type))
                {
                    objectidClass = ClassNameConstants.IDENTITY_SINGLEFIELD_CHAR;
                }
                else if (Integer.class.isAssignableFrom(pk_type) || int.class.isAssignableFrom(pk_type))
                {
                    objectidClass = ClassNameConstants.IDENTITY_SINGLEFIELD_INT;
                }
                else if (Long.class.isAssignableFrom(pk_type) || long.class.isAssignableFrom(pk_type))
                {
                    objectidClass = ClassNameConstants.IDENTITY_SINGLEFIELD_LONG;
                }
                else if (Short.class.isAssignableFrom(pk_type) || short.class.isAssignableFrom(pk_type))
                {
                    objectidClass = ClassNameConstants.IDENTITY_SINGLEFIELD_SHORT;
                }
                else if (String.class.isAssignableFrom(pk_type))
                {
                    objectidClass = ClassNameConstants.IDENTITY_SINGLEFIELD_STRING;
                }
                else if (Object.class.isAssignableFrom(pk_type))
                {
                    objectidClass = ClassNameConstants.IDENTITY_SINGLEFIELD_OBJECT;
                }
                else
                {
                    NucleusLogger.METADATA.error(Localiser.msg("044066", fullName, pk_type.getName()));
                    throw new InvalidClassMetaDataException("044066", fullName, pk_type.getName());
                }
            }
        }
        /*else
        {
            // Check no of primary key fields (inc superclass)
            int no_of_pk_fields = getNoOfPopulatedPKMembers();
            if (no_of_pk_fields == 0 && identityType == IdentityType.APPLICATION)
            {
                // No primary key fields found (even in superclasses)
                throw new InvalidMetaDataException(Localiser, "044077", fullName, objectidClass);
            }
        }*/
    }

    /**
     * Validate the objectid-class of this class.
     * @param clr ClassLoader resolver
     * @param mmgr MetaData manager
     */
    protected void validateObjectIdClass(ClassLoaderResolver clr, MetaDataManager mmgr)
    {
        if (getPersistableSuperclass() == null)
        {
            // Only check root persistable class PK
            if (objectidClass != null)
            {
                ApiAdapter api = mmgr.getApiAdapter();
                Class obj_cls = null;
                try
                {
                    // Load the class, using the same class loader resolver as this class
                    obj_cls = clr.classForName(objectidClass);
                }
                catch (ClassNotResolvedException cnre)
                {
                    // ObjectIdClass not found
                    throw new InvalidClassMetaDataException("044079", fullName, objectidClass);
                }

                boolean validated = false;
                Set errors = new HashSet();
                try
                {
                    // Check against the API Adapter in use for this MetaData
                    if (api.isValidPrimaryKeyClass(obj_cls, this, clr, getNoOfPopulatedPKMembers(), mmgr))
                    {
                        validated = true;
                    }
                }
                catch (NucleusException ex)
                {
                    errors.add(ex);
                }
                if (!validated)
                {
                    // Why is this wrapping all exceptions into 1 single exception? 
                    // This needs coordinating with the test expectations in the enhancer unit tests.
                    throw new NucleusUserException(Localiser.msg("019016", getFullClassName(), obj_cls.getName()), (Throwable[]) errors.toArray(new Throwable[errors.size()]));
                }
            }
        }
    }

    /**
     * Method to provide the details of the class being represented by this
     * MetaData. This can be used to firstly provide defaults for attributes
     * that aren't specified in the MetaData, and secondly to report any errors
     * with attributes that have been specifed that are inconsistent with the
     * class being represented.
     * 

* This method must be invoked by subclasses during populate operations *

* @param clr ClassLoaderResolver to use in loading any classes * @param primary the primary ClassLoader to use (or null) * @param mmgr MetaData manager */ abstract public void populate(ClassLoaderResolver clr, ClassLoader primary, MetaDataManager mmgr); /** * Method to initialise the sub-objects of this class metadata. * If populate() is going to be used it should be used BEFORE calling this method. * Subclasses must overwrite this method and invoke this * @param clr ClassLoader resolver * @param mmgr MetaData manager */ public abstract void initialise(ClassLoaderResolver clr, MetaDataManager mmgr); /** * Method to initialise all convenience information about member positions and what role * each position performs. * @param mmgr MetaDataManager */ protected void initialiseMemberPositionInformation(MetaDataManager mmgr) { memberCount = noOfInheritedManagedMembers + managedMembers.length; dfgMemberFlags = new boolean[memberCount]; scoMutableMemberFlags = new boolean[memberCount]; nonPkMemberFlags = new boolean[memberCount]; int pk_field_count=0; int dfg_field_count=0; int scm_field_count=0; for (int i=0;i 0 && identityType != IdentityType.APPLICATION) { // primary key fields found, but not using application identity throw new InvalidClassMetaDataException("044078", fullName, Integer.valueOf(pk_field_count), identityType); } else if (pk_field_count > 0) { pkMemberPositions = new int[pk_field_count]; for (int i=0,pk_num=0;i * Uses recursion to add all referenced ClassMetaData for any fields, * objectid classes, superclasses, and extension RDBMS "views". *

*

* This is the entry point for this process, and provides the core of the * "persistence-by-reachability" concept. *

* @param orderedCMDs List of ordered ClassMetaData objects (added to). * @param referencedCMDs Set of all ClassMetaData objects (added to). * @param clr the ClassLoaderResolver * @param mmgr MetaData manager */ void getReferencedClassMetaData(final List orderedCMDs, final Set referencedCMDs, final ClassLoaderResolver clr, final MetaDataManager mmgr) { Map viewReferences = new HashMap(); getReferencedClassMetaData(orderedCMDs, referencedCMDs, viewReferences, clr, mmgr); } /** * Method to return the ClassMetaData for classes referenced by this * object. This method does the actual work of addition. * @param orderedCMDs List of ordered ClassMetaData objects (added to). * @param referencedCMDs Set of all ClassMetaData objects (added to). * @param viewReferences Map, mapping class name to set of referenced class for all views. * @param clr the ClassLoaderResolver * @param mmgr MetaData manager */ private void getReferencedClassMetaData(final List orderedCMDs, final Set referencedCMDs, final Map viewReferences, final ClassLoaderResolver clr, final MetaDataManager mmgr) { // Recursively call getReferencedClassMetaData(...) before adding them // to the orderedCmds and referenced. This will ensure that any // classes with dependencies on them are put in the orderedCmds List // in the correct order. if (!referencedCMDs.contains(this)) { // Go ahead and add this class to the referenced Set, it will // get added to the orderedCMDs List after all classes that this // depends on have been added. referencedCMDs.add(this); for (int i=0;i getFetchGroupMetaData() { return fetchGroups; } /** * Accessor for fetch group metadata for the specified groups (if present). * The returned metadata is what is defined for this class that matches any of the names in the input set. * @param groupNames Names of the fetch groups * @return MetaData for the groups */ public Set getFetchGroupMetaData(Collection groupNames) { Set results = new HashSet(); for (Iterator iter = groupNames.iterator(); iter.hasNext();) { String groupname = (String) iter.next(); FetchGroupMetaData fgmd = getFetchGroupMetaData(groupname); if (fgmd != null) { results.add(fgmd); } } return results; } /** * Accessor for the fetch group metadata for the group specified. * @param groupname Name of the fetch group * @return MetaData for this group */ public FetchGroupMetaData getFetchGroupMetaData(String groupname) { FetchGroupMetaData fgmd = (fetchGroupMetaDataByName != null ? fetchGroupMetaDataByName.get(groupname) : null); if (fgmd == null && pcSuperclassMetaData != null) { return pcSuperclassMetaData.getFetchGroupMetaData(groupname); } return fgmd; } public IdentityType getIdentityType() { return identityType; } public synchronized void setIdentityType(IdentityType type) { checkNotYetPopulated(); this.identityType = type; } /** * Accessor for indexMetaData * @return Returns the indexMetaData. */ public final IndexMetaData[] getIndexMetaData() { return indexMetaData; } /** * Accessor for foreignKeyMetaData * @return Returns the foreignKeyMetaData. */ public final ForeignKeyMetaData[] getForeignKeyMetaData() { return foreignKeyMetaData; } /** * Accessor for UniqueMetaData * @return Returns the UniqueMetaData. */ public final UniqueMetaData[] getUniqueMetaData() { return uniqueMetaData; } /** * Accessor for the unmapped columns require for the datastore table. * @return The list of unmapped columns */ public final List getUnmappedColumns() { return unmappedColumns; } /** * Accessor for the simple class name (without package name). * @return class name */ public String getName() { return name; } /** * Accessor for the full class name including any package name. * @return full class name. */ public String getFullClassName() { return fullName; } public String getObjectidClass() { return objectidClass; } public AbstractClassMetaData setObjectIdClass(String objectidClass) { this.objectidClass = (StringUtils.isWhitespace(objectidClass) ? this.objectidClass : objectidClass); return this; } public String getEntityName() { return entityName; } public AbstractClassMetaData setEntityName(String name) { this.entityName = (StringUtils.isWhitespace(name) ? this.entityName : name); return this; } public String getCatalog() { if (this.catalog == null) { // Nothing defined here for catalog, so get from package return ((PackageMetaData)parent).getCatalog(); } return catalog; } public AbstractClassMetaData setCatalog(String catalog) { this.catalog = (StringUtils.isWhitespace(catalog) ? this.catalog : catalog); return this; } public String getSchema() { if (this.schema == null) { // Nothing defined here for schema, so get from package return ((PackageMetaData)parent).getSchema(); } return schema; } public AbstractClassMetaData setSchema(String schema) { this.schema = (StringUtils.isWhitespace(schema) ? this.schema : schema); return this; } public String getTable() { return table; } public AbstractClassMetaData setTable(String table) { this.table = (StringUtils.isWhitespace(table) ? this.table : table); return this; } public boolean isRequiresExtent() { return requiresExtent; } public AbstractClassMetaData setRequiresExtent(boolean flag) { this.requiresExtent = flag; return this; } public AbstractClassMetaData setRequiresExtent(String flag) { if (!StringUtils.isWhitespace(flag)) { this.requiresExtent = Boolean.parseBoolean(flag); } return this; } public boolean isDetachable() { return detachable; } public AbstractClassMetaData setDetachable(boolean flag) { this.detachable = flag; return this; } public AbstractClassMetaData setDetachable(String flag) { if (!StringUtils.isWhitespace(flag)) { this.detachable = Boolean.parseBoolean(flag); } return this; } public Boolean isCacheable() { return cacheable; } public AbstractClassMetaData setCacheable(boolean cache) { cacheable = cache; return this; } public AbstractClassMetaData setCacheable(String cache) { if (!StringUtils.isWhitespace(cache)) { this.cacheable = Boolean.parseBoolean(cache); } return this; } public boolean isEmbeddedOnly() { return embeddedOnly; } public AbstractClassMetaData setEmbeddedOnly(boolean flag) { embeddedOnly = flag; return this; } public AbstractClassMetaData setEmbeddedOnly(String flag) { if (!StringUtils.isWhitespace(flag)) { this.embeddedOnly = Boolean.parseBoolean(flag); } return this; } /** * Accessor for identityMetaData * @return Returns the identityMetaData. */ public final IdentityMetaData getIdentityMetaData() { return identityMetaData; } /** * Convenience method to return the root identity metadata for this inheritance tree. * @return IdentityMetaData at the base */ public final IdentityMetaData getBaseIdentityMetaData() { if (pcSuperclassMetaData != null) { return pcSuperclassMetaData.getBaseIdentityMetaData(); } return identityMetaData; } /** * Accessor for inheritanceMetaData * @return Returns the inheritanceMetaData. */ public final InheritanceMetaData getInheritanceMetaData() { return inheritanceMetaData; } /** * Accessor for primaryKeyMetaData * @return Returns the primaryKey MetaData. */ public final PrimaryKeyMetaData getPrimaryKeyMetaData() { return primaryKeyMetaData; } /** * Convenience accessor for the parent Package MetaData. * @return MetaData for parent package. */ public PackageMetaData getPackageMetaData() { if (parent != null) { return (PackageMetaData)parent; } return null; } /** * Convenience accessor for the package name. * @return package name. */ public String getPackageName() { return getPackageMetaData().getName(); } /** * Accessor for the number of fields/properties. * This is the total number of fields/properties (inc static, final etc) in this class * @return no of fields/properties. */ public int getNoOfMembers() { return members.size(); } /** * Accessor for the metadata of a field/property. Does not include superclasses. * In general this should never be used; always use "getMetaDataForManagedMemberAtAbsolutePosition". * @param index field index relative to this class only starting from 0 * @return Meta-Data for the field/property */ public AbstractMemberMetaData getMetaDataForMemberAtRelativePosition(int index) { if (index < 0 || index >= members.size()) { return null; } return members.get(index); } public ClassPersistenceModifier getPersistenceModifier() { return persistenceModifier; } public AbstractClassMetaData setPersistenceModifier(ClassPersistenceModifier modifier) { this.persistenceModifier = modifier; return this; } public String getPersistableSuperclass() { return persistableSuperclass; } /** * Whether this persistable class uses SingleFieldIdentity * @return true if using SingleFieldIdentity as objectid class */ public boolean usesSingleFieldIdentityClass() { return usesSingleFieldIdentityClass; } /** * Accessor for whether this class is fully specified by this metadata and that any * annotations should be ignored. * @return Whether we should ignore any annotations */ public boolean isMetaDataComplete() { return metaDataComplete; } public boolean isMappedSuperclass() { return mappedSuperclass; } public boolean isSerializeRead() { return serializeRead; } /** * Check if the argument cmd is the same as this or a descedent. * @param cmd the AbstractClassMetaData to be verify if this is an ancestor * @return true if the argument is a child or same as this */ public boolean isSameOrAncestorOf(AbstractClassMetaData cmd) { checkInitialised(); if (cmd == null) { return false; } if (fullName.equals(cmd.fullName)) { return true; } AbstractClassMetaData parent = cmd.getSuperAbstractClassMetaData(); while( parent != null ) { if (fullName.equals(parent.fullName)) { return true; } parent = parent.getSuperAbstractClassMetaData(); } return false; } /** * Accessor for the names of the primary key fields/properties. Only valid after being populated. * Provided as a convenience where we need to get the names of the PK members but cant wait til initialisation. * @return names of the PK fields/properties */ public String[] getPrimaryKeyMemberNames() { if (identityType != IdentityType.APPLICATION) { return null; } List memberNames = new ArrayList(); // Use list to preserve ordering Iterator iter = members.iterator(); while (iter.hasNext()) { AbstractMemberMetaData mmd = iter.next(); if (Boolean.TRUE.equals(mmd.primaryKey)) { memberNames.add(mmd.name); } } if (memberNames.size() > 0) { return (String[])memberNames.toArray(new String[memberNames.size()]); } memberNames = null; return pcSuperclassMetaData.getPrimaryKeyMemberNames(); } /** * Method to check if a field/property exists in this classes definition. * Will include any superclasses in the check. * @param memberName Name of field/property * @return return true if exists. */ public boolean hasMember(String memberName) { Iterator iter = members.iterator(); while (iter.hasNext()) { AbstractMemberMetaData mmd = iter.next(); if (mmd.getName().equals(memberName)) { return true; } } if (pcSuperclassMetaData != null) { return pcSuperclassMetaData.hasMember(memberName); } return false; } /** * Accessor for the Meta-Data for a field/property. Include superclasses. * @param name the name of the field/property * @return Meta-Data for the field. */ public AbstractMemberMetaData getMetaDataForMember(String name) { if (name == null) { return null; } Iterator iter = members.iterator(); while (iter.hasNext()) { AbstractMemberMetaData mmd = iter.next(); if (mmd.getName().equals(name)) { return mmd; } } // Check superclass for the field/property with this name if (pcSuperclassMetaData != null) { return pcSuperclassMetaData.getMetaDataForMember(name); } return null; } /** * Accessor for the number of managed fields/properties (this class only). * @return no of managed fields/properties in this class */ public int getNoOfManagedMembers() { // checkInitialised(); if (managedMembers == null) { return 0; } return managedMembers.length; } /** * Accessor for the managed fields/properties in this class (not including superclass, but including overridden). * @return MetaData for the managed fields/properties in this class */ public AbstractMemberMetaData[] getManagedMembers() { checkInitialised(); return managedMembers; } /** * Accessor for the number of overridden fields/properties (this class only). * @return no of overridden fields/properties in this class */ public int getNoOfOverriddenMembers() { if (overriddenMembers == null) { return 0; } return overriddenMembers.length; } /** * Accessor for the overridden fields/properties in this class. * @return The overridden fields/properties in this class */ public AbstractMemberMetaData[] getOverriddenMembers() { checkInitialised(); return overriddenMembers; } /** * Accessor for an overridden field/property with the specified name. * @param name Name of the field/property * @return The MetaData for the field/property */ public AbstractMemberMetaData getOverriddenMember(String name) { checkInitialised(); if (overriddenMembers == null) { return null; } for (int i=0;i iter = members.iterator(); while(iter.hasNext()) { AbstractMemberMetaData mmd = iter.next(); if (mmd.name.equals(name) && mmd.fieldBelongsToClass()) { return mmd; } } if (pcSuperclassMetaData != null) { return pcSuperclassMetaData.getMemberBeingOverridden(name); } return null; } /** * Accessor for the number of inherited managed fields/properties in superclasses. * @return No of inherited managed fields/properties in superclasses. */ public int getNoOfInheritedManagedMembers() { checkInitialised(); return noOfInheritedManagedMembers; } /** * Accessor for the number of managed fields/properties from this class plus inherited classes. * @return The number of managed fields/properties from this class plus inherited classes. */ public int getMemberCount() { return memberCount; } /** * Accessor for MetaData for a managed field/property in this class. The position is relative to * the first field in this class (ignores superclasses). * @param position The position of the managed field. 0 = first in the class * @return The managed member at that position */ public AbstractMemberMetaData getMetaDataForManagedMemberAtRelativePosition(int position) { checkInitialised(); if (managedMembers == null) { return null; } if (position < 0 || position >= managedMembers.length) { return null; } return managedMembers[position]; } /** * Accessor for a managed field/property including superclass fields. * @param abs_position The position of the managed member including the * superclass. Fields are numbered from 0 in the root superclass. * @return The managed field/property at this "absolute" position. */ public AbstractMemberMetaData getMetaDataForManagedMemberAtAbsolutePosition(int abs_position) { checkInitialised(); return getMetaDataForManagedMemberAtAbsolutePositionInternal(abs_position); } /** * Internal method to get the field/property for an absolute field number. * If the field for that absolute field position is overridden by a field in this class * then this field/property will be returned. * @param abs_position The position of the managed field including the * superclass. Fields are numbered from 0 in the root superclass. * @return The managed field at this "absolute" position. */ protected AbstractMemberMetaData getMetaDataForManagedMemberAtAbsolutePositionInternal(int abs_position) { // If the field is in a superclass, go there if (abs_position < noOfInheritedManagedMembers) { if (pcSuperclassMetaData == null) { return null; } AbstractMemberMetaData mmd = pcSuperclassMetaData.getMetaDataForManagedMemberAtAbsolutePositionInternal(abs_position); if (mmd != null) { for (int i=0;i= managedMembers.length) { return null; } else { return managedMembers[abs_position - noOfInheritedManagedMembers]; } } /** * Accessor for the absolute position for a relative position. * @param relativePosition The relative field number * @return The absolute position */ public int getAbsoluteMemberPositionForRelativePosition(int relativePosition) { return noOfInheritedManagedMembers + relativePosition; } /** * Accessor for the position of the field/property with the specified name. * The returned position is relative to this class only. * @param memberName Name of the field/property * @return Position of the field/property in this class. */ public int getRelativePositionOfMember(String memberName) { checkInitialised(); if (memberName == null) { return -1; } Integer i = memberPositionsByName.get(memberName); return i == null ? -1 : i.intValue(); } /** * Accessor for the absolute position of the field/property with the specified name. * The absolute position has origin of the root superclass, starting at 0. * @param memberName Name of the field/property * @return Absolute position of the field/property. */ public int getAbsolutePositionOfMember(String memberName) { checkInitialised(); if (memberName == null) { return -1; } int i = getRelativePositionOfMember(memberName); if (i < 0) { if (pcSuperclassMetaData != null) { i = pcSuperclassMetaData.getAbsolutePositionOfMember(memberName); } } else { i += noOfInheritedManagedMembers; } return i; } /** * Accessor for the absolute position of the field/property with the specified name. * The absolute position has origin of the root superclass, starting at 0. * Will only retrieve the absolute field number if the field_name * is of type given by class_name. * @param className Name of the class * @param memberName Name of the field/property * @return Absolute position of the field/property. */ public int getAbsolutePositionOfMember(String className, String memberName) { checkInitialised(); if (memberName == null) { return -1; } int i=-1; if (className.equals(getFullClassName())) { i=getRelativePositionOfMember(memberName); } if (i < 0) { if (pcSuperclassMetaData != null) { i = pcSuperclassMetaData.getAbsolutePositionOfMember(className,memberName); } } else { i += noOfInheritedManagedMembers; } return i; } /** * Convenience method to check the number of fields/properties in this class that have been populated and * that are primary key members. This is only ever called during populate() since the accessor * for PK members cant be used yet due to lack of initialisation. * Recurses to its superclass if it has a superclass. * @return The number of PK members */ private int getNoOfPopulatedPKMembers() { if (pcSuperclassMetaData != null) { return pcSuperclassMetaData.getNoOfPopulatedPKMembers(); } Iterator fields_iter = members.iterator(); int noOfPks = 0; while (fields_iter.hasNext()) { AbstractMemberMetaData mmd = fields_iter.next(); if (mmd.isPrimaryKey()) { noOfPks++; } } return noOfPks; } /** * Accessor for the number of primary key fields/properties. * @return no of primary key fields/properties */ public int getNoOfPrimaryKeyMembers() { if (pkMemberPositions == null) { return 0; } return pkMemberPositions.length; } /** * Accessor for all field/property positions. * These are absolute numbers and include superclasses and are really just 0, 1, 2, ... n. * @return The positions of all (managed) fields/properties. */ public int[] getAllMemberPositions() { checkInitialised(); if (allMemberPositions == null) { allMemberPositions = new int[memberCount]; for (int i=0;i iter = members.iterator(); int numBasics = 0; while (iter.hasNext()) { AbstractMemberMetaData mmd = iter.next(); if (mmd.getRelationType(clr) == RelationType.NONE && !mmd.isPersistentInterface(clr, mmgr) && !Collection.class.isAssignableFrom(mmd.getType()) && !Map.class.isAssignableFrom(mmd.getType()) && !mmd.getType().isArray()) { numBasics++; } } int[] inheritedBasicPositions = null; if (pcSuperclassMetaData != null) { inheritedBasicPositions = pcSuperclassMetaData.getBasicMemberPositions(clr, mmgr); } int[] basicPositions = new int[numBasics + (inheritedBasicPositions != null ? inheritedBasicPositions.length : 0)]; int number = 0; if (inheritedBasicPositions != null) { for (int i=0;i iter = members.iterator(); int numMultivalues = 0; while (iter.hasNext()) { AbstractMemberMetaData mmd = iter.next(); if (mmd.getType().isArray() || Collection.class.isAssignableFrom(mmd.getType()) || Map.class.isAssignableFrom(mmd.getType())) { numMultivalues++; } } int[] inheritedMultivaluePositions = null; if (pcSuperclassMetaData != null) { inheritedMultivaluePositions = pcSuperclassMetaData.getMultivaluedMemberPositions(); } int[] multivaluePositions = new int[numMultivalues + (inheritedMultivaluePositions != null ? inheritedMultivaluePositions.length : 0)]; int number = 0; if (inheritedMultivaluePositions != null) { for (int i=0;i 0); } return hasRelations.booleanValue(); } public int[] getNonRelationMemberPositions(ClassLoaderResolver clr, MetaDataManager mmgr) { int[] relPositions = getRelationMemberPositions(clr, mmgr); if (relPositions == null || relPositions.length == 0) { return getAllMemberPositions(); } int[] allPositions = getAllMemberPositions(); int[] nonrelPositions = new int[allPositions.length - relPositions.length]; int nonrelPos = 0; int nextRelPos = 0; for (int i=0;i 0) { for (int i=0;i numRelationsSuperclass) { for (int i=0;i iter = members.iterator(); while (iter.hasNext()) { AbstractMemberMetaData md = iter.next(); if (mmd.getName().equals(md.getName())) { if ((mmd instanceof PropertyMetaData && md instanceof PropertyMetaData) || (mmd instanceof FieldMetaData && md instanceof FieldMetaData)) { // Duplicate entry for the same field or property throw new NucleusUserException(Localiser.msg("044090", fullName, mmd.getName())); } } String existingName = md.getName(); boolean existingIsProperty = (md instanceof PropertyMetaData); if (existingIsProperty) { existingName = ((PropertyMetaData)md).getFieldName(); if (existingName == null) { // "fieldName" not specified so default to the property name existingName = md.getName(); } } String newName = mmd.getName(); boolean newIsProperty = (mmd instanceof PropertyMetaData); if (newIsProperty) { newName = ((PropertyMetaData)mmd).getFieldName(); if (newName == null) { // "fieldName" not specified so default to the property name newName = mmd.getName(); } } if (existingName.equals(newName)) { if (existingIsProperty && newIsProperty) { // Duplicate entry for the same field or property throw new NucleusUserException(Localiser.msg("044090", fullName, mmd.getName())); } else if (existingIsProperty && !newIsProperty) { // We have the property and this is a field so ignore it // TODO Check if one is not persistent before discarding NucleusLogger.METADATA.debug("Ignoring metadata for field " + mmd.getFullFieldName() + " since we already have MetaData for the property " + md.getFullFieldName()); return; } else if (!existingIsProperty && newIsProperty) { // We have the field and this is property so replace the field with it // TODO Check if one is not persistent before discarding NucleusLogger.METADATA.debug("Ignoring existing metadata for field " + md.getFullFieldName() + " since now we have MetaData for the property " + mmd.getFullFieldName()); iter.remove(); } } } mmd.parent = this; members.add(mmd); } /** * Method to add a fetch-group to this class. * @param fgmd Meta-Data for the fetch-group. */ public void addFetchGroup(FetchGroupMetaData fgmd) { if (fgmd == null) { return; } if (isInitialised()) { throw new NucleusUserException("Already initialised"); } fetchGroups.add(fgmd); fgmd.parent = this; } /** * Method to create a new fetchgroup metadata, add it, and return it. * @param name Name of the group * @return The fetchgroup metadata */ public FetchGroupMetaData newFetchGroupMetaData(String name) { FetchGroupMetaData fgmd = new FetchGroupMetaData(name); addFetchGroup(fgmd); return fgmd; } /** * Method to add a join to this class. * Rejects the addition of duplicate named fields. * @param jnmd Meta-Data for the join. */ public void addJoin(JoinMetaData jnmd) { if (jnmd == null) { return; } if (isInitialised()) { throw new NucleusUserException("Already initialised"); } joins.add(jnmd); jnmd.parent = this; } /** * Method to create a new join metadata, add it, and return it. * @return The join metadata */ public JoinMetaData newJoinMetaData() { JoinMetaData joinmd = new JoinMetaData(); addJoin(joinmd); return joinmd; } /** * Add a listener class name * @param listener the listener metadata. Duplicated classes are ignored */ public void addListener(EventListenerMetaData listener) { if (listeners == null) { listeners = new ArrayList(); } if (!listeners.contains(listener)) { listeners.add(listener); listener.parent = this; } } /** * Accessor for the EventListener info for an EventListener class name * @param className Name of the event listener class * @return EventListener info for this class (or null if the class isnt an EventListener) */ public EventListenerMetaData getListenerForClass(String className) { if (listeners == null) { return null; } for (int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy