/*
 * DatabaseManager.java
 */

package dblpconverter;

/**
 *
 * @author Gersk
 *
 * DatabaseManager is the wrapper to DBMS It's also produce the output file of SQL command.
 *
 */

import java.sql.*;
import java.io.*;

public class DatabaseManager 
{
    private Window window;
    
    private Statement stmt;
    private Connection connection; 
    
    private File outFile;
    private File tableFile;
    private FileWriter fstream;
    private FileWriter tablefstream;
    private BufferedWriter out;
    private BufferedWriter tableout;
    
    public DatabaseManager() { }

    public void setWindow(Window window)
    {
        this.window = window;
    }
    
    public void connect(String link, String db, String name, String password) throws Exception
  {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
       
        String url = "jdbc:mysql://" + link + "/" + db;
        connection = DriverManager.getConnection(url, name, password);

        stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        
        if(connection != null) 
            window.append("Successfully connected");
        
        DatabaseMetaData meta = connection.getMetaData();
        
        window.append("----- Driver information -----");
        window.append("Driver Name: " + meta.getDriverName());
        window.append("Driver Version: " + meta.getDriverVersion());
        window.append("----- Database information -----");
        window.append("Database Name: "+ meta.getDatabaseProductName());
        window.append("Database Version: "+ meta.getDatabaseProductVersion());
    }
    
    public void disconnect()
    {
        try {
            connection.close(); 
        }   catch(SQLException e)
            {
                window.append("Error disconnect from db: " + e);
            }
    }
    
    public void setQueryFile(String fileName) 
    {
        outFile = new File(fileName);
        if(outFile.exists()) 
        {
            outFile.delete();
            try {
                outFile.createNewFile();
                }
                catch (IOException e) {
                    System.out.println("Can't create output file");
                }
        }
        else     
            try {
                outFile.createNewFile();
                }
                catch (IOException e) {
                    System.out.println("Can't create output file");
                }
        try {
            fstream = new FileWriter(outFile, true);
            out = new BufferedWriter(fstream);
        } catch (Exception e) {
            System.out.println("Can't create the output filestream, error: " + e.getMessage());
        }
    }
    
    public void setTableFile(String fileName)
    {
        tableFile = new File(fileName);
        if(tableFile.exists()) 
        {
            tableFile.delete();
            try {
                tableFile.createNewFile();
                }
                catch (IOException e) {
                    System.out.println("Can't create output file");
                }
        }
        else     
            try {
                tableFile.createNewFile();
                }
                catch (IOException e) {
                    System.out.println("Can't create output file");
                }
        try {
            tablefstream = new FileWriter(tableFile, true);
            tableout = new BufferedWriter(tablefstream);
        } catch (Exception e) {
            window.append("Can't create the output stream for table, error: " + e.getMessage());
        }
    }
    
    public void query(String query, char c)
    {
        if(window.rbutton1.isSelected()) 
        {
            try {
                stmt.execute(query); 
            } catch (SQLException e) {
                window.append(e.getMessage()); 
            }
        }
        else
        {
            try {
                if(c == 't')
                    tableout.write(query + ";");
                else
                    out.write(query + ";");
            } catch (IOException e) {
                window.append(e.getMessage());
            }
        }
    }
    
    public void closeFile()
    {
        try {
            out.close();
        } catch (Exception e) {
            window.append("Can't close the out file, error: " + e.getMessage());
        }
    }
    
    public void closeTableFile()
    {
        try {
            tableout.close();
        } catch (Exception e) {
            window.append("Can't close the table file, error: " + e.getMessage());
        }
    }
    
    public void close() 
    {
        if(connection != null)
        {    
            try {
                connection.close();
            } catch (SQLException e) {
                window.append("Can't close the db, error" + e.getMessage()); 
            }
        } 
    }
}
