package exemplo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;

public class RepositorioPessoasBDR 
    implements exemplo.RepositorioPessoas {

    private exemplo.MecanismoPersistenciaAdHoc mecanismoPersistencia;

    public RepositorioPessoasBDR(exemplo.MecanismoPersistenciaAdHoc mecanismoPersistencia) {
        this.mecanismoPersistencia = mecanismoPersistencia;
    }

    public java.util.Enumeration retornaTodos() throws exemplo.BancoDadosException {
        exemplo.RepositorioPessoasArray resposta = new RepositorioPessoasArray(100);
        java.sql.Connection con = null;
        java.sql.Statement stmt = null;
        java.sql.ResultSet rs = null;
        try {
            java.lang.String query = "SELECT nome , cpf, timestamp FROM PESSOAS_SCBS";
            con = (java.sql.Connection)mecanismoPersistencia.getCanalComunicacao();
            stmt = con.createStatement();
            for (rs = stmt.executeQuery(query); rs.next(); resposta.inserir(new Pessoa(rs.getString("nome"), rs.getString("cpf"), rs.getLong("timestamp")))) { }
        }
        catch (java.sql.SQLException ex) {
            throw new BancoDadosException(ex);
        }
        finally {
            if (rs != null) {
                try {
                    rs.close();
                }
                catch (java.lang.Exception exception1) { }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                }
                catch (java.lang.Exception exception2) { }
            }
            if (con != null) {
                mecanismoPersistencia.liberarCanalComunicacao(con);
            }
        }
        return resposta;
    }

    public void inserir(exemplo.Pessoa pessoa) throws exemplo.BancoDadosException {
        if (pessoa == null) {
            throw new IllegalArgumentException("Pessoa invalida");
        }
        java.sql.Connection con = null;
        try {
            java.lang.String query = "INSERT INTO PESSOAS_SCBS (nome, cpf, timestamp) VALUES('" + pessoa.getNome() + "' , '" + pessoa.getCpf() + "'" + " , " + pessoa.getTimestamp() + ")";
            con = (java.sql.Connection)mecanismoPersistencia.getCanalComunicacao();
            java.sql.Statement stmt = con.createStatement();
            stmt.executeUpdate(query);
            stmt.close();
        }
        catch (java.sql.SQLException ex) {
            throw new BancoDadosException(ex);
        }
        finally {
            if (con != null) {
                mecanismoPersistencia.liberarCanalComunicacao(con);
            }
        }
    }

    public void atualizar(exemplo.Pessoa pessoa) throws exemplo.PessoaNaoCadastradaException, exemplo.BancoDadosException, exemplo.ObjetoDesatualizadoException {
        if (pessoa == null) {
            throw new IllegalArgumentException("Pessoa invalida");
        }
        java.sql.Connection con = null;
        try {
            java.lang.String query = "UPDATE PESSOAS_SCBS SET nome='" + pessoa.getNome() + "'" + ", " + "timestamp=" + (pessoa.getTimestamp() + 1L) + " WHERE " + "cpf ='" + pessoa.getCpf() + "'" + " AND " + "timestamp=" + pessoa.getTimestamp() + "";
            con = (java.sql.Connection)mecanismoPersistencia.getCanalComunicacao();
            java.sql.Statement stmt = con.createStatement();
            int numLinhas = stmt.executeUpdate(query);
            stmt.close();
            if (numLinhas == 0) {
                if (existe(pessoa.getCpf())) {
                    throw new ObjetoDesatualizadoException();
                } else {
                    throw new PessoaNaoCadastradaException();
                }
            }
            pessoa.incrementarTimestamp();
        }
        catch (java.sql.SQLException ex) {
            throw new BancoDadosException(ex);
        }
        finally {
            if (con != null) {
                mecanismoPersistencia.liberarCanalComunicacao(con);
            }
        }
    }

    public void remover(java.lang.String cpf) throws exemplo.PessoaNaoCadastradaException, exemplo.BancoDadosException {
        if (cpf == null) {
            throw new IllegalArgumentException("Pessoa invalida");
        }
        java.sql.Connection con = null;
        try {
            java.lang.String query = "DELETE PESSOAS_SCBS WHERE cpf ='" + cpf + "'";
            con = (java.sql.Connection)mecanismoPersistencia.getCanalComunicacao();
            java.sql.Statement stmt = con.createStatement();
            int numLinhas = stmt.executeUpdate(query);
            stmt.close();
            if (numLinhas == 0) {
                throw new PessoaNaoCadastradaException();
            }
        }
        catch (java.sql.SQLException ex) {
            throw new BancoDadosException(ex);
        }
        finally {
            if (con != null) {
                mecanismoPersistencia.liberarCanalComunicacao(con);
            }
        }
    }

    public exemplo.Pessoa procurar(java.lang.String cpf) throws exemplo.PessoaNaoCadastradaException, exemplo.BancoDadosException {
        if (cpf == null) {
            throw new IllegalArgumentException("Pessoa invalida");
        }
        java.sql.Connection con = null;
        java.sql.Statement stmt = null;
        exemplo.Pessoa resposta = null;
        java.sql.ResultSet rs = null;
        try {
            java.lang.String query = "SELECT nome , cpf, timestamp FROM PESSOAS_SCBS WHERE cpf='" + cpf + "'";
            con = (java.sql.Connection)mecanismoPersistencia.getCanalComunicacao();
            stmt = con.createStatement();
            rs = stmt.executeQuery(query);
            if (rs.next()) {
                resposta = new Pessoa(rs.getString("nome"), rs.getString("cpf"), rs.getLong("timestamp"));
            } else {
                throw new PessoaNaoCadastradaException();
            }
        }
        catch (java.sql.SQLException ex) {
            throw new BancoDadosException(ex);
        }
        catch (java.lang.Exception ex) {
            throw new BancoDadosException(ex);
        }
        finally {
            if (rs != null) {
                try {
                    rs.close();
                }
                catch (java.lang.Exception exception1) { }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                }
                catch (java.lang.Exception exception2) { }
            }
            if (con != null) {
                mecanismoPersistencia.liberarCanalComunicacao(con);
            }
        }
        return resposta;
    }

    public boolean existe(java.lang.String cpf) throws exemplo.BancoDadosException {
        if (cpf == null) {
            throw new IllegalArgumentException("Pessoa invalida");
        }
        boolean resposta = false;
        java.sql.Connection con = null;
        java.sql.Statement stmt = null;
        java.sql.ResultSet rs = null;
        try {
            java.lang.String query = "SELECT * FROM PESSOAS_SCBS WHERE cpf='" + cpf + "'";
            con = (java.sql.Connection)mecanismoPersistencia.getCanalComunicacao();
            stmt = con.createStatement();
            rs = stmt.executeQuery(query);
            resposta = rs.next();
        }
        catch (java.sql.SQLException ex) {
            throw new BancoDadosException(ex);
        }
        finally {
            if (rs != null) {
                try {
                    rs.close();
                }
                catch (java.lang.Exception exception1) { }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                }
                catch (java.lang.Exception exception2) { }
            }
            if (con != null) {
                mecanismoPersistencia.liberarCanalComunicacao(con);
            }
        }
        return resposta;
    }
}
