/**
 * Copyright (c) 2003-2004 System Integrator Corporation.
 *                 All Rights Reserved.
 */
package jp.co.sint.database;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SIDBMultiConf;

import org.apache.log4j.Category;

/**
 * DBへの接続用の共通コレクションクラス
 * @version $Id: SIDatabaseConnection.java,v 1.0 2003/06/23 jwchen Exp $
 * @author  Jinwang Chen
 */

public class SIDatabaseConnection{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  //データソース名
  String dataSoureName=SIDBMultiConf.SIDSN_CURR_NAME;
  //
  Connection connection=null;
  //データベースタイプ
  String databaseType=null;
  //トランザクションフラグ
  boolean autoCommit=false;

  /**
   * <b>SPDatabaseConnection</b>
   * コンストラクタ
   * @param なし
   * @return なし
   * @throws なし
   */
  public SIDatabaseConnection(){}

  /**
   * <b>SIDatabaseConnection</b>
   * コンストラクタ
   * @param lDataSoureName データソース名
   * @param lAutoCommit トランザクションフラグ
   * @return なし
   * @throws なし
   */
  public SIDatabaseConnection(String lDataSoureName,boolean lAutoCommit){
    this.dataSoureName=lDataSoureName;
    this.autoCommit=lAutoCommit;
  }

  /**
   * <b>SIDatabaseConnection</b>
   * コンストラクタ
   * @param lAutoCommit トランザクション
   * @return なし
   * @throws なし
   */
  public SIDatabaseConnection(boolean lAutoCommit){
    this.autoCommit=lAutoCommit;
  }

  /**
   * <b>setDataSourceName</b>
   *  データソース名を設定する
   * @param lDataSourceName  データソース名
   * @return なし
   * @throws なし
   */
  public void setDataSourceName(String lDataSourceName){
    this.dataSoureName =lDataSourceName;
  }

  /**
   * <b>setAutoCommit</b>
   *  トランザクションフラグを設定する
   * @param lAutoCommit トランザクションフラグ
   * @return なし
   * @throws なし
   */
  public void setAutoCommit(boolean lAutoCommit){
    this.autoCommit =lAutoCommit;
  }
  /**
   * <b>setDatabaseType</b>
   *  データベースタイプ名を設定する
   * @param lDatabaseType  データベースタイプ名
   * @return なし
   * @throws なし
   */
  public void setDatabaseType(String lDatabaseType){
    this.databaseType =lDatabaseType;
  }

  /**
   * <b>getDataSoureName</b>
   * データソース名を取得する
   * @param なし
   * @return データソース名
   * @throws なし
   */
  public String getDataSoureName(){
    return this.dataSoureName;
  }

  /**
   * <b>getDatabaseType</b>
   * データベースタイプ名を取得する
   * @param なし
   * @return データベースタイプ名
   * @throws なし
   */
  public String getDatabaseType(){
    return this.databaseType;
  }

  /**
   * <b>getAutoCommit</b>
   * トランザクションのフラグを取得する
   * @param なし
   * @return トランザクションのフラグ
   * @throws なし
   */
  public boolean getAutoCommit(){
    return this.autoCommit;
  }

  /**
   * <b>getConnection</b>
   * DBへの接続用のコネクションを取得する
   * @param  なし
   * @return コネクション
   * @throws NamingException
   * @throws SQLException
   */
  public Connection getConnection() throws SQLException,NamingException {
  	if (connection!=null) {
  		log.debug("getConnection:A exist connection will be return.");
  		return connection;
    }
		log.debug("getConnection:A new connection is been requested.");

		if(SIConfig.SISERVER_TYPE.equalsIgnoreCase("Tomcat")){ //7.1.1 ST0225 追加
			Context ctx = new InitialContext();
			Context envContext=(Context)ctx.lookup("java:/comp/env");
			DataSource dataSource=(DataSource)envContext.lookup(dataSoureName);			
			connection=dataSource.getConnection();
			connection.setAutoCommit(this.autoCommit);
		//7.1.1 ST0225 追加　ここから
		}else if(SIConfig.SISERVER_TYPE.equalsIgnoreCase("WebSphere")){
			InitialContext ctx = new InitialContext();
			DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/"+dataSoureName); 			
			connection=dataSource.getConnection();
			connection.setAutoCommit(this.autoCommit);
		}
		//7.1.1 ST0225 追加　ここまで
		return connection;
  }

  public void close(){
    try {
      if (this.connection!=null){
				if (!this.connection.isReadOnly())this.connection.rollback(); //7.1.1 ST0225 追加
        if (this.connection.isClosed())log.debug("A closed connection is been asked to close.");
        else log.debug("A active connection is been asked to close.");
        this.connection.close();
        this.connection=null;
      }else log.debug("A null connection is been asked to close.");
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}
