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

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.naming.NamingException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jp.co.sint.config.SIConfig;
import jp.co.sint.database.SIDBAccessException;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.database.SIDatabaseConnection;
import jp.co.sint.database.SIDuplicateKeyException;
import jp.co.sint.database.SIModifyRec;
import jp.co.sint.tools.SIUtil;
import jp.co.sint.tools.SIURLMap;

/**
 * @version $Id: SIUserInfo.java,v 1.0 2003/10/07 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/07 10:22:38  Original
 */
public class SIUserInfo extends SICust{

  public boolean isLogin(HttpServletRequest lRequest){
    String logsess = (String)lRequest.getAttribute("LogSess");
    if(logsess == "invalid") return true;

    HttpSession session = lRequest.getSession(true);// セッションの取得
    SIDatabaseConnection databaseConnection = new SIDatabaseConnection();// DBへのコネクションの作成  
    String sid = "";
    
    // 携帯の場合
    if(SIURLMap.isMobile(lRequest)){
      return !SIUtil.isNull(getCustName());
    }
    // DBセッションの有無
    sid = SILoginSession.checkSession(lRequest);
    if (sid == "") return false;
    
    // Java側のセッション切れの場合再書き込み
    if (SIUtil.isNull(getCustName())) {
      try {
        Connection lConnection = databaseConnection.getConnection();
        String custcode = SIDBUtil.getFirstData(lConnection, "SELECT custcode FROM loginsessiontbl WHERE sessionid="+SIDBUtil.SQL2Str(sid) + " AND delflg = 0");
        String email    = SIDBUtil.getFirstData(lConnection, "SELECT email FROM custtbl WHERE custcode = " + SIDBUtil.SQL2Str(custcode));
        this.setFlg("0");//ログイン処理を行うときのみセッションの最終ログイン日時を更新するため
        this.reset(lConnection,email);
        session.setAttribute(SIConfig.SISESSION_USERINFO_NAME,this);
      } catch (SQLException e) {
        e.printStackTrace();
      } catch (NamingException e) {
        e.printStackTrace();
      } catch (SIDBAccessException e) {
        e.printStackTrace();
      } finally {
        databaseConnection.close();
      }      
    }
    lRequest.setAttribute("LogSess","invalid");
    
    return true;
  }
  public boolean isNotLogin(HttpServletRequest lRequest){
    return !isLogin(lRequest);
  }
}
