Package org.jpox.metadata

Source Code of org.jpox.metadata.ValueMetaData

/**********************************************************************
Copyright (c) 2004 Erik Bengtson 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 Andy Jefferson - added toString(), MetaData docs, javadocs.
2004 Andy Jefferson - added unique, indexed
    ...
**********************************************************************/
package org.jpox.metadata;

import org.jpox.ClassLoaderResolver;
import org.jpox.exceptions.ClassNotResolvedException;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.util.ClassUtils;

/**
* This element specifies the mapping for the value component of maps.
* The serialized attribute specifies that the key values are to be serialized into the named column.
*
* @since 1.1
* @version $Revision: 1.28 $
*/
public class ValueMetaData extends AbstractElementMetaData
{
    /**
     * Constructor to create a copy of the passed metadata using the provided parent.
     * @param parent The parent
     * @param vmd The metadata to copy
     */
    public ValueMetaData(MetaData parent, ValueMetaData vmd)
    {
        super(parent, vmd);
    }

    /**
     * Constructor.
     * @param parent MetaData of parent element
     * @param column The column name
     * @param deleteAction attribute delete-action value
     * @param updateAction attribute update-action value
     * @param indexed Whether to index this
     * @param unique Whether to add a unique constraint
     * @param mappedBy Field the value is mapped by in the key object
     */
    public ValueMetaData(MetaData parent,
                         String column,
                         String deleteAction,
                         String updateAction,
                         String indexed,
                         String unique,
                         String mappedBy)
    {
        super(parent, column, deleteAction, updateAction, indexed, unique, mappedBy);
    }

    /**
     * Populate the MetaData.
     * @param clr Class loader to use
     * @param primary the primary ClassLoader to use (or null)
     */
    public void populate(ClassLoaderResolver clr, ClassLoader primary)
    {
        AbstractMemberMetaData fmd = (AbstractMemberMetaData)parent;
        if (fmd.getMap() == null)
        {
            // TODO Localise this
            throw new JPOXUserException("The field "+fmd.getFullFieldName()+" is defined with <value>, however no <map> definition was found.").setFatal();
        }

        String valueType = fmd.getMap().getValueType();
        Class valueTypeClass = null;
        try
        {
            valueTypeClass = clr.classForName(valueType, 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
                valueTypeClass = clr.classForName(ClassUtils.getJavaLangClassForType(valueType), primary, false);
                fmd.getMap().value.type = valueTypeClass.getName(); // Update the MapMetaData since not yet populated
            }
            catch (ClassNotResolvedException cnre2)
            {
                throw new InvalidMetaDataException(LOCALISER,
                    "044150",
                    fmd.getFullFieldName(),fmd.getClassName(false),
                    valueType);
            }
        }
        if (embeddedMetaData != null &&
            (valueTypeClass.isInterface() || valueTypeClass.getName().equals("java.lang.Object")))
        {
            throw new InvalidMetaDataException(LOCALISER,
                "044152",
                fmd.getFullFieldName(), valueTypeClass.getName());
        }

        // TODO This will not work currently since MapMetaData is populated *after* ValueMetaData and so the
        // valueClassMetaData is not yet populated. What we should do is provide a postPopulate() method here
        // that MapMetaData can call when it is populated
        if (embeddedMetaData == null &&
            ((AbstractMemberMetaData)parent).hasMap() &&
            ((AbstractMemberMetaData)parent).getMap().isEmbeddedValue() &&
            ((AbstractMemberMetaData)parent).getJoinMetaData() != null &&
            ((AbstractMemberMetaData)parent).getMap().getValueClassMetaData() != null)
        {
            // User has specified that the value is embedded in a join table but not how we embed it
            // so add a dummy definition
            embeddedMetaData = new EmbeddedMetaData(this, null, null, null);
        }

        super.populate(clr, primary);
    }

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

    /**
     * Returns a string representation of the object using a prefix
     * This can be used as part of a facility to output a MetaData file.
     * @param prefix prefix string
     * @param indent indent string
     * @return a string representation of the object.
     */
    public String toString(String prefix, String indent)
    {
        // Field needs outputting so generate metadata
        StringBuffer sb = new StringBuffer();
        sb.append(prefix).append("<value");
        if (mappedBy != null)
        {
            sb.append(" mapped-by=\"" + mappedBy + "\"");
        }
        if (columnName != null)
        {
            sb.append("\n");
            sb.append(prefix).append("       column=\"" + columnName + "\"");
        }
        sb.append(">\n");

        // Add columns
        for (int i=0;i<columns.size();i++)
        {
            ColumnMetaData colmd=(ColumnMetaData)columns.get(i);
            sb.append(colmd.toString(prefix + indent,indent));
        }

        // Add index metadata
        if (indexMetaData != null)
        {
            sb.append(indexMetaData.toString(prefix + indent,indent));
        }

        // Add unique metadata
        if (uniqueMetaData != null)
        {
            sb.append(uniqueMetaData.toString(prefix + indent,indent));
        }

        // Add embedded metadata
        if (embeddedMetaData != null)
        {
            sb.append(embeddedMetaData.toString(prefix + indent,indent));
        }

        // Add foreign-key metadata
        if (foreignKeyMetaData != null)
        {
            sb.append(foreignKeyMetaData.toString(prefix + indent,indent));
        }

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

        sb.append(prefix).append("</value>\n");
        return sb.toString();
    }
}
TOP

Related Classes of org.jpox.metadata.ValueMetaData

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.