/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package adgsyncms.repository;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;

/**
 *
 * @author amartinez
 */
public class db {

    public static Connection _connection;
    public static boolean _return = false;
    public static Statement _stmt;
    public static String _SQLQuery = "";
    //String type
    static String _Host;
    static String _Database;
    static String _User;
    static String _Port;
    static String _Password;

    public static boolean openConnection() {
        repository repo1 = new repository();
        HashMap hashMap = repo1.readSetting();

        try {
            //set value to variables
            _Host = hashMap.get("db_host_name").toString();
            _Database = hashMap.get("db_name").toString();
            _User = hashMap.get("db_user").toString();
            _Port = hashMap.get("db_port").toString();
            _Password = hashMap.get("db_secret").toString();

            //register class name 
            Class.forName("org.postgresql.Driver");

            //connect to database
            _connection = DriverManager.getConnection("jdbc:postgresql://" + _Host + ":" + _Port + "/" + _Database, _User, _Password);

            //set auto commit false
            _connection.setAutoCommit(false);
            //_connection.setClientInfo("ApplicationName", "ADGSync MS");
            _connection.getClientInfo();

            //set return to true
            _return = true;
        } catch (ClassNotFoundException | SQLException e) {
            //set return 
//            message.show(e.getMessage() + "\n" + _Host + "\n" + _Database + "\n" + _Port + "\n" + _User + "\n" + e.getClass().toString());
            _return = false;

        }
        return _return;
    }

    public static void closeConnection() {
        //if connection if not null
        if (_connection != null) {
            //close 
            try {
                _connection.close();
            } catch (Exception e) {
                //message.show(e.getMessage());
            }
        }
    }

}
