/* 
    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 EDADocumentServlet 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("EDADocumentServlet Destroy() 호출");
		super.destroy();
		DBConnectionManager.instance.release();
		System.out.println("EDADocumentServlet Destroy() 완료");
	}

/*----------------------------------------------------------------- */
/**
 [메소드이름]: doGet<br>
 [기능]: 문서에 화면을 보여준다.<br>
 [파라미터]: HttpServletRequest req,HttpServletResponse res<br>
 [호출자] : EDADocumentBoxServlet , EDASearchServlet<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){
			String docID;
			String listID;
			String Search;
			String docIDs;
			String[] docIDArray;
			int position;
			int length;
			
			String itemSel = null; 
			String docItem = null;
			String initDay = null;
			String startYear = null;
			String startMon = null;
			String startDay = null;
			String stopYear = null;
			String stopMon = null;
			String stopDay = null;
			String personSel = null;
			String docPerson = null;
			String titleSel = null;
			String docTitle = null;
			String commSel = null;
			String docComment = null;
			String andSearch = null;
			
			ApprovalDocument document;
			int count;
			int oldCount=0;
			ApprovalCourse[] pArray;
			AttatchFile[] faArray;
			boolean rejectDoc = false;
					 
			docID = req.getParameter("DocID");
			System.out.println("docID : " + docID );

			count = Integer.parseInt(req.getParameter("count"));
			System.out.println("count : " + count );
			
			position = Integer.parseInt( req.getParameter("position"));
			System.out.println("position : " + position );
			
			length = Integer.parseInt( req.getParameter("length"));
			System.out.println("length : " + length );
			
			listID = req.getParameter("listID");
			System.out.println("listID : " + listID );
			
			docIDs = req.getParameter("DocIDS");
			System.out.println("DocIDS : " + docIDs );
			
			StringTokenizer token = new StringTokenizer ( docIDs , "," );
			docIDArray = new String [ length ];
			int loopCount = 0;
			while( token.hasMoreTokens() ){
				docIDArray[ loopCount ] = token.nextToken();
				loopCount ++;
			}
			Search = req.getParameter("Search");

			if( Search.equals("YES")){
				oldCount = Integer.parseInt( req.getParameter("oldCount"));
				itemSel = req.getParameter("itemSel");
				docItem = req.getParameter("docItem");
				initDay = req.getParameter("initDay");
				startYear = req.getParameter("startYear");
				startMon = req.getParameter("startMon");
				startDay = req.getParameter("startDay");
				stopYear = req.getParameter("stopYear");
				stopMon = req.getParameter("stopMon");
				stopDay = req.getParameter("stopDay");
				personSel = req.getParameter("personSel");
				docPerson = req.getParameter("docPerson");
				titleSel = req.getParameter("titleSel");
				docTitle = req.getParameter("docTitle");
				commSel = req.getParameter("commSel");
				docComment = req.getParameter("docComment");
				andSearch = req.getParameter("andSearch");
			}


			try{
				out.println("<html><head><title>결재 문서 </title></head>");
				out.println("<body bgcolor=\"white\" text=\"black\" link=\"blue\" vlink=\"purple\" alink=\"red\">");		
				out.println("<p align=right><img src=\'../image/우측_기안결재.gif\'></p>");
				document = edaController.getDocument( docID , name + "(" + id + ")" , listID );
				
				if( document != null ){
					document.setinitDay( (document.getinitDay()).substring(0,10));
					out.println("<p align=\"center\"><font size=\"4\" color=\"gray\" >"+
					 "<b>문서 보기</b></font></br>");
					out.println("<hr width=600></p>");
					 
					out.println("<div align=\"center\">");
					out.println("<table  cellpadding=\"2\" width=\"600\" >");
				
					out.println("<tr>");
					out.println("<td width=100 bgcolor=\"#C7E4FF\" >");
					out.println("<p align=\"center\"><font color=\"blue\">"+
					 "제&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;목</font></td>");
					out.println("<td width=500 colspan=5 >"+ document.gettitle() + "</td>");
					out.println("</tr>");
					
					out.println("<tr>");
					out.println("<td width=100 bgcolor=\"#C7E4FF\" >"+
					 "<p align=\"center\"><font color=\"blue\">문서 분류</font></td>");
					out.println("<td width=500 colspan=5>"+ docID.substring(0,4) +"</td>");
					out.println("</tr>");
					
					out.println("<tr>");
					out.println("<td width=100 bgcolor=\"#C7E4FF\">" +
					 "<p align=\"center\"><font color=\"blue\">문서 번호</font></td>");
					out.println("<td width=500 colspan=5>" +  docID + "</td>");
					out.println("</tr>");
				
					StringTokenizer initPerson = new StringTokenizer(document.getinitPersonID() ,"(");
				
					out.println("<tr>");
					out.println("<td width=100 bgcolor=\"#C7E4FF\">"+
					 "<p align=\"center\"><font color=\"blue\">기 &nbsp;안 &nbsp;자</font></td>");
					out.println("<td width=500 colspan=5>"+ initPerson.nextToken()+"</td>");
					out.println("</tr>");
					
					out.println("<td width=100 bgcolor=\"#C7E4FF\">"+
					 "<p align=\"center\"><font color=\"blue\">작성 일자</font></td>");
					out.println("<td width=500 colspan=5>"+
					 document.getinitDay() +"</td>");
					out.println("</tr>");
		
					out.println("<tr>");
					out.println("<td width=100 bgcolor=\"#C7E4FF\">"+
					 "<p align=\"center\"><font color=\"blue\">기안&nbsp;&nbsp;내용</font></td>");
					StringTokenizer st = new StringTokenizer( document.getcomment() , ";");
					out.println("<td width=500 colspan=5>");
					while(st.hasMoreTokens())
						out.println("<p align=\"left\" style=\"line-height:100%; margin-top:0; margin-bottom:0;\">"+
						 st.nextToken()+"</p>");
					out.println("</td>");
					out.println("</tr>");
		
					pArray = document.getapprovalCourse();
	
					out.println("<tr>");
					out.println("<td width=100 rowspan =\""+ ( pArray.length + 1) +
					 "\" bgcolor=\"#C7E4FF\"><p align=\"center\"><font color=\"blue\">"+
					 "결재&nbsp;상황</font></td>");
					out.println("<td width=70 bgcolor=\"#C7E4FF\">"+
					 "<p align=\"center\"><font color=\"blue\">결재자</font></td>");
					out.println("<td width=60 bgcolor=\"#C7E4FF\">"+
					 "<p align=\"center\"><font color=\"blue\">형태</font></td>");
					out.println("<td width=60 bgcolor=\"#C7E4FF\">"+
					 "<p align=\"center\"><font color=\"blue\">상황</font></td>");
					out.println("<td width=130 bgcolor=\"#C7E4FF\">"+
					 "<p align=\"center\"><font color=\"blue\">처리일</font></td>");
					out.println("<td width=180 bgcolor=\"#C7E4FF\">"+
					 "<p align=\"center\"><font color=\"blue\">결재의견</font></td>");
					out.println("</tr>");
					
					for(int i = 0 ; i<pArray.length ; i++ ){
						StringTokenizer person = new StringTokenizer( pArray[i].getpersonID(),"(");
						out.println("<tr>");
						out.println("<td width=70><p align=\"center\">"+
						 person.nextToken() + "</td>");
						if((pArray[i].getconditionCode()).equals(ApprovalCourse.ABORT)){
								out.println("<td width=60>"+
								 "<p align=\"center\">상신</td>");
								out.println("<td width=60>"+
								 "<p align=\"center\">취소</td>");
								out.println("<td width=130><p align=\"center\">"+
								 (pArray[i].getworkDate()).substring(0,10) + "</p></td>");
								out.println("<td width=180>"+
								 "&nbsp;</td>");
								 rejectDoc = true;
						}					
						else if(pArray[i].gettype()== ApprovalCourse.SUBMIT) {
								out.println("<td width=60>"+
							 	 "<p align=\"center\">상신</td>");
								out.println("<td width=60>"+
							 	 "<p align=\"center\">상신</td>");
								out.println("<td width=130><p align=\"center\">"+
							 	 document.getinitDay()+"</p></td>");
							 	out.println("<td width=180>"+
							 	 "&nbsp;</td>");
						}
						else if(pArray[i].gettype() == ApprovalCourse.SUBMITINFORM){
							out.println("<td width=60>"+
							 "<p align=\"center\">통보</td>");
							if((pArray[i].getconditionCode()).equals(ApprovalCourse.APPROVAL)){
								out.println("<td width=60>"+
								 "<p align=\"center\">전달</td>");
								out.println("<td colspan=2>"+
								 "&nbsp</td>");
							}
							else{
								out.println("<td colspan=3>"+
								 "<p align=\"center\">&nbsp</td>");
							}
						}
						else {
							out.println("<td width=60>"+
							 "<p align=\"center\">결재</td>");
							if((pArray[i].getconditionCode()).equals(ApprovalCourse.NOTCOMPLETE)){
								if( rejectDoc == true )
									out.println("<td colspan=3>"+
									"&nbsp</td>");
								else{
									out.println("<td width=60>"+
								 	 "<p align=\"center\">미결</td>");
									out.println("<td colspan=2>"+
								 	 "&nbsp</td>");
								}
							}
							else{
								if((pArray[i].getconditionCode()).equals(ApprovalCourse.REJECT)){
									out.println("<td width=60>"+
									 "<p align=\"center\">반려</td>");
									rejectDoc = true;
								}
								else
									out.println("<td width=60>"+
									 "<p align=\"center\">결재</td>");
								out.println("<td width=130><p align=\"center\">"+
								 (pArray[i].getworkDate()).substring(0,10) + "</p></td>");
								if( pArray[i].getdescription() !=null)
									out.println("<td width=180>"+
								 	 "<p align=\"left\" style=\"line-height:100%; margin-top:0; margin-bottom:0;\">"+
								 	 pArray[i].getdescription() +"</td>");
								else
									out.println("<td width=180>" +
									 "&nbsp</td>");
							}
						}
						out.println("</tr>");
					}
					faArray = document.getattatchFile();
					out.println("<tr>");
					out.println("<td width=100 rowspan="+faArray.length+" bgcolor=\"#C7E4FF\">"+
					 "<p align=\"center\"><font color=\"blue\">첨부  파일</font></td>");
					if ( faArray.length == 0 )
						out.println("<td colspan=5><p align=\"center\">"+
						 "첨부파일이 없습니다.</p></td></tr>");
					for(int i=0;i<faArray.length;i++){
						out.println("<td colspan=5><p align=\"center\">"+
						 "<a href=\"http:../"+faArray[i].getfilePath()+"/"+faArray[i].getfileName() +
						 "\">"+ (faArray[i].getfileName()).substring(9)+"</a></p></td>");
						out.println("</tr>");
					}
					if(listID.equals(Lists.INGBOX)){
						out.println("<tr>");
						out.println("<td width=100 bgcolor=\"#C7E4FF\" ><font color=\"blue\"><p align=\"center\">"+
						 "의견</p></td>");
						out.println("<td width=500 colspan=5><form method =\"GET\" name=\"form\""+
						 " action=\"/servlet/EDAApprovalServlet\">"+
						 "<p align=\"left\">" +
				        	 "<input type=text name=description size=60></td>");
				        	out.println("</tr>");
				        }
					out.println("</table>");
					out.println("</div>");
		    			
		    			out.println("<div align=\"center\">");
		    			out.println("<table width=600>");
					out.println("<tr>");
		    			if(listID.equals(Lists.SUBMITBOX)){
		    				out.println("<td width=150 height=\"16\"><form method=\"GET\" name=\"form1\""+
		    				 " action=\"/servlet/EDAApprovalServlet\">"+
		    				 "<p align=center>"+
						 "<INPUT type=submit name=\"Approval\" value=\"상신취소\"></td>");	    				
						out.println("<input type=hidden name=length VALUE=" + length + "> ");
						out.println("<input type=hidden name=docIDS VALUE=" + docIDs + "> ");
		    				out.println("<input type=hidden name=docID VALUE=" + docID + "> ");
						out.println("<input type=hidden name=listID VALUE=" + listID + "> ");
						out.println("<input type=hidden name=count VALUE=" + count + "> ");
						out.println("<input type=hidden name=doGet VALUE=\"NO\" >");
						out.println("<input type=hidden name=Search VALUE=" + Search + ">");
						if(Search.equals("YES")){
							out.println("<input type=hidden name = itemSel VALUE=" + itemSel + ">");
							out.println("<input type=hidden name = docItem VALUE=" + docItem + ">");
							out.println("<input type=hidden name = initDay VALUE=" + initDay + ">");
							out.println("<input type=hidden name = startYear VALUE=" + startYear + ">");
							out.println("<input type=hidden name = startMon VALUE=" + startMon + ">");
							out.println("<input type=hidden name = startDay VALUE=" + startDay + ">");
							out.println("<input type=hidden name = stopYear VALUE=" + stopYear + ">");
							out.println("<input type=hidden name = stopMon VALUE=" + stopMon + ">");
							out.println("<input type=hidden name = stopDay VALUE=" + stopDay + ">");
							out.println("<input type=hidden name = personSel VALUE=" + personSel + ">");
							out.println("<input type=hidden name = docPerson VALUE=" + docPerson + ">");
							out.println("<input type=hidden name = titleSel VALUE=" + titleSel + ">");
							out.println("<input type=hidden name = docTitle VALUE=" + docTitle + ">");
							out.println("<input type=hidden name = commSel VALUE=" + commSel + ">");
							out.println("<input type=hidden name = docComment VALUE=" + docComment + ">");
							out.println("<input type=hidden name = oldCount VALUE=" + oldCount + ">");
							out.println("<input type=hidden name = andSearch VALUE=" + andSearch + ">");
						
			    				out.println("<td width=150><a href=\'javascript:history.go(-1)\'>"+
			    				 "<img src=\'/image/BBS_list.gif\' border=0 alt=\'검색함으로\'></a></td>");
						}
						else	
			    				out.println("<td width=150><p align=center><a href=\"http:/servlet/EDADocumentBoxServlet?"+
			    				 "count=" + count + "&listID="+listID+"&doGet=YES\" >"+
			    				 "<img src=\'/image/BBS_list.gif\' border=0 alt=\'문서함으로\'></a></td>");
		    				
						out.println("</form>");
					}
					if(listID.equals(Lists.INGBOX)){
						out.println("</tr>");
						out.println("</table>");
						out.println("</div>");
						out.println("<div align=\"center\">");
						out.println("<table border=0>");
						out.println("<tr>");
						out.println("<td width=150 height=\"16\"> "+
						 "<p align=\"center\">"+
						 "<INPUT  type=submit name=\"Approval\" value=\"결재\"></td>");
						out.println("<td width=150 height=\"16\"><p align=\"center\">"+
						 "<INPUT type=submit name=\"Approval\" value=\"반려\"></td>");
		    			}
					if( listID.equals(Lists.COMPLETEBOX) ||
					    listID.equals(Lists.REJECTBOX) ||
					    listID.equals(Lists.INFORMBOX) ||
					    listID.equals(Lists.ABORTBOX)||
					    ( ( listID.equals(Lists.PUBLICBOX) ) &&
					     ( (document.getinitPersonID()).equals(name+"("+id+")")))){
						out.println("<td width=150 ><form method=\"GET\" name=\"form1\""+
		    				 " action=\"/servlet/EDAApprovalServlet\">"+
		    				 "<p align=center>"+
						 "<INPUT type=submit name=\"Approval\" value=\"삭제\"></td>");
					}
					if(!(listID.equals(Lists.SUBMITBOX))){
						out.println("<input type=hidden name=length VALUE=" + length + "> ");
						out.println("<input type=hidden name=docIDS VALUE=" + docIDs + "> ");
						out.println("<input type=hidden name=docID VALUE=" + docID + "> ");
						out.println("<input type=hidden name=listID VALUE=" + listID + "> ");
						out.println("<input type=hidden name=count VALUE=" + count + "> ");
						out.println("<input type=hidden name=doGet VALUE=\"NO\" >");
						out.println("<input type=hidden name=Search VALUE=" + Search + ">");
						if(Search.equals("YES")){
							out.println("<input type=hidden name = itemSel VALUE=" + itemSel + ">");
							out.println("<input type=hidden name = docItem VALUE=" + docItem + ">");
							out.println("<input type=hidden name = initDay VALUE=" + initDay + ">");
							out.println("<input type=hidden name = startYear VALUE=" + startYear + ">");
							out.println("<input type=hidden name = startMon VALUE=" + startMon + ">");
							out.println("<input type=hidden name = startDay VALUE=" + startDay + ">");
							out.println("<input type=hidden name = stopYear VALUE=" + stopYear + ">");
							out.println("<input type=hidden name = stopMon VALUE=" + stopMon + ">");
							out.println("<input type=hidden name = stopDay VALUE=" + stopDay + ">");
							out.println("<input type=hidden name = personSel VALUE=" + personSel + ">");
							out.println("<input type=hidden name = docPerson VALUE=" + docPerson + ">");
							out.println("<input type=hidden name = titleSel VALUE=" + titleSel + ">");
							out.println("<input type=hidden name = docTitle VALUE=" + docTitle + ">");
							out.println("<input type=hidden name = commSel VALUE=" + commSel + ">");
							out.println("<input type=hidden name = docComment VALUE=" + docComment + ">");
							out.println("<input type=hidden name = oldCount VALUE=" + oldCount + ">");
							out.println("<input type=hidden name = andSearch VALUE=" + andSearch + ">");
						}
					}
	
				}else {
					throw new Exception ("문서를 가져오지 못했습니다.");
				}
			if( !(listID.equals(Lists.SUBMITBOX))){
				//검색 부분 완성 후 수정
				if( Search.equals("YES"))
		    			out.println("<td width=150><p align=center><a href=\"http:/servlet/EDASearchServlet\">"+
		    				 "<img src=\'/image/BBS_list.gif\' border=0 alt=\'검색함으로\'></a></td>");
				else
	    				out.println("<td width=150><p align=center><a href=\"http:/servlet/EDADocumentBoxServlet?"+
	    				 "count=" + count + "&listID="+listID+"&doGet=YES\" >"+
	    				 "<img src=\'/image/BBS_list.gif\' border=0 alt=\'문서함으로\'></a></td>");
			}
			out.println("</form>");
			if( position != 0 )
				out.println("<td width=150><p align=center><a href=\"http:/servlet/EDADocumentServlet?"+
				 "count="+count+"&listID="+listID+"&DocID="+docIDArray[position - 1]+
				 "&DocIDS="+docIDs+"&position="+ ( position - 1 )+"&length=" + length +
				 "&Search="+Search+"&oldCount="+oldCount+"&itemSel="+itemSel+"&docItem="+
				 docItem+"&initDay="+initDay+"&startYear="+startYear+"&startMon="+startMon+
				 "&startDay="+startDay+"&stopYear="+stopYear+"&stopMon="+stopMon+"&stopDay="+
				 stopDay+"&personSel="+personSel+"&docPerson="+docPerson+"&titleSel="+titleSel+
				 "&docTitle="+docTitle+"&commSel="+commSel+"&docComment="+docComment+"&andSearch="+
				 andSearch+"\"><img src=\'/image/BBS_back.gif\' border=0 alt=\'이전 문서\'></a></td>");
			if( position + 1 < length )	       	 
				out.println("<td width=150><p align=center><a href=\"http:/servlet/EDADocumentServlet?"+
				 "count="+count+"&listID="+listID+"&DocID="+docIDArray[position + 1]+
				 "&DocIDS="+docIDs+"&position="+  (position+1)  +"&length=" + length +
				 "&Search="+Search+"&oldCount="+oldCount+"&itemSel="+itemSel+"&docItem="+
				 docItem+"&initDay="+initDay+"&startYear="+startYear+"&startMon="+startMon+
				 "&startDay="+startDay+"&stopYear="+stopYear+"&stopMon="+stopMon+"&stopDay="+
				 stopDay+"&personSel="+personSel+"&docPerson="+docPerson+"&titleSel="+titleSel+
				 "&docTitle="+docTitle+"&commSel="+commSel+"&docComment="+docComment+"&andSearch="+
				 andSearch+"\"><img src=\'/image/BBS_forward.gif\' border=0 alt=\'다음 문서\'></a></td>");
			out.println("</tr>");
   			out.println("</table>");
    			out.println("</div>");
			}
			catch(Exception e){
				out.println("<p align=\"center\"><font size=\"4\" color=\"gray\" >"+
				 "<b>문서 보기</b></font></br>");
				out.println("<hr width=600></p>");
				
				out.println("<p align=\"center\">문서를 가져오는 중 오류가 발생했습니다.</br>");	
				out.println( e.toString() +"</br>");
			}
		        		
			out.println("</body></html>");
		}
		else {
			//사용자 인증에 실패하였습니다.
			res.sendRedirect("../Login.html");
		}
		out.close();
	}
}	