Package org.jpox.store.rdbms.scostore

Source Code of org.jpox.store.rdbms.scostore.BaseContainerStore

/**********************************************************************
Copyright (c) 2005 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:
    ...
**********************************************************************/
package org.jpox.store.rdbms.scostore;

import java.sql.PreparedStatement;

import org.jpox.ObjectManager;
import org.jpox.StateManager;
import org.jpox.metadata.AbstractMemberMetaData;
import org.jpox.state.StateManagerFactory;
import org.jpox.store.StoreManager;
import org.jpox.store.mapped.DatastoreAdapter;
import org.jpox.store.mapped.mapping.InterfaceMapping;
import org.jpox.store.mapped.mapping.JavaTypeMapping;
import org.jpox.store.mapped.mapping.OIDMapping;
import org.jpox.store.rdbms.RDBMSManager;
import org.jpox.store.rdbms.adapter.RDBMSAdapter;
import org.jpox.store.rdbms.mapping.RDBMSMapping;
import org.jpox.store.rdbms.table.JoinTable;
import org.jpox.util.Localiser;

/**
* Base class for all RDBMS container stores (collections, maps, arrays).
* Provides a series of helper methods for handling the store process.
*
* @version $revision$
*/
abstract class BaseContainerStore
{
    /** Localiser for messages. */
    protected static final Localiser LOCALISER = Localiser.getInstance("org.jpox.store.rdbms.Localisation",
        RDBMSManager.class.getClassLoader());

    /** Manager for the store. */
    protected RDBMSManager storeMgr;

    /** Datastore adapter in use by this store. */
    protected DatastoreAdapter dba;

    /** Mapping to the owner of the container. */
    protected JavaTypeMapping ownerMapping;

    /** MetaData for the field/property in the owner with this container. */
    protected AbstractMemberMetaData ownerMemberMetaData;

    /** Whether the container allows null elements/values. */
    protected boolean allowsNull = false;

    /**
     * Constructor.
     * @param storeMgr Manager for the datastore being used
     */
    protected BaseContainerStore(StoreManager storeMgr)
    {
        this.storeMgr = (RDBMSManager)storeMgr;
        this.dba = this.storeMgr.getDatastoreAdapter();
    }

    /**
     * Method to set the owner field/property MetaData and sets whether null elements/values are allowed.
     * @param mmd MetaData for the field/property owning this backing store.
     */
    protected void setOwnerMemberMetaData(AbstractMemberMetaData mmd)
    {
        this.ownerMemberMetaData = mmd;
        if (ownerMemberMetaData.hasExtension("allow-nulls") &&
            ownerMemberMetaData.getValueForExtension("allow-nulls").equalsIgnoreCase("true"))
        {
            // User has requested allowing nulls in this field/property
            allowsNull = true;
        }
    }

    /**
     * Accessor for the RDBMSManager.
     * @return The RDBMSManager.
     */
    public StoreManager getStoreManager()
    {
        return storeMgr;
    }

    /**
     * Accessor for the owner mapping.
     * @return Owner mapping.
     */
    public JavaTypeMapping getOwnerMapping()
    {
        return ownerMapping;
    }

    /**
     * Check if the mapping correspond to a non pc object or embedded field
     * @param mapping the mapping
     * @return true if the field is embedded into one column
     */
    protected boolean isEmbeddedMapping(JavaTypeMapping mapping)
    {
        return !InterfaceMapping.class.isAssignableFrom(mapping.getClass()) &&
               !OIDMapping.class.isAssignableFrom(mapping.getClass());
    }

    /**
     * Convenience method to populate the passed PreparedStatement with the value from the owner.
     * @param sm State Manager
     * @param om Object Manager
     * @param ps The PreparedStatement
     * @param jdbcPosition Position in JDBC statement to populate
     * @return The next position in the JDBC statement
     */
    protected int populateOwnerInStatement(StateManager sm, ObjectManager om, PreparedStatement ps, int jdbcPosition)
    {
        if (!((RDBMSMapping)ownerMapping.getDataStoreMapping(0)).insertValuesOnInsert())
        {
            // Dont try to insert any mappings with insert parameter that isnt ? (e.g Oracle)
            return jdbcPosition;
        }
        if (ownerMemberMetaData != null)
        {
            ownerMapping.setObject(om, ps,
                org.jpox.store.mapped.mapping.Mappings.getParametersIndex(jdbcPosition, ownerMapping),
                sm.getObject(),  sm, ownerMemberMetaData.getAbsoluteFieldNumber());
        }
        else
        {
            ownerMapping.setObject(om, ps,
                org.jpox.store.mapped.mapping.Mappings.getParametersIndex(jdbcPosition, ownerMapping),
                sm.getObject());
        }
        return jdbcPosition + ownerMapping.getNumberOfDatastoreFields();
    }

    /**
     * Method to return the StateManager for an embedded PC object (element, key, value).
     * It creates one if the element is not currently managed.
     * @param sm State Manager of the owner
     * @param obj The embedded PC object
     * @param table Join table where the objects are stored
     * @return The state manager
     */
    protected StateManager getStateManagerForEmbeddedPCObject(StateManager sm, Object obj, JoinTable table)
    {
        ObjectManager om = sm.getObjectManager();
        StateManager objSM = om.findStateManager(obj);
        if (objSM == null)
        {
            objSM = StateManagerFactory.newStateManagerForEmbedded(om, obj, false);

            AbstractMemberMetaData ownerFmd = table.getOwnerFieldMetaData();
            objSM.addEmbeddedOwner(sm, ownerFmd.getAbsoluteFieldNumber());
        }
        return objSM;
    }

    /**
     * Convenience method to return if the RDBMS supports batching and the user wants batching.
     * @return If batching of statements is permissible
     */
    protected boolean allowsBatching()
    {
        if (((RDBMSAdapter)dba).supportsStatementBatching() &&
            storeMgr.getOMFContext().getPersistenceConfiguration().getIntProperty("org.jpox.rdbms.statementBatchLimit") != 0)
        {
            return true;
        }
        return false;
    }
}
TOP

Related Classes of org.jpox.store.rdbms.scostore.BaseContainerStore

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.