package util;

import global.Data;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
/**
 *
 * @author nick
 */
public class DBManager {

    private static DataSource dataSource;
    private static Connection connection;
    private static Statement statement;

    private static String dbUrl;
    private static String dbUser;
    private static String dbPassword;

    public static void init() throws Exception {
        initDataFromSystemConfig();

        if (dataSource == null) {
            PoolProperties p = new PoolProperties();
            p.setDriverClassName("com.mysql.jdbc.Driver");
            p.setUrl(dbUrl);
            p.setUsername(dbUser);
            p.setPassword(dbPassword);
            p.setJmxEnabled(true);
            p.setTestWhileIdle(false);
            p.setTestOnBorrow(true);
            p.setValidationQuery("SELECT 1");
            p.setTestOnReturn(false);
            p.setValidationInterval(30000);
            p.setTimeBetweenEvictionRunsMillis(30000);
            p.setMaxActive(100);
            p.setInitialSize(10);
            p.setMaxWait(10000);
            p.setRemoveAbandonedTimeout(60);
            p.setMinEvictableIdleTimeMillis(30000);
            p.setMinIdle(10);
            p.setLogAbandoned(true);
            p.setRemoveAbandoned(true);
            p.setJdbcInterceptors(
                    "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
                    + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
            dataSource = new DataSource();
            dataSource.setPoolProperties(p);
            connection = dataSource.getConnection();
            statement = connection.createStatement();
        }

        if (connection == null || connection.isClosed()) {
            connection = dataSource.getConnection();
            statement = connection.createStatement();
        } else if (statement.isClosed()) {
            statement = connection.createStatement();
        }
    }

    private static void initDataFromSystemConfig() {
        try {
            // server config
//            InputStream input = new FileInputStream(Data.SYSTEM_CONFIG);
//            Properties prop = new Properties();
//            prop.load(input);
//
//            dbUrl = prop.getProperty("dburl");
//            dbUser = prop.getProperty("dbuser");
//            dbPassword = prop.getProperty("dbpassword");

            // local config
             dbUrl = System.getProperty("DB_URL");//"jdbc:mysql://localhost:3306/alsdb";
             dbUser = System.getProperty("DB_USER");//"test";
             dbPassword = System.getProperty("DB_PASSWORD");//"test";

             //dbUrl = "jdbc:mysql://alsdbcluster.cluster-ro-cyxscvzmumwf.us-east-2.rds.amazonaws.com:3306/alsdb";
             //dbUser = "alsdb";
             //dbPassword = "igm-alsdb";
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ResultSet executeQuery(String sqlQuery) throws SQLException {
        return statement.executeQuery(sqlQuery);
    }
    
    public static PreparedStatement prepareStatement(String sqlQuery) throws SQLException{
        return connection.prepareStatement(sqlQuery);
    }
}

@@ -2235,356 +2235,24 @@
 nfig
-%0A//            InputStream input = new FileInputStream(Data.SYSTEM_CONFIG);%0A//            Properties prop = new Properties();%0A//            prop.load(input);%0A//%0A//            dbUrl = prop.getProperty(%22dburl%22);%0A//            dbUser = prop.getProperty(%22dbuser%22);%0A//            dbPassword = prop.getProperty(%22dbpassword%22);%0A%0A            // local config
+ from enviroment
 %0A   
@@ -2302,46 +2302,8 @@
 L%22);
-//%22jdbc:mysql://localhost:3306/alsdb%22;
 %0A   
@@ -2351,25 +2351,16 @@
 _USER%22);
-//%22test%22;
 %0A       
@@ -2416,17 +2416,8 @@
 D%22);
-//%22test%22;
 %0A%0A  
@@ -2433,94 +2433,68 @@
   //
-dbUrl = %22jdbc:mysql://alsdbcluster.cluster-ro-cyxscvzmumwf.us-east-2.rds.amazonaws.com
+ local config%0A//             dbUrl = %22jdbc:mysql://localhost
 :330
@@ -2495,32 +2495,34 @@
 st:3306/alsdb%22;%0A
+//
              //d
@@ -2518,18 +2518,16 @@
         
-//
 dbUser =
@@ -2528,24 +2528,25 @@
 User = %22
-alsdb
+test
 %22;%0A
+//
         
@@ -2550,18 +2550,16 @@
         
-//
 dbPasswo
@@ -2568,17 +2568,12 @@
  = %22
-igm-alsdb
+test
 %22;%0A 

remove unused code from db manager