/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package paqueteDAO;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import paqueteBL.Plantilla;
import paqueteBL.Pool;

/**
 *
 * @author sysmq
 */
public class PoolDAO extends BaseDAO{

    public void update(Pool pool) throws Exception
    {
        Statement scSQL = null;
        Connection co=null;
        try
        {
            co=getConexion();
            scSQL=co.createStatement();
            String s=sets("","responsable",pool.getResponsable());
            s=sets(s,"datastore",pool.getDataStore().replace("\\", "\\\\"));
            s=sets(s,"pathbackup",pool.getPathBackup().replace("\\", "\\\\"));
            s=sets(s,"horabackup",BaseDAO.getBDHora(pool.getHoraBackup()));
            System.out.println("UPDATE pool SET "+s+" WHERE idpool="+pool.getId());
            scSQL.executeUpdate("UPDATE pool SET "+s+" WHERE idpool="+pool.getId());
            scSQL.executeUpdate("DELETE FROM plantilla");
            int i;
            List<Plantilla> lista=pool.getPlantillas();
            for(i=0;i<lista.size();i++)
                //if (lista.get(i).getId()==-1)
                {
                    s="null";
                    s=concatena(s,lista.get(i).getNombre());
                    s=concatena(s,lista.get(i).getPath().replace("\\", "\\\\"));
                    System.out.println("INSERT INTO plantilla (idplantilla,nombre,path) values("+s+")");
                    scSQL.executeUpdate("INSERT INTO plantilla (idplantilla,nombre,path) values("+s+")");
                }
        } finally
        {
            if(scSQL!=null) scSQL.close();
            if (co!=null) liberaConexion(co);
        }
    }

    public void readFromRS(Pool pool,ResultSet rs) throws Exception
    {
        pool.setDataStore(rs.getString("datastore"));
        pool.setResponsable(rs.getString("responsable"));
        pool.setHoraBackup(rs.getTime("horaBackup"));
        pool.setPathBackup(rs.getString("pathBackup"));
    }

    public Pool read() throws Exception
    {
        String sql="SELECT * FROM pool WHERE idpool=1";

        Statement scSQL = null;
        ResultSet rsListaRegistros;
        Connection co=null;
        try
        {
        co=getConexion();
        scSQL=co.createStatement();
        rsListaRegistros = scSQL.executeQuery(sql);
        if(rsListaRegistros.next())
        {
            Pool pool = new Pool();
            readFromRS(pool,rsListaRegistros);
            pool.setPlantillas(loadPlantillas());
            return pool;
        }
        else
           return null;
        } finally
        {
            if(scSQL!=null) scSQL.close();
            if (co!=null) liberaConexion(co);
        }
    }

    public void readPlantillaFromRS(Plantilla plantilla,ResultSet rs) throws Exception
    {
        plantilla.setId(rs.getInt("idPlantilla"));
        plantilla.setNombre(rs.getString("nombre"));
        plantilla.setPath(rs.getString("path"));
    }

    public List<Plantilla> loadPlantillas() throws Exception
    {
        List<Plantilla> lista=new ArrayList<Plantilla>();
        ResultSet rsListaRegistros;
        Statement scSQL = null;
        Connection co=null;
        try
        {
            co=getConexion();
            scSQL=co.createStatement();

        rsListaRegistros = scSQL.executeQuery("Select * from plantilla");

        while(rsListaRegistros.next())
        {
            Plantilla plantilla=new Plantilla();
            readPlantillaFromRS(plantilla,rsListaRegistros);
            lista.add(plantilla);
        }
        return lista;
        } finally
        {
            if(scSQL!=null) scSQL.close();
            if (co!=null) liberaConexion(co);
        }
    }

}
