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

import java.io.IOException;
import java.io.PrintWriter;
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.UIRegShopInfo;
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 追加

/**
 * @version $Id: SIRegShopInfoSrv.java,v 1.0 Exp $
 * @author  arai
 * <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>
 * arai             2003/9/26
 */
public class SIRegShopInfoSrv extends SIServlet {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  /**
   * <b>doUpdate</b>
   * HTTP リクエストの処理
   * @param  HttpServletRequest
   * @param HttpServletResponse
   * @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);//セッションの取得

    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();

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

    UIRegShopInfo regList = new UIRegShopInfo();
    SIDatabaseConnection databaseConnection = new SIDatabaseConnection();
    try{
      Connection connection= databaseConnection.getConnection();

      if (SIConfig.SIACTION_MODIFY.equalsIgnoreCase(actionName)) {
        //一覧と検索などの画面
		regList.init(request,urlParam);//7.1.1 ST0236 修正
        forwardKey(request, response, "webshop.jsp.manager.shopinfo.edit");

      } else if (SIConfig.SIACTION_REMODIFY.equalsIgnoreCase(actionName)) {
        //一覧と検索などの画面
        regList = (UIRegShopInfo) session.getAttribute(SIConfig.SISESSION_MAN_SHOP_INFO_EDIT_NAME);
        regList.setActionNameTxt(SIConfig.SIACTION_REMODIFY);
        session.setAttribute(SIConfig.SISESSION_MAN_SHOP_INFO_EDIT_NAME, regList);
        forwardKey(request, response, "webshop.jsp.manager.shopinfo.edit");
      } else if (SIConfig.SIACTION_PREVIEW.equalsIgnoreCase(actionName)){//プレビュー画面
	  regList.init(request,urlParam);//7.1.1 ST0236 修正
      session.setAttribute(SIConfig.SISESSION_MAN_SHOP_INFO_PREVIEW_NAME, regList);
      forwardKey(request, response, "webshop.jsp.manager.shopinfo.preview");
      } 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_SHOP_INFO_EDIT_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.shopinfo.edit");
        } else {
          session.setAttribute(SIConfig.SISESSION_MAN_SHOP_INFO_EDIT_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.shopinfo.confirm");
        }

      } else {
        //DBへのデータ登録
        //データの取得
        regList = (UIRegShopInfo) session.getAttribute(SIConfig.SISESSION_MAN_SHOP_INFO_EDIT_NAME);
        //データのチェック
        if (regList.validate(request, connection) == false) {
          forwardKey(request, response, "webshop.jsp.manager.shopinfo.edit");
        } else {
          regList.setActionNameTxt(SIConfig.SIACTION_CONFIRM);
          regList.setEditModeTxt(editMode);
          session.setAttribute(SIConfig.SISESSION_MAN_SHOP_INFO_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.modify"));
              forwardKey(request,response,"webshop.jsp.manager.shopinfo.result");
            }else{
              log.error("no known edit mode.editMode=" + editMode);
              forwardKey(request,response,"webshop.jsp.manager.shopinfo.edit");
            }
          } 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.shopinfo.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.shopinfo.edit");
          }
        }
      }
    }catch (SQLException e){
      e.printStackTrace();
      throw new ServletException();
    }catch (NamingException e){
      e.printStackTrace();
      throw new ServletException();
    }finally{
      databaseConnection.close();
    }
  }

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

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

    lRec.addCondition("shopcode", regList.getShopCode());

    lRec.add("shopComment", regList.getShopComment());
    lRec.add("title", regList.getTitle());
    lRec.add("htmlfilename", regList.getHTMLFileName());

    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 UIRegShopInfo
   * @param connection DBへのコネクション
   * @return なし
   * @throws SIDuplicateKeyException
   * @throws SIDBAccessException
   */
  public static void insertTableData(UIRegShopInfo regShopInfo, Connection connection) throws SIDuplicateKeyException, SIDBAccessException {
	SISpcType lSpcType = new SISpcType();

	SIInsertRec lRec = new SIInsertRec("ShopInfoMTbl");
	lRec.add("ShopCode", regShopInfo.getShopCode());

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

  public void destroy() {
  }
}
