/*
 * 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 javax.sql.DataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import sikd.sikd5.sikd5bean.util.PropertiesLoader;
import sikd.sikd5.sikd5ejb.mapper.HeaderapbdskpdMapper;
import sikd.sikd5.sikd5ejb.mapper.HomeMapper;
import sikd.sikd5.sikd5ejb.mapper.KontakPemdaMapper;
import sikd.sikd5.sikd5ejb.mapper.LaporanMapper;
import sikd.sikd5.sikd5ejb.mapper.LraMapper;
import sikd.sikd5.sikd5ejb.mapper.PerkiraanBelanjaMapper;
import sikd.sikd5.sikd5ejb.mapper.PosisiKasMapper;
import sikd.sikd5.sikd5ejb.mapper.RefSimpatikMapper;
import sikd.sikd5.sikd5ejb.mapper.SettingsMapper;
import sikd.sikd5.sikd5ejb.mapper.UploadFileMapper;
import sikd.sikd5.sikd5ejb.mapper.login.LoginMapper;
import sikd.sikd5.sikd5ejb.mapper.ref.VersiMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.ApbddetilMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.ApbddetilakunMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.BelanjaMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.BelanjaakunMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.BelanjapotonganMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.BelanjaspjbuktiMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.DthMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.HeaderDthMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.HeaderapbdMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.HeaderjurnalMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.HeaderlogMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.JurnalMapper;

/**
 *
 * @author agus
 * @tanggal Oct 31, 2018
 */
public class MyBatisConnectionFactory {

    private static SqlSessionFactory sqlSessionFactory;
    private static SqlSessionFactory sqlSessionFactoryLogin;
    private static SqlSessionFactory sqlSessionFactoryIKonsol;
    

    static {
        try {
            System.out.println("1.........................");
            if (sqlSessionFactory == null) {
                System.out.println("2........................");
//                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
                Properties pr = PropertiesLoader.loadProperties("sikd.properties");
                String user = "postgres";
                String password = "manutd123";
//                String password = "postgres";
                String dbServer = pr.getProperty("dbServer");
                String dbName = pr.getProperty("dbName");
                String dbPort = pr.getProperty("dbPort");
                String databasenameURL = "jdbc:postgresql://" + dbServer + ":" + dbPort + "/" + dbName;
                String dbDriver = "org.postgresql.Driver";
                /**
                 * org.postgresql.Driver || jdbc:postgresql://{host}:{port}/{database}
                 * com.mysql.jdbc.Driver || jdbc:mysql://{host}:{port}/{database}
                 * net.sourceforge.jtds.jdbc.Driver || jdbc:sqlserver://{host}:{port}/;[databaseName={database}]
                 * com.microsoft.sqlserver.jdbc.SQLServerDriver || jdbc:jtds:sqlserver://{host}:{port}/{database}
                 * org.firebirdsql.jdbc.FBDriver || jdbc:firebirdsql://{host}:{port}/{file}
                 * sun.jdbc.odbc.JdbcOdbcDriver || jdbc:odbc:mydsn
                 */
                System.out.println("databseurl : " + databasenameURL);
                DataSource dataSource = new org.apache.ibatis.datasource.pooled.PooledDataSource(
                        dbDriver, databasenameURL, user, password);
//                DataSource dataSource = new org.apache.ibatis.datasource.unpooled.UnpooledDataSource(
//                        dbDriver, databasenameURL, user, password);
                TransactionFactory transactionFactory = new JdbcTransactionFactory();
                Environment environment = new Environment("development",
                        transactionFactory, dataSource);
                Configuration configuration = new Configuration(environment);
                configuration.addMapper(KontakPemdaMapper.class);
                configuration.addMapper(PerkiraanBelanjaMapper.class);
                configuration.addMapper(PosisiKasMapper.class);
                configuration.addMapper(UploadFileMapper.class);
                configuration.addMapper(SettingsMapper.class);
                configuration.addMapper(LraMapper.class);
                configuration.addMapper(RefSimpatikMapper.class);
                
                configuration.addMapper(BelanjaMapper.class);
                configuration.addMapper(BelanjaakunMapper.class);
                configuration.addMapper(BelanjaspjbuktiMapper.class);
                configuration.addMapper(BelanjapotonganMapper.class);
                configuration.addMapper(HeaderlogMapper.class);
                configuration.addMapper(HeaderjurnalMapper.class);
                configuration.addMapper(JurnalMapper.class);
                configuration.addMapper(ApbddetilakunMapper.class);
                configuration.addMapper(ApbddetilMapper.class);
                configuration.addMapper(HomeMapper.class);
                configuration.addMapper(LaporanMapper.class);
                configuration.addMapper(HeaderapbdMapper.class);
                configuration.addMapper(HeaderDthMapper.class);
                configuration.addMapper(DthMapper.class);
                configuration.addMapper(VersiMapper.class);
                configuration.addMapper(HeaderapbdskpdMapper.class);

                sqlSessionFactory = new SqlSessionFactoryBuilder()
                        .build(configuration);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                throw ex;
            } catch (Exception ex1) {
                Logger.getLogger(MyBatisConnectionFactory.class.getName()).log(Level.SEVERE, null, ex1);
            }

        }
    }
    static {
        try {
System.out.println("1 login.........................");
            if (sqlSessionFactoryLogin == null) {
                System.out.println("2 login .........................");
//                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
                Properties pr = PropertiesLoader.loadProperties("loginsikd.properties");
                String user = "postgres";
                String password = "haromain182";
//                String password = "postgres";
                String dbServer = pr.getProperty("dbServer");
                String dbName = pr.getProperty("dbName");
                String dbPort = pr.getProperty("dbPort");
                String databasenameURL = "jdbc:postgresql://" + dbServer + ":" + dbPort + "/" + dbName;
                String dbDriver = "org.postgresql.Driver";
                System.out.println("databseurl : " + databasenameURL);
                DataSource dataSource = new org.apache.ibatis.datasource.pooled.PooledDataSource(
                        dbDriver, databasenameURL, user, password);

                TransactionFactory transactionFactory = new JdbcTransactionFactory();
                Environment environment = new Environment("development",
                        transactionFactory, dataSource);
                Configuration configuration = new Configuration(environment);
	            configuration.addMapper(LoginMapper.class);  
	                
                sqlSessionFactoryLogin = new SqlSessionFactoryBuilder()
                        .build(configuration);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                throw ex;
            } catch (Exception ex1) {
                Logger.getLogger(MyBatisConnectionFactory.class.getName()).log(Level.SEVERE, null, ex1);
            }

        }
    }

//    public static SqlSessionFactory getSqlSessionFactory() throws Exception {
//        try {
//
//            if (sqlSessionFactory == null) {
//                Properties pr = PropertiesLoader.loadProperties("sikd.properties");
//                String user = "postgres";
//                String password = "SIKDpostgresSiKd";
//                String dbServer = pr.getProperty("dbServer");
//                String dbName = pr.getProperty("dbName");
//                String dbPort = pr.getProperty("dbPort");
//                String databasenameURL = "jdbc:postgresql://" + dbServer + ":" + dbPort + "/" + dbName;
//                String dbDriver = "org.postgresql.Driver";
//
//                DataSource dataSource = new org.apache.ibatis.datasource.pooled.PooledDataSource(
//                        dbDriver, databasenameURL, user, password);
//                TransactionFactory transactionFactory = new JdbcTransactionFactory();
//                Environment environment = new Environment("development",
//                        transactionFactory, dataSource);
//                Configuration configuration = new Configuration(environment);
//                configuration.addMapper(KontakPemdaMapper.class);
//   
//                sqlSessionFactory = new SqlSessionFactoryBuilder()
//                        .build(configuration);
//            }
//            return sqlSessionFactory;
//        } catch (Exception ex) {
//            ex.printStackTrace();
//            throw ex;
//        }
//
//    }
    
    
      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 (sqlSessionFactoryIKonsol == null) {
                sqlSessionFactoryIKonsol = 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 getSqlSessionFactoryIkonsol() {
        return sqlSessionFactoryIKonsol;
    }
    
    
      public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }
      public static SqlSessionFactory getSqlSessionFactoryLogin() {
          return sqlSessionFactoryLogin;
      }
      
       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;
    }
}
