package ve.gob.cnti.receiver.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.apache.commons.dbcp2.BasicDataSource;

import ve.gob.cnti.receiver.utils.IRamdomNumber;
import ve.gob.cnti.receiver.utils.PropertiesReader;
import ve.gob.cnti.receiver.utils.RamdomNumber;


/**
 * Esta clase se encarga de extraer informacion de la tabla arch_process_instance_lastupdate, process_instance y data_instance
 * para implementar esta clase utilice la clase CaseImpl.java
 * @author Yamil Orozco
 */

public class FuncionarioGI implements IFuncionarioGI {
	
	Connection cnn=null;
	Statement ps = null;
    ResultSet rs=null;
    private BasicDataSource basicDataSource=new BasicDataSource();
    PropertiesReader properties = new PropertiesReader();		
    public String DATABASE_URL = properties.getProperty("DATABASE_URL");
    public String DATABASE_USER = properties.getProperty("DATABASE_USER");
    public String DATABASE_PASSWORD = properties.getProperty("DATABASE_PASSWORD");
    public Integer MAX_CONNECTION = Integer.valueOf(properties.getProperty("MAX_CONNECTION"));
    public Integer INITIAL_CONNECTION = Integer.valueOf(properties.getProperty("INITIAL_CONNECTION"));
    public Integer MAX_IDLE = Integer.valueOf(properties.getProperty("MAX_IDLE"));
    public Integer MIN_IDLE = Integer.valueOf(properties.getProperty("MIN_IDLE"));
    public Boolean REMOVE_ABANDONED = Boolean.valueOf(properties.getProperty("REMOVE_ABANDONED"));
    public Integer REMOVE_ABANDONED_TIME = Integer.valueOf(properties.getProperty("REMOVE_ABANDONED_TIME"));
    private static final Logger log = Logger.getLogger(ConnectionJDBC.class.getName());
    Boolean next=null;
    
    public FuncionarioGI(){
    	
		basicDataSource.setDriverClassName("org.postgresql.Driver");
		basicDataSource.setUrl(DATABASE_URL);
		basicDataSource.setUsername(DATABASE_USER);
		basicDataSource.setPassword(DATABASE_PASSWORD);
		basicDataSource.setMaxTotal(MAX_CONNECTION);
		basicDataSource.setInitialSize(INITIAL_CONNECTION);
		basicDataSource.setValidationQuery("select 1");
		basicDataSource.setRemoveAbandonedOnBorrow(REMOVE_ABANDONED);
		basicDataSource.setRemoveAbandonedTimeout(REMOVE_ABANDONED_TIME);
		basicDataSource.setMaxIdle(MAX_IDLE);
		basicDataSource.setMinIdle(MIN_IDLE);
    }
  
   public String getEmail(String tramite){
	   
	   List<String> emails=new ArrayList<String>();
	   
		String query="select distinct email from tramite_interno where tramite='"+tramite+"' and nivel=1";
		System.out.println(query);
		String email=null;	
		try {
			cnn =basicDataSource.getConnection();
			ps=cnn.createStatement();			
			rs = ps.executeQuery(query);				
			while(rs.next()){					
				emails.add(rs.getString("email"));		
				System.out.println("email"+rs.getString("email"));
			}
			
			int total=emails.size();
			
			int tamanoEmails=emails.size();
			System.out.println("el correo es "+tamanoEmails);
			IRamdomNumber ramdom=new RamdomNumber();
			int numeroFuncionario=ramdom.randInt(0, total-1);
			System.out.println("el numero aleatorio es"+String.valueOf(numeroFuncionario));
			email=emails.get(numeroFuncionario);		
			try {
				if(rs!=null)rs.close();
				if(ps!=null) ps.close();
				if(cnn!=null) cnn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return email;   
	   
	   
   }
   
	
	
   
	public void closeConnection(){
		
		try {
			
			//ps.close();
			//cnn.close();
			if(basicDataSource!=null)
				basicDataSource.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
	}



	
    
	

}
