/**
 * 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.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jp.co.sint.basic.SICartManager;
import jp.co.sint.basic.SIFrontBasic;
import jp.co.sint.basic.SILogin;
import jp.co.sint.basic.SIPointMan;
import jp.co.sint.basic.SIUserInfo;
import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
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: UIRegNewOrderUserLogin.java,v 1.0 2003/10/27 Exp $
 * @author  yamauchi
 * <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>
 * yamauchi   2004/01/23 15:36:15  Original
 */
public class UIRegNewOrderUserLogin extends SIFrontBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  
  //顧客メールアドレス
  private String emailTxt="";
  
  //顧客コード
  private String custCodeTxt="";
  
  public UIRegNewOrderUserLogin(){}
  
  public void setEmailTxt(String lEmailTxt){
    if (SIUtil.isNull(lEmailTxt)) lEmailTxt="";
    this.emailTxt=SIUtil.changeTo(lEmailTxt.trim(),this.encode);
  }
  
  public void setCustCode(String lCustCodeTxt){
    if (SIUtil.isNull(lCustCodeTxt)) lCustCodeTxt="";
    this.custCodeTxt=SIUtil.changeTo(lCustCodeTxt.trim(),this.encode);
  }
  
  public String getEmailTxt(){
    return this.emailTxt ;
  }
  
  public String getCustCode(){
    return this.custCodeTxt;
  }
  
  public boolean init(HttpServletRequest lRequest,Connection lConnection,SIURLParameter lUrlParam){//7.1.1 ST0236 修正
    SILogin lLogin=SIHTMLUtil.getLogin(lRequest);
    SIUserInfo lUserLogin=new SIUserInfo();
    Statement lStatement = null;
    ResultSet lResultSet = null;
    HttpSession session=lRequest.getSession();
    boolean lRes=false;
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    super.init(lRequest,lUrlParam);//7.1.1 ST0236 修正
    this.setCustCode((String)lUrlParam.getParam("custcode"));//7.1.1 ST0236 修正
    
    if (lLogin.isShop()&&SIUtil.isNotNull(getCustCode())){
      StringBuffer lSqlBuf=new StringBuffer("SELECT ShopCode FROM ShopCustConnVW WHERE ShopCode=");
      lSqlBuf.append(SIDBUtil.SQL2Str(lLogin.getMallShopCode())).append(" AND CustCode=");
      lSqlBuf.append(SIDBUtil.SQL2Str(getCustCode()));
      try {
        if (!SIDBUtil.hasData(lConnection,lSqlBuf.toString())) return false;
      } catch (SIDBAccessException e) {
        e.printStackTrace();
        return false;
      }
    }
    
    SIUserInfo userInfoID=(SIUserInfo)session.getAttribute(SIConfig.SISESSION_USERINFO_NAME);
    if(userInfoID==null) userInfoID = new SIUserInfo();
    
    //前回と異なるユーザを選択した場合セッションをクリア
    if(!this.getCustCode().equals(userInfoID.getCustCode())) SICartManager.clear(lRequest);
    
    if (SIUtil.isNull(this.getCustCode())) return false;
    //該当顧客の検索
    StringBuffer lSqlBuf = new StringBuffer();
    lSqlBuf.append("SELECT * FROM CustTbl WHERE CustCode=").append(SIDBUtil.SQL2Str(this.getCustCode()));
    
    try {
      log.debug("setup:lSql="+lSqlBuf.toString());
      
      lStatement = lConnection.createStatement();
      lResultSet = lStatement.executeQuery(lSqlBuf.toString());
      
      if (lResultSet.next()) {
      this.setEncode(SIConfig.SIENCODE_NONE);
      this.setEmailTxt(lResultSet.getString("email")); //メールアドレス
      lRes=true;
      }
    } catch (SQLException sqle) {
      sqle.printStackTrace();
    } finally {
      SIDBUtil.close(lStatement, lResultSet);
    }
    
    SIPointMan.clear(lConnection,this.getCustCode());
    lUserLogin.reset(lConnection,getEmailTxt());
    
    //ログインユーザ情報をセッションに設定します。
    session.setAttribute(SIConfig.SISESSION_USERINFO_NAME,lUserLogin);
    return lRes;
  }
}