package com.fitbank.fitpatch;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.fitbank.util.Debug;

/**
 * Clase para acceder a un repositorio de mantis.
 *
 * @author FitBank GF
 */
public class MantisQueries {

    public static class ConnectionException extends Exception {

        public ConnectionException(Throwable cause) {
            super(cause);
        }

    }

    private Connection con;

    /**
     * Crea una nueva instancia de NotesQuery
     * 
     * @throws ConnectionException
     */
    public MantisQueries(String dbServer, String user, String pass) throws
            ConnectionException {
        try {
            Debug.info("Abriendo conexion a mantis...");
            Class.forName("com.mysql.jdbc.Driver");
            this.con = DriverManager.getConnection("jdbc:mysql://" + dbServer
                    + "/mantis", user, pass);
            Debug.info("Conexion abierta con exito.");
        } catch (SQLException e) {
            throw new ConnectionException(e);
        } catch (ClassNotFoundException e) {
            throw new ConnectionException(e);
        }
    }

    public void close() throws SQLException {
        this.con.close();
    }

    public Map<String, Set<Integer>> revisionMap(long pBug, String pRepo) {
        try {
            PreparedStatement pst =
                    this.con.prepareStatement("select branch, revision"
                    + " from mantis_plugin_Source_bug_table b"
                    + " join mantis_plugin_Source_changeset_table c on b.change_id=c.id"
                    + " join mantis_plugin_Source_repository_table r on r.id=c.repo_id"
                    + " where bug_id=? and name=? order by revision");
            ResultSet rst = null;
            Map<String, Set<Integer>> revisiones =
                    new HashMap<String, Set<Integer>>();
            try {
                pst.setLong(1, pBug);
                pst.setString(2, pRepo);
                rst = pst.executeQuery();
                while (rst.next()) {
                    String branch = rst.getString(1);
                    if (!branch.equals("trunk")) {
                        branch = "branches/" + branch;
                    }
                    String rev = rst.getString(2);
                    if (!revisiones.containsKey(branch)) {
                        revisiones.put(branch,
                                new TreeSet<Integer>());
                    }
                    revisiones.get(branch).add(Integer.parseInt(rev));
                }
            } finally {
                if (rst != null) {
                    rst.close();
                }
                pst.close();
            }

            return revisiones;

        } catch (SQLException e) {
            Debug.error(e);
            return new HashMap<String, Set<Integer>>();
        }
    }

}
