/**
 * Created on 2003/11/13
 *
 * 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.UIDeliveryTimetableListCond;
import jp.co.sint.beans.mallmgr.UIRegDeliveryTimetable;
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 arai
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class SIRegDeliveryTimetableSrv 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 修正

      UIDeliveryTimetableListCond deliveryTimeList = new UIDeliveryTimetableListCond();
      session.setAttribute(SIConfig.SISESSION_MAN_DELIVERYTIME_LIST_NAME, deliveryTimeList);

      UIRegDeliveryTimetable deliveryTime = new UIRegDeliveryTimetable();
      session.removeAttribute(SIConfig.SISESSION_MAN_DELIVERYTIME_EDIT_NAME);

      if (SIConfig.SIACTION_LIST.equalsIgnoreCase(actionName)) { //一覧と検索などの画面
        forwardKey(request, response, "webshop.jsp.manager.deliverytime.list");
      } else if (SIConfig.SIACTION_MODIFY.equalsIgnoreCase(actionName)) { //レコードの修正

        //データの取得とデータのチェック
		deliveryTime.initModify(request,urlParam);//7.1.1 ST0236 修正
        if (!deliveryTime.validateModify(request)) { //エラーがあれば
          session.setAttribute(SIConfig.SISESSION_MAN_DELIVERYTIME_EDIT_NAME, deliveryTime);
          forwardKey(request, response, "webshop.jsp.manager.deliverytime.list");
        } else { //エラーがなければ
          try {
            //レコードの修正
            updateTableData(databaseConnection.getConnection(), deliveryTime);
            request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory.getErrorMsg("manager.message.success.modify"));
            forwardKey(request, response, "webshop.jsp.manager.deliverytime.list");
          } catch (SIDuplicateKeyException e) {
        session.setAttribute(SIConfig.SISESSION_MAN_DELIVERYTIME_EDIT_NAME, deliveryTime);
        SICustomErrors errors = new SICustomErrors();
        errors.addError(new SICustomError("database.insert.duplicate2","時間帯設定が同一"));
        request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK, errors);
        forwardKey(request, response, "webshop.jsp.manager.deliverytime.list");
          } catch (SIDBAccessException e) {
            session.setAttribute(SIConfig.SISESSION_MAN_DELIVERYTIME_EDIT_NAME, deliveryTime);
            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.deliverytime.list");
          }
        }
      } else if (SIConfig.SIACTION_NEW.equalsIgnoreCase(actionName)) { //新規のレコード

        //データの取得とデータのチェック
		deliveryTime.initInsert(request,urlParam);//7.1.1 ST0236 修正
        if (!deliveryTime.validateInsert(request, databaseConnection.getConnection())) { //エラーがあれば
          session.setAttribute(SIConfig.SISESSION_MAN_DELIVERYTIME_EDIT_NAME, deliveryTime);
          forwardKey(request, response, "webshop.jsp.manager.deliverytime.list");
        } else { //エラーがなければ
          try {
            //データレコードの作製
            insertTableData(databaseConnection.getConnection(), deliveryTime);
            request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory.getErrorMsg("manager.message.success.insert"));
            forwardKey(request, response, "webshop.jsp.manager.deliverytime.list");
          } catch (SIDuplicateKeyException e) {
            session.setAttribute(SIConfig.SISESSION_MAN_DELIVERYTIME_EDIT_NAME, deliveryTime);
            SICustomErrors errors = new SICustomErrors();
            errors.addError(new SICustomError("database.insert.duplicate2","時間帯設定が同一"));
            request.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK, errors);
            forwardKey(request, response, "webshop.jsp.manager.deliverytime.list");
          } catch (SIDBAccessException e) {
            session.setAttribute(SIConfig.SISESSION_MAN_DELIVERYTIME_EDIT_NAME, deliveryTime);
            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.deliverytime.list");
          }
        }
      } else if (SIConfig.SIACTION_DELETE.equalsIgnoreCase(actionName)) { //レコードの削除
        //キーデータの取得とデータのチェック
		deliveryTime.initModify(request,urlParam);//7.1.1 ST0236 修正
        if (!deliveryTime.validateDelete(request, databaseConnection.getConnection())) { //エラーがあれば
          forwardKey(request, response, "webshop.jsp.manager.deliverytime.list");
        } else { //エラーがなければ
          try {
            //レコードの削除
            deleteTableData(databaseConnection.getConnection(), deliveryTime);
            request.setAttribute(SIConfig.SIMESSAGE_ATTRIBUTE_RESULT_NAME, SIErrorFactory.getErrorMsg("manager.message.success.delete"));
            forwardKey(request, response, "webshop.jsp.manager.deliverytime.list");
          } 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.deliverytime.list");
          }
        }
      }
    }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 regStndrdName 入力したデータ
   * @return なし
   * @throws SIDuplicateKeyException
   * @throws SIDBAccessException
   */
  public void insertTableData(Connection lConnection, UIRegDeliveryTimetable deliveryTime) throws SIDuplicateKeyException, SIDBAccessException {
    SISpcType lSpcType = new SISpcType();
    SIInsertRec lRec = new SIInsertRec("DeliveryTimeMtbl");

    lRec.add("mallShopCode", deliveryTime.getMallShopCode());
    lRec.add("deliveryTypeCode", deliveryTime.getDeliveryTypeCode());
    lRec.add("deliveryTimeFrom", deliveryTime.getDeliveryTimeFrom());
    lRec.add("deliveryTimeTo", deliveryTime.getDeliveryTimeTo());

    //データの更新
    lRec.execute(lConnection);
    try {
      lConnection.commit();
    } catch (SQLException sqle) {
		sqle.printStackTrace();
      throw new SIDBAccessException("insert data error.");
    }
  }

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

    lRec.addCondition("mallShopCode", deliveryTime.getRegMallShopCode());
    lRec.addCondition("deliveryTypeCode", deliveryTime.getRegDeliveryTypeCode());

    lRec.addCondition("deliveryTimeFrom", deliveryTime.getRegDeliveryTimeFrom());
    lRec.addCondition("deliveryTimeTo", deliveryTime.getRegDeliveryTimeTo());

    lRec.add("deliveryTimeFrom", deliveryTime.getDeliveryTimeFrom());
    lRec.add("deliveryTimeTo", deliveryTime.getDeliveryTimeTo());

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

  /**
   * <b>deleteTableData</b>
   * データベースにレコードを削除します。
   * @param lConnection DBへのコネクション
   * @param regStndrdName 削除するデータ
   * @return なし
   * @throws SIDuplicateKeyException
   * @throws SIDBAccessException
   */
  private void deleteTableData(Connection lConnection, UIRegDeliveryTimetable deliveryTime) throws SIDBAccessException {
    SIDeleteRec lRec = new SIDeleteRec("DeliveryTimeMtbl");

    lRec.addCondition("mallShopCode", deliveryTime.getRegMallShopCode());
    lRec.addCondition("deliveryTypeCode", deliveryTime.getRegDeliveryTypeCode());
    lRec.addCondition("deliveryTimeFrom", deliveryTime.getRegDeliveryTimeFrom());
    lRec.addCondition("deliveryTimeTo", deliveryTime.getRegDeliveryTimeTo());

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

  public void destroy() {
  }

}
