All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openbp.server.persistence.BasicPersistenceContext Maven / Gradle / Ivy

There is a newer version: 0.9.11
Show newest version
/*
 *   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.
 */
package org.openbp.server.persistence;

import org.openbp.common.logger.LogUtil;
import org.openbp.common.util.ToStringHelper;

/**
 * Basic implementation of a persistence context.
 *
 * This object should be rather lightweight than heavyweight.
 *
 * @author Heiko Erhardt
 */
public abstract class BasicPersistenceContext
	implements PersistenceContext
{
	/** Transactionality flag */
	private boolean transactional;

	/** Persistence context provider that created this context */
	private BasicPersistenceContextProvider persistenceContextProvider;

	/**
	 * Constructor.
	 */
	public BasicPersistenceContext()
	{
	}

	/**
	 * Constructor.
	 *
	 * @param persistenceContextProvider Persistence context provider
	 */
	public BasicPersistenceContext(BasicPersistenceContextProvider persistenceContextProvider)
	{
		this.persistenceContextProvider = persistenceContextProvider;
		setTransactional(persistenceContextProvider.isTransactional());
	}

	/**
	 * Gets the transactionality flag.
	 */
	public boolean isTransactional()
	{
		return transactional;
	}

	/**
	 * Sets the transactionality flag.
	 */
	public void setTransactional(boolean transactional)
	{
		this.transactional = transactional;
	}

	/**
	 * Releases the context.
	 * The underlying database session will be closed.
	 */
	public void release()
	{
		persistenceContextProvider.unbindThreadContext(this);
	}

	/**
	 * Returns a string representation of this object.
	 */
	public String toString()
	{
		return ToStringHelper.toString(this);
	}

	/**
	 * Sets the persistence context provider that created this context.
	 */
	public void setPersistenceContextProvider(BasicPersistenceContextProvider persistenceContextProvider)
	{
		this.persistenceContextProvider = persistenceContextProvider;
	}

	/**
	 * Gets the persistence context provider that created this context.
	 */
	public PersistenceContextProvider getPersistenceContextProvider()
	{
		return persistenceContextProvider;
	}

	/**
	 * Creates a query descriptor object for the given object class.
	 *
	 * @param cls Cls
	 * @return The new query descriptor object
	 */
	public PersistenceQuery createQuery(Class cls)
	{
		return new PersistenceQuery(this, cls);
	}

	/**
	 * Returns a new instance of an entity.
	 *
	 * @param entityClass Entity (interface or object) class that the instance must match.
	 * @return The new entity instance
	 */
	public Object createEntity(Class entityClass)
		throws PersistenceException
	{
		return persistenceContextProvider.createEntity(entityClass, this);
	}

	/**
	 * Returns the actual bean class of a given entity.
	 *
	 * @param entityClass Entity (interface) class that the instance must match
	 * @return The class of the instantiated bean for the given entity class or the argument if no entity definition can be found
	 */
	public Class determineEntityClass(Class entityClass)
	{
		return persistenceContextProvider.determineEntityClass(entityClass);
	}

	/**
	 * Begins a new transaction.
	 * In non-transactional persistence context implementations, this operation will do nothing.
	 * Does nothing if a transaction is already running.
	 */
	public void beginTransaction()
		throws PersistenceException
	{
		if (isTransactional())
		{
			doBeginTransaction();
		}
	}

	/**
	 * Reverts any changes that have occurred to objects within the transaction.
	 * In non-transactional persistence context implementations, this operation will merely mark the transaction as 'rollback only' if the supported by the underlying persistence store.
	 * Does nothing if no transaction is currently running.
	 */
	public void rollbackTransaction()
		throws PersistenceException
	{
		if (isTransactional())
		{
			doRollbackTransaction();
		}
	}

	/**
	 * Commits any changes that have occurred to objects within the transaction.
	 * In non-transactional persistence context implementations, this operation will do nothing.
	 * @throws PersistenceException On error performing the operations on the persistent storage
	 */
	public void commitTransaction()
		throws PersistenceException
	{
		if (isTransactional())
		{
			doCommitTransaction();
		}
	}

	//////////////////////////////////////////////////
	// @@ Helpers
	//////////////////////////////////////////////////

	/**
	 * Creates a persistence exception and logs it.
	 *
	 * @param cause Underlying database exception
	 * @return The exception
	 */
	protected PersistenceException createLoggedException(Throwable cause)
	{
		String msg = LogUtil.error(getClass(), "Persistence error.", cause);
		return new PersistenceException(msg, cause);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy