Package com.evasion.dao.api

Source Code of com.evasion.dao.api.DefaultDAO

/*
* 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<EntityJPA> findAll(Class<? extends EntityJPA> 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<? extends EntityJPA> 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<? extends EntityJPA> 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;
    }
}
TOP

Related Classes of com.evasion.dao.api.DefaultDAO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.