/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package empresa;

import com.tecgraf.dao.FacadeJPA;
import com.tecgraf.dao.IEntityDAOJPA;
import com.tecgraf.dao.IGenericDAOJPA;
import empresa.dao.DepartamentoDAO;
import empresa.dao.DependenteDAO;
import empresa.dao.EmpregadoDAO;
import empresa.dao.ProjetoDAO;
import empresa.negocio.Departamento;
import empresa.negocio.Empregado;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 42071420684
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main( String[] args ) {
        // Criate FacadeJPA and DAO objects
        String persistUnitStr = "EmpresaJPAPU";        
        FacadeJPA facJPA = FacadeJPA.getInstance(persistUnitStr);
        
        DepartamentoDAO daoDep = DepartamentoDAO.getInstance(facJPA);
        EmpregadoDAO daoEmp = EmpregadoDAO.getInstance(facJPA);
        DependenteDAO daoDepend = DependenteDAO.getInstance(facJPA);
        ProjetoDAO daoProj = ProjetoDAO.getInstance(facJPA);

        // Test daoDep ------------------------------------------------------------
        Departamento newEntity = new Departamento();
        newEntity.setNome("COMUNICACAO"); newEntity.setNumero(15);
        newEntity.setLocacao("RIO DE JANEIRO"); newEntity.setOrcamento(new BigDecimal(350000));
        
        String queryStr1 = "Select d from " + daoDep.getEntityName() + " d where d.orcamento > ?1 ";
        Object[] posParam = { new BigDecimal(10000) };

        Departamento removeEntity1 = daoDep.findById(Departamento.class, new Integer(1));
        
        Map<String, Object> namedParams = new HashMap<String, Object>();
        namedParams.put("valor", new BigDecimal(10000));
        String queryStr2= "Select d from " + daoDep.getEntityName() + " d where d.orcamento > :valor ";
        
        Departamento removeEntity2 = daoDep.findById(Departamento.class, new Integer(2));
        
        testDAO(daoDep, Departamento.class, newEntity, queryStr1, posParam, removeEntity1,
                queryStr2, namedParams, removeEntity2);
        
        // Test daoEmp ------------------------------------------------------------
        Empregado e = daoEmp.findById(Empregado.class, new Integer(2));
        List<Empregado> empregados = new ArrayList<Empregado>(); empregados.add(e);
        newEntity.setEmpregadoCollection(empregados);

        
        facJPA.shutdown();
    }
    
    /**
     * Generic Method for testing all DAO's basic functions.
     * Observe that it is just a convenience method not a Unit Test
     * @param <T>
     * @param dao
     * @param c
     * @param newEntity
     * @param queryStr1
     * @param posParam
     * @param removeEntity1
     * @param queryStr2
     * @param namedParams
     * @param removeEntity2
     */
    public static <T extends IEntityDAOJPA<Integer>> void testDAO( IGenericDAOJPA<T, Integer> dao,
                                                                Class<T> c, 
                                                                T newEntity,
                                                                String queryStr1, Object[] posParam,
                                                                T removeEntity1, 
                                                                String queryStr2, Map<String, Object> namedParams,
                                                                T removeEntity2 ) {
      try {
        System.out.println("----- List All " + dao.getEntityName() + " ------");
        List<T> l = dao.findAll(c);
        for( T d : l ) {
            System.out.println(d);
        }
        
        System.out.println("\n////// Removing entity-1 - pk= " + removeEntity1 + " //////");
        boolean deleted = false;
        dao.deletePersistent(c, removeEntity1);
        System.out.println("status  --> " + deleted);
                
        System.out.println("\n----- List " + queryStr1 + " = " + posParam[0]);
        l = dao.queryPositionalParams(queryStr1, posParam);                
        for( T d : l ) {
            System.out.println(d);
        }
        
        System.out.println("\n////// Removing entity-2 - pk= " + removeEntity2 + " //////");
        deleted = dao.deletePersistent(c, removeEntity2);
        System.out.println("status  --> " + deleted);
           
        System.out.println("\n----- List " + queryStr2 + " = "+ namedParams);
        l = dao.queryNamedParams(queryStr2, namedParams);
        for( T d : l ) {
            System.out.println(d);
        }
    } catch( Exception ex  ) {
        Logger.getLogger(dao.getEntityClassName()).log(Level.SEVERE, null, ex);
    }
  }
}
