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

com.scalified.jpa.manager.JpaTransactionalManager Maven / Gradle / Ivy

/*
 * MIT License
 *
 * Copyright (c) 2018 Scalified
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.scalified.jpa.manager;

import com.scalified.jpa.function.CriteriaFunction;
import com.scalified.jpa.function.ExpressionFunction;
import com.scalified.jpa.function.ResultFunction;
import com.scalified.jpa.sp.SpQuery;
import com.scalified.jpa.specification.Specification;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * A {@link JpaManager} decorator, which adds transaction support
 * for entity write operations
 *
 * @author shell
 * @since 2018-02-06
 */
public class JpaTransactionalManager implements JpaManager {

	/**
	 * An underlying {@link JpaManager}
	 */
	protected final JpaManager manager;

	/**
	 * Creates {@link JpaTransactionalManager} instance
	 *
	 * @param manager an {@link JpaManager} to decorate
	 */
	public JpaTransactionalManager(JpaManager manager) {
		this.manager = manager;
	}

	/**
	 * Returns an entity found by its {@code primaryKey}
	 *
	 * @param entityClass a class of a searched entity
	 * @param primaryKey  a primary key of a searched entity
	 * @param          type of a searched entity
	 * @param          type of a primary key of a searched entity
	 * @return entity object
	 */
	@Override
	public  T find(Class entityClass, K primaryKey) {
		return manager.find(entityClass, primaryKey);
	}

	/**
	 * Returns the {@link List} of all generic results found by the specified
	 * {@code entityClass}
	 *
	 * @param entityClass a class of a searched entity
	 * @param          type of searched entity
	 * @return {@link List} of all generic results
	 */
	@Override
	public  List find(Class entityClass) {
		return manager.find(entityClass);
	}

	/**
	 * Returns the generic result found by the specified {@code entityClass}
	 * and derived from applying the specified {@code resultFunction}
	 *
	 * @param entityClass    a class of a searched entity
	 * @param resultFunction a function, which maps {@link CriteriaBuilder}
	 *                       to a generic result
	 * @param             type of an entity
	 * @param             type of the result
	 * @return generic result object
	 */
	@Override
	public  R find(Class entityClass, ResultFunction resultFunction) {
		return manager.find(entityClass, resultFunction);
	}

	/**
	 * Returns the {@link Stream} of generic results found by the specified {@code entityClass}
	 *
	 * @param entityClass a class of a searched entity
	 * @param          type of searched entity
	 * @return {@link Stream} of generic results
	 */
	@Override
	public  Stream stream(Class entityClass) {
		return manager.stream(entityClass);
	}

	/**
	 * Returns the {@link Stream} of generic results found by the specified {@code entityClass},
	 * which has the specified {@code chunkSize}
	 *
	 * @param entityClass a class of a searched entity
	 * @param chunkSize   size of chunk
	 * @param          type of searched entity
	 * @return {@link Stream} of generic results
	 */
	@Override
	public  Stream stream(Class entityClass, int chunkSize) {
		return manager.stream(entityClass, chunkSize);
	}

	/**
	 * Returns the generic result found by the specified {@code criteriaFunction} and
	 * derived from applying the specified {@code resultFunction}
	 *
	 * @param criteriaFunction a function to find result
	 * @param resultFunction   a function, which maps {@link CriteriaBuilder}
	 *                         to a generic result
	 * @param               type of an entity
	 * @param               type of the result
	 * @return generic result object
	 */
	@Override
	public  R find(CriteriaFunction criteriaFunction, ResultFunction resultFunction) {
		return manager.find(criteriaFunction, resultFunction);
	}

	/**
	 * Returns the {@link Stream} of generic results found by the specified {@code criteriaFunction}
	 *
	 * @param criteriaFunction a function to find result
	 * @param               type of an entity
	 * @return {@link Stream} of generic results
	 */
	@Override
	public  Stream stream(CriteriaFunction criteriaFunction) {
		return manager.stream(criteriaFunction);
	}

	/**
	 * Returns the {@link Stream} of generic results found by the specified {@code criteriaFunction},
	 * which has the specified {@code chunkSize}
	 *
	 * @param criteriaFunction a function to find result
	 * @param chunkSize        size of chunk
	 * @param               type of an entity
	 * @return {@link Stream} of generic results
	 */
	@Override
	public  Stream stream(CriteriaFunction criteriaFunction, int chunkSize) {
		return manager.stream(criteriaFunction, chunkSize);
	}

	/**
	 * Returns the generic result found by the specified {@code specification} and
	 * derived from applying the specified {@code resultFunction}
	 *
	 * @param specification  a specification to find result
	 * @param resultFunction a function, which maps {@link CriteriaBuilder}
	 *                       to a generic result
	 * @param             type of an entity
	 * @param             type of the result
	 * @return generic result object
	 */
	@Override
	public  R find(Specification specification, ResultFunction resultFunction) {
		return manager.find(specification, resultFunction);
	}

	/**
	 * Returns the raw result as a list containing column values in array of objects produced
	 * by stored procedure execution built from the specified {@code spQuery}
	 *
	 * @param spQuery stored procedure configuration object
	 * @param      type of result
	 * @return the raw result as a list containing column values in array of objects
	 */
	@Override
	public  List query(SpQuery spQuery) {
		return manager.query(spQuery);
	}

	/**
	 * Returns the count of all entities with the specified {@code entityClass}
	 *
	 * @param entityClass a class of an entity
	 * @param          type of an entity
	 * @return count of all entities
	 */
	@Override
	public  long count(Class entityClass) {
		return manager.count(entityClass);
	}

	/**
	 * Returns the count of entities with the specified {@code entityClass} filtered
	 * by the specified expression {@code function}
	 *
	 * @param entityClass a class of an entity
	 * @param function    an {@link ExpressionFunction} to apply filter
	 * @param          type of an entity
	 * @return count of filtered entities
	 */
	@Override
	public  long count(Class entityClass, ExpressionFunction function) {
		return manager.count(entityClass, function);
	}

	/**
	 * Inserts an entity object
	 *
	 * 

* Returns the inserted entity object * * @param entity an entity object to insert * @param type of an entity * @return inserted entity object */ @Override public T insert(T entity) { return applyInTransaction(transaction -> manager.insert(entity)); } /** * Inserts the collection of entities * *

* Returns the collection of inserted entities * * @param entities a collection of entities to insert * @param type of an entity * @return a collection of inserted entities */ @Override public Collection insert(Collection entities) { return applyInTransaction(transaction -> manager.insert(entities)); } /** * Updates the entity * *

* Returns the updated entity * * @param entity an entity object to update * @param type of an entity * @return updated entity object */ @Override public T update(T entity) { return applyInTransaction(transaction -> manager.update(entity)); } /** * Updates the collection of entities * *

* Returns the collection of updated entities * * @param entities the collection of updated entities * @param type of an entity * @return a collection of updated entities */ @Override public Collection update(Collection entities) { return applyInTransaction(transaction -> manager.update(entities)); } /** * Deletes the entity * * @param entity an entity object to delete * @param type of an entity */ @Override public void delete(T entity) { acceptInTransaction(transaction -> manager.delete(entity)); } /** * Deletes the collection of entities * * @param entities the collection of entities to delete * @param type of an entity */ @Override public void delete(Collection entities) { acceptInTransaction(transaction -> manager.delete(entities)); } /** * Refreshes the state of an entity * * @param entity an entity object to refresh the state of * @param type of an entity */ @Override public void refresh(T entity) { manager.refresh(entity); } /** * Refreshes the state of each entity in the specified collection * * @param entities the collection of entities to refresh states of * @param type of an entity */ @Override public void refresh(Collection entities) { manager.refresh(entities); } /** * Detaches an entity from context * * @param entity an entity object to detach * @param type of an entity */ @Override public void detach(T entity) { manager.detach(entity); } /** * Detaches the each entity in the specified collection from context * * @param entities the collection of entities to detach * @param type of an entity */ @Override public void detach(Collection entities) { manager.detach(entities); } /** * Returns the underlying {@link EntityManager} * * @return underlying {@link EntityManager} instance */ @Override public EntityManager em() { return manager.em(); } /** * Applies the specified function in a new transaction * *

* Returns generic result * * @param function a function to execute * @param type of the result * @return result of function execution */ private R applyInTransaction(Function function) { EntityTransaction transaction = manager.em().getTransaction(); transaction.begin(); R result = function.apply(transaction); transaction.commit(); return result; } /** * Accepts the specified consumer in a new transaction * * @param consumer a consumer to accept */ private void acceptInTransaction(Consumer consumer) { EntityTransaction transaction = manager.em().getTransaction(); transaction.begin(); consumer.accept(transaction); transaction.commit(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy