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

com.evasion.dao.api.DefaultDAO Maven / Gradle / Ivy

There is a newer version: 2.0.0.2
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evasion.dao.api;

import com.evasion.EntityJPA;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;
import java.util.logging.Level;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author glon-56610
 */
public class DefaultDAO implements Serializable{

    /** LOGGER */
    public static final Logger LOGGER = LoggerFactory.getLogger(DefaultDAO.class.getName());
    private transient EntityManager em;

    /**
     * Vide la session (ou l'EntityManager). Tous les changements qui n'ont pas
     * ete persistes sont perdus. Les entites sont toutes detachees de la
     * session (donc ne sont plus gerees automatiquement par la session).
     * 
     */
    public void clear() {
        LOGGER.debug("Nettoyage (clear) de la session.");
        this.em.clear();
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List findAll(Class clazz) {
        String entityName =null;
        if (clazz.isAnnotationPresent(Entity.class)) {
            entityName=clazz.getAnnotation(Entity.class).name();
        }else {
            entityName=clazz.getSimpleName();
        }
        Query query = this.em.createQuery("SELECT e FROM " + entityName + " e");
        final List result = query.getResultList();
        LOGGER.debug("La recherche de toute les entités de type {} a retourné {} résultats",
                new Object[]{clazz, result.size()});
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public EntityJPA findById(Class clazz, final Object id) {
        final EntityJPA result = clazz.cast(this.em.find(clazz,id));
        LOGGER.debug("La recherche de l'entite de type {} avec l'id {} a retourne {}",
                new Object[]{clazz, id, result});
        return result;
    }

    /**
     * {@inheritDoc}
     */
    public void flush() {
        LOGGER.debug("Synchronisation (flush) de la session counrante.");
        this.em.flush();
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public EntityJPA getReference(final Class clazz, final Object id) {
        LOGGER.debug("Recuperation d'une reference a l'entite de type {} avec l'id {}",
                new Object[]{clazz, id});
        return this.em.getReference(clazz, id);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public EntityJPA merge(final EntityJPA entity) {
        LOGGER.debug("Merging entity {}", new Object[]{entity});
        return em.merge(entity);
    }

    /**
     * {@inheritDoc}
     */
    public void persist(final EntityJPA entity) {
        LOGGER.debug("Persisting entity {}", entity);
        this.em.persist(entity);
    }

    /**
     * {@inheritDoc}
     */
    public void remove(EntityJPA entity) {
        LOGGER.debug("Suppression de l'entite {}", entity);
        this.em.remove(entity);
    }

    /**
     * {@inheritDoc}
     */
    public EntityManager getEntityManager() {
        return em;
    }

    /**
     * {@inheritDoc}
     */
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy