/**
 * Created on 2003/09/30
 *
 * 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.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;

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.SIMsgTemp;
import jp.co.sint.beans.mallmgr.UIRegMsgTemp;
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 追加

/**
 * @author shionoya
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class SIRegMsgTempSrv 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);//セッションの取得

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

    UIRegMsgTemp regList = new UIRegMsgTemp();
    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.msgtemp.edit");
      }else if(SIConfig.SIACTION_REMODIFY.equalsIgnoreCase(actionName)) {
        regList = (UIRegMsgTemp) session.getAttribute(SIConfig.SISESSION_MAN_MSG_TEMP_EDIT_NAME);
        regList.setActionNameTxt(SIConfig.SIACTION_REMODIFY);
        session.setAttribute(SIConfig.SISESSION_MAN_MSG_TEMP_EDIT_NAME, regList);
        forwardKey(request, response, "webshop.jsp.manager.msgtemp.edit");
      }else if(SIConfig.SIACTION_REFRESH.equalsIgnoreCase(actionName)) {//リフレッシュ
        regList.initReload(request,connection,urlParam);//7.1.1 ST0236 修正
        session.setAttribute(SIConfig.SISESSION_MAN_MSG_TEMP_EDIT_NAME, regList);
        forwardKey(request, response, "webshop.jsp.manager.msgtemp.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_MSG_TEMP_EDIT_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.msgtemp.edit");
        }else{
          session.setAttribute(SIConfig.SISESSION_MAN_MSG_TEMP_EDIT_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.msgtemp.confirm");
        }
      }else if(SIConfig.SIACTION_LIST.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_MSG_TEMP_EDIT_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.msgtemp.edit");
        }else{
          session.setAttribute(SIConfig.SISESSION_MAN_MSG_TEMP_EDIT_NAME, regList);
          forwardKey(request, response, "webshop.jsp.manager.msgtemp.list");
        }
      }else{
        regList = (UIRegMsgTemp) session.getAttribute(SIConfig.SISESSION_MAN_MSG_TEMP_EDIT_NAME);
        //データのチェック
        if(regList.validate(request, connection) == false){
          forwardKey(request, response, "webshop.jsp.manager.msgtemp.edit");
        }else{
          regList.setActionNameTxt(SIConfig.SIACTION_CONFIRM);
          regList.setEditModeTxt(editMode);
          session.setAttribute(SIConfig.SISESSION_MAN_MSG_TEMP_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.msgtemp.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.msgtemp.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.msgtemp.edit");
          }
        }
      }
    }catch (SQLException e){
      e.printStackTrace();
      throw new ServletException();
    }catch (NamingException e){
      e.printStackTrace();
      throw new ServletException();
    }finally{
      databaseConnection.close();
    }
  }

  /**
  * insertTableData
  * データベースにレコードを挿入します。
  * @param
  * @return
  * @throws
  */
  public void insertTableData(UIRegMsgTemp regList, Connection connection) throws SIDuplicateKeyException, SIDBAccessException {
    Hashtable hashParameters = SIMsgTemp.getHashtable_MailTemp();

    //SISpcType lSpcType = new SISpcType();
    SIInsertRec lRec = new SIInsertRec("msgtemplateMtbl");

    Object key;

    for (Enumeration e = hashParameters.keys(); e.hasMoreElements();) {
      key = e.nextElement();

      lRec = new SIInsertRec("msgtemplateMtbl");
      lRec.add("mallshopcode", regList.getMallShopCode());
      lRec.add("msgType", (String) key);
      lRec.add("classify", (String) hashParameters.get(key));
      lRec.add("title", (String) hashParameters.get(key) + "です。");
      lRec.add("content1", regList.getContent1());
      lRec.add("content2", regList.getContent2());
      lRec.add("content3", regList.getContent3());

      log.debug("insert sql=" + lRec.getSQL());
      lRec.execute(connection);
    }
    hashParameters.clear();
  }

  /**
  * updateTableData
  * データベースにレコードを修正します。
  * @param
  * @return
  * @throws
  */
  private void updateTableData(UIRegMsgTemp regList, Connection connection) throws SIDuplicateKeyException, SIDBAccessException {
    //SISpcType lSpcType = new SISpcType();
    SIModifyRec lRec = new SIModifyRec("msgtemplateMtbl");
    lRec.addCondition("mallshopcode", regList.getMallShopCode());
    lRec.addCondition("msgType", regList.getMsgType());
    lRec.add("title", regList.getTitle());
    lRec.add("content1", regList.getContent1());
    lRec.add("content2", regList.getContent2());
    lRec.add("content3", regList.getContent3());

    log.debug("update sql=" + lRec.getSQL());
    lRec.execute(connection);

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

  /**
  * deleteTableData
  * データベースにレコードを削除します。
  * @param
  * @return
  * @throws
  */
  public void deleteTableData(UIRegMsgTemp regList, Connection connection) throws SIDuplicateKeyException, SIDBAccessException {
    //SISpcType lSpcType = new SISpcType();
    SIDeleteRec lRec = new SIDeleteRec("msgtemplateMtbl");
    lRec.addCondition("mallshopcode", regList.getMallShopCode());

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

  public void destroy() {
  }
}
