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

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

import javax.servlet.http.HttpServletRequest;

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

import org.apache.log4j.Category;

/**
 * @version $Id: UIUserLogin.java,v 1.0 2003/10/27 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>
 * jwchen 2003/10/27 15:36:15 Original
 */
public class UIRemainder extends SIFrontBasic {
  // ログ用のインスタンスの生成
  private static Category log = Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  // email
  private String emailTxt = "";
  
  // 回答
  private String pwanswerTxt = "";
  
  // 質問
  private String pwquestion = "";
  
  private String dispFlg = "0";
  
  public UIRemainder() {}
  
  public void setEmailTxt(String lEmailTxt) {
    if (SIUtil.isNull(lEmailTxt)) lEmailTxt = "";
    this.emailTxt = SIUtil.changeTo(lEmailTxt.trim(), this.encode);
  }
  
  public void setPwanswerTxt(String lPwanswerTxt) {
    if (SIUtil.isNull(lPwanswerTxt)) lPwanswerTxt = "";
    this.pwanswerTxt = SIUtil.changeTo(lPwanswerTxt.trim(), this.encode);
  }
  
  public void setPwquestion(String lPwquestion) {
    if (SIUtil.isNull(lPwquestion)) lPwquestion = "";
    this.pwquestion = SIUtil.changeTo(lPwquestion.trim(), this.encode);
  }
  
  public void setDispFlg(String lDispFlg) {
    if (SIUtil.isNull(lDispFlg)) lDispFlg = "";
    this.dispFlg = SIUtil.changeTo(lDispFlg.trim(), this.encode);
  }
  
  public String getEmailTxt() {
    return this.emailTxt;
  }
  
  public String getPwanswerTxt() {
    return this.pwanswerTxt;
  }
  
  public String getPwquestion() {
    return this.pwquestion;
  }
  
  public String getDispFlg() {
    return this.dispFlg;
  }
  
  public void init(HttpServletRequest lRequest, SIURLParameter lUrlParam) {// 7.1.1 ST0236 修正
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    super.init(lRequest, lUrlParam);// 7.1.1 ST0236 修正
    this.setDispFlg("0");
  }
  
  // 7.1.1 ST0236 修正 ここから
  public void init1(HttpServletRequest lRequest, SIURLParameter lUrlParam) {
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    super.init(lRequest, lUrlParam);
    this.setEmailTxt((String) lUrlParam.getParam("emailTxt"));
    this.setDispFlg("0");
  }
  
  // 7.1.1 ST0236 修正 ここまで
  
  // 7.1.1 ST0236 修正 ここから
  public void init2(HttpServletRequest lRequest, SIURLParameter lUrlParam) {
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    super.init(lRequest, lUrlParam);
    this.setEmailTxt((String) lUrlParam.getParam("emailTxt"));
    this.setPwanswerTxt((String) lUrlParam.getParam("pwanswerTxt"));
    this.setDispFlg("2");
  }
  
  // 7.1.1 ST0236 修正 ここまで
  
  public void validate1(HttpServletRequest lRequest, Connection lConnection) {
    SICustomErrors errors = new SICustomErrors();
    
    SICheckValid.checkValid(errors, "メールアドレス", getEmailTxt(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    SICheckValid.checkValid(errors, "メールアドレス", getEmailTxt(), SICheckDataConf.SICHECK_DATA_MAIL_TYPE);
    
    // メールアドレスのチェック
    Statement lStatement = null;
    ResultSet lResultSet = null;
    try {
      String str = "SELECT pwquestion FROM CustTbl WHERE Email = " + SIDBUtil.SQL2Str(getEmailTxt());
      
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(str);
      
      if (lResultSet.next()) {
        setEncode(SIConfig.SIENCODE_NONE);
        this.setPwquestion(lResultSet.getString("pwquestion"));
        this.setDispFlg("1");
      } else {
        errors.addError(new SICustomError("app.invalid.email"));
      }
    } catch (Exception ex) {} finally {
      SIDBUtil.close(lResultSet, lStatement);
    }
    
    if (!errors.isEmpty()) {
      lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    } else {
      lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    }
  }
  
  public boolean validate2(HttpServletRequest lRequest, Connection lConnection) {
    boolean result = false;
    SICustomErrors errors = new SICustomErrors();
    SICheckValid.checkValid(errors, "回答", getPwanswerTxt(), SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    SICheckValid.checkValid(errors, "回答", getPwanswerTxt(), SICheckDataConf.SICHECK_DATA_BYTE_LEN_WITHIN_TYPE, 100);
    
    if (!errors.isEmpty()) {
      lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
      this.setDispFlg("1");
    } else {
      lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
      
      // 質問の回答
      try {
        String str = "SELECT * FROM CustTbl WHERE Email = " + SIDBUtil.SQL2Str(getEmailTxt()) + " AND Pwanswer = " + SIDBUtil.SQL2Str(getPwanswerTxt());
        result = SIDBUtil.hasData(lConnection, str);
        
      } catch (SIDBAccessException ex) {
        ex.printStackTrace();
        result = false;
      }
    }
    return result;
  }
}
