/*
 * 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 sikd.sikd5.sikd5ejb.session.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import sikd.sikd5.sikd5bean.util.PropertiesLoader;

/**
 *
 * @author User
 */
public class MyBatisConnectionFactoryIKonsol {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {

            String resource = "sikd/sikd5/sikd5ejb/session/util/mybatis-config.xml";
            Reader reader = Resources.getResourceAsReader(resource);

            Properties pr = PropertiesLoader.loadProperties("sikd.properties");

            String dbServer = pr.getProperty("dbServer");
            String dbName = pr.getProperty("dbName");
            String dbPort = pr.getProperty("dbPort");
            String dbtype = "?stringtype=unspecified";
            String dbUrl = "jdbc:postgresql://" + dbServer + ":" + dbPort + "/" + dbName + dbtype;
            //String dbDriver = "org.postgresql.Driver";

            Properties properties = new Properties();
            properties.setProperty("username", "postgres");
            properties.setProperty("password", "haromain182");
            properties.setProperty("url", dbUrl);

            if (sqlSessionFactory == null) {
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, properties);
            }
        } catch (FileNotFoundException fileNotFoundException) {
            fileNotFoundException.printStackTrace();
        } catch (IOException iOException) {
            iOException.printStackTrace();
        } catch (Exception ex) {
            Logger.getLogger(MyBatisConnectionFactoryIKonsol.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public static Map<String, String> urlConnection() {
        Map<String, String> map = new HashMap<>();
        String resource = "sikd/sikd5/sikd5ejb/session/util/mybatis-config.xml";
        try {
            Reader reader = Resources.getResourceAsReader(resource);
            Properties pr = PropertiesLoader.loadProperties("sikd.properties");

            String dbServer = pr.getProperty("dbServer");
            String dbName = pr.getProperty("dbName");
            String dbPort = pr.getProperty("dbPort");
            String dbtype = "?stringtype=unspecified";
            String dbUrl = "jdbc:postgresql://" + dbServer + ":" + dbPort + "/" + dbName;
            //String dbDriver = "org.postgresql.Driver";
            map.put("dbUrl", dbUrl);
            map.put("username", "postgres");
            map.put("password", "haromain182");
//            Properties properties = new Properties();
//            properties.setProperty("username", "postgres");
//            properties.setProperty("password", "haromain182");
//            properties.setProperty("url", dbUrl);
        } catch (IOException ex) {
            Logger.getLogger(MyBatisConnectionFactoryIKonsol.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(MyBatisConnectionFactoryIKonsol.class.getName()).log(Level.SEVERE, null, ex);
        }
        
            return map;
    }

}
