package testfactorymethod;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class FuncDaoJDBC implements FuncionarioDAO {
    
    private Connection c = null;
    
    FuncDaoJDBC() throws SQLException{
        c = SimpleDataSource.getConnection();
    }
    
    public void incluir( Funcionario f) throws NotFoundException, SQLException {
        String sql = "";
        
        PreparedStatement stmt = null;
        
        try {
            sql = "INSERT INTO funcionario ( ID, datacontratacao, matricula, "
                    + "nome) VALUES (?, ?, ?, ?) ";
            stmt = c.prepareStatement(sql);
            
            stmt.setObject(1, f.getID(), Types.INTEGER);
            stmt.setString(2, f.getDatacontratacao());
            stmt.setInt(3, f.getMatricula());
            stmt.setString(4, f.getNome());
            
            int rowcount = databaseUpdate(c, stmt);
            if (rowcount != 1) {
                throw new SQLException("PrimaryKey Error when updating DB!");
            }
            
        } finally {
            if (stmt != null)
                stmt.close();
        }
    }
    
    public void alterar(Funcionario f)throws NotFoundException, SQLException {
        
        String sql = "UPDATE funcionario SET datacontratacao = ?, matricula = ?, nome = ? WHERE (ID = ? ) ";
        PreparedStatement stmt = null;
        
        try {
            stmt = c.prepareStatement(sql);
            stmt.setString(1, f.getDatacontratacao());
            stmt.setInt(2, f.getMatricula());
            stmt.setString(3, f.getNome());
            
            stmt.setObject(4, f.getID(), Types.INTEGER);
            
            int rowcount = databaseUpdate(c, stmt);
            if (rowcount == 0) {
                throw new NotFoundException("Object could not be saved! (PrimaryKey not found)");
            }
            if (rowcount > 1) {
                throw new SQLException("PrimaryKey Error when updating DB! (Many objects were affected!)");
            }
        } finally {
            if (stmt != null)
                stmt.close();
        }
    }
    
    public List<Funcionario> listarFuncionarios() throws SQLException {
        ArrayList searchResults = new ArrayList();
        ResultSet result = null;
        PreparedStatement stmt = c.prepareStatement("SELECT	* FROM FUNCIONARIO");
        
        try {
            result = stmt.executeQuery();
            
            while (result.next()) {
                Funcionario temp = new Funcionario();
                
                temp.setID(result.getInt("ID"));
                temp.setDatacontratacao(result.getString("datacontratacao"));
                temp.setMatricula(result.getInt("matricula"));
                temp.setNome(result.getString("nome"));
                
                searchResults.add(temp);
            }
            
        } finally {
            if (result != null)
                result.close();
            if (stmt != null)
                stmt.close();
        }
        
        return (List)searchResults;
    }
    
    protected int databaseUpdate(Connection conn, PreparedStatement stmt) throws NotFoundException, SQLException {
        
        int result = stmt.executeUpdate();
        
        return result;
    }
    
}
