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

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.co.sint.basic.SILogin;
import jp.co.sint.beans.mallmgr.UIMngAccesslogListCond;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDatabaseConnection;
import jp.co.sint.servlet.SIServlet;
import jp.co.sint.tools.SIHTMLUtil;
import jp.co.sint.tools.SIURLParameter;

import org.apache.log4j.Category;

import jp.co.sint.config.SIDBMultiConf;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.database.SIDuplicateKeyException;
import jp.co.sint.database.SIInsertRec;
import jp.co.sint.database.SISpcType;

/**
 * @version $Id: SIRegMngAccesslogSrv.java,v 1.0 2003/07/25 Exp $
 * @author  Abukawa
 * <br>Description: 管理側アクセスログのレコードを登録．修正することなどを行うServlet
 * <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>
 * Abukawa      2005/04/28  Original
 */

public class SIRegMngAccesslogSrv extends SIServlet {
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  /**
   * <b>doUpdate</b>
   * HTTP リクエストの処理
   * @param  request リクエスト
   * @param  response
   * @return なし
   * @throws ServletException
   * @throws IOException
   */
  public void doUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    SILogin manLogin=SIHTMLUtil.getLogin(request);
    if (!manLogin.isLogin()){
      forwardKey(request,response,"webshop.jsp.manager.login");
      return;
    }
    
    HttpSession session=request.getSession(true);//セッションの取得
    SIDatabaseConnection databaseConnection=new SIDatabaseConnection();//DBへのコネクションの作成
    SIURLParameter urlParam = new SIURLParameter(request);
    
    try {
      
      Connection connection;
      
      String actionName=this.getActionName(urlParam);//画面からのアクション
      String editMode=this.getEditMode(urlParam);//DBへの編集モード
      
      UIMngAccesslogListCond accessList=new UIMngAccesslogListCond();
      
      if (SIConfig.SIACTION_LIST.equalsIgnoreCase(actionName)){//一覧と検索などの画面
        //データの取得とデータのチェック
        accessList=new UIMngAccesslogListCond(request,urlParam);
        accessList.validate(request,databaseConnection.getConnection());
        session.setAttribute(SIConfig.SISESSION_MAN_MNGACCESSLOG_LIST_NAME,accessList);
        forwardKey(request,response,"webshop.jsp.manager.mngaccesslog.list");
      }
    }catch (SQLException e){
      e.printStackTrace();
      throw new ServletException();
    }catch (NamingException e){
      e.printStackTrace();
      throw new ServletException();
    }finally{
      databaseConnection.close();
    }
  }
  
  public void destroy() {
  }
  /**
   * insertTableData
   * 管理者ログファイルにログを出力する。
   * 
   * @param   Connection              DBコネクション
   * @param   HttpServletRequest      リクエスト
   * @param   String                  オペレーションコード
   * @param   String                  日時
   * @throws  SIDBAccessException
   */
  public void insertTableData(Connection connection, HttpServletRequest request, String strOpCd, String strDateTime) throws SIDBAccessException {
    SIInsertRec siInsertRec = null;
    SILogin siLogin = null;
    HttpSession session = request.getSession(true);
    SISpcType siSpcType = null;
    
    try {
      siLogin = SIHTMLUtil.getLogin(request);
      siInsertRec = new SIInsertRec(SIConfig.SITABLE_MAN_ACCESSLOG_NAME);
      
      //項目のセット
      siInsertRec.add("mnglogcode",SIDBUtil.getFirstData(connection,SIDBUtil.getNextVal("MNGACCESSLOGTBL_MNGLOGCD_SEQ"))); //ログコード
      siInsertRec.add("mallshopcode",siLogin.getMallShopCode());  //ショップコード
      siInsertRec.add("usercode",siLogin.getUserCode());          //ユーザーコード
      siInsertRec.add("username",siLogin.getUserName());          //ユーザー名
      siInsertRec.add("ipaddress",request.getRemoteAddr());       //IPアドレス
      siInsertRec.add("operation",strOpCd);                       //オペレーション
      siInsertRec.add("sessionid",session.getId());               //セッションID
      // DBサーバ日付の取得文作成
      // DBタイプの判別
      if ( strDateTime == null ) {
        //各マスタとログで日時の同期が必要ない場合(nullが渡される)
        siSpcType = new SISpcType("TO_CHAR(now(),'YYYY/MM/DD HH24:MI:SS')::timestamp");
      } else {
        //各マスタとログで日時の同期が必要な場合(null以外が渡される)
        siSpcType = new SISpcType(SIDBUtil.SQL2Str(strDateTime,"::timestamp"));
      }
      siInsertRec.add("initdatetime",siSpcType);                  //日時
      
      //更新実行
      siInsertRec.execute(connection);
      //コミット
      connection.commit();
    } catch (SQLException sqle) {
      throw new SIDBAccessException("insert data error.");
    } catch (SIDuplicateKeyException e) {
      throw new SIDBAccessException(e);
    } finally {
      siInsertRec = null;
    }
  }
}