/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.qsoft.rs.sincoar.dao;
import com.qsoft.dao.bdd.AbstractDaoGenericoBDD;
import java.io.Serializable;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;
/**
 *
 * @author developer2
 */
public class DaoGenerico <T extends Serializable, PK extends Serializable> extends AbstractDaoGenericoBDD<T, PK>{
     private final static Integer MAX_REGISTROS = 1000;
    
    @PersistenceContext(unitName = "flipPU")    
    private EntityManagerFactory emf;
    /**
     * Unidad de persistencia
     */
    @PersistenceContext(unitName = "flipPU")
    private EntityManager em;
    /**
     * datasource inyectado, se obtiene del contenedor EJB
     */
    @Resource(mappedName = "jdbc/FlipDS")
    private DataSource dataSource;

    public DaoGenerico(Class<T> tipo) {
        super(tipo, MAX_REGISTROS);
    }
    
    public DaoGenerico(Class<T> tipo, Integer maxRegistros) {
        super(tipo, maxRegistros);
    }

    private EntityManagerFactory getEmf() {
        if (this.emf == null || !this.emf.isOpen()) {//TODO: este codigo es para ejecucion local o ejecucion forzada 
            this.emf = Persistence.createEntityManagerFactory("wmpPU");
        }
        return this.emf;
    }

    @Override
    public EntityManager getEm() {        
        if(this.em == null){
            this.em = this.getEmf().createEntityManager();
        }
        return this.em;
    }

    @Override
    public DataSource getDataSource() {
        return this.dataSource;
    }
}
