Package org.jpox.jpa.state

Source Code of org.jpox.jpa.state.Hollow

/**********************************************************************
Copyright (c) 2006 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:
2008 Andy Jefferson - fixed transitionReadField, transitionWriteField exceptions
    ...
**********************************************************************/
package org.jpox.jpa.state;

import org.jpox.FetchPlan;
import org.jpox.StateManager;
import org.jpox.Transaction;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.state.IllegalStateTransitionException;
import org.jpox.state.LifeCycleState;

/**
* Class representing the life cycle state of Hollow.
*/
class Hollow extends LifeCycleState
{
    /** Protected Constructor to prevent external instantiation. */
    protected Hollow()
    {
        isPersistent = true;
        isDirty = false;
        isNew = false;
        isDeleted = false;
        isTransactional = false;

        stateType = HOLLOW;
    }

    /**
     * Method to transition to delete persistent.
     * @param sm StateManager.
     * @return new LifeCycle state.
     */
    public LifeCycleState transitionDeletePersistent(StateManager sm)
    {
        return changeState(sm, P_DELETED);
    }

    /**
     * Method to transition to transactional.
     * @param sm StateManager.
     * @return new LifeCycle state.
     */
    public LifeCycleState transitionMakeTransactional(StateManager sm)
    {
        sm.refreshLoadedFields();
        return changeState(sm, P_CLEAN);
    }

    /**
     * Method to transition to transient.
     * @param sm StateManager.
     * @param useFetchPlan to make transient the fields in the fetch plan
     * @return new LifeCycle state.
     **/
    public LifeCycleState transitionMakeTransient(StateManager sm, boolean useFetchPlan, boolean detachAllOnCommit)
    {
        if (useFetchPlan)
        {
            sm.loadUnloadedFieldsInFetchPlan();
        }
        return changeState(sm, TRANSIENT);
    }

    /**
     * Method to transition to commit state.
     * @param sm StateManager.
     * @return new LifeCycle state.
     */
    public LifeCycleState transitionCommit(StateManager sm)
    {
        throw new IllegalStateTransitionException(this, "commit", sm);
    }

    /**
     * Method to transition to rollback state.
     * @param sm StateManager.
     * @return new LifeCycle state.
     */
    public LifeCycleState transitionRollback(StateManager sm)
    {
        throw new IllegalStateTransitionException(this, "rollback", sm);
    }

    /**
     * Method to transition to read-field state.
     * @param sm StateManager.
     * @param isLoaded if the field was previously loaded
     * @return new LifeCycle state.
     */
    public LifeCycleState transitionReadField(StateManager sm, boolean isLoaded)
    {
        Transaction tx = sm.getObjectManager().getTransaction();
        if (!tx.isActive() && !tx.getNontransactionalRead())
        {
            throw new JPOXUserException(LOCALISER.msg("027001"), sm.getInternalObjectId());
        }

        if (!tx.getOptimistic() && tx.isActive())
        {
            return changeState(sm, P_CLEAN);
        }
        else
        {
            return changeState(sm, P_NONTRANS);
        }
    }

    /**
     * Method to transition to write-field state.
     * @param sm StateManager.
     * @return new LifeCycle state.
     */
    public LifeCycleState transitionWriteField(StateManager sm)
    {
        Transaction tx = sm.getObjectManager().getTransaction();
        if (!tx.isActive() && !tx.getNontransactionalWrite())
        {
            throw new JPOXUserException(LOCALISER.msg("027001"), sm.getInternalObjectId());
        }
        return changeState(sm, tx.isActive() ? P_DIRTY : P_NONTRANS);
    }

    /**
     * Method to transition to retrieve state.
     * @param sm StateManager.
     * @param fgOnly only the current fetch group fields
     * @return new LifeCycle state.
     */
    public LifeCycleState transitionRetrieve(StateManager sm, boolean fgOnly)
    {
        if (fgOnly)
        {
            sm.loadUnloadedFieldsInFetchPlan();
        }
        else
        {
            sm.loadUnloadedFields();
        }
        Transaction tx = sm.getObjectManager().getTransaction();
        if (!tx.getOptimistic() && tx.isActive())
        {
            return changeState(sm, P_CLEAN);
        }
        else if (tx.getOptimistic())
        {
            return changeState(sm, P_NONTRANS);
        }
        return super.transitionRetrieve(sm, fgOnly);
    }
   
    /**
     * Method to transition to retrieve state.
     * @param sm StateManager.
     * @param fetchPlan the fetch plan to load fields
     * @return new LifeCycle state.
     **/
    public LifeCycleState transitionRetrieve(StateManager sm, FetchPlan fetchPlan)
    {
        sm.loadUnloadedFieldsOfClassInFetchPlan(fetchPlan);
        Transaction tx = sm.getObjectManager().getTransaction();
        if (!tx.getOptimistic() && tx.isActive())
        {
            return changeState(sm, P_CLEAN);
        }
        else if (tx.getOptimistic())
        {
            return changeState(sm, P_NONTRANS);
        }
        return super.transitionRetrieve(sm, fetchPlan);
    }

    /**
     * Method to transition to refresh state.
     * @param sm StateManager.
     * @return new LifeCycle state.
     */
    public LifeCycleState transitionRefresh(StateManager sm)
    {
        sm.clearSavedFields();

        // Refresh the FetchPlan fields and unload all others
        sm.refreshFieldsInFetchPlan();
        sm.unloadNonFetchPlanFields();

        // We leave in the same state to be consistent with JDO section 5.9.1
        return this;
    }

    /**
     * Method to transition to detached-clean.
     * @param sm StateManager.
     * @return new LifeCycle state.
     **/
    public LifeCycleState transitionDetach(StateManager sm)
    {
        return changeState(sm, DETACHED_CLEAN);
    }

    /**
     * Method to transition when serialised.
     * @param sm State Manager
     * @return The new LifeCycle state
     */
    public LifeCycleState transitionSerialize(StateManager sm)
    {
        Transaction tx = sm.getObjectManager().getTransaction();
        if (tx.isActive() && !tx.getOptimistic())
        {
            return changeState(sm, P_CLEAN);
        }
        return this;
    }

    /**
     * Method to return a string version of this object.
     * @return The string "HOLLOW".
     */
    public String toString()
    {
        return "HOLLOW";
    }
}
TOP

Related Classes of org.jpox.jpa.state.Hollow

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.