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

import java.sql.Connection;
import java.sql.SQLException;  //7.3.0 ST2028 追加

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

import jp.co.sint.basic.SILogin;
import jp.co.sint.basic.SIMallShop;
import jp.co.sint.config.SIConfig;
//7.3.0 ST2028 追加  ここから
import jp.co.sint.config.SIDBMultiConf;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.database.SIDuplicateKeyException;
import jp.co.sint.database.SIModifyRec;
import jp.co.sint.database.SISpcType;
//7.3.0 ST2028 追加  ここまで
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: UILogin.java,v 1.0 2003/10/01 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>
 * Jinwang Chen   2003/10/01 14:34:33  Original
 */
public class UILogin extends SIMallShop{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  //モールショップコード
  private String mallShopCodeTxt="";

  //管理者ID
  private String adminNameTxt="";

  //管理者パスワード
  private String adminPassTxt="";

  public UILogin(){
  }

  //setter of モールショップコード
  public void setMallShopCodeTxt(String lMallShopCodeTxt){
    if (SIUtil.isNull(lMallShopCodeTxt)) lMallShopCodeTxt="";
    this.mallShopCodeTxt=SIUtil.changeTo(lMallShopCodeTxt.trim(),this.encode);
  }

  //setter of 管理者ID
  public void setAdminNameTxt(String lAdminNameTxt){
    if (SIUtil.isNull(lAdminNameTxt)) lAdminNameTxt="";
    this.adminNameTxt=SIUtil.changeTo(lAdminNameTxt.trim(),this.encode);
  }

  //setter of 管理者パスワード
  public void setAdminPassTxt(String lAdminPassTxt){
    if (SIUtil.isNull(lAdminPassTxt)) lAdminPassTxt="";
    this.adminPassTxt=SIUtil.changeTo(lAdminPassTxt.trim(),this.encode);
  }

  //getter of モールショップコード
  public String getMallShopCodeTxt(){
    return this.mallShopCodeTxt;
  }

  //getter of 管理者ID
  public String getAdminNameTxt(){
    return this.adminNameTxt;
  }

  //getter of 管理者パスワード
  public String getAdminPassTxt(){
    return this.adminPassTxt;
  }

	//7.1.1 ST0236 修正　ここから
  public void init(SIURLParameter lUrlParam){
    this.setEncode(SIConfig.SIENCODE_SHIFT_JIS);
    this.setActionNameTxt((String)lUrlParam.getParam("actionNameTxt"));
    this.setMallShopCodeTxt((String)lUrlParam.getParam("mallShopCodeTxt"));
    this.setAdminNameTxt((String)lUrlParam.getParam("adminNameTxt"));
    this.setAdminPassTxt((String)lUrlParam.getParam("adminPassTxt"));
  }
  //7.1.1 ST0236 修正　ここまで

  public boolean validate(HttpServletRequest lRequest,Connection lConnection){
    SICustomErrors errors=new SICustomErrors();
    SILogin lLogin=new SILogin();
    HttpSession session=lRequest.getSession();
    //7.3.0 ST2028 追加  ここから
    String strNowDateTime = null;
    SIModifyRec siRec = null;

    strNowDateTime = SIDBUtil.getDBDateTime(lConnection); //DBから今日の日付を取得
    //7.3.0 ST2028 追加  ここまで

    SICheckValid.checkValid(errors,"管理コード",getMallShopCodeTxt(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);//7.1.1 ST0162 修正
    SICheckValid.checkValid(errors,"管理者ID",getAdminNameTxt(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);
    SICheckValid.checkValid(errors,"パスワード",getAdminPassTxt(),SICheckDataConf.SICHECK_DATA_EMPTY_TYPE);

    if (errors.isEmpty()){
      lLogin.setMallShopCode( getMallShopCodeTxt());
      //7.3.0 ST2022 修正 ここから
      //lLogin.reset(lConnection);
      lLogin.reset(lConnection,getAdminNameTxt());
      //7.3.0 ST2022 修正 ここまで
      //7.3.0 ST2028 修正 ここから
      // ログインエラーの日付と回数を確認 - Web.Xmlの設定値が0以下の場合は機能させない
      if ( lLogin.getLoginErrDate() != null 
           && lLogin.getLoginErrDateEdit().equals(strNowDateTime.substring(0,10))
           && Integer.parseInt(lLogin.getLoginErrCount()) >= SIConfig.LoginCount
           && SIConfig.LoginCount > 0 ) {
        errors.addError(new SICustomError("manager.app.invalid.login"));
      } else {
        if (lLogin.getAdminName().equals(getAdminNameTxt())&&lLogin.getAdminPass().equals(getAdminPassTxt())){
        }else{
          //テーブル名設定
          siRec = new SIModifyRec("MNGUSERMTBL");
          //キー項目設定
          siRec.addCondition("MallShopCode",lLogin.getMallShopCode());
          siRec.addCondition("UserCode",lLogin.getUserCode());
          //更新内容設定 日付が本日でない場合は１にする、それ以外は＋１
          if( lLogin.getLoginErrDateEdit().equals(strNowDateTime.substring(0,10)) ) {
            siRec.add("LOGINERRORCOUNT",new SISpcType(" LOGINERRORCOUNT + 1 "));
          } else {
            siRec.add("LOGINERRORCOUNT",1);
          }
          if (SIDBMultiConf.SIDB_CURRENT_INX == SIDBMultiConf.SIDB_POSTGRESQL_INX){
            siRec.add("LOGINERRORDATETIME", strNowDateTime);
          } else {
            siRec.add("LOGINERRORDATETIME", new SISpcType("TO_DATE('" + strNowDateTime + "','YYYY/MM/DD HH24:MI:SS') ") );
          }
          
          //更新実行
          try {
            siRec.execute(lConnection);
            lConnection.commit();
          } catch ( SIDuplicateKeyException siDKExp) {
            siDKExp.printStackTrace();
          } catch ( SIDBAccessException siDAExp) {
            siDAExp.printStackTrace();
          } catch ( SQLException SQLExp) {
          	SQLExp.printStackTrace();
          }

          errors.addError(new SICustomError("manager.app.invalid.login"));
        }
      }
      //7.3.0 ST2028 修正 ここまで
    }

    if (!errors.isEmpty()){//エラーがあれば、ユーザーログイン情報の廃棄
      session.removeAttribute(SIConfig.SISESSION_MAN_LOGIN_NAME);
      lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY,errors);
    }else {//認証OKだったら、ログインユーザ情報をセッションに設定します。
      session.setAttribute(SIConfig.SISESSION_MAN_LOGIN_NAME,lLogin);
      lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
      //7.3.0 ST2028 追加  ここから　　ログインエラー回数、エラー日付のクリア
      //テーブル名設定
      siRec = new SIModifyRec("MNGUSERMTBL");
      //キー項目設定
      siRec.addCondition("MallShopCode",lLogin.getMallShopCode());
      siRec.addCondition("UserCode",lLogin.getUserCode());
      //更新内容設定
      siRec.add("LOGINERRORCOUNT",0 );
      siRec.add("LOGINERRORDATETIME", new SISpcType("NULL") );
          
      //更新実行
      try {
        siRec.execute(lConnection);
        lConnection.commit();
      } catch ( SIDuplicateKeyException siDKExp) {
        siDKExp.printStackTrace();
      } catch ( SIDBAccessException siDAExp) {
        siDAExp.printStackTrace();
      } catch ( SQLException SQLExp) {
        SQLExp.printStackTrace();
      }
      //7.3.0 ST2028 追加　ここまで
    }

    return errors.isEmpty();
  }
}
