/* 
    IntraPMS Project
    인증 DBManager

    
*/

import java.sql.* ;

import com.etri.groupware.*;
import com.etri.groupware.authentication.*;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import sun.misc.*;
import java.util.*;

public class PandaSoftAuthenticationDBManager implements AuthenticationDBManagerInterface {
	// DBConnectionPool을 사용하기 위한 전역 변수들.....
	static DBConnectionManager connectionManager = null ;
	int servletInits = 0 ;
	

	/*----------------------------------------------------------------- */
	/**
	 [메소드이름]: init<br>
	 [기능]: DBConnectionManager를 가져온다.<br>
	 [파라미터]: 없음<br>
	 [호출자] : OrganizationManagementController<br>
	 */
	/*--------------------------------------------------------------- */  
	public void init(){
		connectionManager = DBConnectionManager.getInstance();
	}

	
	/*----------------------------------------------------------------- */
	/**
	 [메소드이름]: loginUser<br>
	 [기능]: 접속한 사람이 로그인할 권한이 있는 없는지 검사한다.<br>
	 [파라미터]: String id , String password<br>
	 [호출자] : LogInServlet<br>
	 */
	/*--------------------------------------------------------------- */  	
	public LoginResultStatus loginUser ( String id , String password ) {

		
		Connection con = null ;
		Statement stmt = null ;		
		LoginResultStatus loginResultStatus = null;
		try{
		      con = connectionManager.getConnection("access");
		      System.out.println ( "커넥션 풀 객체로부터 커넥션을 얻어냈습니다." ) ;
		      
		      stmt = con.createStatement (); 
		      System.out.println ( "커넥션으로부터 스테이트먼트를 얻어냈습니다." ) ;
		      
		      String query = "select * from official where id='" + id + "' and password='" + password + "'";
		      System.out.println ( "만들어진 쿼리는" + query ) ;
		      
		      ResultSet rs = stmt.executeQuery(query);
		      
		      boolean result = rs.next();
		      System.out.println("result = " + result); 
		      if(result == true){
		      	  
		          loginResultStatus = new LoginResultStatus(LoginResultStatus.SUCCESS);
		      
		          String firstName = rs.getString("firstName");
		          String lastName = rs.getString("familyName");
		          loginResultStatus.userName = lastName + firstName;
		          loginResultStatus.department = rs.getString("department");
		          loginResultStatus.team = rs.getString("team");
		          
		          
		          
		          int state = rs.getInt("userKind");
		          if(state == 0){
		      	      loginResultStatus.userKind = "user";
		          }else{
		              loginResultStatus.userKind = "manager";
		          }
		          
		      }else{
		          loginResultStatus = new LoginResultStatus(LoginResultStatus.FAILURE);
			// 아이디가 존재하지 않는지 비밀번호가 틀리는 지를 검사한다.
		          String idquery = "select * from official where id='" + id + "'";
		          ResultSet idrs = stmt.executeQuery(idquery);
		          boolean idresult = idrs.next();
		          if ( idresult == true ) {
		          	// 아이디가 존재합니다.
		          	loginResultStatus.userName = "id" ;
		          } else {
		          	// 아이디가 존재하지 않습니다.
		          	loginResultStatus.userName = "null" ;
		          }

		      }
		 }catch( SQLException sqle ) {
				while ( sqle != null ) {
					System.out.println ( "SQLState : " + sqle.getSQLState ( ) ) ;
					System.out.println ( "Message : " + sqle.getMessage ( ) ) ; 
					System.out.println ( "Error Code : " + sqle.getErrorCode ( ) ) ;
					sqle = sqle.getNextException ( ) ;
					System.out.println ( " " ) ;
				} ;
				connectionManager.freeConnection ("access", con ) ;  
		                loginResultStatus = new LoginResultStatus(LoginResultStatus.FAILURE,sqle.toString());
		                
		                
		 }catch( Exception e){
		 	 System.out.println(e.toString());                  
		         loginResultStatus = new LoginResultStatus(LoginResultStatus.FAILURE,e.toString());
		         
		 }finally{
			try { 
				stmt.close ( ) ; 
			} catch ( SQLException sqle ) {
				while ( sqle != null ) {
					System.out.println ( "SQLState : " + sqle.getSQLState ( ) ) ;
					System.out.println ( "Message : " + sqle.getMessage ( ) ) ; 
					System.out.println ( "Error Code : " + sqle.getErrorCode ( ) ) ;
					sqle = sqle.getNextException ( ) ;
					System.out.println ( " " ) ;
				} ;
				connectionManager.freeConnection ("access", con ) ;  
			}
			connectionManager.freeConnection ("access", con ) ;  
		}          		      
		return loginResultStatus;
	}

	

	
	
}

      