/*************************************************************************************************
	프로그램명		: HttpReReportCancle.java
	프로그램설명	: 재보고 취소
	작성자				:
	작성일				:
	변경일				:
**************************************************************************************************/

package kr.co.kihyun.beans.totsys.totper;

import java.io.*;
//import java.sql.*;
//import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
//import java.net.*;
//import java.net.URLDecoder;

//import kr.co.kihyun.io.*;
//import kr.co.kihyun.db.*;
//import kr.co.kihyun.moumi.Moumi;
//import kr.co.kihyun.lang.MInteger;
import kr.co.kihyun.lang.Encoder;
import kr.co.kihyun.lang.MString;
import kr.co.kihyun.text.html.ServletUtil;
import kr.co.kihyun.beans.user.HttpSSOLogin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import kr.co.kihyun.beans.user.UserView;
//import kr.co.kihyun.beans.totsys.report.Report;
//import kr.co.kihyun.beans.totsys.totper.ReportRecall;
@WebServlet("/servlet/kr.co.kihyun.beans.totsys.totper.HttpReportRecall")
public class HttpReportRecall extends HttpServlet{
    private static final Logger LOG = LoggerFactory.getLogger(HttpReportRecall.class);
    
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{

		res.setContentType("text/html;charset=UTF-8");
		PrintWriter out = res.getWriter();
		HttpSession session = req.getSession(true);

		try{

			/*********** User HttpSSOLogin check ***********/
			if(!HttpSSOLogin.isLogin(req)){
				//login 성공후 되돌아올 주소
				String connURL="location='/totsys/login/login.jsp';";
				out.println(ServletUtil.getJavaScript(connURL));
				return;
			}

			/********** session내의 userId **********/
			String usID = HttpSSOLogin.getLoginID(req);
			String usName = HttpSSOLogin.getUserName(req);
			String dpID = HttpSSOLogin.getDeptID(req);

			/********** parameter value **********/
			String docID = "";
			String deptID = "";
			String userID = "";
			String url = "";

			docID = Encoder.toJava(req.getParameter("docID"));
			deptID = Encoder.toJava(req.getParameter("deptID"));
			userID = Encoder.toJava(req.getParameter("userID"));
			url = MString.checkNull(req.getParameter("url"));

			int succNum = 0;
			succNum = recall(docID,deptID,userID);

			if( succNum == 0 ){
					out.println(ServletUtil.alert("자료회수 실패!"));
					if("mmReportRecall".equals(url)){
						out.println(ServletUtil.getJavaScript("document.location='/totsys/sysadm/prssbox/reports/report_list.jsp?docID="+docID+"';"));
					}else{
						out.println(ServletUtil.getJavaScript("document.location='/totsys/repoper/mydocbox/compbox/report_view.jsp?docID="+docID+"&deptID="+deptID+"';"));
					}
			}else if( succNum == 1 ){
					out.println(ServletUtil.alert("자료회수 완료!\\n \\n회수된 자료는 임시저장함에서 확인할 수 있습니다."));
					if("mmReportRecall".equals(url)){
						out.println(ServletUtil.getJavaScript("document.location='/totsys/sysadm/prssbox/reports/report_list.jsp?docID="+docID+"';"));
					}else{
                                                //v2. 9.크로스사이트 스크립트 (PrintWrier) : Update by KWON,HAN 
//						out.println(ServletUtil.getJavaScript("document.location='/totsys/repoper/mydocbox/compbox/report_list.jsp';"));
                                                
                                                // 수정 : 외부 입력값 필터링
                                                String callbackFunc = "document.location='/totsys/repoper/mydocbox/compbox/report_list.jsp';";
                                                String filtered_callbackFunc = callbackFunc.replaceAll("<","").replaceAll(">","").replaceAll("&","").replaceAll(",","");
                                                LOG.debug("v2 9.크로스사이트 스크립트 (PrintWrier) : HttpReportRecall.doPost() filtered_callbackFunc={}, Not Test", filtered_callbackFunc);
                                                out.println(ServletUtil.getJavaScript(filtered_callbackFunc));      
                                                //========================================                                                
					}
			}

		}catch (Exception ex){
			ex.printStackTrace();
		}
	}

	private int recall(String docID, String deptID, String userID){
		int succNum = 0;

		ReportRecall rr = new ReportRecall();
		succNum = rr.reportRecall(docID, deptID, userID);

		return succNum;
	}
}
