//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ag.psamathe.api.util;

import com.ag.psamathe.api.pojo.ModuleVersion;
import com.ag.util.ResourceUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataSourceUtil {
    private static DataSourceUtil instance;
    private static final Logger log = LoggerFactory.getLogger(DataSourceUtil.class);
    private Connection conn;
    private Statement stat;
    private ResultSet rs;
    private String sql = "select * from MODULE_VERSION";

    private DataSourceUtil() {
    }

    public static DataSourceUtil getInstance() {
        if (instance == null) {
            instance = new DataSourceUtil();
        }

        return instance;
    }

    private void initDataSourceForJNDI() throws Exception {
        Context ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/wb_oracle");
        this.conn = ds.getConnection();
        this.stat = this.conn.createStatement();
    }

    private void initDataSourceForJDBC() throws Exception {
        log.info("mode==initDataSourceForJDBC");
        ResourceBundle rb = ResourceUtil.getResourceBundle("datasource");
        String classForName = rb.getString("jdbc.driverClassName");
        String url = rb.getString("main.url");
        String username = rb.getString("main.username");
        String password = rb.getString("main.password");
        Class.forName(classForName);
        this.conn = DriverManager.getConnection(url, username, password);
        this.stat = this.conn.createStatement();
    }

    private void initDataSource() throws Exception {
        ResourceBundle rb = ResourceUtil.getResourceBundle("system");
        String connectModel = rb.getString("connectModel");
        if ("JDBC".equals(connectModel)) {
            this.initDataSourceForJDBC();
        }

        if ("JNDI".equals(connectModel)) {
            this.initDataSourceForJNDI();
        }

    }

    private void initDataSource(DataSource ds) throws Exception {
        log.info("mode==JDBCTemplate");
        this.conn = ds.getConnection();
        this.stat = this.conn.createStatement();
    }

    public List<ModuleVersion> queryVersions(DataSource ds, String userName) {
        ArrayList versions = new ArrayList();

        try {
            if (ds != null && ds.getConnection() != null && !ds.getConnection().isClosed()) {
                this.initDataSource(ds);
            } else {
                this.initDataSource();
            }

            this.rs = this.stat.executeQuery(this.sql + " where name='" + userName + "'");
            log.info(this.sql + " where name='" + userName + "'");

            while(this.rs.next()) {
                ModuleVersion version = new ModuleVersion();
                version.setId(this.rs.getString("ID"));
                version.setName(this.rs.getString("NAME"));
                version.setEnabled(Integer.valueOf(this.rs.getString("ENABLED")));
                version.setVersion(this.rs.getString("VERSION"));
                version.setJarPath(this.rs.getString("JAR_PATH"));
                version.setScanPackage(this.rs.getString("SCAN_PACKAGE"));
                version.setDesc(this.rs.getString("DESC"));
                version.setOverridePackages(this.rs.getString("OVERRIDE_PACKAGES"));
                versions.add(version);
            }
        } catch (Exception var8) {
            log.error(var8.getMessage());
        } finally {
            this.closeConn();
        }

        return versions;
    }

    private void closeConn() {
        try {
            if (this.rs != null && !this.rs.isClosed()) {
                this.rs.close();
            }

            if (this.stat != null && !this.stat.isClosed()) {
                this.stat.close();
            }

            if (this.conn != null && !this.conn.isClosed()) {
                this.conn.close();
            }
        } catch (Exception var2) {
            log.error(var2.getMessage());
        }

    }
}
