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

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SICmdtyCtgry;
import jp.co.sint.basic.SILogin;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.tools.SICheckDataConf;
import jp.co.sint.tools.SICheckValid;
import jp.co.sint.tools.SICustomError;
import jp.co.sint.tools.SICustomErrors;
import jp.co.sint.tools.SIHTMLUtil;
import jp.co.sint.tools.SIUtil;
import jp.co.sint.tools.SIURLParameter;//7.1.1 ST0236 追加

import org.apache.log4j.Category;

/**
 * @version $Id: UIRegCtgryCmdty.java,v 1.0 2003/09/24 Exp $
 * @author  Jinwang Chen
 * <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>
 * Jinwang Chen   2003/09/24 15:02:52  Original
 */
public class UIRegCtgryCmdty extends SICmdtyCtgry{

  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  /**
   * UIRegCtgryCmdty
   * コンストラクタ
   * @param なし
   * @return なし
   * @throws なし
   */
  public UIRegCtgryCmdty(){}

  /**
   * UIRegCtgryCmdty
   * コンストラクタ
   * @param lRequest リクエスト
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  //7.1.1 ST0236 修正　ここから
  public UIRegCtgryCmdty(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    init(lRequest,lUrlParam);
  }
  //7.1.1 ST0236 修正　ここまで

  /**
   * init
   * 画面から入力したデータを取得します。
   * @param lRequest リクエスト
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  //7.1.1 ST0236 修正　ここから
  public void init(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    SILogin lLogin=SIHTMLUtil.getLogin(lRequest);

    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    if (lLogin.isShop()){
      this.setShopCode(lLogin.getMallShopCode());//ショップコード
    }else{
      this.setShopCode((String)lUrlParam.getParam("shopCode"));//ショップコード
    }
    this.setCmdtyCode((String)lUrlParam.getParam("cmdtyCode"));//商品コード
    this.setCtgryCode((String)lUrlParam.getParam("ctgryCode"));//カテゴリコード
  }
  //7.1.1 ST0236 修正　ここまで

  /**
   * validate
   * 入力したデータをチェックします。
   * @param lRequest リクエスト
   * @param lConnection DBへのコネクション
   * @return エラーがあるかどうか
   * @throws なし
   */
  public boolean validate(HttpServletRequest lRequest,Connection lConnection){
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors=new SICustomErrors();
    SICustomError err;
    StringBuffer lSqlBuf=new StringBuffer();

    SICheckValid.checkValid(errors,"ショップコード",this.getShopCode(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE+SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    SICheckValid.checkValid(errors,"親コード",this.getCmdtyCode(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE+SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);

    if (SIUtil.isNotNull(this.getShopCode())&&SIUtil.isNotNull(this.getCmdtyCode())){
      lSqlBuf.append("SELECT ShopCode FROM CmdtyMTbl ");
      lSqlBuf.append("WHERE ShopCode=" + SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND CmdtyCode="+SIDBUtil.SQL2Str(this.getCmdtyCode()));
      SICheckValid.checkExist(errors,lConnection,"親コード",lSqlBuf.toString());

      //重複レコードの存在でかどうかをチェックします
      lSqlBuf=new StringBuffer();
      lSqlBuf.append("SELECT CtgryCode FROM CmdtyCtgryMTbl ");
      lSqlBuf.append("WHERE ShopCode=" + SIDBUtil.SQL2Str(this.getShopCode()," "));
      lSqlBuf.append("AND CmdtyCode="+SIDBUtil.SQL2Str(this.getCmdtyCode()));
      lSqlBuf.append("AND CtgryCode="+SIDBUtil.SQL2Str(this.getCtgryCode()));
      SICheckValid.checkDuplicate(errors,lConnection,"入力したショップコード、親コードと同じ値",lSqlBuf.toString());
    }

    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY,errors);
    return errors.isEmpty();
  }
}
