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

de.alpharogroup.db.strategies.DefaultDeleteStrategy Maven / Gradle / Ivy

/**
 * Copyright (C) 2015 Asterios Raptis
 *
 * 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 de.alpharogroup.db.strategies;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;

import de.alpharogroup.db.entity.BaseEntity;
import de.alpharogroup.db.repository.AbstractRepository;
import de.alpharogroup.db.strategies.api.DeleteStrategy;
import de.alpharogroup.lang.TypeArgumentsExtensions;
import lombok.Getter;
import lombok.NonNull;

/**
 * The class {@link DefaultDeleteStrategy} is a default implementation of the
 * {@link DeleteStrategy}.
 *
 * @param 
 *            the type of the entity object
 * @param 
 *            the type of the primary key from the entity object
 */
public class DefaultDeleteStrategy, PK extends Serializable>
	implements
		DeleteStrategy
{

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	/** The repository. */
	@NonNull
	private final AbstractRepository repository;

	/** The class type of the entity. */
	@Getter
	@SuppressWarnings("unchecked")
	private final Class type = (Class)TypeArgumentsExtensions
		.getFirstTypeArgument(DefaultDeleteStrategy.class, this.getClass());

	/**
	 * Instantiates a new {@link DefaultDeleteStrategy}.
	 *
	 * @param repository
	 *            the repository
	 */
	public DefaultDeleteStrategy(AbstractRepository repository)
	{
		this.repository = repository;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void delete(List objects)
	{
		for (final T entity : objects)
		{
			if (getEntityManager().contains(entity))
			{
				getEntityManager().remove(entity);
			}
			else
			{
				getEntityManager().remove(getEntityManager().merge(entity));
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void delete(PK id)
	{
		final T entity = getEntityManager().find(type, id);
		delete(entity);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void delete(T entity)
	{
		if (getEntityManager().contains(entity))
		{
			getEntityManager().remove(entity);
		}
		else
		{
			getEntityManager().remove(getEntityManager().merge(entity));
		}
	}

	/**
	 * Gets the entity manager.
	 *
	 * @return the entity manager
	 */
	private EntityManager getEntityManager()
	{
		return this.repository.getEntityManager();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy