Package org.jpox.metadata

Source Code of org.jpox.metadata.CollectionMetaData

/**********************************************************************
Copyright (c) 2004 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:
2004 Kikuchi Kousuke - org.jpox.enhancer.conf.JDOConfigCollection
2004 Erik Bengtson - add dependent elements
    ...
**********************************************************************/
package org.jpox.metadata;

import java.util.List;
import java.util.Set;

import org.jpox.ClassLoaderResolver;
import org.jpox.api.ApiAdapter;
import org.jpox.exceptions.ClassNotResolvedException;
import org.jpox.util.ClassUtils;
import org.jpox.util.JPOXLogger;
import org.jpox.util.StringUtils;

/**
* Representation of the MetaData of a collection.
*
* @since 1.1
* @version $Revision: 1.37 $
*/
public class CollectionMetaData extends ContainerMetaData
{
    /** Representation of the element of the collection. */
    protected ContainerComponent element;

    /**
     * Constructor to create a copy of the passed metadata using the passed parent.
     * @param parent The parent
     * @param colmd The metadata to copy
     */
    public CollectionMetaData(AbstractMemberMetaData parent, CollectionMetaData colmd)
    {
        super(parent);
        element = new ContainerComponent();
        element.embedded = colmd.element.embedded;
        element.serialized = colmd.element.serialized;
        element.dependent = colmd.element.dependent;
        element.type = colmd.element.type;
        element.classMetaData = colmd.element.classMetaData;
    }

    /**
     * Constructor.
     * @param parent The parent Field element
     * @param elementType The type of element
     * @param embeddedElement Whether elements are embedded
     * @param dependentElement attribute dependent-element value
     * @param serializedElement attribute serialized-element value
     */
    public CollectionMetaData(AbstractMemberMetaData parent,
                              String elementType,
                              String embeddedElement,
                              String dependentElement,
                              String serializedElement)
    {
        super(parent);

        if (!StringUtils.isWhitespace(elementType) && elementType.indexOf(',') > 0)
        {
            throw new InvalidMetaDataException(LOCALISER, "044131",
                parent.getName(), parent.getClassName());
        }
        element = new ContainerComponent(parent.getAbstractClassMetaData().getPackageName(), elementType,
            embeddedElement, serializedElement, dependentElement);
    }

    /**
     * Method to populate any defaults, and check the validity of the MetaData.
     * @param clr ClassLoaderResolver to use for any loading operations
     * @param primary the primary ClassLoader to use (or null)
     */
    public void populate(ClassLoaderResolver clr, ClassLoader primary)
    {
        ApiAdapter api = getMetaDataManager().getApiAdapter();

        // Check the field type and see if it is castable to a Collection
        Class field_type = getMemberMetaData().getType();
        if (!java.util.Collection.class.isAssignableFrom(field_type))
        {
            throw new InvalidMetaDataException(LOCALISER,
                    "044132",
                    getFieldName(),getMemberMetaData().getClassName(false));
        }

        // "element-type"
        if (element.type == null)
        {
            throw new InvalidMetaDataException(LOCALISER,
                    "044133",
                    getFieldName(),getMemberMetaData().getClassName(false));
        }

        // Check that the key type exists
        Class elementTypeClass = null;
        try
        {
            elementTypeClass = clr.classForName(element.type, primary);
        }
        catch (ClassNotResolvedException cnre)
        {
            try
            {
                // Maybe the user specified a java.lang class without fully-qualifying it
                // This is beyond the scope of the JDO spec which expects java.lang cases to be fully-qualified
                elementTypeClass = clr.classForName(ClassUtils.getJavaLangClassForType(element.type), primary);
            }
            catch (ClassNotResolvedException cnre2)
            {
                throw new InvalidMetaDataException(LOCALISER,
                    "044134",
                    getFieldName(),getMemberMetaData().getClassName(false),
                    element.type);
            }
        }

        if (!elementTypeClass.getName().equals(element.type))
        {
            // The element-type has been resolved from what was specified in the MetaData - update to the fully-qualified name
            JPOXLogger.METADATA.info(LOCALISER.msg("044135", getFieldName(), getMemberMetaData().getClassName(false),
                element.type, elementTypeClass.getName()));
            element.type = elementTypeClass.getName();
        }

    // "embedded-element"
        if (element.embedded == null)
        {
            // Assign default for "embedded-element" based on 18.13.1 of JDO 2 spec
            // Note : this fails when using in the enhancer since not yet PC
            if (getMetaDataManager().getOMFContext().getTypeManager().isDefaultEmbeddedType(elementTypeClass))
            {
                element.embedded = Boolean.TRUE;
            }
            else if (api.isPersistable(elementTypeClass) ||
                Object.class.isAssignableFrom(elementTypeClass) ||
                elementTypeClass.isInterface())
            {
                element.embedded = Boolean.FALSE;
            }
            else
            {
                element.embedded = Boolean.TRUE;
            }
        }
        if (element.embedded == Boolean.FALSE)
        {
            // If the user has set a non-PC/non-Interface as not embedded, correct it since not supported.
            // Note : this fails when using in the enhancer since not yet PC
            if (!api.isPersistable(elementTypeClass) && !elementTypeClass.isInterface() &&
                elementTypeClass != java.lang.Object.class)
            {
                element.embedded = Boolean.TRUE;
            }
        }

        ElementMetaData elemmd = ((AbstractMemberMetaData)parent).getElementMetaData();
        if (elemmd != null && elemmd.getEmbeddedMetaData() != null)
        {
            element.embedded = Boolean.TRUE;
        }

        if (element.dependent == Boolean.TRUE)
        {
            // If the user has set a non-PC/non-reference as dependent, correct it since not valid.
            // Note : this fails when using in the enhancer since not yet PC
            if (!api.isPersistable(elementTypeClass) && !elementTypeClass.isInterface() &&
                elementTypeClass != java.lang.Object.class)
            {
                element.dependent = Boolean.FALSE;
            }
        }

        // Keep a reference to the MetaData for the element
        element.classMetaData = getMemberMetaData().getAbstractClassMetaData().getMetaDataManager().getMetaDataForClassInternal(elementTypeClass, clr);

        setPopulated();
    }

    // ------------------------------ Accessors --------------------------------

    /**
     * Accessor for the element-type tag value.
     * This can contain comma-separated values.
     * @return element-type tag value
     */
    public String getElementType()
    {
        return element.type;
    }

    /**
     * Accessor for the Element ClassMetaData
     * @return element ClassMetaData
     */
    public AbstractClassMetaData getElementClassMetaData()
    {
        if (element.classMetaData != null && !element.classMetaData.isInitialised())
        {
            element.classMetaData.initialise();
        }
        return element.classMetaData;
    }

    /**
     * Accessor for the embedded-element tag value
     * @return embedded-element tag value
     */
    public boolean isEmbeddedElement()
    {
        if (element.embedded == null)
        {
            return false;
        }
        else
        {
            return element.embedded.booleanValue();
        }
    }

    /**
     * Accessor for The dependent-element attribute indicates that the
     * collection's element contains a reference that is to be deleted if the
     * referring instance is deleted.
     *
     * @return dependent-element tag value
     */
    public boolean isDependentElement()
    {
        if (element.dependent == null)
        {
            return false;
        }
        else if (element.classMetaData == null)
        {
            return false;
        }
        else
        {
            return element.dependent.booleanValue();
        }
    }

    /**
     * Accessor for the serialized-element tag value
     * @return serialized-element tag value
     */
    public boolean isSerializedElement()
    {
        if (element.serialized == null)
        {
            return false;
        }
        else
        {
            return element.serialized.booleanValue();
        }
    }

    // ------------------------------- Utilities -------------------------------

    /**
     * Accessor for all ClassMetaData referenced by this array.
     * @param orderedCMDs List of ordered ClassMetaData objects (added to).
     * @param referencedCMDs Set of all ClassMetaData objects (added to).
     * @param dba_vendor_id Vendor ID of the DBA. Used for view addition.
     * @param clr the ClassLoaderResolver
     **/
    void getReferencedClassMetaData(final List orderedCMDs,
                                    final Set referencedCMDs,
                                    final String dba_vendor_id,
                                    final ClassLoaderResolver clr)
    {
        AbstractClassMetaData element_cmd=getMetaDataManager().getMetaDataForClass(element.type, clr);
        if (element_cmd != null)
        {
            element_cmd.getReferencedClassMetaData(orderedCMDs,referencedCMDs,dba_vendor_id,clr);
        }
    }

    /**
     * Returns a string representation of the object.
     * @param prefix prefix string
     * @param indent indent string
     * @return a string representation of the object.
     */
    public String toString(String prefix,String indent)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(prefix).append("<collection element-type=\"").append(element.type).append("\"");
        if (element.embedded != null)
        {
            sb.append(" embedded-element=\"").append(element.embedded).append("\"");
        }
        if (element.dependent != null)
        {
            sb.append(" dependent-element=\"").append(element.dependent).append("\"");
        }
        if (element.serialized != null)
        {
            sb.append(" serialized-element=\"").append(element.serialized).append("\"");
        }
        sb.append(">\n");

        // Add extensions
        sb.append(super.toString(prefix + indent,indent));
        sb.append(prefix).append("</collection>\n");
        return sb.toString();
    }
}
TOP

Related Classes of org.jpox.metadata.CollectionMetaData

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.