/* 
    IntraPMS Project
    로그인에 성공한 후 각 프레임들을 가지는 프레임셋 html을 보내는 서블릿

    */

import java.io.*;
import java.lang.*;

import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

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

public class MainServlet extends HttpServlet
{
	public void init ( ServletConfig config ) throws ServletException {
		super.init ( config ) ;
		System.out.println ( "Groupware IndexServlet init () Call..........." ) ;
	}
	
	public void destroy ( ) {
		System.out.println ( "Groupware IndexServlet destroy () Call..........." ) ;
	}
	
	/*----------------------------------------------------------------- */
	/**
	 [메소드이름]: doGet<br>
	 [기능]: 인증에 성공한후 각각의 프레임마다 첫 화면을 보여준다.<br>
	 [파라미터]: HttpServletRequest req , HttpServletResponse res<br>
	 [호출자] : 사용자<br>
	 */
	/*--------------------------------------------------------------- */		
	public void doGet (HttpServletRequest	req ,HttpServletResponse res)throws ServletException, IOException {
	     
	try{
		PrintWriter		out;
		String			title = "Simple Servlet Output";
		
		// set content type and other response header fields first
		res.setContentType("text/html;charset=euc-kr");
		res.setHeader("cache-control","no-cache") ;
		
		out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(),"EUC-KR"));
		
		boolean authentication = false ;
		String kind = null ;
		String id = null ;
		String name = null ;
		String loginStatus = null;
		
		HttpSession session = req.getSession(true);
		
		if ((session.isNew())) {
			session.invalidate();
			res.sendRedirect("../Login.html");
		} else if(session.getValue("Authentication").equals("true")) {
			authentication = true;
			kind = (String)session.getValue("Kind");
			id = (String)session.getValue("Id");
			name = (String)session.getValue("Name");
		} else {
			session.invalidate();
			res.sendRedirect("../Login.html");
		}
	
		/////////////////////////////////////		
		if ( authentication ) {
			// 사용자 인증이 성공하였습니다...	
			
			out.println ( "<html>" ) ;
			out.println ( "<head>" ) ;
			out.println ( "<title>팬더 소프트 ( Panda Soft ) 인트라넷 그룹웨어 </title>" ) ;
			out.println ( "</head>" ) ;
			out.println ( "<frameset rows=\"65, 14, 100%\" cols=\"1*\" border=\"0\">" ) ;
			if ( kind.equals("manager") ) {
				// 관리자인 경우
				out.println ( "    <frame src=\"/servlet/ShowMenuServlet?kind=1&name=" + name + "\" name=\"banner\" scrolling=\"auto\" marginwidth=\"1\" marginheight=\"0\" namo_target_frame=\"contents\">" ) ;
			} else {
				// 일반 사용자인 경우
				out.println ( "    <frame src=\"/servlet/ShowMenuServlet?kind=2&name=" + name + "\" name=\"banner\" scrolling=\"auto\" marginwidth=\"1\" marginheight=\"0\" namo_target_frame=\"contents\">" ) ;
			}
			out.println ( "    <frame src=\"/servlet/ShowUserKindServlet\" name=\"kind\" scrolling=\"no\" marginwidth=\"1\" marginheight=\"0\">" ) ;
			
			out.println ( "    <frameset rows=\"1*\" cols=\"17%, 83%\">" ) ;
			
			out.println ( "        <frame src=\"/tree.htm\" name=\"contents\" scrolling=\"auto\" marginwidth=\"1\" marginheight=\"0\" namo_target_frame=\"detail\">" ) ;
			
			out.println ( "        <frame src=\"/servlet/ShowStartPageServlet\" name=\"detail\" scrolling=\"auto\" marginwidth=\"0\" marginheight=\"1\">" ) ;				
						
			out.println ( "    </frameset>" ) ;
			out.println ( "    <noframes>" ) ;
			out.println ( "    <body bgcolor=\"white\" text=\"black\" link=\"blue\" vlink=\"purple\" alink=\"red\">" ) ;
			out.println ( "    <p>이 페이지를 보려면, 프레임을 볼 수 있는 브라우저가 필요합니다.</p>" ) ;
			out.println ( "</body>" ) ;
			out.println ( "</noframes>" ) ;
			out.println ( "</frameset>" ) ;
			out.println ( "</html>" ) ;
		} else {
			// 사용자 인증에 실패하였습니다.
			res.sendRedirect ( "../Login.html" ) ;
		}	
		out.close ( ) ;
	}catch(Exception e) { }
   }
}