Package org.jpox.store.mapped.mapping

Source Code of org.jpox.store.mapped.mapping.DiscriminatorMapping

/**********************************************************************
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:
2005 Andy Jefferson - added capability to have String or long type discriminators
    ...
**********************************************************************/
package org.jpox.store.mapped.mapping;

import org.jpox.ClassLoaderResolver;
import org.jpox.ClassNameConstants;
import org.jpox.ObjectManager;
import org.jpox.metadata.ColumnMetaData;
import org.jpox.metadata.DiscriminatorMetaData;
import org.jpox.store.mapped.DatastoreAdapter;
import org.jpox.store.mapped.DatastoreContainerObject;
import org.jpox.store.mapped.DatastoreField;
import org.jpox.store.mapped.DatastoreIdentifier;
import org.jpox.store.mapped.IdentifierFactory;
import org.jpox.store.mapped.expression.LogicSetExpression;
import org.jpox.store.mapped.expression.QueryExpression;
import org.jpox.store.mapped.expression.ScalarExpression;

/**
* Mapping for a discriminator column in a table used in inheritance.
* The discriminator column is, by default, a String type, typically VARCHAR.
* It can however be "long" based if the user specifies INTEGER, BIGINT, or NUMERIC as the jdbc-type.
* In the latter case we make the necessary conversions between value types in this mapping class.
*
* This class is for internal use only. It should not be used in user mappings nor extended.
*
* @version $Revision: 1.34 $
*/
public final class DiscriminatorMapping extends SingleFieldMapping
{
    private final JavaTypeMapping delegate;

    /**
     * Constructor.
     * @param dba Datastore Adapter
     * @param datastoreContainer Datastore table
     * @param delegate The JavaTypeMapping to delegate storage
     */
    public DiscriminatorMapping(DatastoreAdapter dba, DatastoreContainerObject datastoreContainer, JavaTypeMapping delegate)
    {
        initialize(dba, delegate.getType());
        this.delegate = delegate;

        DiscriminatorMetaData dismd = datastoreContainer.getDiscriminatorMetaData();
        IdentifierFactory idFactory = datastoreContainer.getStoreManager().getIdentifierFactory();
        DatastoreIdentifier id = null;
        if (dismd.getColumnMetaData() == null)
        {
            // No column name so generate a default
            id = idFactory.newDiscriminatorFieldIdentifier();
            ColumnMetaData colmd = new ColumnMetaData(dismd, id.getIdentifier());
            dismd.setColumnMetaData(colmd);
        }
        else
        {
            // Column name defined
            ColumnMetaData colmd = dismd.getColumnMetaData();
            id = idFactory.newDatastoreFieldIdentifier(colmd.getName());
        }

        DatastoreField column = datastoreContainer.addDatastoreField(getType(), id, this, dismd.getColumnMetaData());
        datastoreContainer.getStoreManager().getMappingManager().createDatastoreMapping(delegate, datastoreContainer.getStoreManager(),
            column, getType());
    }

    /**
     * Accessor for the type represented here, returning the class itself
     * @return This class.
     */
    public Class getJavaType()
    {
        return DiscriminatorMapping.class;
    }

    /**
     * Accessor for a sample value for this type.
     * @return Sample value
     */
    public Object getSampleValue(ClassLoaderResolver clr)
    {
        return delegate.getSampleValue(clr);
    }

    /**
     * Accessor for a new literal for this mapping.
     * @param qs The QueryStatement
     * @param value The value of the object
     * @return The new literal
     */
    public ScalarExpression newLiteral(QueryExpression qs, Object value)
    {
        Object valueObj = value;
        if (value instanceof java.lang.String &&
            (getType().equals(ClassNameConstants.LONG) || getType().equals(ClassNameConstants.JAVA_LANG_LONG)))
        {
            valueObj = new Long((String)value);
        }
        return delegate.newLiteral(qs, valueObj);
    }

    /**
     * Accessor for a new scalar expression including this mapping.
     * @param qs The QueryStatement
     * @param te The table Expression
     * @return The new scalar expression
     */
    public ScalarExpression newScalarExpression(QueryExpression qs, LogicSetExpression te)
    {
        return delegate.newScalarExpression(qs, te);
    }

    /**
     * Mutator for the object in this column
     * @param om The Object Manager
     * @param preparedStatement The statement
     * @param exprIndex The indexes
     * @param value The value to set it to
     */
    public void setObject(ObjectManager om, Object preparedStatement, int[] exprIndex, Object value)
    {
        Object valueObj = value;
        if (value instanceof java.lang.String)
        {
            if (getType().equals(ClassNameConstants.LONG) || getType().equals(ClassNameConstants.JAVA_LANG_LONG))
            {
                valueObj = new Long((String)value);
            }
        }
        delegate.setObject(om, preparedStatement, exprIndex, valueObj);
    }

    /**
     * Accessor for the object in this column
     * @param om The ObjectManager
     * @param resultSet The ResultSet to get the value from
     * @param exprIndex The indexes
     * @return The object
     */
    public Object getObject(ObjectManager om, Object resultSet, int[] exprIndex)
    {
        Object value = delegate.getObject(om, resultSet, exprIndex);
        Object valueObj = value;
        if (value instanceof java.lang.String)
        {
            if (getType().equals(ClassNameConstants.LONG) || getType().equals(ClassNameConstants.JAVA_LANG_LONG))
            {
                valueObj = new Long((String)value);
            }
        }
        return valueObj;
    }

    /**
     * Accessor for the number of datastore fields.
     * @return Number of datastore fields
     */
    public int getNumberOfDatastoreFields()
    {
        return delegate.getNumberOfDatastoreFields();
    }

    /**
     * Accessor for a datastore mapping
     * @param index Index of the mapping
     * @return The datastore mapping.
     */
    public DatastoreMapping getDataStoreMapping(int index)
    {
        return delegate.getDataStoreMapping(index);
    }

    /**
     * Mutator to add a datastore mapping
     * @param datastoreMapping Datastore mapping
     */
    public void addDataStoreMapping(DatastoreMapping datastoreMapping)
    {
        delegate.addDataStoreMapping(datastoreMapping);
    }
}
TOP

Related Classes of org.jpox.store.mapped.mapping.DiscriminatorMapping

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.