/**
 * Created on 2003/09/24
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package jp.co.sint.beans.mallmgr;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SIExRule;
import jp.co.sint.basic.SILogin;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.database.SITableCondition;
import jp.co.sint.database.SITableConditionManager;
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.SIURLParameter;//7.1.1 ST0236 追加

import org.apache.log4j.Category;

/**
 * @author arai
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class UIRegExRule extends SIExRule {
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  /**
   * init
   * 入力したデータから、このbeansを設定します。
   * @param HttpServletRequest
   * @param SIURLParameter
   * @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.setMallShopCode(lLogin.getMallShopCode());
    }else{
      this.setMallShopCode((String)lUrlParam.getParam("mallShopCode"));
    }
    this.setSellerName((String)lUrlParam.getParam("sellerName"));
    this.setOwnerName((String)lUrlParam.getParam("ownerName"));
    this.setLocation((String)lUrlParam.getParam("location"));
    this.setTel((String)lUrlParam.getParam("tel"));
    this.setFax((String)lUrlParam.getParam("fax"));
    this.setEmail((String)lUrlParam.getParam("email"));
    this.setCommentOfOrder((String)lUrlParam.getParam("commentOfOrder"));
    this.setCommentOfPrice((String)lUrlParam.getParam("commentOfPrice"));
    this.setCommentOfPeriod((String)lUrlParam.getParam("commentOfPeriod"));
    this.setCommentOfAmount((String)lUrlParam.getParam("commentOfAmount"));
    this.setCommentOfDeliver((String)lUrlParam.getParam("commentOfDeliver"));
    this.setCommentOfPayment((String)lUrlParam.getParam("commentOfPayment"));
    this.setCommentOfPayPeriod((String)lUrlParam.getParam("commentOfPayPeriod"));
    this.setCommentOfReturn((String)lUrlParam.getParam("commentOfReturn"));
    this.setActionNameTxt((String)lUrlParam.getParam("actionNameTxt"));
    this.setEditModeTxt((String)lUrlParam.getParam("editModeTxt"));
  }
  //7.1.1 ST0236 修正ここまで
  
  /**
   * validate
   * 入力したデータをチェックします。
   * 不正なデータがある場合、エラーをオブジェクトに格納します。
   * そのオブジェクトは、エラーメッセージとして画面に表示されます。
   * @param HttpServletRequest ，Connection
   * @return true:エラーがない false:エラーが１つ以上ある
   * @throws なし
   */
  public boolean validate(HttpServletRequest lRequest, Connection lConnection) {
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors = new SICustomErrors();
    
    SICheckValid.checkValid(errors, "管理コード", this.getMallShopCode(), SICheckDataConf.SICHECK_DATA_ALPHA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "管理コード", this.getMallShopCode(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 16);
    
    SICheckValid.checkValid(errors, "販売業者", this.getSellerName(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 100);
    
    SICheckValid.checkValid(errors, "代表者", this.getOwnerName(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 50);
    
    SICheckValid.checkValid(errors, "所在地", this.getLocation(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);//7.1.1 ST0162 修正
    
    SICheckValid.checkValid(errors, "電話番号", this.getTel(), SICheckDataConf.SICHECK_DATA_TEL_TYPE);
    SICheckValid.checkValid(errors, "電話番号", this.getTel(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 16);
    
    SICheckValid.checkValid(errors, "ＦＡＸ番号", this.getFax(), SICheckDataConf.SICHECK_DATA_TEL_TYPE);
    SICheckValid.checkValid(errors, "ＦＡＸ番号", this.getFax(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 16);
    
    SICheckValid.checkValid(errors, "メールアドレス", this.getEmail(), SICheckDataConf.SICHECK_DATA_MAIL_TYPE);
    SICheckValid.checkValid(errors, "メールアドレス", this.getEmail(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 128);
    
    SICheckValid.checkValid(errors, "注文方法", this.getCommentOfOrder(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);
    
    SICheckValid.checkValid(errors, "必要料金", this.getCommentOfPrice(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);
    
    SICheckValid.checkValid(errors, "有効期限", this.getCommentOfPeriod(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);
    
    SICheckValid.checkValid(errors, "販売数量", this.getCommentOfAmount(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);
    
    SICheckValid.checkValid(errors, "引渡時期", this.getCommentOfDeliver(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);
    
    SICheckValid.checkValid(errors, "支払方法", this.getCommentOfPayment(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);
    
    SICheckValid.checkValid(errors, "支払期限", this.getCommentOfPayPeriod(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);
    
    SICheckValid.checkValid(errors, "返品", this.getCommentOfReturn(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 1000);
    
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    return errors.isEmpty();
  }
  
  /**
   * reset
   * 入力したデータを基づいて、このbeansを設定します。
   * @param request データベースへのコネンクション
   * @return
   * @throws SIDBAccessException
   */
  public void reset(Connection lConnection) throws SIDBAccessException {
    Statement lStatement = null;
    ResultSet lResultSet = null;
    StringBuffer lSqlStatement = new StringBuffer();
    
    //基本のSQL
    lSqlStatement.append("SELECT * FROM exchangeruleMtbl WHERE 1=1 ");
    SITableConditionManager lConditionMan = new SITableConditionManager();
    SITableCondition DD = new SITableCondition("", "mallshopcode", this.getMallShopCode(), SIConfig.SICONDITION_TYPE_EQUAL, SIConfig.SICONDITION_TYPE_AND);
    DD.setBlankEnable(false);
    lConditionMan.add(DD);
    lSqlStatement.append(lConditionMan.getCondtionSQL());
    lSqlStatement.append(" ORDER BY mallshopcode asc");
    
    log.debug("sqlStatement=" + lSqlStatement.toString());
    
    //実行
    try {
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlStatement.toString());
      
      if (lResultSet.next()) {
        //配送先送料
        this.setEncode(SIConfig.SIENCODE_NONE);
        this.setMallShopCode(lResultSet.getString("mallShopCode"));
        this.setSellerName(lResultSet.getString("sellerName"));
        this.setOwnerName(lResultSet.getString("ownerName"));
        this.setLocation(lResultSet.getString("location"));
        this.setTel(lResultSet.getString("tel"));
        this.setFax(lResultSet.getString("fax"));
        this.setEmail(lResultSet.getString("email"));
        this.setCommentOfOrder(lResultSet.getString("commentOfOrder"));
        this.setCommentOfPrice(lResultSet.getString("commentOfPrice"));
        this.setCommentOfPeriod(lResultSet.getString("commentOfPeriod"));
        this.setCommentOfAmount(lResultSet.getString("commentOfAmount"));
        this.setCommentOfDeliver(lResultSet.getString("commentOfDeliver"));
        this.setCommentOfPayment(lResultSet.getString("commentOfPayment"));
        this.setCommentOfPayPeriod(lResultSet.getString("commentOfPayPeriod"));
        this.setCommentOfReturn(lResultSet.getString("commentOfReturn"));
      }
    } catch (Exception ex) {
      throw new SIDBAccessException(ex);
    } finally {
      SIDBUtil.close(lResultSet, lStatement);
    }
  }
}