/* 
    ETRI IntraPMS Project
    전자 결재 초기 메뉴를 위한Servlet

    
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.etri.groupware.*;
import com.etri.groupware.eda.*;

public class EDAStartPanelServlet extends HttpServlet{

	GroupwareControllerInterface groupwareController = null;
	EDAControllerInterface edaController = null;
	Properties pro=null;
			
	public void init( ServletConfig sc ) throws ServletException{
		super.init(sc);
		
		pro = new Properties();
		pro.put("DriverName","weblogic.jdbc.mssqlserver4.Driver");
		pro.put("DBServer","jdbc:weblogic:mssqlserver4:GWDB@Server:1433");
		pro.put("User","sa");
		pro.put("Password","");
		
		pro.put("OrganizationDBManagerClassName","PandaSoftOrganizationDBManager");
		pro.put("BBSDBManagerClassName","PandaSoftBBSDBManager");
		pro.put("EDADBManagerClassName","PandaSoftEDADBManager");
		pro.put("MailDBManagerClassName","PandaSoftMailDBManager");
		
		try{
			groupwareController = GroupwareController.init( pro );
			System.out.println("그룹웨어 컨트롤러 생성을 마쳤습니다.");
			
			edaController = groupwareController.createEDAController();
			System.out.println("전자결재 컨트롤러 생성도 무사히 마쳤습니다.");	
		}
		catch( Exception e){
			System.out.println( e.toString() );
		}
	}

	public void destroy(){
		System.out.println("EDAStartPanelServlet Destroy() 호출");
		super.destroy();
		DBConnectionManager.instance.release();
		System.out.println("EDAStartPanelServlet Destroy() 완료");
	}

/*----------------------------------------------------------------- */
/**
 [메소드이름]: doGet<br>
 [기능]: 전자 결재 초기 화면을 보여준다.<br>
 [파라미터]: HttpServletRequest req,HttpServletResponse res<br>
 [호출자] : 사용자<br>
 */
/*--------------------------------------------------------------- */	
	
	public void doGet(HttpServletRequest req,HttpServletResponse res) 
	 throws ServletException,IOException{
	 	System.out.println("GET 함수 시작");
	 	
	 	HttpSession session = req.getSession(true);
	 	
		res.setContentType("text/html;charset=edu-kr");
		PrintWriter out = new PrintWriter( new OutputStreamWriter(
		 res.getOutputStream(),"EUC-KR"));
		
		boolean authentication = false;	
		
		String id = null ;
		String name = null;		

		if((session.isNew())){
			session.invalidate();
			res.sendRedirect("../Login.html");
		}
		else if(session.getValue("Authentication").equals("true")){
			authentication = true;
			id = (String)session.getValue("Id");
			name = (String)session.getValue("Name");
		}
		else{
			session.invalidate();
			res.sendRedirect("../Login.html");
		}
		
		if( authentication ){

			out.println("<html><head>");
			out.println("<FORM METHOD = GET  name=\"form1\" action=\"/servlet/EDADocumentServlet\">");
			out.println("<body  text=\"black\""+
			 "link=\"blue\" vlink=\"purple\" alink=\"red\">");
			out.println("<p align=right><img src=\'../image/우측_기안결재.gif\'></p>");					
			try{
				ApprovalDocument[] documents;

				documents = edaController.getStartPanel( name + "(" + id + ")" );

				if( documents.length > 0 ){
					List ingBox = new ArrayList();
					List completeBox = new ArrayList();
					List rejectBox = new ArrayList()	;
					for( int i = 0 ; i < documents.length ; i ++ ){
						String listID = documents[i].getlistID();
						if( listID.equals( Lists.INGBOX ))
							ingBox.add ( documents[i] );
						else if ( listID.equals( Lists.COMPLETEBOX ) )
							completeBox.add ( documents[i] );
						else if ( listID.equals( Lists.REJECTBOX ) )
							rejectBox.add ( documents[i] );
					}

					out.println("<p align=\"center\"><font size=\"4\" color=\"gray\">"+
					 "<b>전자 결재 초기메뉴</b></font></p>");
					out.println("<p align=\"center\"><hr width=600><br>");					 
					if( ingBox.size() > 0 ){
						out.println("<div align=\"center\">");
						out.println("<table border=\"0\" cellpadding=\"0\" "+
						 "cellspacing=\"1\" align=\"center\">");
						out.println("<tr>");
						out.println("<td width=\"191\" rowspan=\"" + (ingBox.size() + 1) + "\">"+
						 "<p align=\"center\"><font size=\"3\" color=\"gray\"><b>결재 해야 될 문서</b></font></p>");
						out.println("<p align=\"center\"><font size=1 color=black><b>개수=" + ingBox.size() + "</b></font></p></td>");
 						 						
						out.println("<td width=\"300\" bgcolor=#c7e4ff><p align=\"center\">"+
						 "<font face=\"굴림체\" size=\"2\" color=\"black\"><b>제 목</b></font></p></td>");
						out.println("<td width=\"100\" bgcolor=#c7e4ff><p align=\"center\">"+
						 "<font face=\"굴림체\" size=\"2\" color=\"black\"><b>작성자</b></font></p></td>");
						
						out.println("</tr>");
				    		
					    	String docIDS = ((ApprovalDocument)ingBox.get(0)).getdocID();
					    	for( int i = 1 ; i < ingBox.size() ; i++ )
					    		docIDS = docIDS + "," + ((ApprovalDocument)ingBox.get(i)).getdocID();
				    						    		
				    		for ( int i = 0 ; i < ingBox.size() ; i ++ ){
				    			out.println("<tr>");
				    			ApprovalDocument document = (ApprovalDocument)ingBox.get(i);

					        	System.out.println("문서 Link 기록 중");
				        		out.println("<td width=300 align=\"center\" >"+
				       			 "<p align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"http:/servlet/EDADocumentServlet?count=1"+
				       			 "&listID="+Lists.INGBOX+"&DocID="+document.getdocID()+ /*여기서부터는 debug용 */"&id=" + id + "&name=" + name +
			       				 "&Search=NO&DocIDS="+docIDS+"&position="+i+"&length="+ingBox.size()+"\">" +
			       				 "<font size=2>" + document.gettitle() +"</font></a></p></td>");

						 	System.out.println("작성자 기록 중 ");
						 	StringTokenizer initPerson = new StringTokenizer(document.getinitPersonID(),"(");
							System.out.println("작성자:" + document.getinitPersonID());
			    				
			    				
			    				out.println("<td width=100 align=\"right\" >"+
			        		 	"<p align=\"center\"><font size=2>"+
			        		 	initPerson.nextToken() + "</font></td>");
			        		 	out.println("</tr>");
						}
						out.println("</tr></table>");
						out.println("</div>");
						out.println("<p align=\"center\"><hr width=\"600\"><br></p>");				
						
				    	} 
					if( completeBox.size() > 0 ){
						out.println("<div align=\"center\">");
						out.println("<table border=\"0\" cellpadding=\"0\" "+
						 "cellspacing=\"1\" align=\"center\">");
						out.println("<tr>");
						out.println("<td width=\"191\" rowspan=\"" + (completeBox.size() + 1) + "\">"+
						 "<p align=\"center\"><font size=\"3\" color=\"gray\"><b>새로 결재된 결재 문서</b></font></p>");
						out.println("<p align=\"center\"><font size=1 color=black><b>개수=" + completeBox.size() + "</b></font></p></td>");
 						 						
						out.println("<td width=\"300\" bgcolor=#c7e4ff><p align=\"center\">"+
						 "<font face=\"굴림체\" size=\"2\" color=\"black\"><b>제 목</b></font></p></td>");
						out.println("<td width=\"100\" bgcolor=#c7e4ff><p align=\"center\">"+
						 "<font face=\"굴림체\" size=\"2\" color=\"black\"><b>완료일</b></font></p></td>");
						out.println("</tr>");
					    	
					    	String docIDS = ((ApprovalDocument)completeBox.get(0)).getdocID();
					    	for( int i = 1 ; i < completeBox.size() ; i++ )
					    		docIDS = docIDS + "," + ((ApprovalDocument)completeBox.get(i)).getdocID();
				    		
				    		for ( int i = 0 ; i < completeBox.size() ; i ++ ){
				    			ApprovalDocument document = (ApprovalDocument)completeBox.get(i);
				    						 	
					        	System.out.println("문서 Link 기록 중");
				        		out.println("<td width=300 align=\"center\" >"+
				       			 "<p align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"http:/servlet/EDADocumentServlet?count=1"+
				       			 "&listID="+Lists.COMPLETEBOX+"&DocID="+document.getdocID()+ 
			       				 "&Search=NO&DocIDS="+docIDS+"&position="+i+"&length="+completeBox.size()+"\">" +
			       				 "<font size=2>" + document.gettitle() +"</font></a></p></td>");
	
							System.out.println("종료일 기록 중");
							document.setendDay();
							System.out.println("종료일 :"+ document.getendDay());
							out.println("<td width=100 align=\"center\" >"+
						 	 "<p align=\"center\"><font size=2>"+
						 	 (document.getendDay()).substring(0,10) +"</font></td>");			
							out.println("</tr>");
						}
						out.println("</tr></table>");
						out.println("</div>");
						out.println("<p align=\"center\"><hr width=\"600\"><br></p>");
				    	} 
					if( rejectBox.size() > 0 ){
						out.println("<div align=\"center\">");
						out.println("<table border=\"0\" cellpadding=\"0\" "+
						 "cellspacing=\"1\" align=\"center\">");
						out.println("<tr>");
						out.println("<td width=\"191\" rowspan=\"" + (rejectBox.size() + 1) + "\">"+
						 "<p align=\"center\"><font size=\"3\" color=\"gray\"><b>새로 반려된 결재 문서</b></font></p>");
						out.println("<p align=\"center\"><font size=1 color=black><b>개수=" + rejectBox.size() + "</b></font></p></td>");
 						 						
						out.println("<td width=\"300\" bgcolor=#c7e4ff><p align=\"center\">"+
						 "<font face=\"굴림체\" size=\"2\" color=\"black\"><b>제 목</b></font></p></td>");
						out.println("<td width=\"100\" bgcolor=#c7e4ff><p align=\"center\">"+
						 "<font face=\"굴림체\" size=\"2\" color=\"black\"><b>반려자</b></font></p></td>");
						out.println("</tr>");
					    	
					    	String docIDS = ((ApprovalDocument)rejectBox.get(0)).getdocID();
					    	for( int i = 1 ; i < rejectBox.size() ; i++ )
					    		docIDS = docIDS + "," + ((ApprovalDocument)rejectBox.get(i)).getdocID();
				    		
				    		for ( int i = 0 ; i < rejectBox.size() ; i ++ ){
				    			ApprovalDocument document = (ApprovalDocument)rejectBox.get(i);
				  						 	
			    				out.println("<tr>");
			  
					        	System.out.println("문서 Link 기록 중");
				        		out.println("<td width=300 align=\"center\" >"+
				       			 "<p align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"http:/servlet/EDADocumentServlet?count=1"+
				       			 "&listID="+Lists.REJECTBOX+"&DocID="+document.getdocID()+ /*여기서부터는 debug용 */"&id=" + id + "&name=" + name +
			       				 "&Search=NO&DocIDS="+docIDS+"&position="+i+"&length="+rejectBox.size()+"\">" +
			       				 "<font size=2>" + document.gettitle() +"</font></a></p></td>");
	
			
							System.out.println("반려자 기록 중");
							document.setreturnPerson();
							System.out.println("반려자 작성 중 .반려자 :"+ document.getreturnPerson());
							StringTokenizer rejectPerson = new StringTokenizer( document.getreturnPerson() ,"(");
							out.println("<td width=100 align=\"center\" >"+
							 "<p align=\"center\"><font size=2>"+
							 rejectPerson.nextToken() + "</font></td>");
							out.println("</tr>");
						}
						out.println("</tr></table>");
						out.println("</div>");
						out.println("<p align=\"center\"><hr width=\"600\"><br></p>");
				    	} 
				}
				else { 
					out.println("<p align=\"center\"><font size=\"4\" color=\"gray\">"+
					 "<b>전자 결재 초기메뉴</b></font></p>");
					out.println("<p align=\"center\"><hr width=600><br></p>");
					out.println("<p align=\"center\">새로 작업된 문서가 없습니다.</p>");						 						
				}
				//System.out.println("HTML작성완료");//debug
				out.println("</p>");
				out.println("</body>");
				out.println("</form>");
				out.println("</html>");
			}
			catch( Exception e){
				out.println("<p align=\"center\"><font size=\"4\" color=\"gray\">"+
				 "<b>전자 결재 초기메뉴</b></font></p>");
				out.println("<p align=\"center\"><hr width=600><br>");
				out.println("<p align=\"center\">문서함을 검색하는 중 에러가 발생했습니다.<br>");
				out.println("<p align=\"center\">" + e.toString() +"<br>");
			}				
		}
		else{
			//사용자 인증에 실패했습니다.
			res.sendRedirect("../Login.html");
		}		
		out.close();
		
	}
}