
<%@page import="kr.co.kihyun.beans.tosys.etc1.TotsysEtc"%>
<%
/************************************************************@@
  * Program Name	: popup_inform_preview.jsp
  * Description		: 집계문서 미리보기(등록과, 변경에서 같이 사용
  * Author			: 기현테크
  * Create Date		: 2010.11.25
  * History			:
@@************************************************************/
%>
<%@ page contentType="text/html; charset=UTF-8"
	import="java.net.URLEncoder" import="java.net.URLDecoder"
	import="javax.jdo.PersistenceManager"
	import="kr.co.kihyun.beans.entity.util.*"
	import="kr.co.kihyun.beans.entity.TotDoc"
	import="kr.co.kihyun.lang.MString" import="kr.co.kihyun.lang.MLong"
	import="kr.co.kihyun.lang.Encoder" import="kr.co.kihyun.lang.MInteger"
	import="kr.co.kihyun.text.html.TagFilter" import="kr.co.kihyun.beans.entity.*"
	import="kr.co.kihyun.moumi.doc.MDocPreview"

import="javax.jdo.Transaction"
	import="kr.co.kihyun.moumi.doc.table.item.MItem"
	import="kr.co.kihyun.beans.totsys.doc.table.item.ItemList"
	import="kr.co.kihyun.moumi.Moumi" import="java.util.*"%>

<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>

<%
PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
Transaction tx = pm.currentTransaction();
try{
	/*********** parameter value ***********/
	Long docID = MLong.parseLong(request.getParameter("docID"),null);
	String docName = clearXSS(URLDecoder.decode(MString.checkNull(request.getParameter("docName")), "UTF-8"),"");
	String acceptanceLimitStrategy = clearXSS(request.getParameter("acceptanceLimitStrategy"),LimitByDeadlineStrategy.class.getSimpleName().toString());
	String usrFilenames = clearXSS(URLDecoder.decode(MString.checkNull(request.getParameter("usrFilenames")), "UTF-8"),"");
	String svrFilenames = clearXSS(URLDecoder.decode(MString.checkNull(request.getParameter("svrFilenames")), "UTF-8"),"");
	String itemNames = Encoder.toJava(clearXSS(MString.checkNull(request.getParameter("itemNames")),""));
	String itemSizes = Encoder.toJava(clearXSS(MString.checkNull(request.getParameter("itemSizes")),""));
	String itemDescs = Encoder.toJava(clearXSS(MString.checkNull(request.getParameter("itemDescs")),""));
	String itemExprs = Encoder.toJava(clearXSS(MString.checkNull(request.getParameter("itemExprs")),""));
	String itemTypes = Encoder.toJava(clearXSS(MString.checkNull(request.getParameter("itemTypes")),""));
	int defItemSize = MInteger.parseInt(MString.checkNull(request.getParameter("defItemSize")));
	String mode = clearXSS(MString.checkNull(request.getParameter("mode")),"");
	String callType = MString.checkNull(request.getParameter("callType"));

	if(defItemSize < 1 ) defItemSize = 10;

	/************ doc values ************/
	TotDoc totDoc = docID == null ? null : pm.getObjectById(TotDoc.class, docID);
	String docForm = "";

	/************* item values ***********/
	StringBuffer itemNamesBuf = new StringBuffer();
	StringBuffer itemSizesBuf = new StringBuffer();
	StringBuffer itemDescsBuf = new StringBuffer();
	StringBuffer itemExprsBuf = new StringBuffer();
	StringBuffer itemTypesBuf = new StringBuffer();
	StringBuilder insertInputCellName = new StringBuilder();

	if(mode != null && mode.equals("regist")){
		docForm = (String)session.getAttribute("docForm");
	}else if(mode != null && mode.equals("modify")){
		docForm = totDoc.getForm();
	}else	if(mode != null && mode.equals("preview"))
    {
		docForm = totDoc.getForm();
		docName = totDoc.getName();
		if(totDoc.getTotTables().iterator().hasNext())
			defItemSize = totDoc.getTotTables().iterator().next().getItemSize();

		/********** Item values **********/
		ItemList itemList = new ItemList();
                //v2. 13.SQL 삽입 : itemList.executeQuery()에서 prepare SQL 문으로 되어 있다.
		itemList.executeQuery(docID);
                //=================
		int itemCount = itemList.getCount();
		String[] itemNameList = itemList.getNameList();
		String[] itemDescList = itemList.getDescList();
		String[] itemExprList = itemList.getExprList();
		int[] itemTypeList = itemList.getTypeList();
		int[] itemNumList = itemList.getNumList();
		int[] itemSizeList = itemList.getSizeList();
		int[] tableNumList = itemList.getTableNumList();
		int[] colNumList = itemList.getFieldNumList();
		int[] rowNumList = itemList.getRecordNumList();
		int[] itemBNWList = itemList.getBNWList();
		String[] itemTypeNameList = itemList.getTypeNameList();
		//dform.itemNames.value = dform.itemNames.value + i + ':' + itemNameList[i] + 'MItem.LIST_DELIM';

		for(int i=0; i<itemCount; i++)
        {
			if(insertInputCellName.toString().length() == 0)
            {
				if("empty".equals(itemDescList[i].trim()))
					insertInputCellName.append(itemNameList[i]);
			}else
            {
				if("empty".equals(itemDescList[i].trim()))
					insertInputCellName.append(","+itemNameList[i]);
			}			
			itemNamesBuf.append(i + ":" + itemNameList[i] + MItem.LIST_DELIM);
			itemSizesBuf.append(i + ":" + itemSizeList[i] + MItem.LIST_DELIM);
			itemDescsBuf.append(i + ":" + itemDescList[i] + MItem.LIST_DELIM);
			itemExprsBuf.append(i + ":" + itemExprList[i] + MItem.LIST_DELIM);
			itemTypesBuf.append(i + ":" + MItem.getTypeName(itemTypeList[i]) + MItem.LIST_DELIM);
		}

		itemNames = itemNamesBuf.toString();
		itemSizes = itemSizesBuf.toString();
		itemDescs = itemDescsBuf.toString();
		itemExprs = itemExprsBuf.toString();
		itemTypes = itemTypesBuf.toString();
	}

	/*********** preview로 docForm 변환 ***********/
	String putValScript = "";
	String changeAllScript = "";
	String docBodyForm = "";
	String docTitleForm = "";
	String inputForm = "";

	MDocPreview docPreView = new MDocPreview();
    /****
    System.out.println("000****************************************************************************************** "+itemNames);
    System.out.println("000****************************************************************************************** "+itemSizes);
    System.out.println("000****************************************************************************************** "+itemExprs);
    System.out.println("000****************************************************************************************** "+itemTypes);
    System.out.println("000****************************************************************************************** "+itemDescs);
    System.out.println("000****************************************************************************************** "+defItemSize);
    ****/
	docPreView.convert(docForm, itemNames, itemSizes, itemExprs, itemTypes, itemDescs, defItemSize);

	docForm = docPreView.getInputForm();
	putValScript = docPreView.getPutValScript();
    
	changeAllScript = docPreView.getChangeAllScript();
	changeAllScript = changeAllScript.replaceAll("[$]",".");

	inputForm = docPreView.getInputForm();

    /*
	if(mode != null && !mode.equals("preview")){
		StringTokenizer stkNames = new StringTokenizer(itemNames,(MItem.LIST_DELIM));
		StringTokenizer stkDesc = new StringTokenizer(itemDescs,(MItem.LIST_DELIM));

		int itemCount = stkNames.countTokens();
		String strTmp;
		String[] itemNameListScript = new String[itemCount];
		String[] itemDescListScript = new String[itemCount];

		for (int i=0; i < itemCount; i++) {
			strTmp = stkNames.nextToken();
			itemNameListScript[i] = strTmp.substring(strTmp.indexOf(":")+1, strTmp.length()).trim();
			
			strTmp = stkDesc.nextToken();
			itemDescListScript[i] = strTmp.substring(strTmp.indexOf(":")+1, strTmp.length()).trim();
			
			if("empty".equals(itemDescListScript[i].trim())){
				if(insertInputCellName.toString().length() == 0)
					insertInputCellName.append(itemNameListScript[i]);
				else
					insertInputCellName.append(","+itemNameListScript[i]);
			}
		}
	}
    */
    if(mode != null && !mode.equals("preview"))
    {		
        String[] stkNames=TotsysEtc.getDeliToArr(itemNames,"!@!");
        String[] stkDesc=TotsysEtc.getDeliToArr(itemDescs,"!@!");
		int itemCount = stkNames.length;
		String strTmp;
		String[] itemNameListScript = new String[itemCount];
		String[] itemDescListScript = new String[itemCount];
		for(int i=0; i < itemCount; i++)
        {
			strTmp = stkNames[i];
			itemNameListScript[i] = strTmp.substring(strTmp.indexOf(":")+1, strTmp.length()).trim();
			strTmp = stkDesc[i];
			itemDescListScript[i] = strTmp.substring(strTmp.indexOf(":")+1, strTmp.length()).trim();
			if("empty".equals(itemDescListScript[i].trim()))
            {
				if(insertInputCellName.toString().length() == 0)
					insertInputCellName.append(itemNameListScript[i]);
				else
					insertInputCellName.append(","+itemNameListScript[i]);
			}
            
		}
	}
    
%>
<html>
<head>
<title></title>
<body>
</body>
<script language="javascript">
    <!--
            parent.document.getElementById('start_dialog').style.display = 'block';
    //-->
    </script>
</html>
<%
}
catch(Exception ex)
{	
    %>
<script language="javascript">
		<!--
        alert('서식에서 에러가 발생했습니다. 서식을 점검하신후 다시 시도하여주십시요.');
		//-->
	</script>
<%
}
finally
{
    if(tx.isActive())
tx.rollback();
    pm.close(); 
}
%>
