/**
 * 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.basic.SIPayMethodFee;
import jp.co.sint.beans.mallmgr.UIPayMethodFeeListCond;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDatabaseConnection;
import jp.co.sint.database.SIDeleteRec;
import jp.co.sint.database.SIDuplicateKeyException;
import jp.co.sint.database.SIInsertRec;
import jp.co.sint.database.SIModifyRec;
import jp.co.sint.database.SISpcType;
import jp.co.sint.servlet.SIServlet;
import jp.co.sint.tools.SICustomError;
import jp.co.sint.tools.SICustomErrors;
import jp.co.sint.tools.SIErrorFactory;
import jp.co.sint.tools.SIHTMLUtil;

import org.apache.log4j.Category;
import jp.co.sint.tools.SIURLParameter;//7.1.1 ST0236 追加

/**
 * @version $Id: SIRegPayMethodFeeSrv.java,v 1.0 2003/11/12 Exp $
 * @author  asakura
 * <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>
 * asakura   2003/11/12   Original
 */
public class SIRegPayMethodFeeSrv 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);//7.1.1 ST0236 追加

    try {
	  String actionName=this.getActionName(urlParam);//画面からのアクション	//7.1.1 ST0236 修正
	  String editMode = this.getEditMode(urlParam); //DBへの編集モード	//7.1.1 ST0236 修正

      if (SIConfig.SIACTION_LIST.equalsIgnoreCase(actionName)) { //一覧と検索などの画面
	  UIPayMethodFeeListCond payMethodListFee = new UIPayMethodFeeListCond(request,urlParam);//7.1.1 ST0236 修正
      session.setAttribute(SIConfig.SISESSION_MAN_PAYMETHODFEE_LIST_NAME,payMethodListFee);
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
      } else if (SIConfig.SIACTION_MODIFY.equalsIgnoreCase(actionName)){ //レコードの変更
	  UIPayMethodFeeListCond payMethodListFee = new UIPayMethodFeeListCond(request,urlParam);//7.1.1 ST0236 修正
      //データの取得とデータのチェック
	  SIPayMethodFee payMethodFee = payMethodListFee.getModifyData(request,urlParam);//7.1.1 ST0236 修正
      if (!payMethodListFee.validateModify(request)) { //エラーがあれば
        session.setAttribute(SIConfig.SISESSION_MAN_PAYMETHODFEE_LIST_NAME, payMethodListFee);
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
      } else { //エラーがなければ
        try {
        //レコードの修正
        updateTableData(databaseConnection.getConnection(), payMethodFee);
        request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory.getErrorMsg("manager.message.success.modify"));
        session.setAttribute(SIConfig.SISESSION_MAN_PAYMETHODFEE_LIST_NAME,payMethodListFee);
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
        } catch (SIDBAccessException e) {
        session.setAttribute(SIConfig.SISESSION_MAN_PAYMETHODFEE_LIST_NAME, payMethodListFee);
        SICustomErrors errors = new SICustomErrors();
        errors.addError(new SICustomError("database.execute.error"));
        request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK, errors);
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
        }
      }
      } else if (SIConfig.SIACTION_NEW.equalsIgnoreCase(actionName)) { //新規のレコード
	  UIPayMethodFeeListCond payMethodListFee = new UIPayMethodFeeListCond(request,urlParam);//7.1.1 ST0236 修正
	  SIPayMethodFee payMethodFee = payMethodListFee.getInsertData(urlParam);
      //データの取得とデータのチェック
      if (!payMethodListFee.validateInsert(request)) { //エラーがあれば
        //画面の値を取得
        session.setAttribute(SIConfig.SISESSION_MAN_PAYMETHODFEE_LIST_NAME,payMethodListFee);
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
      } else { //エラーがなければ
      try {
          //データレコードの作成
        insertTableData(databaseConnection.getConnection(), payMethodFee);
        try {
          databaseConnection.getConnection().commit();
        } catch (SQLException sqle) {
          throw new SIDBAccessException("insert data error.");
        }
		session.setAttribute(SIConfig.SISESSION_MAN_PAYMETHODFEE_LIST_NAME,new UIPayMethodFeeListCond(request,urlParam));//7.1.1 ST0236 修正
        request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory.getErrorMsg("manager.message.success.insert"));
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
      } catch (SIDuplicateKeyException e) {
        //画面の値を取得
        session.setAttribute(SIConfig.SISESSION_MAN_PAYMETHODFEE_LIST_NAME,payMethodListFee);
        SICustomErrors errors = new SICustomErrors();
        errors.addError(new SICustomError("database.insert.duplicate2","購入金額" + payMethodListFee.getToPriceTxt() + "円"));
        request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK, errors);
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
      } catch (SIDBAccessException e) {
        //画面の値を取得
        session.setAttribute(SIConfig.SISESSION_MAN_PAYMETHODFEE_LIST_NAME,payMethodListFee);
        SICustomErrors errors = new SICustomErrors();
        errors.addError(new SICustomError("database.execute.error"));
        request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK, errors);
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
      }
      }
      } else if (SIConfig.SIACTION_DELETE.equalsIgnoreCase(actionName)) { //レコードの削除
	  UIPayMethodFeeListCond payMethodListFee = new UIPayMethodFeeListCond(request,urlParam);//7.1.1 ST0236 修正
      //キーデータの取得とデータのチェック
      try {
        //レコードの削除
		SIPayMethodFee payMethodFee = payMethodListFee.getModifyData(request,urlParam);//7.1.1 ST0236 修正
        deleteTableData(databaseConnection.getConnection(), payMethodFee);
        request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory.getErrorMsg("manager.message.success.delete"));
        session.setAttribute(SIConfig.SISESSION_MAN_PAYMETHODFEE_LIST_NAME,payMethodListFee);
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
      } catch (SIDBAccessException e) {
        e.printStackTrace();
        SICustomErrors errors = new SICustomErrors();
        errors.addError(new SICustomError("database.execute.error"));
        request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK, errors);
        forwardKey(request, response, "webshop.jsp.manager.paymethod.fee");
      }
      }
    }catch (SQLException e){
      e.printStackTrace();
      throw new ServletException();
    }catch (NamingException e){
      e.printStackTrace();
      throw new ServletException();
    }finally{
      databaseConnection.close();
    }
  }

  /**
   * <b>insertTableData</b>
   * データベースにレコードを作成します。
   * @param lConnection DBへのコネクション
   * @param payMethodFee 入力したデータ
   * @return なし
   * @throws SIDuplicateKeyException
   * @throws SIDBAccessException
   */
  public void insertTableData(Connection lConnection, SIPayMethodFee payMethodFee) throws SIDuplicateKeyException, SIDBAccessException {
    SISpcType lSpcType = new SISpcType();
    SIInsertRec lRec = new SIInsertRec("payMethodFeeMTbl");

    lRec.add("mallShopCode", payMethodFee.getMallShopCode());
    lRec.add("payMethodCode", payMethodFee.getPayMethodCode());
    lRec.add("priceTo", payMethodFee.getToPrice());
    lRec.add("fee", payMethodFee.getFee());

    //データの更新
    lRec.execute(lConnection);
  }

  /**
   * <b>updateTableData</b>
   * データベースにレコードを修正します。
   * @param lConnection DBへのコネクション
   * @param payMethodFee 入力したデータ
   * @return なし
   * @throws SIDBAccessException
   */
  private void updateTableData(Connection lConnection, SIPayMethodFee payMethodFee) throws SIDBAccessException {
    SIModifyRec lRec = new SIModifyRec("payMethodFeeMTbl");

    lRec.addCondition("mallShopCode", payMethodFee.getMallShopCode());
    lRec.addCondition("payMethodCode", payMethodFee.getPayMethodCode());
	lRec.addCondition("priceTo", payMethodFee.getToPrice());

    lRec.add("fee", payMethodFee.getFee());
    try {
      lRec.execute(lConnection);
      lConnection.commit();
    } catch (SIDuplicateKeyException e) {
    } catch (SQLException sqle) {
      sqle.printStackTrace();
      throw new SIDBAccessException("modify data error.");
    }
  }

  /**
   * <b>deleteTableData</b>
   * データベースにレコードを削除します。
   * @param lConnection DBへのコネクション
   * @param payMethodFee 削除するデータ
   * @return なし
   * @throws SIDBAccessException
   */
  public void deleteTableData(Connection lConnection, SIPayMethodFee payMethodFee) throws SIDBAccessException {
    SIDeleteRec lRec = new SIDeleteRec("payMethodFeeMTbl");
    lRec.addCondition("mallShopCode", payMethodFee.getMallShopCode());
    lRec.addCondition("payMethodCode", payMethodFee.getPayMethodCode());
	lRec.addCondition("priceTo", payMethodFee.getToPrice());

    try {
      lRec.execute(lConnection);
      lConnection.commit();
    } catch (SIDuplicateKeyException e) {
    } catch (SQLException sqle) {
      throw new SIDBAccessException("delete data error.");
    }
  }

  public void destroy() {
  }

}
