<commit_before>package org.amc.dao;

import org.amc.DAOException;
import org.amc.EntityManagerThreadLocal;
import org.amc.game.chess.Player;
import org.apache.log4j.Logger;

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

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

/**
 * 
 * Represents a general Data Access Object to provide the link between the
 * business logic and database. Fetches and holds a reference to the Persistence
 * EntityManager
 * 
 * @author <NAME>
 * @version 1
 * @param <T> WorkEntity
 */
public class DAO<T> implements Serializable {
    /**
     * Serializable
     */
    private static final long serialVersionUID = 854157422459241714L;

    /**
     * Logger used by the object
     */
    private static final Logger LOG = Logger.getLogger(DAO.class);

    /**
     * The class this DAO is handling
     */
    private final Class<?> entityClass;

    public DAO(Class<?> entityClass) {
        this.entityClass = entityClass;
    }

    /**
     * Adds the {@link Player} object to the database
     * 
     * @param entity
     *            the new <code>Player</code> to be stored in the database
     * @throws DAOException
     *             if a problem occurs in the underlying database
     */
    public void addEntity(T entity) throws DAOException {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(entity);
            em.getTransaction().commit();
        } catch (PersistenceException pe) {
            em.getTransaction().rollback();
            em.close();
            LOG.error("DAO<" + entityClass.getSimpleName()
                            + ">:Error has occurred when trying to persist entity");
            throw new DAOException(pe);
        }
    }

    /**
     * Deletes the {@link Player} object from the database
     * 
     * @param entity
     *            the <code>Player</code> to be deleted
     * @throws DAOException
     *             if a problem occurs in the underlying database
     */
    public void deleteEntity(T entity) throws DAOException {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            T entityToBeRemoved = em.merge(entity);
            em.remove(entityToBeRemoved);
            em.getTransaction().commit();
        } catch (PersistenceException pe) {
            em.close();
            LOG.error("DAO<" + entityClass.getSimpleName()
                            + ">:Error has occurred when trying to delete entity");
            throw new DAOException(pe);
        }
    }

    /**
     * Fetchs a list of all objects of {@link Player} stored in the
     * underlying database. The results from the database are stored in a
     * <code>java.util.List</code>.
     * <p>
     * Caution if there are a lot of rows in the database if may return a very
     * large <code>Collection</code> of objects.
     * 
     * @return List of all WorkEntities in the database
     * @throws DAOException
     *             if a problem occurs in the underlying database
     * @deprecated as of 30/09/14, no replacement.
     */
    @SuppressWarnings("unchecked")
    public List<T> findEntities() throws DAOException {
        try {
            Query query = getEntityManager().createQuery(
                            "Select x from " + entityClass.getSimpleName() + " x");
            List<T> resultList = query.getResultList();
            return resultList;
        } catch (PersistenceException pe) {
            LOG.error("DAO<" + entityClass.getSimpleName()
                            + ">:Error has occurred when trying to find entities");
            throw new DAOException(pe);
        }
    }

    /**
     * Fetchs a list of objects of class {@link Player} stored in the
     * underlying database. The results from the database are stored in a
     * <code>java.util.List</code>.
     * <commit_msg>undeprecate ublic List<T> findEntities() throws DAOException<commit_after>package org.amc.dao;

import org.amc.DAOException;
import org.amc.EntityManagerThreadLocal;
import org.amc.game.chess.Player;
import org.apache.log4j.Logger;

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

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

/**
 * 
 * Represents a general Data Access Object to provide the link between the
 * business logic and database. Fetches and holds a reference to the Persistence
 * EntityManager
 * 
 * @author <NAME>
 * @version 1
 * @param <T> WorkEntity
 */
public class DAO<T> implements Serializable {
    /**
     * Serializable
     */
    private static final long serialVersionUID = 854157422459241714L;

    /**
     * Logger used by the object
     */
    private static final Logger LOG = Logger.getLogger(DAO.class);

    /**
     * The class this DAO is handling
     */
    private final Class<?> entityClass;

    public DAO(Class<?> entityClass) {
        this.entityClass = entityClass;
    }

    /**
     * Adds the {@link Player} object to the database
     * 
     * @param entity
     *            the new <code>Player</code> to be stored in the database
     * @throws DAOException
     *             if a problem occurs in the underlying database
     */
    public void addEntity(T entity) throws DAOException {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(entity);
            em.getTransaction().commit();
        } catch (PersistenceException pe) {
            em.getTransaction().rollback();
            em.close();
            LOG.error("DAO<" + entityClass.getSimpleName()
                            + ">:Error has occurred when trying to persist entity");
            throw new DAOException(pe);
        }
    }

    /**
     * Deletes the {@link Player} object from the database
     * 
     * @param entity
     *            the <code>Player</code> to be deleted
     * @throws DAOException
     *             if a problem occurs in the underlying database
     */
    public void deleteEntity(T entity) throws DAOException {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            T entityToBeRemoved = em.merge(entity);
            em.remove(entityToBeRemoved);
            em.getTransaction().commit();
        } catch (PersistenceException pe) {
            em.close();
            LOG.error("DAO<" + entityClass.getSimpleName()
                            + ">:Error has occurred when trying to delete entity");
            throw new DAOException(pe);
        }
    }

    /**
     * Fetchs a list of all objects of {@link Player} stored in the
     * underlying database. The results from the database are stored in a
     * <code>java.util.List</code>.
     * <p>
     * Caution if there are a lot of rows in the database if may return a very
     * large <code>Collection</code> of objects.
     * 
     * @return List of all WorkEntities in the database
     * @throws DAOException
     *             if a problem occurs in the underlying database
     * 
     */
    @SuppressWarnings("unchecked")
    public List<T> findEntities() throws DAOException {
        try {
            Query query = getEntityManager().createQuery(
                            "Select x from " + entityClass.getSimpleName() + " x");
            List<T> resultList = query.getResultList();
            return resultList;
        } catch (PersistenceException pe) {
            LOG.error("DAO<" + entityClass.getSimpleName()
                            + ">:Error has occurred when trying to find entities");
            throw new DAOException(pe);
        }
    }

    /**
     * Fetchs a list of objects of class {@link Player} stored in the
     * underlying database. The results from the database are stored in a
     * <code>java.util.List</code>.
     * 
{'fraction_non_alphanumeric': 0.09585492227979274, 'fraction_numerical': 0.005674808783617074, 'mean_word_length': 2.1422480620155038, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '228807', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2148, 'n_words': 677}