
<%/************************************************************@@
  * Program Name	: doc_view.jsp
  * Description		: /집계자/진행함/집계현황
  * Author			: 기현테크
  * Create Date		: 2010.11.24
  * History			:
@@************************************************************/%>

<%@ page contentType="text/html; charset=UTF-8"
	import="java.net.URLEncoder" import="java.net.URLDecoder"
	import="java.util.StringTokenizer" import="kr.co.kihyun.lang.Encoder"
	import="kr.co.kihyun.text.excel.Excel"
	import="kr.co.kihyun.text.html.TagFilter"
	import="kr.co.kihyun.lang.MString" import="kr.co.kihyun.lang.MLong"
	import="kr.co.kihyun.lang.MInteger"
	import="kr.co.kihyun.util.Stopwatch" import="kr.co.kihyun.text.hml.Hwp"
	import="kr.co.kihyun.text.html.HtmlUtil"
	import="kr.co.kihyun.moumi.doc.MDoc"
	import="kr.co.kihyun.moumi.doc.table.MTableAnalysis"
	import="kr.co.kihyun.moumi.Moumi"
	import="kr.co.kihyun.moumi.report.MReport"
	import="kr.co.kihyun.beans.user.Part"
	import="kr.co.kihyun.beans.user.PartList"
	import="kr.co.kihyun.beans.totsys.doc.table.Table"
	import="kr.co.kihyun.beans.totsys.doc.table.item.Item"
	import="kr.co.kihyun.beans.totsys.report.tots.ReportsViewer"
	import="kr.co.kihyun.beans.totsys.report.tot.TotReportViewer"
	import="kr.co.kihyun.beans.totsys.report.ReportViewManager"
	import="kr.co.kihyun.beans.totsys.report.ReportFormElement"
	import="kr.co.kihyun.beans.totsys.doc.table.item.ItemList"
	import="kr.co.kihyun.beans.user.HttpSSOLogin"
	import="kr.co.kihyun.beans.totsys.doc.Doc"
	import="kr.co.kihyun.beans.entity.TotReport"
	import="kr.co.kihyun.beans.entity.TotReportProcess"
	import="javax.jdo.PersistenceManager" import="javax.jdo.Transaction"
	import="kr.co.kihyun.beans.entity.util.*"
	import="kr.co.kihyun.beans.entity.TotDoc"
	import="kr.co.kihyun.beans.entity.MDept" import="java.util.List"
	import="javax.jdo.Query"
	import="kr.co.kihyun.beans.entity.util.TotReportUtil"
	import="kr.co.kihyun.beans.entity.TotDocType"%>
<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>
<%
	PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
	Transaction tx = pm.currentTransaction();
	try{
	
		/********** session내의 userId **********/
		Long docID = MLong.parseLong(request.getParameter("docID"), null);
		int gradeCode = MInteger.parseInt(request.getParameter("gradeCode"), -1);
		int tableNum = MInteger.parseInt(request.getParameter("tableNum"), 0);
		int searchType = MInteger.parseInt(request.getParameter("searchType"), -1);
		String displaySummary = MString.checkNull(request.getParameter("displaySummary"));

        tx.begin();

		TotDoc totDoc = (TotDoc)pm.getObjectById(TotDoc.class, docID);
		List<TotReport> reportList = totDoc.getAllReports(pm);
		List<TotReport> compReportList=null;
                //LJH
		/*
        Query q = pm.newNamedQuery(TotReport.class, "CompReports");
		q = pm.newQuery(TotReport.class, (List<TotReport>) q.execute());
		q.setFilter("totDoc == tot_doc");
		q.declareParameters("kr.co.kihyun.beans.entity.TotDoc tot_doc");
		q.setOrdering("mDept");
		compReportList = (List<TotReport>) q.execute(totDoc);
        */
                Query q = pm.newQuery(TotReport.class, compReportList);
				q.setFilter("totDoc == tot_doc && process == kr.co.kihyun.beans.entity.TotReportProcess.COMP");
        q.declareParameters("kr.co.kihyun.beans.entity.TotDoc tot_doc");
				q.setOrdering("mDept");
                                //v2. 13.SQL 삽입 : JDO형식이므로 해결책에 따른 prepare SQL 문으로 변경할 수 없음
				compReportList = (List<TotReport>) q.execute(totDoc);
                                //=================
        compReportList=totDoc.getRecursiveAllReports(pm, compReportList, null);


 
for(int ii=0;ii<compReportList.size();ii++)
{
    TotReport tr=compReportList.get(ii);

    if(tr.getProcess()!=TotReportProcess.COMP)
    {

        compReportList.remove(ii);
        ii=0;
    }

}

		if(searchType >= 0) {
			if(searchType == TotReportProcess.COMP.ordinal()) {
				reportList = compReportList;
			} else if(searchType == TotReportProcess.ACCEPT.ordinal()) {
				q = pm.newQuery(TotReport.class, reportList);
				q.setFilter("process == kr.co.kihyun.beans.entity.TotReportProcess.ACCEPT");
				q = pm.newQuery(TotReport.class, (List<TotReport>) q.execute());
				q.setFilter("totDoc == tot_doc");
				q.declareParameters("kr.co.kihyun.beans.entity.TotDoc tot_doc");
				q.setOrdering("mDept");
				reportList = (List<TotReport>) q.execute(totDoc);
			} else {
				q = pm.newQuery(TotReport.class, reportList);
				q = pm.newQuery(TotReport.class, (List<TotReport>) q.execute());
				q.setFilter("totDoc == tot_doc");
				q.declareParameters("kr.co.kihyun.beans.entity.TotDoc tot_doc");
				q.setOrdering("mDept");
				reportList = (List<TotReport>) q.execute(totDoc);
			}
		}

		tx.rollback();
	
		int allReportCount = reportList.size();
		int totReportCount = compReportList.size();
    %>
    <%
        int index=-1;
		tx.begin();
		for(TotReport report : reportList) 
        {
            index++;
            if(index%100==0) { tx.rollback();tx.begin(); }
            MDept organCode=report.getDept(pm).getOrgan(pm);
            String organName=report.getDept(pm).getOrgan(pm).getName();
            String deptName=report.getDept(pm).getName();
            String ownerName=report.getOwnerName(pm);
            TotReportProcess process=report.getProcess();
            String html=TotReportUtil.toHTML(report, tableNum, tableNum > 0, true);
    %>
    <table cellspacing="0" cellpadding="1" align="left" border="0" width="800">
        <tr width="100%">
		<td align="left"><%=deptName%>-<%=ownerName%></td>
	</tr>
	<tr width="100%">
		<td align="left"><%= process == TotReportProcess.COMP ? Excel.removeFmla(html) : process%></td>
	</tr>
    </table>
	<%}
		tx.rollback();
	%>
    <%
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if(tx.isActive())
                tx.rollback();
            pm.close();
        }
    %>
