package jp.co.sint.beans.mallmgr;

import java.sql.*;

import javax.servlet.http.*;

import org.apache.log4j.Category;
import jp.co.sint.tools.*;
import jp.co.sint.config.*;
import jp.co.sint.basic.*;
import jp.co.sint.tools.SIURLParameter;

public class UIRegCatalogCust extends SICust {
  // ƒƒO—p‚ÌƒCƒ“ƒXƒ^ƒ“ƒX‚Ì¶¬
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  private String catalogRequest = "";
  
  public String getCatalogRequest() {
    return catalogRequest;
  }
  
  public void setCatalogRequest(String value){
    if (value==null) value="";
    this.catalogRequest = value;
  }
  
  public void init(HttpServletRequest lRequest, SIURLParameter lUrlParam) {
    super.init(lRequest, lUrlParam);
    this.setCustCode((String) lUrlParam.getParam("custCode"));
    this.setCustName((String) lUrlParam.getParam("custName"));
    this.setCustPronName((String) lUrlParam.getParam("custPronName"));
    this.setPostCode1((String) lUrlParam.getParam("postCode1"));
    this.setPostCode2((String) lUrlParam.getParam("postCode2"));
    this.setAddress1((String) lUrlParam.getParam("address1"));
    this.setAddress2((String) lUrlParam.getParam("address2"));
    this.setAddress3((String) lUrlParam.getParam("address3"));
    this.setCompanyName((String) lUrlParam.getParam("companyName"));
    this.setCorporationName((String) lUrlParam.getParam("corporationName"));
    this.setStoreTel((String) lUrlParam.getParam("storeTel"));
    this.setFax((String) lUrlParam.getParam("fax"));
    this.setEmail((String) lUrlParam.getParam("email"));
    this.setJob((String) lUrlParam.getParam("job"));
    String catalogRequest = "0";
    if (SIUtil.isNotNull((String) lUrlParam.getParam("catalogType1")) && SICheckUtil.isDigit((String) lUrlParam.getParam("catalogType1"))) {
      catalogRequest = SIUtil.add(catalogRequest, (String) lUrlParam.getParam("catalogType1"));
    }
    if (SIUtil.isNotNull((String) lUrlParam.getParam("catalogType2")) && SICheckUtil.isDigit((String) lUrlParam.getParam("catalogType2"))) {
      catalogRequest = SIUtil.add(catalogRequest, (String) lUrlParam.getParam("catalogType2"));
    }
    if (!"0".equals(catalogRequest)) this.setCatalogRequest(catalogRequest);
    
    if (getActionNameTxt().equals(SIConfig.SIACTION_ZIPDIC) && SIConfig.SIZIP != null) {// —X•Ö”Ô†‚©‚çŒ§–¼‚ÆŽs‹æ’¬‘º–¼‚ðŒŸõ
      String lEncode = this.getEncode();// Šù‘¶‚ÌƒR[ƒh‚Ì•Û‘¶
      this.setEncode(SIConfig.SIENCODE_NONE);
      String lResult = SIConfig.SIZIP.getPrefectureName(getPostCode1(), getPostCode2());// Œ§–¼‚ÌŒŸõ
      if (SIUtil.isNotNull(lResult)) this.setAddress1(lResult);// Œ§–¼‚ÌÝ’è
      lResult = SIConfig.SIZIP.getAddressName(getPostCode1(), getPostCode2());// Žs‹æ’¬‘º–¼‚ÌŒŸõ
      if (SIUtil.isNotNull(lResult)) this.setAddress2(lResult);// Žs‹æ’¬‘º–¼‚ÌÝ’è
      this.setEncode(lEncode);// Šù‘¶‚ÌƒR[ƒh‚Ì‰ñ•œ
    }
  }
  
  public boolean validate(HttpServletRequest lRequest) {
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors = new SICustomErrors();
    SICheckValid.checkValid(errors, "‰c‹ÆŒ`‘Ô", this.getJob(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    SICheckValid.checkValid(errors, "‚¨–¼‘O", this.getCustName(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    SICheckValid.checkValid(errors, "‚¨–¼‘O", this.getCustName(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 40);
    SICheckValid.checkValid(errors, "‚¨–¼‘OiƒJƒij", this.getCustPronName(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_ZENKAKU_KANA_TYPE);
    SICheckValid.checkValid(errors, "‚¨–¼‘OiƒJƒij", this.getCustPronName(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 50);
    SICheckValid.checkValid(errors, "—X•Ö”Ô†(ã3Œ…)", this.getPostCode1(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
    SICheckValid.checkValid(errors, "—X•Ö”Ô†(‰º4Œ…)", this.getPostCode2(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_DIGIT_TYPE);
    if (this.getPostCode1().toString().trim().length() != 3 || this.getPostCode2().toString().trim().length() != 4) {
      errors.addError(new SICustomError("manager.app.postcode.form"));
    }
    
    SICheckValid.checkValid(errors, "ZŠ1i“s“¹•{Œ§j", this.getAddress1(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    SICheckValid.checkValid(errors, "ZŠ2(Žs‹æ’¬‘ºE”Ô’n)", this.getAddress2(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    SICheckValid.checkValid(errors, "ZŠ2(Žs‹æ’¬‘ºE”Ô’n)", this.getAddress2(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 100);
    SICheckValid.checkValid(errors, "ZŠ3(ƒ}ƒ“ƒVƒ‡ƒ“–¼“™)", this.getAddress3(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 100);
    SICheckValid.checkValid(errors, "ƒTƒƒ“–¼", this.getCompanyName(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    SICheckValid.checkValid(errors, "ƒTƒƒ“–¼", this.getCompanyName(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 100);
    SICheckValid.checkValid(errors, "‰ïŽÐ–¼", this.getCorporationName(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 100);
    SICheckValid.checkValid(errors, "“d˜b”Ô†", this.getStoreTel(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    if (SICheckValid.checkValid(errors, "“d˜b”Ô†", this.getStoreTel(), SICheckDataConf.SICHECK_DATA_PHONE_TYPE));
    SICheckValid.checkValid(errors, "“d˜b”Ô†", this.getStoreTel(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_TYPE, 9, 16);
    if (SICheckValid.checkValid(errors, "FAX”Ô†", this.getFax(), SICheckDataConf.SICHECK_DATA_PHONE_TYPE));
    SICheckValid.checkValid(errors, "FAX”Ô†", this.getFax(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_TYPE, 9, 16);
    SICheckValid.checkValid(errors, "E-mail", this.getEmail(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE + SICheckDataConf.SICHECK_DATA_MAIL_TYPE);
    SICheckValid.checkValid(errors, "E-mail", this.getEmail(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 128);
    SICheckValid.checkValid(errors, "‚²Šó–]‚ÌƒJƒ^ƒƒO", this.getCatalogRequest(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    return errors.isEmpty();
  }
  
  public void setLoginData(HttpServletRequest lRequest){
    if (SIUtil.isNotNull(this.getCustCode())) return;
    SIUserInfo lUserInfo=SIHTMLUtil.getUserInfo(lRequest);
    if (lUserInfo.isLogin(lRequest)){
      this.setCustCode(lUserInfo.getCustCode());
      this.setCustName(lUserInfo.getCustName());
      this.setCustPronName(lUserInfo.getCustPronName());
      this.setPostCode1(lUserInfo.getPostCode1());
      this.setPostCode2(lUserInfo.getPostCode2());
      this.setAddress1(lUserInfo.getAddress1());
      this.setAddress2(lUserInfo.getAddress2());
      this.setAddress3(lUserInfo.getAddress3());
      this.setCompanyName(lUserInfo.getCompanyName());
      this.setCorporationName(lUserInfo.getCorporationName());
      this.setStoreTel(lUserInfo.getStoreTel());
      this.setFax(lUserInfo.getFax());
      this.setEmail(lUserInfo.getEmail());
      this.setJob(lUserInfo.getJob());
    }
  }
}