Package org.jpox.store.mapped.mapping

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

/**********************************************************************
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 javadocs
2004 Andy Jefferson - changed to use Column spec from MetaData
    ...
**********************************************************************/
package org.jpox.store.mapped.mapping;

import java.sql.Timestamp;

import org.jpox.ClassLoaderResolver;
import org.jpox.ObjectManager;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.metadata.ColumnMetaData;
import org.jpox.metadata.VersionMetaData;
import org.jpox.metadata.VersionStrategy;
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 class for mapping version state/timestamp columns in the database.
* This class is for internal use only. It should not be used in user mappings.
*
* @version $Revision: 1.29 $
*/
public final class VersionMapping extends SingleFieldMapping implements SimpleDatastoreRepresentation
{
    private final JavaTypeMapping delegate;

    private final VersionMetaData versionMetaData;

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

        // Currently we only use a single column mapping for versioning.
        // The MetaData supports multiple columns and so we could extend this in the future
        // to use all MetaData information.
        ColumnMetaData[] versionColumnMetaData = versionMetaData.getColumnMetaData();
        ColumnMetaData colmd;
        IdentifierFactory idFactory = datastoreContainer.getStoreManager().getIdentifierFactory();
        DatastoreIdentifier id = null;
        if (versionColumnMetaData.length == 0)
        {
            // No column name so generate a default
            id = idFactory.newVersionFieldIdentifier();
            colmd = new ColumnMetaData(versionMetaData, id.getIdentifier());
            datastoreContainer.getVersionMetaData().addColumn(colmd);
        }
        else
        {
            // Column name defined
            colmd = versionColumnMetaData[0];
            id = idFactory.newDatastoreFieldIdentifier(colmd.getName());
        }
        DatastoreField column = datastoreContainer.addDatastoreField(getType(), id, this, colmd);
        datastoreContainer.getStoreManager().getMappingManager().createDatastoreMapping(delegate, datastoreContainer.getStoreManager(),
            column, getType());
    }

    /**
     * Accessor for a sample value for this column
     * @return The 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)
    {
        return delegate.newLiteral(qs, value);
    }

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

    /**
     * Accessor for whether to include this column in any fetch statement
     * @return Whether to include the column when fetching.
     */
    public boolean includeInFetchStatement()
    {
        return false;
    }

    /**
     * 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 The mapping index
     * @return the datastore mapping
     */
    public DatastoreMapping getDataStoreMapping(int index)
    {
        return delegate.getDataStoreMapping(index);
    }

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

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

    /**
     * Mutator for the object in this column
     * @param om The ObjectManager
     * @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)
    {
        delegate.setObject(om, preparedStatement, exprIndex, value);
    }

    /**
     * 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)
    {
        return delegate.getObject(om, resultSet, exprIndex);
    }

    /**
     * Accessor for the next version of the object.
     * Returns an object of type Long (NONE, VERSION_NUMBER strategy) or Timestamp (DATE_TIME strategy).
     * TODO Move this to a utilities class where it can be used by other datastore that dont use mappings.
     * @param vermd MetaData defining the version.
     * @param currentVersion The current version
     * @return The next version
     */
    public static Object getNextVersion(VersionMetaData vermd, Object currentVersion)
    {
        if (vermd == null)
        {
            // TODO Support optimistic when not using strategy
            throw new JPOXUserException("JPOX doesnt currently support the use of optimistic transactions when " +
                "no strategy is specified");
        }
        else if (vermd.getVersionStrategy() == VersionStrategy.NONE)
        {
            // Just increment the version
            if (currentVersion == null)
            {
                return new Long(1);
            }
            return new Long(((Long)currentVersion).longValue()+1);
        }
        else if (vermd.getVersionStrategy() == VersionStrategy.DATE_TIME)
        {
            return new Timestamp(System.currentTimeMillis());
        }
        else if (vermd.getVersionStrategy() == VersionStrategy.VERSION_NUMBER)
        {
            if (currentVersion == null)
            {
                return new Long(1);
            }
            return new Long(((Long)currentVersion).longValue()+1);
        }
        else if (vermd.getVersionStrategy() == VersionStrategy.STATE_IMAGE)
        {
            // TODO Support state-image strategy
            throw new JPOXUserException("JPOX doesnt currently support optimistic version strategy \"state-image\"");
        }
        else
        {
            throw new JPOXUserException("Unknown optimistic version strategy - not supported");
        }
    }
}
TOP

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

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.