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

package main;

import com.tecgraf.dao.FacadeJPA;
import com.tecgraf.dao.GenericDAOJPAException;
import dao.AlunoDAO;
import dao.DisciplinaDAO;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import negocio.Aluno;
import negocio.Disciplina;

/**
 *
 * @author 42071420687
 */
public class Main {

    public static void main( String[] args ) {
        // Criate FacadeJPA and DAO objects
        String persistUnitStr = "ExemploAulaPU";
        
/*-----  Funcionando OK ----------------
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistUnitStr);
        EntityManager em = emf.createEntityManager();
        // Aluno -----
        Query query = em.createNamedQuery("Aluno.findById");
        Aluno a1 = (Aluno)query.setParameter("id", 1).getSingleResult();
        System.out.println(a1);
        
        query = em.createNamedQuery("Aluno.findByNome");
        a1 = (Aluno)query.setParameter("nome", "maria").getSingleResult();
        System.out.println(a1);

        query = em.createNamedQuery("Aluno.findByMatricula");
        a1 = (Aluno)query.setParameter("matricula", "33333").getSingleResult();
        System.out.println(a1);
        
        System.out.println();

        // Aluno->Disciplinas -----
        query = em.createNamedQuery("Aluno.findById");
        a1 = (Aluno)query.setParameter("id", 1).getSingleResult();
        System.out.println("Lista de disciplinas: " + a1);        
        for( Disciplina d : a1.getDisciplinaCollection()  ){
          System.out.print("  " + d);        
        }
        System.out.println("\n");
        
        // Disciplina -----
        query = em.createNamedQuery("Disciplina.findById");
        Disciplina d1 = (Disciplina)query.setParameter("id", 2).getSingleResult();
        System.out.println(d1);
        
        query = em.createNamedQuery("Disciplina.findByNome");
        d1 = (Disciplina)query.setParameter("nome", "FrameWorks").getResultList().get(0);
        System.out.println(d1);

        query = em.createNamedQuery("Disciplina.findByCodigo");
        d1 = (Disciplina)query.setParameter("codigo", "ALG").getResultList().get(0);
        System.out.println(d1);
        
        System.out.println();
        
        System.exit(0);
 -----*/        
        // initialize Facade JPA
        FacadeJPA facJPA = FacadeJPA.getInstance(persistUnitStr);
        
        // Disciplinas -----
        DisciplinaDAO daoDisciplina = DisciplinaDAO.getInstance(facJPA);
        List<Disciplina> ld = daoDisciplina.findAll(Disciplina.class);
        System.out.println("1-Lista de disciplinas: ");   
        for(Disciplina d : ld ) {
            System.out.print("  " + d);        
        }
        System.out.println("\n");
        
        // Aluno-Disciplinas ------
        AlunoDAO daoAluno = AlunoDAO.getInstance(facJPA);
        List<Aluno> la = daoAluno.findAll(Aluno.class);
        for(Aluno a : la ) {
          System.out.println("2-Lista de disciplinas: " + a);     
          for( Disciplina d : a.getDisciplinaCollection()  ){
            System.out.print("  " + d);        
          }
          System.out.println();
        }
        System.out.println("\n");
        
        // Updating Aluno
        Aluno a1 = daoAluno.findById(Aluno.class, 1);
        Aluno a2 = new Aluno(a1);
        try {
          a1.setNome("MATEUS");
          daoAluno.updatePersistent(Aluno.class, a1);
          
          la = daoAluno.findAll(Aluno.class);
          for(Aluno a : la ) {
            System.out.println("3-Lista de disciplinas: " + a);     
            for( Disciplina d : a.getDisciplinaCollection()  ){
              System.out.print("  " + d);        
            }
            System.out.println();
          }
          // restaura banco
          daoAluno.updatePersistent(Aluno.class, a2);

        } catch( GenericDAOJPAException ex ) {
          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("\n");
        
        // Removing Aluno
        Aluno a3 = daoAluno.findById(Aluno.class, 3);
        Aluno a4 = daoAluno.findById(Aluno.class, 3);
        try {
          
          daoAluno.deletePersistent(Aluno.class, a3);
          
          List<Aluno> lb = daoAluno.findAll(Aluno.class);
          for(Aluno a : lb ) {
            System.out.println("4-Lista de disciplinas: " + a);     
            for( Disciplina d : a.getDisciplinaCollection()  ){
              System.out.print("  " + d);        
            }
            System.out.println();
          }
          // restaura banco
          daoAluno.updatePersistent(Aluno.class, a4);
          System.out.println("\n");        
        } catch( GenericDAOJPAException ex ) {
          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // Aluno-Disciplinas estado inicial do Banco ------
        List<Aluno> lc = daoAluno.findAll(Aluno.class);
        for(Aluno a : lc ) {
          System.out.println("5-Lista de disciplinas: " + a);     
          for( Disciplina d : a.getDisciplinaCollection()  ){
            System.out.print("  " + d);        
          }
          System.out.println();
        }
        System.out.println("\n");
        
        // shutdown Facade JPA
        System.out.println("\n");
        facJPA.shutdown();
    }    
}
