/*************************************************************************************************
 * 프로그램명 : HttpReportRegister.java 프로그램설명 : 집계 문서에대한 보고자 보고 또는 보고자 자료 변경에 사용 작성자 : 강원중 작성일 : 2004.01.12 변경일 : 2003.11.30
 **************************************************************************************************/

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

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import kr.co.kihyun.beans.entity.util.*;
import kr.co.kihyun.beans.user.HttpSSOLogin;
import kr.co.kihyun.io.FileUtil;
import kr.co.kihyun.io.MultipartRequest;
import kr.co.kihyun.lang.MInteger;
import kr.co.kihyun.lang.MLong;
import kr.co.kihyun.lang.MString;
import kr.co.kihyun.moumi.MoumiConfig;

import kr.co.kihyun.service.TotDocService;
import kr.co.kihyun.text.html.HtmlUtil;
import kr.co.kihyun.text.html.ServletUtil;
import org.datanucleus.ObjectManager;
import org.datanucleus.jdo.JDOPersistenceManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebServlet("/servlet/kr.co.kihyun.beans.totsys.report.CheckIRTable")
public class CheckIRTable extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory.getLogger(CheckIRTable.class);

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		doPost(req, res);
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		res.setContentType("text/html;charset=UTF-8");
		res.setHeader("Cache-Control","no-cache");
		res.setHeader("Pragma","no-cache");
		res.setDateHeader ("Expires", -1);
		PrintWriter out = res.getWriter();
		HttpSession session = req.getSession(true);

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

		//Long reportID = MLong.parseLong(req.getParameter("reportID"));
                String[] rptIds = String.valueOf(req.getParameter("reportID")).split("\\|");//20160419 추가
		MPersistenceManager moumiPm = new MPersistenceManager(PMF.get().getPersistenceManager());
                
                try {
                    
                        //20160428 IR 테이블 IS_APLIED N 인 데이타 유무 조회 추가 시작
                    int ir_tbl_count = 0;
                        Map param_tmp = new HashMap();
                        param_tmp.put("isAplied", "N");
                        TotDocService docService_tmp = new TotDocService(moumiPm);
                        docService_tmp.createIR_Limit_SearchQuery(param_tmp);
                        ir_tbl_count = docService_tmp.getCountFromMainQuery();
                        //20160428 IR 테이블 IS_APLIED N 인 데이타 유무 조회 추가 끝
                        
                    if(ir_tbl_count == 0){
                           //out.println(ServletUtil.alert("교육부에서 요청한 자료가 존재하지 않습니다.(종료방법)"));//20160429 추가
                          docService_tmp = new TotDocService(moumiPm);
                          docService_tmp.createIR_TOT_DOC_SearchQuery(param_tmp);
                          ir_tbl_count = docService_tmp.getCountFromMainQuery();

                          if(ir_tbl_count == 0){
                            //out.println(ServletUtil.alert("교육부에서 요청한 자료가 존재하지 않습니다.(문서정보)"));//20160429 추가
                            docService_tmp = new TotDocService(moumiPm);
                            docService_tmp.createIR_TOT_TABLE_SearchQuery(param_tmp);
                            ir_tbl_count = docService_tmp.getCountFromMainQuery();
                            if(ir_tbl_count == 0){
                              //out.println(ServletUtil.alert("교육부에서 요청한 자료가 존재하지 않습니다.(표정보)"));//20160429 추가
                                docService_tmp = new TotDocService(moumiPm);
                                docService_tmp.createIR_TOT_ITEM_SearchQuery(param_tmp);
                                ir_tbl_count = docService_tmp.getCountFromMainQuery();
                                if(ir_tbl_count == 0){
                                  //out.println(ServletUtil.alert("교육부에서 요청한 자료가 존재하지 않습니다.(항목방법)"));//20160429 추가
                                        docService_tmp = new TotDocService(moumiPm);
                                        docService_tmp.createIR_BOARD_SearchQuery(param_tmp);
                                        ir_tbl_count = docService_tmp.getCountFromMainQuery();
                                        if(ir_tbl_count == 0){
                                                //out.println(ServletUtil.alert("교육부에서 요청한 자료가 존재하지 않습니다.(게시물)"));//20160429 추가
                                            docService_tmp = new TotDocService(moumiPm);
                                            docService_tmp.createIR_TOT_REPORT_SearchQuery(param_tmp);
                                                ir_tbl_count = docService_tmp.getCountFromMainQuery();
                                                if(ir_tbl_count == 0){
                                                  //out.println(ServletUtil.alert("교육부에서 요청한 자료가 존재하지 않습니다.(제출문서)"));//20160429 추가
                                                  out.println(ServletUtil.alert("교육부에서 요청한 자료가 존재하지 않습니다."));//20160429 추가
                            }else{
                                              out.println(ServletUtil.getJavaScript("parent.fn_irDataSaveExe();"));//20160421 추가  
                                            }//end tot_report
                                        }else{
                                            out.println(ServletUtil.getJavaScript("parent.fn_irDataSaveExe();"));//20160421 추가  
                                        }//end if board    
                                }else{
                                  out.println(ServletUtil.getJavaScript("parent.fn_irDataSaveExe();"));//20160421 추가  
                                }//end if tot_item    
                             }else{
                               out.println(ServletUtil.getJavaScript("parent.fn_irDataSaveExe();"));//20160421 추가                              
                             } //end if tot_table   
                          }else{
                            out.println(ServletUtil.getJavaScript("parent.fn_irDataSaveExe();"));//20160421 추가  
                          }//end if tot_doc    
                    }else{
                        out.println(ServletUtil.getJavaScript("parent.fn_irDataSaveExe();"));//20160421 추가  
                    }//end if limit 
                       
                } catch (Exception ex) {
                        ex.printStackTrace();
                        LOG.error(MoumiConfig.getMessageBundle().getString("moumi.message.tot_report") + MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.save")+ MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.fail") +" {}, {}",
                                        new Object[] { usID, req.getRemoteAddr() });//20160419 수정
                        out.println(ServletUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.popup.saveFailAdmin")));
                        //out.println(ServletUtil.getJavaScript("history.back();"));
                } finally {
                        moumiPm.close();
                        out.println(ServletUtil.getJavaScript("parent.fn_indiClose();"));//20160421 추가
                }
	}
        
        
        
}
