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

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;

import jp.co.sint.basic.SIFrontBasic;
import jp.co.sint.beans.mallmgr.UIRegCust;
import jp.co.sint.config.SIConfig;
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.SIUtil;
import jp.co.sint.tools.SIURLParameter;//7.1.1 ST0236 追加

import org.apache.log4j.Category;

/**
 * @version $Id : UIRegShop.java,v 1.0 2003/10/29 Exp $
 *  * @author      : Jinwang Chen
 * <br>Description : ショップ管理の編集画面に対する管理 beans
 * <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/08/06  Original
 */

public class UIUserEdit extends SIFrontBasic {
  
  //ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //ショップコード
  private String shopCode ="";
  
  private UIRegCust regCust=new UIRegCust();
  
  private String nextUrlTxt="";
  
  private String ssouserId="";
  
  private String custAddress2Head = "";
  
  private String custAddress2Tail = "";
  
  public UIUserEdit(){}
  
  //setter of ショップコード
  public void setShopCode(String lShopCode){
    if (SIUtil.isNull(lShopCode)) lShopCode="";
    this.shopCode=SIUtil.changeTo(lShopCode.trim(),this.encode);
  }
  
  public void setEditModeTxt(String lEditModeTxt){
    this.editModeTxt =lEditModeTxt;
    regCust.setEditModeTxt(lEditModeTxt);
  }
  
  public void setRegCust(UIRegCust lRegCust){
    if (lRegCust==null)lRegCust=new UIRegCust();
    regCust=lRegCust;
  }
  
  public void setRegCust(String lCustCode){
    regCust.setCustCode(lCustCode);
  }
  
  public void setNextUrlTxt(String lNextUrlTxt){
    if (SIUtil.isNull(lNextUrlTxt)) lNextUrlTxt="";
    this.nextUrlTxt=SIUtil.changeTo(lNextUrlTxt.trim(),this.encode);
  }
  
  public String getNextUrlTxt(){
    return this.nextUrlTxt;
  }
  
  public void setSsouserId(String ssouserId){
    if (SIUtil.isNull(ssouserId)) ssouserId="";
    this.ssouserId=SIUtil.changeTo(ssouserId.trim(),this.encode);
  }
  
  public String getSsouserId(){
    return this.ssouserId;
  }
  
  //getter of ショップコード
  public String getShopCode(){
    return this.shopCode;
  }
  
  public void reset(Connection lConnection){
    regCust.reset(lConnection);
  }
  
  public void reset1(Connection lConnection){
    regCust.reset(lConnection,"1");
  }
  
  public void reset2(Connection lConnection){
    regCust.reset(lConnection,"2");
  }
  
  public UIRegCust getRegCust(){
    return regCust;
  }
  
  public String getCustAddress2Head() {
    return custAddress2Head;
  }
  
  public void setCustAddress2Head(String custAddress2Head) {
    if (SIUtil.isNull(custAddress2Head)) custAddress2Head = "";
    this.custAddress2Head = custAddress2Head;
  }
  
  public String getCustAddress2Tail() {
    return custAddress2Tail;
  }
  
  public void setCustAddress2Tail(String custAddress2Tail) {
    if (SIUtil.isNull(custAddress2Tail)) custAddress2Tail = "";
    this.custAddress2Tail = custAddress2Tail;
  }
  
  public void init(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    super.init(lRequest,lUrlParam);
    regCust.init(lRequest,lUrlParam);
    this.setShopCode((String)lUrlParam.getParam("shc"));
    this.setNextUrlTxt((String)lUrlParam.getParam("nid"));
    // add mikami
    this.setSsouserId((String)lUrlParam.getParam("sid"));
    this.setCustAddress2Head((String)lUrlParam.getParam("address2a"));
    this.setCustAddress2Tail((String)lUrlParam.getParam("address2b"));
  }
  
  public void initNew(HttpServletRequest lRequest,SIURLParameter lUrlParam){
    super.init(lRequest,lUrlParam);
    regCust.initNew(lRequest,lUrlParam);
    this.setShopCode((String)lUrlParam.getParam("shc"));
    this.setNextUrlTxt((String)lUrlParam.getParam("nid"));
  }
  
  public boolean validate(HttpServletRequest lRequest, Connection lConnection){
    SICustomErrors errors = new SICustomErrors();
    
    regCust.validateAccount(lRequest,lConnection);
    errors=(SICustomErrors)lRequest.getAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    if (errors==null) errors = new SICustomErrors();
    if (errors.isEmpty() && this.getSsouserId() == ""){
      regCust.validateAccount2(lRequest,lConnection);
      errors=(SICustomErrors)lRequest.getAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    }
    if (errors==null)errors=new SICustomErrors();
    SICheckValid.checkValid(errors,"メールアドレス","メールアドレス(確認用)",regCust.getEmail(),regCust.getEmailCon(),SICheckDataConf.SICHECK_DATA_MATCH_TYPE);
    SICheckValid.checkValid(errors,"パスワード","パスワード(確認用)",regCust.getPassword(),regCust.getPasswordCon(),SICheckDataConf.SICHECK_DATA_MATCH_TYPE);
    if (!SICheckValid.checkValid(errors, "パスワードリマインダ質問", regCust.getPWQuestion(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE)) {
      SICheckValid.checkValid(errors, "パスワードリマインダ回答", regCust.getPWAnswer(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    }

    if (!errors.isEmpty())lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY,errors);
    else lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    return errors.isEmpty();
  }
  
  public boolean validateEmail(HttpServletRequest lRequest, Connection lConnection){
    SICustomErrors errors = new SICustomErrors();
    
    regCust.validateEmail(lRequest,lConnection);
    errors=(SICustomErrors)lRequest.getAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    if (errors==null)errors=new SICustomErrors();
    SICheckValid.checkValid(errors,"メールアドレス","メールアドレス(確認用)",regCust.getEmail(),regCust.getEmailCon(),SICheckDataConf.SICHECK_DATA_MATCH_TYPE);
    if (!errors.isEmpty())lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY,errors);
    else lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    return errors.isEmpty();
  }
}