/*
 * Factory.java
 *
 * Created on 12 de Outubro de 2004, 01:09
 */

package locadora.factory;

/**
 *
 * @author  rbarcelos
 */

import locadora.core.*;
import locadora.main.*;
import java.util.*;
import javax.jdo.*;
import java.io.*;

public class Factory {
    
    PersistenceManager pm ;
    PersistenceManagerFactory pmf;
    Transaction trans;
    
    /** Creates a new instance of Factory */
    public Factory(Class classe) {
        Properties prop = new Properties();
        InputStream fileProp;
        try {
            ClassLoader resourceLoader = classe.getClassLoader();
            fileProp = resourceLoader.getResourceAsStream("locadora.jdogenie");
            
            System.out.println(fileProp.toString());
            prop.load(fileProp);
            pmf = JDOHelper.getPersistenceManagerFactory(prop);
            pm = pmf.getPersistenceManager();
            init();
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        
    }
    
    
    public void init() {
        trans=pm.currentTransaction();
        trans.setOptimistic(true);
        trans.begin();
    }
    
    public void close() {
        trans.commit();
        pm.close();
    }
    
    
    public Vector<Filme> getFilmes()
    {   Collection res;
        //Iterator i;
        
        Query q = pm.newQuery(Filme.class);
        res = null;
        try {
            res = (Collection)q.execute();
        /*    i= res.iterator();
            while(i.hasNext())
                System.out.println(i.next().toString());
         */
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            //if(res!=null) q.close(res);
            if(res!=null)
                return new Vector<Filme>(res);
            else
                return new Vector<Filme>();
            
        }
        
        
        
        
        
        
    }
    
    
    
    public Vector<Video> getVideos()
    {  Collection res;
       Iterator i;
       Query q = pm.newQuery(Video.class);
       res = null;
       try {
           res = (Collection)q.execute();
           
       }
       catch(Exception e) {
           e.printStackTrace();
       }
       finally {
           //if(res!=null) q.close(res);
           
       }
       return new Vector<Video>(res);
    }
    
    public Vector<Cliente> getClientes()
    {   Collection res;
        Iterator i;
        Query q = pm.newQuery(Cliente.class);
        res = null;
        try {
            res = (Collection)q.execute();
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            //if(res!=null) q.close(res);
            
        }
        return new Vector<Cliente>(res);
    }
    
    public Cliente getCliente(String cpf)
    {   Cliente cliente;
        
        cliente = null;
        Query query = pm.newQuery(Cliente.class);
        //query.declareVariables("Cliente cli");
        query.declareParameters("String cpf");
        query.setFilter("CPF == cpf");
        
        try {
              Collection<Cliente> col = (Collection<Cliente>)query.execute(cpf);
              System.out.println(col.toString());
              cliente = col.iterator().next();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            //if(res!=null) q.close(res);
            
        }
        return cliente;
    }
    
    public Emprestimo getEmprestimo(Date dataLocacao,Date dataDevolucao, Cliente cliente, Video video)
    {    Emprestimo emprestimo = null;
         Query query = pm.newQuery(Emprestimo.class);
         query.declareImports("import java.util.Date");
         
         query.declareParameters("Date dataLoc, Date dataDev, Cliente cli");
         query.setFilter("dataLocacao == dataLoc && dataDevolucao == dataDev && cliente == cli");
        
        try {
              Collection<Emprestimo> col = (Collection<Emprestimo>)query.execute(dataLocacao,dataDevolucao,cliente);
              System.out.println(col.toString());
              emprestimo = col.iterator().next();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            //if(res!=null) q.close(res);
            
        }
        return emprestimo;
    }
    
    public void stubInsertFilmes() {
        int cont;
        Filme film;
      /* trans=pm.currentTransaction();
         trans.setOptimistic(true);
         trans.begin();*/
        cont=10;
        String[] nomes = new String[cont];
        nomes[0]="Titanic";
        nomes[1]="Matrix";
        nomes[2]="Senhor dos Aneis 1";
        nomes[3]="Senhor dos Aneis 2";
        nomes[4]="Senhor dos Aneis 3";
        nomes[5]="Matrix 2";
        nomes[6]="Matrix 3";
        nomes[7]="Rei Artur";
        nomes[8]="Homem Aranha";
        nomes[9]="Homem Aranha 2";
        
        
        for(int i=0;i<cont;i++) {
            film = new Filme();
            film.setAnoLancamento(1990+10*i);
            film.setCategoria("Terror");
            film.setTitulo(nomes[i]);
            pm.makePersistent(film);
        }
        trans.commit();
        
    }
    
    public void save(Filme film) {
        
        pm.makePersistent(film);
        trans.commit();
        init();
    }
    
    public void save(Video film) {
        
        pm.makePersistent(film);
        trans.commit();
        init();
    }
    
    public void save(Cliente film) {
        
        pm.makePersistent(film);
        trans.commit();
        init();
    }
    
    public void save(Emprestimo film) {
        
        pm.makePersistent(film);
        trans.commit();
        init();
        
    }
    
    
    public void delete(Filme film) {
        
        pm.deletePersistent(film);
        trans.commit();
        init();
    }
    
    public void delete(Video film) {
        
        pm.deletePersistent(film);
        trans.commit();
        init();
    }
    
    public void delete(Cliente film) {
        
        pm.deletePersistent(film);
        trans.commit();
        init();
    }
    
    public void delete(Emprestimo film) {
        
        pm.deletePersistent(film);
        trans.commit();
        init();
        
    }
    
}
