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

import java.util.*;

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

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

/**
 *  그룹웨어 게시판 트리를 보여주는 부분.
 */
public class BBSTreeServlet extends HttpServlet
{
		GroupwareControllerInterface groupwareController=null;
	BBSControllerInterface bbsController = null;
	ResultStatus bbsResultStatus = null;
	Properties pro=null;
	
	public void init(ServletConfig sc) throws ServletException
	{
		super.init(sc);
		
		//그룹웨어 컨트롤러를 생성한다.
		//먼저 프라퍼티를 생성한다.
		pro = new Properties();
		
		//DB매너저 구현 클래스 이름 설정
		pro.put("OrganizationDBManagerClassName", "PandaSoftOrganizationDBManager");
		pro.put("BBSDBManagerClassName", "PandaSoftBBSDBManager");
		pro.put("EDADBManagerClassName", "PandaSoftEDADBManager");
		pro.put("MailDBManagerClassName", "PandaSoftMailDBManger");
		
		try
		{
			groupwareController = GroupwareController.init( pro );
			System.out.println("그룹웨어 컨트롤러 생성을 마쳤습니다.");
			
			//게시판 관리 컨트롤러를 가져온다.
			bbsController = groupwareController.createBBSController();
			System.out.println("게시판 컨트롤러 생성도 무사히 마쳤습니다.");
		}
		catch(Exception e)
		{
			System.out.println( e.toString() );			
		}
	}

	public void destroy()
	{
		super.destroy();
		DBConnectionManager.instance.release();
		System.out.println("destory 끝...");
	}
	
	/*----------------------------------------------------------------- */
	/**
	 [메소드이름]: doGet<br>
	 [기능]: 게시판 트리와 동아리 트리, Option 설정, 게시판관리, 동아리관리등을 보여주는 서블릿<br>
	 [파라미터]: HttpServletRequest req, HttpServletResponse res<br>
	 [호출자] : 사용자<br>
	 */
	/*--------------------------------------------------------------- */		
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
	{
		HttpSession session = req.getSession(true);
		
		boolean authentication = false ;
		String kind = null ;
		String id = null ;
		String name = null ;
		String department = null;
		String team = null;
		
		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");          
            department = (String)session.getValue("Department");
            team = (String)session.getValue("Team");
       }
       else {
            session.invalidate();
            res.sendRedirect("../Login.html");
       }
       
       try
       {
			res.setContentType("text/html;charset=euc-kr");
			// 캐쉬를 못하도록..
			res.setHeader("cache-control", "no-cache");			
			PrintWriter out = new PrintWriter ( new OutputStreamWriter ( res.getOutputStream ( ) , "EUC-KR" ) ) ;
			
			String	title = "BBS Tree!!!" ;
			String companyName = "팬더 소프트" ; 
			String[][][] bbsTree = bbsController.getBBStree ( ) ;
			System.out.println("bbsTree 끝.");
								
			out.println ( "<HTML><HEAD><TITLE>" ) ;
			out.println ( title ) ;
			out.println ( "</TITLE>");
			out.println("</HEAD>" ) ;
			out.println("");
			
			out.println("<basefont size=2>");
			
			out.println( "<body bgcolor=white text=\"#0000FF\" link=\"#0000FF\" alink=\"#FF2020\" vlink=\"#000000\">" ) ;
	
			out.println ( "<div style=\"position:relative; left:5; top:5; width:90%; height=+10px;\" id=\"DivRule\"></div>" ) ;
			
			out.println ( "<div id=\"Out000\" style=\"position:relative; left:15; top:+5px; width:90%;\">" ) ;
			// option 설정하는 곳으로 가기..
			out.println ( "<p><image src='../image/BBS_only.gif'>   <a href=\"/servlet/BBSOptionPropertyServlet\" target=\"detail\">Option</a></p>" ) ;
			
			if ( kind.equals("user") )
			{
				// 회사, 부서, 팀게시판
				System.out.println("회사게시판을 보여준다.");						
			
				out.println ( "<p><image  name=\"imgCom\" src='../image/BBS_hidden.jpg'>" ) ;
				out.println ( "<u id=\"OutCom\" class=\"Outline\" style=\"cursor: hand; \">게시판</u></p>" ) ;
				out.println ( "<div id=\"OutComdetails\" style=\"display:None; position:relative; left:15;\">" ) ;
				
				for ( int i = 0 ; !( bbsTree[i][0][0].equals("null") ) ; i++ )
				{
					if ( bbsTree[i][0][0].equals("회사") && bbsTree[i][1][0].equals("팀") )
					{
						for ( int  k = 1; !(bbsTree[i][1][k].equals("null") ) ; k++)
							out.println ( "<image src='../image/BBS_only.gif'>&nbsp;<a href=\"/servlet/BBSListServlet?boardName=" + bbsTree[i][1][k]  + "\" target=\"detail\">" + bbsTree[i][1][k].substring( 3 ) + "</a><br><br>" ) ;
					}
				}
				
				for ( int i = 0 ; !( bbsTree[i][0][0].equals("null") ) ; i++ )
				{			
					if ( bbsTree[i][0][0].equals( department ) )
					{							
						System.out.println( bbsTree[i][0][0] + " 부의 게시판트리입니다.");
						if ( bbsTree[i][1][0].equals("null") ) 
							out.println ( "<p><image src='../image/BBS_only.gif'>   <a>" + bbsTree[i][0][0] + "</a></p>" ) ; 
						else
						{
							out.println ( "<image  name=\"img" + i+ "00\" src='../image/BBS_hidden.jpg'>   ");
							out.println ( "<u id=\"Out"+ i +"00\" class=\"Outline\" style=\"cursor: hand; \">" + bbsTree[i][0][0] + "</u></p>" ) ;
						}
						
						out.println ( "<div id=\"Out"+i+"00details\" style=\"display:None; position:relative; left:15;\">" ) ;
						
						for ( int j = 1 ; !(bbsTree[i][j][0].equals("null") ) ; j++)
						{						
							System.out.println( bbsTree[i][j][0] + " 팀의 게시판트리입니다.");
	
							if ( bbsTree[i][j][0].equals( "팀" ) )
							{	
								int k = 1;
								for ( k = 1 ; !(bbsTree[i][j][k].equals("null") ) ; k++)
								{
									String test = bbsTree[i][j][k];
									int temp = test .indexOf("팀");
									String depName= bbsTree[i][j][k].substring( temp+1, test.length() );
													
									out.println ( "<image src='../image/BBS_only.gif'>&nbsp;<a href=\"/servlet/BBSListServlet?boardName=" + bbsTree[i][j][k]  + "\" target=\"detail\">" + depName + "</a><br><br>" ) ;
								}					
							}
						}
						
						for ( int j = 1 ; !(bbsTree[i][j][0].equals("null") ) ; j++)
						{						
							System.out.println( bbsTree[i][j][0] + " 팀의 게시판트리입니다.");
							
							String Team = team;
							if ( team.equals("null") )
								Team = bbsTree[i][j][0];
							
							if ( bbsTree[i][j][0].equals( Team ) && !bbsTree[i][j][0].equals("팀") )
							{
								if ( bbsTree[i][j][1].equals("null") ) 
									out.println ( "<p>       <image src='../image/BBS_only.gif' >   <a>" + bbsTree[i][j][0] + "</a>" ) ;
								else 
								{		
									out.println ( "<p><image  name=\"img" + i+""+j + "0\" src='../image/BBS_hidden.jpg'>") ;
									out.println ( "<a><u id=\"Out" + i+""+j + "0\" class=\"Outline\" style=\"cursor: hand; \">" + bbsTree[i][j][0] + "</u></a><br><br>" ) ;
								}
								
								int k = 1;			
								for ( k = 1; ! (bbsTree[i][j][k].equals("null") ); k++)
								{
									System.out.println( bbsTree[i][j][k] + "  게시판입니다.");
									
									if ( k == 1 ) 
										out.println ( "<div id=\"Out" + i + "" + j + "0details\" style=\"display:None; position:relative; left:20;\">" ) ;
									
									String test = bbsTree[i][j][k];
									int temp = test .lastIndexOf("팀");
									String teamName= bbsTree[i][j][k].substring( temp+1, test.length() );
									
									out.println ( "<image src='../image/BBS_only.gif'>&nbsp;<a href=\"/servlet/BBSListServlet?boardName=" + bbsTree[i][j][k]  + "&mode=list\" target=\"detail\">" + teamName + "</a><br><br>" ) ;
								}	// k
								if ( k != 1 ) 
									out.println ( "</div>" ) ;							
							}
						}	// j
						out.println("</div>");
	
					}	// if end.
						
				}	// i			
				out.println("</div>");
				
				
				// 동아리 게시판
				String[] club= bbsController.getClub( id );
				if ( club != null )
				{
					for ( int i = 0 ; !( bbsTree[i][0][0].equals("null") ) ; i++ )
					{
						if ( bbsTree[i][0][0].equals("동아리") )
						{
							out.println ( "<p><image  name=\"imgClub\" src='../image/BBS_hidden.jpg' >" ) ;
							out.println ( "<u id=\"OutClub\" class=\"Outline\" style=\"cursor: hand; \">동아리</u></p>" ) ;					
							
							out.println ( "<div id=\"OutClubdetails\" style=\"display:None; position:relative; left:12;\">" ) ;
							for (int no=0; no < club.length; no++)
							{
								for ( int  k = 1; !(bbsTree[i][1][k].equals("null") ) ; k++)
								{
									String clubName = bbsTree[i][1][k].substring(4);
									if ( clubName.equals( club[no] ) ) 
										out.println ( "<image src='../image/BBS_only.gif' >&nbsp;<a href=\"/servlet/BBSListServlet?boardName=" + bbsTree[i][1][k]  + "\" target=\"detail\">" + bbsTree[i][1][k].substring( 4 ) + "</a><br><br>" ) ;
								}
							}
							
							out.println ( "</div>" ) ;
						}	
					}
				}
							
				out.println("</div>");
			}
			
			else if ( kind.equals("manager") )
			{
				// 회사, 부서, 팀게시판
				System.out.println("회사게시판을 보여준다.");						
				
				out.println ( "<p><image  name=\"imgCom\" src='../image/BBS_hidden.jpg'>" ) ;
				out.println ( "<u id=\"OutCom\" class=\"Outline\" style=\"cursor: hand; \">게시판</u></p>" ) ;
				out.println ( "<div id=\"OutComdetails\" style=\"display:None; position:relative; left:12;\">" ) ;
				
				for ( int i = 0 ; !( bbsTree[i][0][0].equals("null") ) ; i++ )
				{
					// 회사 게시판
					if ( bbsTree[i][0][0].equals("회사") &&  bbsTree[i][1][0].equals( "팀" ) )
					{
						for (int  k = 1  ; !(bbsTree[i][1][k].equals("null") ) ; k++)
							out.println ( "<image src='../image/BBS_only.gif' >&nbsp;<a href=\"/servlet/BBSListServlet?boardName=" + bbsTree[i][1][k]  + "\" target=\"detail\">" + bbsTree[i][1][k].substring( 3 ) + "</a><br><br>" ) ;
						break;
					}
				}
							
				for ( int i = 0 ; !( bbsTree[i][0][0].equals("null") ) ; i++ )
				{			
					if ( !(bbsTree[i][0][0].equals("회사") | bbsTree[i][0][0].equals("동아리") ) )
					{							
						System.out.println( bbsTree[i][0][0] + " 부의 게시판트리입니다.");
						if ( bbsTree[i][1][0].equals("null") ) 
							out.println ( "<p><image src='../image/BBS_only.gif'>   <a>" + bbsTree[i][0][0] + "</a></p>" ) ; 
						else
							out.println ( "<image  name=\"img" +i +"00\" src='../image/BBS_hidden.jpg'>   <u id=\"Out"+ i +"00\" class=\"Outline\" style=\"cursor: hand; \">" + bbsTree[i][0][0] + "</u></p>" ) ;
						
						out.println ( "<div id=\"Out"+i+"00details\" style=\"display:None; position:relative; left:12;\">" ) ;
						
						for ( int j = 1 ; !(bbsTree[i][j][0].equals("null") ) ; j++)
						{						
							System.out.println( bbsTree[i][j][0] + " 팀의 게시판트리입니다.");
	
							if ( bbsTree[i][j][0].equals( "팀" ) )
							{	
								int k = 1;
								for ( k = 1 ; !(bbsTree[i][j][k].equals("null") ) ; k++)
								{		
									String test = bbsTree[i][j][k];
									int temp = test .indexOf("팀");
									String depName= bbsTree[i][j][k].substring( temp+1, test.length() );
													
									out.println ( "<image src='../image/BBS_only.gif'>&nbsp;<a href=\"/servlet/BBSListServlet?boardName=" + bbsTree[i][j][k]  + "\" target=\"detail\">" + depName + "</a><br><br>" ) ;
								}					
							}
						}
						
						for ( int j = 1 ; !(bbsTree[i][j][0].equals("null") ) ; j++)
						{						
							System.out.println( bbsTree[i][j][0] + " 팀의 게시판트리입니다.");
							
							if ( !bbsTree[i][j][0].equals( "팀" ) )
							{
								if ( bbsTree[i][j][1].equals("null") ) 
									out.println ( "<p>       <image src='../image/BBS_only.gif width=8 height=4 align=center>   <a>" + bbsTree[i][j][0] + "</a>" ) ;
								else 
								{		
									out.println ( "<p><image  name=\"img" + i+""+j + "0\" src='../image/BBS_hidden.jpg' >" ) ;
									out.println ( "<a><u id=\"Out" + i+""+j + "0\" class=\"Outline\" style=\"cursor: hand; \">" + bbsTree[i][j][0] + "</u></a><br><br>" ) ;
								}
								
								int k = 1;			
								for ( k = 1; ! (bbsTree[i][j][k].equals("null") ); k++)
								{
									System.out.println( bbsTree[i][j][k] + "  게시판입니다.");
									
									if ( k == 1 ) 
										out.println ( "<div id=\"Out" + i + "" + j + "0details\" style=\"display:None; position:relative; left:12;\">" ) ;
									
									String test = bbsTree[i][j][k];
									int temp = test .lastIndexOf("팀");
									String teamName= bbsTree[i][j][k].substring( temp+1, test.length() );
									
									out.println ( "<image src='../image/BBS_only.gif'>&nbsp;<a href=\"/servlet/BBSListServlet?boardName=" + bbsTree[i][j][k]  + "&mode=list\" target=\"detail\">" + teamName + "</a><br><br>" ) ;
								}	// k
								if ( k != 1 ) 
									out.println ( "</div>" ) ;							
							}
						}	// j
						out.println("</div>");
	
					}	// if end.
						
				}	// i
				out.println("</div>");
				
				for ( int i = 0 ; !( bbsTree[i][0][0].equals("null") ) ; i++ )
				{					
					// 동아리 게시판
					if ( bbsTree[i][0][0].equals("동아리")  && bbsTree[i][1][0].equals( "팀" ) )
					{
						out.println ( "<p><image name=\"imgClub\"  src='../image/BBS_hidden.jpg'>" ) ;
						out.println ( "<u id=\"OutClub\" class=\"Outline\" style=\"cursor: hand; \">동아리</u></p>" ) ;
					
						 if ( bbsTree[i][1][0].equals( "팀" ) ) 
						 {
							int  k = 1 ;
							for ( ; !(bbsTree[i][1][k].equals("null") ) ; k++)
							{		
								if ( k == 1 ) 
									out.println ( "<div id=\"OutClubdetails\" style=\"display:None; position:relative; left:12;\">" ) ;
												
								out.println ( "<image src='../image/BBS_only.gif'>&nbsp;<a href=\"/servlet/BBSListServlet?boardName=" + bbsTree[i][1][k]  + "\" target=\"detail\">" + bbsTree[i][1][k].substring( 4 ) + "</a><br><br>" ) ;
							}					
							if ( k != 1 ) 
								out.println ( "</div>" ) ;
							break;
						}
					}	
				}
				
				out.println("<br><br>");
	
				// create board, 회사게시판, 부서/팀게시판
				out.println("<p><image  name=\"imgBoard\" src='../image/BBS_hidden.jpg'>");
				out.println("<u id=\"OutBoard\" class=\"Outline\" style=\"cursor: hand; \"> 게시판 관리 </u></p>");
				out.println("<div id=\"OutBoarddetails\" style=\"display:None; position:relative; left:12;\">");
				out.println("<img src='../image/BBS_only.gif'><a href=\"/servlet/BBSCreateBBSServlet\" target=\"detail\">생성</a><br><br>");
				out.println("<img src='../image/BBS_only.gif'><a href=\"/servlet/BBSManagerBBSServlet\" target=\"detail\">수정 및 삭제</a><br><br>");
				out.println("</div>");			
	
				// 동아리를 생성하고, 관리한다... 관리자만의 특권...
				out.println("<p><image  name=\"imgClubManager\" src='../image/BBS_hidden.jpg'>");
				out.println("<u id=\"OutClubManager\" class=\"Outline\" style=\"cursor: hand; \"> 동아리 관리 </u></p>");
				out.println("<div id=\"OutClubManagerdetails\" style=\"display:None; position:relative; left:12;\">");
				out.println("<img src='../image/BBS_only.gif'><a href=\"/servlet/ClubCreateServlet\" target=\"detail\">생성</a><br><br>");
				out.println("<img src='../image/BBS_only.gif'><a href=\"/servlet/ClubMemeberManagerServlet\" target=\"detail\">회원 관리</a><br><br>");
				out.println("<img src='../image/BBS_only.gif'><a href=\"/servlet/ClubManagerServlet\" target=\"detail\">수정 및 삭제</a><br><br>");
				out.println("</div>");			
				
				out.println("</div>");	
				
			}		
						
			System.out.println(" 자바 스크립트 ");
			
			out.println ( "<script language=\"JavaScript\">" ) ;
			
			out.println ( "function clickHandler() {");
			out.println ( "        var targetId, imgId, srcElement, targetElement, imgElement;");
			out.println ( "         srcElement = window.event.srcElement;");
			out.println ( "       if (srcElement.className == \"Outline\") {");
			out.println ( "            targetId = srcElement.id + \"details\";");
			out.println ( "            targetElement = document.all(targetId);");
			out.println ( "        	imgId = \"img\"+ srcElement.id.substring(3,srcElement.id.length) ;");
			out.println ( "        	imgElement = document.all(imgId);");
			out.println ( "    		if (targetElement.style.display == \"none\") {");
			out.println ( "			targetElement.style.display = \"\";		");
			out.println ( "				imgElement.src =\"../image/BBS_show.jpg\"; ");
			out.println ( "			} ");
			out.println ( "			else {      		      		");
			out.println ( "				targetElement.style.display = \"none\";				");
			out.println ( "				imgElement.src =\"../image/BBS_hidden.jpg\"; ");
			out.println ( "    		}");
			out.println ( "    	}");
			out.println ( "    }");
			out.println ( "    document.onclick = clickHandler;");
			
			out.println ( "</script>" ) ;
			
			out.println ( "</BODY></HTML>" ) ;
			out.close ( ) ;
			
		}
		catch(Exception e)
		{
			System.out.println( "======> BBSTreeServlet");
			System.out.println( e.toString() );
		}
		
	}

}