package br.com.MDSGPP.ChamadaParlamentar.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.MDSGPP.ChamadaParlamentar.model.Estatistica;
import br.com.MDSGPP.ChamadaParlamentar.model.Ranking;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

public class RankingDao extends ConnectionFactory {

	public RankingDao() throws ClassNotFoundException, SQLException {
		new ConnectionFactory().getConnection();
	}
	
	/**
	 * This method add the Ranking in the DB table.
	 * @param ranking is a Ranking contains all features of the ranking.
	 * @throws SQLException case the dataBase is off.
	 */
	
	public void adicionarRankingNaTable(Ranking ranking) throws SQLException {
		String sql =  "insert into ranking(nomeParlamentar, porcentagem, numeroSessoes)values (?, ?, ?)";
		PreparedStatement stmt = ConnectionFactory.getConexao().prepareStatement(sql);

		for( int i = 0; i < ranking.getLista().size(); i++ ) {
			try {
				stmt.setString(1, ranking.getLista().get(i).getNome());
				stmt.setString(2, ranking.getLista().get(i).getPorcentagem());
				stmt.setString(3, ranking.getLista().get(i).getNumeroSessao());
				
				stmt.execute();
			} catch (MySQLIntegrityConstraintViolationException e) {
				System.out.println(ranking.getLista().get(i).getNome());
			}
		}
		for( int i = 0; i < ranking.getRemovidos().size(); i++ ) {
			stmt.setString(1, ranking.getRemovidos().get(i).getNome());
			stmt.setString(2, "semDados");
			stmt.execute();
		}

		stmt.close();
	}
	
	/**
	 * This method returns a refreshed status of the Ranking
	 * @return ranking complete.
	 * @throws SQLException case the dataBase is off.
	 */
	public Ranking retornaRanking () throws SQLException {
		Ranking ranking = new Ranking();
		ArrayList<Estatistica> removidos = new ArrayList<Estatistica>();
		ArrayList<Estatistica> lista = new ArrayList<Estatistica>();
		
		String sql = "Select * from ranking";
		
		PreparedStatement stmt = ConnectionFactory.getConexao().prepareStatement(sql);
		ResultSet rs = stmt.executeQuery();
		
		while( rs.next() ) {
			Estatistica estatistica = new Estatistica();
			estatistica.setNome(rs.getString("nomeParlamentar"));
			if( rs.getString("porcentagem").equalsIgnoreCase("semDados") ) {
				removidos.add(estatistica);
			} 
			else {
				estatistica.setPorcentagem(rs.getString("porcentagem"));
				estatistica.setNumeroSessao(rs.getString("numeroSessoes"));
				lista.add(estatistica);
			}
		}
		
		ranking.setLista(lista);
		ranking.setRemovidos(removidos);
		return ranking;
	}


}

@@ -1852,25 +1852,23 @@
 istica%3E 
-removidos
+deleted
  = new A
@@ -1921,17 +1921,16 @@
 ca%3E list
-a
  = new A
@@ -2160,17 +2160,16 @@
 tistica 
-e
 statisti
@@ -2169,17 +2169,16 @@
 tatistic
-a
  = new E
@@ -2194,17 +2194,16 @@
 a();%0A%09%09%09
-e
 statisti
@@ -2199,25 +2199,24 @@
 %09%09%09statistic
-a
 .setNome(rs.
@@ -2318,25 +2318,23 @@
 ) %7B%0A%09%09%09%09
-removidos
+deleted
 .add(est
@@ -2360,25 +2360,24 @@
 %09else %7B%0A%09%09%09%09
-e
 statistica.s
@@ -2369,25 +2369,24 @@
 %09%09%09statistic
-a
 .setPorcenta
@@ -2423,17 +2423,16 @@
 ));%0A%09%09%09%09
-e
 statisti
@@ -2432,17 +2432,16 @@
 tatistic
-a
 .setNume
@@ -2489,17 +2489,16 @@
 %09%09%09%09list
-a
 .add(est
@@ -2543,17 +2543,16 @@
 sta(list
-a
 );%0A%09%09ran
@@ -2573,17 +2573,15 @@
 dos(
-removidos
+deleted
 );%0A%09

Change names variables of RankingDao