Package org.jpox.metadata

Source Code of org.jpox.metadata.ArrayMetaData

/**********************************************************************
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.JDOConfigArray
2007 Xuan Baldauf - Support for the ability of users to explictly state that an array whose component type is not PersistenceCapable may still have PersistenceCapable elements. See http://www.jpox.org/servlet/jira/browse/CORE-3274
    ...
**********************************************************************/
package org.jpox.metadata;

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

import org.jpox.ClassLoaderResolver;
import org.jpox.ClassNameConstants;
import org.jpox.api.ApiAdapter;
import org.jpox.util.StringUtils;

/**
* Representation of the Meta-Data for an Array.
*
* @since 1.1
* @version $Revision: 1.36 $
*/
public class ArrayMetaData extends ContainerMetaData
{
    /** Representation of the element of the array. */
    protected ContainerComponent element;
   
    /** wether this array may contain PersistenceCapable elements */
    protected boolean mayContainPersistenceCapableElements;

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

    /**
     * Constructor.
     * @param parent The parent Field
     * @param elementType Implementation type(s) of element possible (when it is a reference type).
     * @param embeddedElement Whether the element is embedded into the join table
     * @param dependentElement Whether the element is dependent for deletion purposes
     * @param serializedElement Whether the element is serialised into the join table
     */
    public ArrayMetaData(AbstractMemberMetaData parent,
                         String elementType,
                         String embeddedElement,
                         String dependentElement,
                         String serializedElement)
    {
        super(parent);

        if (!StringUtils.isWhitespace(elementType) && elementType.indexOf(',') > 0)
        {
            throw new InvalidMetaDataException(LOCALISER, "044140",
                parent.getName(), parent.getClassName());
        }
        element = new ContainerComponent(parent.getAbstractClassMetaData().getPackageName(), elementType,
            embeddedElement, serializedElement, dependentElement);
        if (StringUtils.isWhitespace(elementType))
        {
            // Arrays dont default to Object
            element.type = null;
        }
    }

    /**
     * Method to populate any defaults, and check the validity of the MetaData.
     * @param clr ClassLoaderResolver to use in loading any classes
     * @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 an array type
        Class field_type = getMemberMetaData().getType();
        if (!field_type.isArray())
        {
            throw new InvalidMetaDataException(LOCALISER,
                    "044141",
                    getFieldName(),getMemberMetaData().getClassName(false));
        }

        // "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
            Class component_type = field_type.getComponentType();
            if (getMetaDataManager().getOMFContext().getTypeManager().isDefaultEmbeddedType(component_type))
            {
                element.embedded = Boolean.TRUE;
            }
            else if (api.isPersistable(component_type) || Object.class.isAssignableFrom(component_type) ||
                component_type.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
            Class component_type = field_type.getComponentType();
            if (!api.isPersistable(component_type) && !component_type.isInterface() &&
                component_type != java.lang.Object.class)
            {
                element.embedded = Boolean.TRUE;
            }
        }

        if (!getMemberMetaData().getAbstractClassMetaData().getMetaDataManager().isEnhancing() && !getMemberMetaData().isSerialized())
        {
            // Catch situations that JPOX doesnt support
            if (getMemberMetaData().getJoinMetaData() == null &&
                !api.isPersistable(getMemberMetaData().getType().getComponentType()) &&
                getMetaDataManager().supportsORM())
            {
                // JPOX only supports persisting particular array types as byte-streams (non-Java-serialised)
                String arrayComponentType = getMemberMetaData().getType().getComponentType().getName();
                if (!arrayComponentType.equals(ClassNameConstants.BOOLEAN) &&
                    !arrayComponentType.equals(ClassNameConstants.BYTE) &&
                    !arrayComponentType.equals(ClassNameConstants.CHAR) &&
                    !arrayComponentType.equals(ClassNameConstants.DOUBLE) &&
                    !arrayComponentType.equals(ClassNameConstants.FLOAT) &&
                    !arrayComponentType.equals(ClassNameConstants.INT) &&
                    !arrayComponentType.equals(ClassNameConstants.LONG) &&
                    !arrayComponentType.equals(ClassNameConstants.SHORT) &&
                    !arrayComponentType.equals(ClassNameConstants.JAVA_LANG_BOOLEAN) &&
                    !arrayComponentType.equals(ClassNameConstants.JAVA_LANG_BYTE) &&
                    !arrayComponentType.equals(ClassNameConstants.JAVA_LANG_CHARACTER) &&
                    !arrayComponentType.equals(ClassNameConstants.JAVA_LANG_DOUBLE) &&
                    !arrayComponentType.equals(ClassNameConstants.JAVA_LANG_FLOAT) &&
                    !arrayComponentType.equals(ClassNameConstants.JAVA_LANG_INTEGER) &&
                    !arrayComponentType.equals(ClassNameConstants.JAVA_LANG_LONG) &&
                    !arrayComponentType.equals(ClassNameConstants.JAVA_LANG_SHORT) &&
                    !arrayComponentType.equals("BigDecimal") &&
                    !arrayComponentType.equals("BigInteger"))
                {
                    // Impossible to persist an array of a non-PC element without a join table or without serialising the array
                    throw new InvalidMetaDataException(LOCALISER, "044142",
                        getFieldName(), getMemberMetaData().getType().getComponentType().getName());
                }
            }
        }

        // Keep a reference to the MetaData for the element
        MetaDataManager mmgr = getMemberMetaData().getAbstractClassMetaData().getMetaDataManager();
        if (element.type != null)
        {
            Class elementCls = clr.classForName(element.type, primary);
           
            if (api.isPersistable(elementCls))
            {
                mayContainPersistenceCapableElements = true;
            }
           
           
            element.classMetaData = mmgr.getMetaDataForClassInternal(elementCls, clr);
        }
        else
        {
            element.type = field_type.getComponentType().getName();
            element.classMetaData = mmgr.getMetaDataForClassInternal(field_type.getComponentType(), clr);
        }
       
        if (element.classMetaData!=null)
        {
            mayContainPersistenceCapableElements = true;
        }

        setPopulated();
    }

    /**
     * Accessor for the element implementation types (when element is a reference type).
     * The return can contain comma-separated values.
     * @return element implementation types
     */
    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;
    }

    /**
     * Returns whether this array may contain PersistenceCapable elements (as indicated by the user).
     * TODO Remove this. The element-type of the array defines such things and this is not the solution
     * @return whether this array may contain PersistenceCapable elements
     */
    public boolean mayContainPersistenceCapableElements()
    {
        return mayContainPersistenceCapableElements;
    }

    /**
     * Accessor for the embedded-element value
     * @return embedded-element value
     */
    public boolean isEmbeddedElement()
    {
        if (element.embedded == null)
        {
            return false;
        }
        else
        {
            return element.embedded.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();
        }
    }

    /**
     * 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
        {
            return element.dependent.booleanValue();
        }
    }

    /**
     * Accessor for all AbstractClassMetaData referenced by this array.
     * @param orderedCMDs List of ordered AbstractClassMetaData objects (added to).
     * @param referencedCMDs Set of all AbstractClassMetaData 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(getMemberMetaData().getType().getComponentType(), clr);
        if (element_cmd != null)
        {
            element_cmd.getReferencedClassMetaData(orderedCMDs, referencedCMDs, dba_vendor_id, clr);
        }
    }

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

            // Add extensions
            sb.append(super.toString(prefix + indent,indent));

            sb.append(prefix).append("</array>\n");
        }
        else
        {
            sb.append(prefix).append("/>\n");
        }

        return sb.toString();
    }
}
TOP

Related Classes of org.jpox.metadata.ArrayMetaData

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.