/*
 * 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.ikonsol.model.HeaderDataKonsolidasi;

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;
    private static SqlSessionFactory sqlSessionFactoryIKonsolDw;
    
    private static SqlSessionFactory sqlSessionFactoryIKonsolDw2;
    

    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 = "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";
                /**
                 * 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);
        }
    }
      
      
          
      static {
        try {

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

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

            String dwServer = pr.getProperty("dwServer");
            String dwName = pr.getProperty("dwName");
            String dwPort = pr.getProperty("dwPort");
            String dwtype = "?stringtype=unspecified";
            String dwVendor = pr.getProperty("dwVendor");
            String dwuser = pr.getProperty("dwusername");
            String dwpass = pr.getProperty("dwpassword");
            String dwUrl = "";
            if (dwVendor.equalsIgnoreCase("mysql")) {
                
           dwUrl = "jdbc:mysql://" + dwServer + ":" + dwPort + "/" + dwName+dwtype;
   
            }else if(dwVendor.equalsIgnoreCase("mssql")){
                
            dwUrl = "jdbc:sqlserver://;servername="+ dwServer + ";integratedSecurity=true;authenticationScheme=JavaKerberos";
            }
            Properties properties = new Properties();
            properties.setProperty("username",dwuser);
            properties.setProperty("password", dwpass);
            properties.setProperty("url", dwUrl);

            if (sqlSessionFactoryIKonsolDw == null) {
                sqlSessionFactoryIKonsolDw = 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 boolean setSqlSessionFactoryIkonsolDw2(String dbserver,String namaDb,String portDb,String vendor,String username,String pasdb){
      boolean con = false ;
         try {

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

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

            String dwServer = dbserver;//obj.getServerDb();//pr.getProperty("dwServer");
            String dwName= namaDb;//= obj.getNamaDb();//pr.getProperty("dwName");
            String dwPort = portDb;//obj.getPortDb();//pr.getProperty("dwPort");
            String dwtype = "?stringtype=unspecified";
            String dwVendor = vendor;//obj.getVendorDb();//pr.getProperty("dwVendor");
            String dwuser = username;//obj.getUserName();//pr.getProperty("dwusername");
            String dwpass = pasdb;//obj.getPassDb();//pr.getProperty("dwpassword");
            String dwUrl = "";
            if (dwVendor.equalsIgnoreCase("mysql")) {
                 System.out.println("mysql");
           dwUrl = "jdbc:mysql://" + dwServer + ":" + dwPort + "/" + dwName+dwtype;
   
            }else if(dwVendor.equalsIgnoreCase("mssql")){
                System.out.println("mssql");
            dwUrl = "jdbc:sqlserver://;servername="+ dwServer + ";integratedSecurity=true;authenticationScheme=JavaKerberos";
            }
            else if(dwVendor.equalsIgnoreCase("psql")){
                System.out.println("psql");
             dwUrl = "jdbc:postgresql://" + dwServer  + ":" + dwPort  + "/" + dwName+dwtype;
                  System.out.println("dwUrl "+dwUrl);
            }
            Properties properties = new Properties();
            properties.setProperty("username",dwuser);
            properties.setProperty("password", dwpass);
            properties.setProperty("url", dwUrl);

            if (sqlSessionFactoryIKonsolDw2 == null) {
                sqlSessionFactoryIKonsolDw2 = new SqlSessionFactoryBuilder().build(reader, properties);
                con=true;
                System.out.println("======= disini======");
            }
        } catch (FileNotFoundException fileNotFoundException) {
            fileNotFoundException.printStackTrace();
            con=false;
        } catch (IOException iOException) {
            iOException.printStackTrace();
        } catch (Exception ex) {
            Logger.getLogger(MyBatisConnectionFactoryIKonsol.class.getName()).log(Level.SEVERE, null, ex);
        }
         return con;
    }

      public static SqlSessionFactory getSqlSessionFactoryIkonsol() {
        return sqlSessionFactoryIKonsol;
    }
    
    
      public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }
      public static SqlSessionFactory getSqlSessionFactoryLogin() {
          return sqlSessionFactoryLogin;
      }
      
      
       public static SqlSessionFactory getSqlSessionFactoryIkonsolDw() {
          return sqlSessionFactoryIKonsolDw;
      }
      
      
       public static SqlSessionFactory getSqlSessionFactoryIkonsolDw2() {
          return sqlSessionFactoryIKonsolDw2;
      }
      
       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;
    }
}
