/**
 * 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.UITaxList;
import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SIDBMultiConf;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDatabaseConnection;
import jp.co.sint.database.SIDateType;
import jp.co.sint.database.SIDeleteRec;
import jp.co.sint.database.SIDuplicateKeyException;
import jp.co.sint.database.SIInsertRec;
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: SIRegTaxSrv.java,v 1.0 2003/8/15 Exp $
 * @author  Arai
 * <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>
 * Arai   2003/8/15 14:25:29  Original
 */
public class SIRegTaxSrv 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 {

      Connection connection;

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

      UITaxList regList = new UITaxList();

      //データベースへのコネクションの作成
      connection = databaseConnection.getConnection();

      if (SIConfig.SIACTION_LIST.equalsIgnoreCase(actionName)) {
        //一覧と検索などの画面
        forwardKey(request, response, "webshop.jsp.manager.tax.list");
      } else {
        //データの取得
		regList.init(urlParam);//7.1.1 ST0236 修正
        //データのチェック
		if (regList.validate(request,urlParam) == false) {//7.1.1 ST0236 修正
          session.setAttribute(SIConfig.SISESSION_MAN_TAX_LIST_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.tax.edit");
        } else {
          regList.setActionNameTxt(SIConfig.SIACTION_LIST);
          session.setAttribute(SIConfig.SISESSION_MAN_TAX_LIST_NAME, regList);
          try {
            if (editMode.equalsIgnoreCase(SIConfig.SIEDIT_MODE_INSERT)){
              insertTableData(regList, connection);
              request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME,
                                   SIErrorFactory.getErrorMsg("manager.message.success.insert"));
            }else if (editMode.equalsIgnoreCase(SIConfig.SIEDIT_MODE_DELETE)){
              deleteTableData(regList, connection);
              request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME,
                                   SIErrorFactory.getErrorMsg("manager.message.success.delete"));
            }else log.error("no known edit mode.editMode=" + editMode);
            session.removeAttribute(SIConfig.SISESSION_MAN_TAX_LIST_NAME);
            forwardKey(request, response, "webshop.jsp.manager.tax.result");
          } catch (SIDuplicateKeyException sqle) {
            SICustomErrors errors = new SICustomErrors();
            errors.addError(new SICustomError("database.insert.duplicate2","適用開始日が同一"));
            request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
            forwardKey(request, response, "webshop.jsp.manager.tax.edit");
          } catch (SIDBAccessException sqle) {
            SICustomErrors errors = new SICustomErrors();
            errors.addError(new SICustomError("database.execute.error"));
            request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
            forwardKey(request, response, "webshop.jsp.manager.tax.edit");
          }
        }
      }
    }catch (SQLException e){
      e.printStackTrace();
      throw new ServletException();
    }catch (NamingException e){
      e.printStackTrace();
      throw new ServletException();
    }finally{
      databaseConnection.close();
    }
  }

  /**
  * insertTableData
  * データベースにレコードを作成します。
  * @param UITaxList , Connection
  * @return
  * @throws SIDuplicateKeyException, SIDBAccessException
  */
  public void insertTableData(UITaxList regList, Connection connection) throws SIDuplicateKeyException, SIDBAccessException {
    SISpcType lSpcType = new SISpcType();
    SIInsertRec lRec = new SIInsertRec("taxmtbl");

    if (SIDBMultiConf.SIDB_CURRENT_INX ==SIDBMultiConf.SIDB_POSTGRESQL_INX){
      lRec.add("startdate", regList.getStartDate());
    }else{
      lRec.add("startdate",new SIDateType(regList.getStartDate()));
    }
    lRec.add("tax", regList.getTax());

    log.debug("insert sql=" + lRec.getSQL());
    lRec.execute(connection);
    try {
      connection.commit();
    } catch (SQLException sqle) {
      throw new SIDBAccessException("insert data error.");
    }
  }

  /**
  * deleteTableData
  * データを取得します。
  * @param UITaxList , Connection
  * @return
  * @throws SIDuplicateKeyException, SIDBAccessException
  */
  public void deleteTableData(UITaxList regList, Connection connection) throws SIDuplicateKeyException, SIDBAccessException {
    SIDeleteRec lDeleteRec = new SIDeleteRec();
    lDeleteRec = new SIDeleteRec();
    lDeleteRec.setTableName("taxmtbl");
    if (SIDBMultiConf.SIDB_CURRENT_INX ==SIDBMultiConf.SIDB_POSTGRESQL_INX){
      lDeleteRec.addCondition("startdate", regList.getStartDate());
    }else{
      lDeleteRec.addCondition("startdate", new SIDateType(regList.getStartDate()));
    }

    log.debug("delete sql=" + lDeleteRec.getSQL());
    lDeleteRec.execute(connection);

    try {
      connection.commit();
    } catch (SQLException sqle) {
      throw new SIDBAccessException("delete data error.");
    }
  }

  public void destroy() {
  }
}
