/**
 * @version $Id: SICustAccessLog,v 1.0 2004/01/19 Exp $
 * @author  yamauchi
 * <br>Description:
 * <p>History</p>
 * <p>Author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reason</p>
 *  ============&nbsp;&nbsp;&nbsp;==========&nbsp;&nbsp;===========================<br>
 * yamauchi   2004/01/19           Original
 */
package jp.co.sint.servlet.front;

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

import javax.naming.NamingException;

import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SIDBMultiConf;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.database.SIDatabaseConnection;
import jp.co.sint.database.SIDateTimeType;
import jp.co.sint.database.SIDuplicateKeyException;
import jp.co.sint.database.SIInsertRec;
import jp.co.sint.database.SISpcType;
import jp.co.sint.tools.SIDateTime;

import org.apache.log4j.Category;

/**
  * フロント、アクセスログ記録
  * @version $Id: SICustAccess.java,v 1.0 2004/01/19 yamauchi Exp $
  * @author  yamauchi
  */
public class SICustAccessLog {

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

  public SICustAccessLog(){
  }

  public static void recordLog(){
    SIDatabaseConnection databaseConnection=new SIDatabaseConnection();
    try {
      Connection lConnection =databaseConnection.getConnection();
      try{
        insertTable(lConnection);
        try {databaseConnection.getConnection().commit();} catch (SQLException sqle) {}
      }catch(SIDuplicateKeyException e){
        try{lConnection.rollback();}catch(SQLException ee){;}
      }catch(SIDBAccessException e){
        try{lConnection.rollback();}catch(SQLException ee){;}
      }
    }catch (SQLException e){
      e.printStackTrace();
    }catch (NamingException e){
      e.printStackTrace();
    }finally{
      databaseConnection.close();
    }
  }

  /**
	 * <b>insertTableData</b>
	 * データベースにレコードを作成します。
	 * @param connection DBへのコネクション
	 * @return なし
	 * @throws SIDuplicateKeyException
	 * @throws SIDBAccessException
  **/
  public static void insertTable(Connection lConnection) throws SIDBAccessException,SIDuplicateKeyException{
	  SISpcType lSpcType=new SISpcType();
    SIDateTime lDateTime= new SIDateTime();

	  String lAccessNumber=SIDBUtil.getFirstData(lConnection,SIDBUtil.getNextVal("ACCESSLOGTBL_LOGCD_SEQ"));	//アクセスID連番

	  SIInsertRec lRec=new SIInsertRec("AccessLogTbl");

	  lRec.add("LogCode",lAccessNumber);//連番
    if (SIDBMultiConf.SIDB_CURRENT_INX ==SIDBMultiConf.SIDB_POSTGRESQL_INX){
      lRec.add("AccessDateTime",lDateTime.getFullDateTime());
    }else{
      lRec.add("AccessDateTime",new SIDateTimeType(lDateTime.getFullDateTime()));
    }

    lRec.execute(lConnection);
  }

}
