/**
 * Created on 2003/09/24
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
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.UIRegExRule;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDatabaseConnection;
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 追加

/**
 * @author arai
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class SIRegExRuleSrv extends SIServlet {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  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 修正

      UIRegExRule regList = new UIRegExRule();

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

      if (SIConfig.SIACTION_MODIFY.equalsIgnoreCase(actionName)) {
		regList.init(request,urlParam);//7.1.1 ST0236 修正
        forwardKey(request, response, "webshop.jsp.manager.exrule.edit");
      } else if (SIConfig.SIACTION_REMODIFY.equalsIgnoreCase(actionName)) {
        //一覧と検索などの画面
        regList = (UIRegExRule) session.getAttribute(SIConfig.SISESSION_MAN_EXRULE_EDIT_NAME);
        regList.setActionNameTxt(SIConfig.SIACTION_REMODIFY);
        session.setAttribute(SIConfig.SISESSION_MAN_EXRULE_EDIT_NAME, regList);
        forwardKey(request, response, "webshop.jsp.manager.exrule.edit");

      } else if (SIConfig.SIACTION_CONFIRM.equalsIgnoreCase(actionName)) {
        //一覧と検索などの画面
		regList.init(request,urlParam);//7.1.1 ST0236 修正

        if (regList.validate(request, connection) == false) {
          regList.setActionNameTxt(SIConfig.SIACTION_REMODIFY);
          session.setAttribute(SIConfig.SISESSION_MAN_EXRULE_EDIT_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.exrule.edit");
        } else {
          session.setAttribute(SIConfig.SISESSION_MAN_EXRULE_EDIT_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.exrule.confirm");
        }

      } else {
        //DBへのデータ登録
        //データの取得
        regList = (UIRegExRule) session.getAttribute(SIConfig.SISESSION_MAN_EXRULE_EDIT_NAME);
        //データのチェック
        if (regList.validate(request, connection) == false) {
          regList.setActionNameTxt(SIConfig.SIACTION_REMODIFY);
          session.setAttribute(SIConfig.SISESSION_MAN_EXRULE_EDIT_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.exrule.edit");
        } else {
          regList.setActionNameTxt(actionName);
          regList.setEditModeTxt(editMode);
          session.setAttribute(SIConfig.SISESSION_MAN_EXRULE_EDIT_NAME, regList);
          try {
            if (editMode.equalsIgnoreCase(SIConfig.SIEDIT_MODE_UPDATE)){
              updateTableData(regList, connection);
              request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME,
                                   SIErrorFactory.getErrorMsg("manager.message.success.insert"));
            }else log.error("no known edit mode.editMode=" + editMode);
            forwardKey(request, response, "webshop.jsp.manager.exrule.result");
          } catch (SIDuplicateKeyException sqle) {
            SICustomErrors errors = new SICustomErrors();
            errors.addError(new SICustomError("database.insert.duplicate"));
            request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
            forwardKey(request, response, "webshop.jsp.manager.exrule.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.exrule.edit");
          }
        }
      }
    }catch (SQLException e){
      e.printStackTrace();
      throw new ServletException();
    }catch (NamingException e){
      e.printStackTrace();
      throw new ServletException();
    }finally{
      databaseConnection.close();
    }
  }

  /**
  * updateTableData
  * データベースにレコードを修正します。
  * @param
  * @return
  * @throws
  */
  private void updateTableData(UIRegExRule regList, Connection connection) throws SIDuplicateKeyException, SIDBAccessException {

    SISpcType lSpcType = new SISpcType();
    SIModifyRec lRec = new SIModifyRec("exchangeruleMtbl");

    lRec.addCondition("mallshopcode", regList.getMallShopCode());

    lRec.add("sellername", regList.getSellerName());
    lRec.add("ownername", regList.getOwnerName());
    lRec.add("location", regList.getLocation());
    lRec.add("tel", regList.getTel());
    lRec.add("fax", regList.getFax());
    lRec.add("email", regList.getEmail());
    lRec.add("commentoforder", regList.getCommentOfOrder());
    lRec.add("commentofprice", regList.getCommentOfPrice());
    lRec.add("commentofperiod", regList.getCommentOfPeriod());
    lRec.add("commentofAmount", regList.getCommentOfAmount());
    lRec.add("commentofdeliver", regList.getCommentOfDeliver());
	lRec.add("commentofpayment", regList.getCommentOfPayment());
    lRec.add("commentofpayperiod", regList.getCommentOfPayPeriod());
    lRec.add("commentofreturn", regList.getCommentOfReturn());

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

  /**
   * <b>insertTableData</b>
   * データベースにデフォルトレコードを作成します。
   * @param connection DBへのコネクション
   * @param regShop 入力したデータ
   * @return なし
   * @throws SIDuplicateKeyException
   * @throws SIDBAccessException
   */
  public void insertTableData(UIRegExRule regList, Connection connection) throws SIDuplicateKeyException, SIDBAccessException {
	SISpcType lSpcType = new SISpcType();

	SIInsertRec lRec = new SIInsertRec("exchangeruleMtbl");
	lRec.add("MallShopCode", regList.getMallShopCode());
	lRec.add("SellerName", regList.getSellerName());
	lRec.add("Location", regList.getLocation());
	lRec.add("Tel", regList.getTel());
	lRec.add("Fax", regList.getFax());
	lRec.add("Email", regList.getEmail());

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


  public void destroy() {
  }
}
