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

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SIBasic;
import jp.co.sint.basic.SILogin;
import jp.co.sint.basic.SIPayMethodFee;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.database.SITableConditionManager;
import jp.co.sint.tools.SICheckDataConf;
import jp.co.sint.tools.SICheckValid;
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: UIShippmentListCond.java,v 1.0 2003/09/19 Exp $
 * @author  asakura
 * <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>
 * asakura        2003/09/19 11:06:28  Original
 */
public class UIPayMethodFeeListCond extends SIBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //管理コード
  private String mallShopCode="";
  
  //支払方法ID
  private String payMethodCode="";
  
  //購入金額
  private String toPriceTxt = "0";
  
  //手数料
  private String feeTxt = "0";
  
  //手数料(更新)
  private String regFee = "0";
  
  public UIPayMethodFeeListCond(){}
  
  //7.1.1 ST0236 修正 ここから
  public UIPayMethodFeeListCond(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    SILogin lLogin=SIHTMLUtil.getLogin(lRequest);
    
    init(lRequest,lUrlParam);
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    if (lLogin.isShop()){
      this.setMallShopCode(lLogin.getMallShopCode());//管理コード
    }else{
      this.setMallShopCode((String)lUrlParam.getParam("mallShopCode"));//管理コード
    }
    this.setPayMethodCode((String)lUrlParam.getParam("payMethodCode"));//支払方法ID
  }
  //7.1.1 ST0236 修正 ここまで
  
  //setter of 管理コード
  public void setMallShopCode(String lMallShopCode){
    if (SIUtil.isNull(lMallShopCode)) lMallShopCode="";
    this.mallShopCode=SIUtil.changeTo(lMallShopCode.trim(),this.encode);
  }
  
  //setter of 支払方法ID
  public void setPayMethodCode(String lPayMethodCode){
    if (SIUtil.isNull(lPayMethodCode)) lPayMethodCode="";
    this.payMethodCode=SIUtil.changeTo(lPayMethodCode.trim(),this.encode);
  }
  
  //setter of 購入金額
  public void setToPriceTxt(String lToPriceTxt){
    if (SIUtil.isNull(lToPriceTxt)) lToPriceTxt="0";
    this.toPriceTxt=SIUtil.changeTo(lToPriceTxt.trim(),this.encode);
  }
  
  //setter of 手数料
  public void setFeeTxt(String lFeeTxt){
    if (SIUtil.isNull(lFeeTxt)) lFeeTxt="0";
    this.feeTxt=SIUtil.changeTo(lFeeTxt.trim(),this.encode);
  }
  
  //setter of 手数料(更新)
  public void setRegFee(String lRegFee){
    if (SIUtil.isNull(lRegFee)) lRegFee="0";
    this.regFee=SIUtil.changeTo(lRegFee.trim(),this.encode);
  }
  
  //getter of 管理コード
  public String getMallShopCode(){
    return this.mallShopCode;
  }
  
  //getter of 支払方法ID
  public String getPayMethodCode(){
    return this.payMethodCode;
  }
  
  //getter of 購入金額
  public String getToPriceTxt(){
    return this.toPriceTxt;
  }
  
  //getter of 手数料
  public String getFeeTxt(){
    return this.feeTxt;
  }
  
  //getter of 手数料(更新)
  public String getRegFee(){
    return this.regFee;
  }
  
  /**
   * <b>getInsertData</b>
   * 入力したデータを基づいて、このbeansを設定します。
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  public SIPayMethodFee getInsertData(SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    SIPayMethodFee pay = new SIPayMethodFee();
    pay.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    pay.setMallShopCode(this.getMallShopCode());//管理コード
    pay.setPayMethodCode(this.getPayMethodCode());//支払方法ID
    pay.setToPrice((String)lUrlParam.getParam("toPriceTxt"));//購入金額  //7.1.1 ST0236 修正
    this.setToPriceTxt(pay.getToPrice());
    pay.setFee((String)lUrlParam.getParam("feeTxt"));//手数料  //7.1.1 ST0236 修正
    this.setFeeTxt(pay.getFee());
    return pay;
  }
  
  /**
   * <b>getModifyData</b>
   * 入力したデータを基づいて、このbeansを設定します。
   * @param request クライアントからリクエスト
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  public SIPayMethodFee getModifyData(HttpServletRequest lRequest,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    SIPayMethodFee pay = new SIPayMethodFee();
    pay.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    pay.setMallShopCode(this.getMallShopCode());//管理コード
    pay.setPayMethodCode(this.getPayMethodCode());//支払方法ID  
    pay.setToPrice((String)lUrlParam.getParam("regToPriceTxt"));//購入金額  //7.1.1 ST0236 修正
    int row = Integer.parseInt((String)lUrlParam.getParam("regRowNumTxt"));//更新する行  //7.1.1 ST0236 修正
    this.setRegFee(lRequest.getParameterValues("regFeeTxt")[row]);
    pay.setFee(this.getRegFee());//手数料
    return pay;
  }
  
  /**
   * <b>setInsertData</b>
   * 入力したデータを基づいて、このbeansを設定します。
   * @param request クライアントからリクエスト
   * @return なし
   * @throws なし
   */
  public void setInsertData(HttpServletRequest lRequest){
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    this.setToPriceTxt("");//購入金額
    this.setFeeTxt("");//手数料
  }
  
  /**
   * <b>getCollection</b>
   * 条件に合ったレコードを検索して、結果のコネクションを作成して、戻します。
   * @param lConnection データベースへの接続コネクション
   * @return レコードのセット
   * @throws なし
   */
  public Collection getCollection(Connection lConnection) throws SIDBAccessException{
    Statement lStatement=null;
    ResultSet lResultSet=null;
    SIPayMethodFee lBasic=new SIPayMethodFee();
    StringBuffer lSqlBuf=new StringBuffer();
    Collection lResultColl=new ArrayList();
    
    lSqlBuf.append("SELECT PriceTo , Fee ");
    lSqlBuf.append("FROM PayMethodFeeMtbl ");
    lSqlBuf.append("WHERE MallShopCode=").append(SIDBUtil.SQL2Str(this.getMallShopCode()," "));
    lSqlBuf.append("AND PayMethodCode=").append(SIDBUtil.SQL2Str(this.getPayMethodCode()," "));
    lSqlBuf.append("ORDER BY PriceTo ASC ");
    
    log.debug("lSqlBuf="+lSqlBuf.toString());
    //実行
     try{
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());
      int fromPrice = 0;
      //商品レコードのセットの作成
      while (lResultSet.next()){
        lBasic=new SIPayMethodFee();
        lBasic.setEncode(SIConfig.SIENCODE_NONE);
        lBasic.setFromPrice(Integer.toString(fromPrice));//金額From
        lBasic.setToPrice(lResultSet.getString("PriceTo"));//金額To
        lBasic.setFee(lResultSet.getString("Fee"));//手数料
        fromPrice = Integer.parseInt(lBasic.getToPrice()) + 1;
        lResultColl.add(lBasic);
      }
    }catch(Exception ex){
      throw new SIDBAccessException(ex);
    }finally{
      SIDBUtil.close(lResultSet,lStatement);
    }
    return lResultColl;
  }
  
  /**
   * <b>validateInsert</b>
   * 入力したデータをチェックして、同時にSQLの条件文を作成します。
   * @param lRequest クライアントからのリクエスト
   * @return なし
   * @throws なし
   */
  public boolean validateInsert(HttpServletRequest lRequest){
    SICustomErrors errors=new SICustomErrors();
    
    //購入金額
    SICheckValid.checkValid(errors, "購入金額To", this.getToPriceTxt(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "購入金額To", this.getToPriceTxt(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 8);
    SICheckValid.checkValid(errors, "購入金額To", this.getToPriceTxt(), SICheckDataConf.SICHECK_DATA_NUMBER_BETWEEN_TYPE, 1 , 99999999);
    //手数料
    SICheckValid.checkValid(errors, "手数料", this.getFeeTxt(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "手数料", this.getFeeTxt(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 6);
    
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK, errors);
    else lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK);
    
    return errors.isEmpty();
  }
  
  /**
   * <b>validateModify</b>
   * 入力したデータをチェックして、同時にSQLの条件文を作成します。
   * @param lRequest クライアントからのリクエスト
   * @return なし
   * @throws なし
   */
  public boolean validateModify(HttpServletRequest lRequest){
    SICustomErrors errors=new SICustomErrors();
    
    //手数料
    SICheckValid.checkValid(errors, "手数料", this.getRegFee(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "手数料", this.getRegFee(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 6);
    
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK, errors);
    else lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY_BAK);
    
    return errors.isEmpty();
  }
}