/***********************************************************************************
 * @@ Program Name : DynaReportsViewer.java Description : 보고한 보고자료와 문서 양식을 조합하여 보고자료 보기 처리 해줌 Author : 강원중 Create Date :
 *    2005-02-04 History :
 * @@
 ***********************************************************************************/

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

import kr.co.kihyun.beans.entity.TotDoc;
import kr.co.kihyun.beans.totsys.report.ReportFormElement;
import kr.co.kihyun.beans.totsys.report.ReportsView;
import kr.co.kihyun.moumi.doc.table.MTableAnalysis;
import kr.co.kihyun.moumi.report.MReport;
import kr.co.kihyun.moumi.report.MReportData;
import kr.co.kihyun.moumi.report.MReportMaker;

@Deprecated
public class ReportsViewer extends ReportFormElement {

	private ReportsView reportsView;

	public ReportsViewer(TotDoc totDoc) {
		this(totDoc, MReport.HTML);
	}

	public ReportsViewer(TotDoc totDoc, int outType) {
		this(totDoc, MReport.SIM_REPORTS_OUT, outType);
	}

	public ReportsViewer(TotDoc totDoc, int viewType, int outType) {
		super(totDoc, viewType, outType);
		itemAnalysis(totDoc);
		reportsView = new ReportsView();
	}

	/*********** 한개의 기관에 대한 보고자료 처리 ***********/
	@Deprecated
	public void setReportData(String partID) {
                //v2. 6.SQL 삽입 : reportsView.executeQuery에서 prepare SQL 문으로 되어 있다.
		reportsView.executeQuery(getTotDoc().getId(),partID);
                //===============
	}

	public void setReportDataAll(String partID) {
		reportsView.executeAllQuery(getTotDoc().getId(),partID);
	}

	public void setReportDataAll(String partID, int searchType) {
		reportsView.executeAllQuery(getTotDoc().getId(),partID, searchType);
	}

	public ReportsView getReportsView() {
		return reportsView;
	}

	public String[] getReportNBSPList() {
		StringBuffer reportBuff = null;
		String[] reportList = new String[getReportsView().getCount()];
		for (int d = 0; d < getReportsView().getCount(); d++) {
			if (getReportsView().getPrssList()[d] < MReport.COMP || getReportsView().getPrssList()[d] == MReport.NOT) {

				reportList[d] = MReport.prss[getReportsView().getPrssList()[d]];
				reportList[d] = "<table><tr><td>" + reportList[d] + "</td></tr></table>";
				continue;
			}

			String[] dataList = getDataList(d);

			String[] tableTypesList = super.getTableTypes().split(",");

			int[] tableColNumList = MReportData.getColNumList(getReportsView().getDatasList()[d]);
			int[] tableRowNumList = MReportData.getRowNumList(getReportsView().getDatasList()[d]);

			String[] titleForms = new String[tableRowNumList.length];
			String[] dataForms = new String[tableRowNumList.length];
			String[] tailForms = new String[tableRowNumList.length];
			String[] subForm = new String[tableRowNumList.length];

			String Form = super.getForm();
			int here = 0;
			int stTABLE = 0;
			int edTABLE = 0;

			for (int i = 0; i < tableRowNumList.length; i++) {
				if (Form.indexOf("</table>", here) < 0)
					break;
				subForm[i] = MReport.geteExtractTable(Form, here);
				here = Form.indexOf("</table>", here) + 8;
			}

			int tablePosition = 0;
			int tp = 0;
			reportBuff = new StringBuffer();

			for (int i = 0; i < tableRowNumList.length; i++) {
				/****** 해당 테이블에 해당하는 아이템 타입 리스트를 구함 ******/
				for (tp = tablePosition; tp < super.getItemList().getTableNumList().length; tp++) {
					if ((i + 1) < super.getItemList().getTableNumList()[tp])
						break;
				}

				int[] tmpItemNumList = new int[tp - tablePosition];
				String[] tmpItemNameList = new String[tp - tablePosition];
				int[] tmpItemBNWList = new int[tp - tablePosition];
				int[] tmpItemTypeList = new int[tp - tablePosition];
				String[] tmpItemExprList = new String[tp - tablePosition];
				int[] tmpItemRowList = new int[tp - tablePosition];

				for (int in = 0, t = tablePosition; t < tp; in++, t++) {
					tmpItemNumList[in] = super.getItemList().getNumList()[t];
					tmpItemNameList[in] = super.getItemList().getNameList()[t];
					tmpItemBNWList[in] = super.getItemList().getBNWList()[t];
					tmpItemTypeList[in] = super.getItemList().getTypeList()[t];
					tmpItemExprList[in] = super.getItemList().getExprList()[t];
					tmpItemRowList[in] = super.getItemList().getRecordNumList()[t];
				}

				int tmpInItemNum = 0;
				int tmpIN = 0;
				for (int a = 0; a < tmpItemTypeList.length; a++) {
					if (tmpItemTypeList[a] != 0) { // MItem.TITLE == 0
						tmpIN = a;
						break;
					}
				}
				for (int a = 0; a < tmpItemRowList.length; a++) {
					if (tmpItemRowList[a] < tmpItemRowList[tmpIN])
						tmpInItemNum++;
					else
						break;
				}
				int tmpTitleRowCount = 0;
				if (tmpInItemNum > 0) {
					tmpTitleRowCount = tmpItemRowList[tmpInItemNum - 1] - tmpItemRowList[0] + 1;
				}

				/***** 해당 테이블에 해당하는 아이템 타입 리스트를 구함 끝 *****/

				String[] subDataList = new String[tableColNumList[i]];

				int stData = 0;
				int stTotData = 0;
				for (int c = 0; c < i; c++) {
					stData = stData + (tableColNumList[c] * tableRowNumList[c]);
					stTotData = stTotData + tableColNumList[c];
				}

				MTableAnalysis tbAnalysis = new MTableAnalysis(subForm[i], tmpItemTypeList);

				titleForms[i] = tbAnalysis.getTitleForm();
				dataForms[i] = tbAnalysis.getDataForm();
				tailForms[i] = tbAnalysis.getTailForm();

				stTABLE = titleForms[i].indexOf("<table");
				edTABLE = titleForms[i].indexOf("<tr", stTABLE);

				if (edTABLE > 0)
					reportBuff.append(titleForms[i].substring(stTABLE, edTABLE));
				else
					reportBuff.append(titleForms[i]);

				for (int r = 0; r < tableRowNumList[i]; r++) {
					for (int cp = 0; cp < (tableColNumList[i]); cp++) {
						subDataList[cp] = dataList[stData + cp];
					}
					reportBuff.append(MReportMaker.reportMake(dataForms[i], subDataList, tmpItemNumList,
							tmpItemNameList, tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum,
							tmpTitleRowCount, tableRowNumList[i], Integer.parseInt(tableTypesList[i]), super.getOutType()));
					stData = stData + tableColNumList[i];
				}

				/*
				 * 건설교통평가원 요청으로 합계를 한글에서 안보이게 하기 위에 여기를 주석처리
				 * for(int cp=0; cp<(tableColNumList[i]);cp++){
				 * subTotDataList[cp]=totDataList[stTotData+cp];
				 * }
				 * //reportBuff.append(MReportMaker.reportMake(dataForms[i], subDataList, tmpItemNumList,
				 * tmpItemNameList, tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum, tmpTitleRowCount,
				 * tableRowNumList[i], Integer.parseInt(tableTypesList[i]), super.outType));
				 * 
				 * if(tableRowNumList[i] > 1) reportBuff.append(MReportMaker.reportMake(dataForms[i], subTotDataList,
				 * tmpItemNumList, tmpItemNameList, tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum,
				 * tmpTitleRowCount, tableRowNumList[i], MTable.DYNATOTLINE, super.outType));
				 */
				reportBuff.append(tailForms[i]);

				tablePosition = tp;
			}
			reportList[d] = reportBuff.toString();
		}

		return reportList;
	}

	public String[] getTotalReportNBSPList(String actionType) {
		StringBuffer reportBuff = null;
		String[] reportList = new String[getReportsView().getCount()];
		for (int d = 0; d < getReportsView().getCount(); d++) {
			if (getReportsView().getPrssList()[d] < MReport.COMP || getReportsView().getPrssList()[d] == MReport.NOT) {
				reportList[d] = MReport.prss[getReportsView().getPrssList()[d]];
				reportList[d] = "<table><tr><td>" + reportList[d] + "</td></tr></table>";
				continue;
			}

			String[] dataList = getDataList(d);
			String[] tableTypesList = super.getTableTypes().split(",");

			int[] tableColNumList = MReportData.getColNumList(getReportsView().getDatasList()[d]);
			int[] tableRowNumList = MReportData.getRowNumList(getReportsView().getDatasList()[d]);

			String[] titleForms = new String[tableRowNumList.length];
			String[] dataForms = new String[tableRowNumList.length];
			String[] tailForms = new String[tableRowNumList.length];
			String[] subForm = new String[tableRowNumList.length];

			String Form = super.getForm();
			int here = 0;

			for (int i = 0; i < tableRowNumList.length; i++) {
				if (Form.indexOf("</table>", here) < 0)
					break;
				subForm[i] = MReport.geteExtractTable(Form, here);
				here = Form.indexOf("</table>", here) + 8;
			}

			subForm = MReport.geteExtractTable(Form);

			int tablePosition = 0;
			int tp = 0;
			reportBuff = new StringBuffer();

			for (int i = 0; i < tableRowNumList.length; i++) {
				/****** 해당 테이블에 해당하는 아이템 타입 리스트를 구함 ******/
				for (tp = tablePosition; tp < super.getItemList().getTableNumList().length; tp++) {
					if ((i + 1) < super.getItemList().getTableNumList()[tp])
						break;
				}

				int[] tmpItemNumList = new int[tp - tablePosition];
				String[] tmpItemNameList = new String[tp - tablePosition];
				int[] tmpItemBNWList = new int[tp - tablePosition];
				int[] tmpItemTypeList = new int[tp - tablePosition];
				String[] tmpItemExprList = new String[tp - tablePosition];
				int[] tmpItemRowList = new int[tp - tablePosition];

				for (int in = 0, t = tablePosition; t < tp; in++, t++) {
					tmpItemNumList[in] = super.getItemList().getNumList()[t];
					tmpItemNameList[in] = super.getItemList().getNameList()[t];
					tmpItemBNWList[in] = super.getItemList().getBNWList()[t];
					tmpItemTypeList[in] = super.getItemList().getTypeList()[t];
					tmpItemExprList[in] = super.getItemList().getExprList()[t];
					tmpItemRowList[in] = super.getItemList().getRecordNumList()[t];
				}

				int tmpInItemNum = 0;
				int tmpIN = 0;
				for (int a = 0; a < tmpItemTypeList.length; a++) {
					if (tmpItemTypeList[a] != 0) { // MItem.TITLE == 0
						tmpIN = a;
						break;
					}
				}
				for (int a = 0; a < tmpItemRowList.length; a++) {
					if (tmpItemRowList[a] < tmpItemRowList[tmpIN])
						tmpInItemNum++;
					else
						break;
				}
				int tmpTitleRowCount = 0;
				if (tmpInItemNum > 0) {
					tmpTitleRowCount = tmpItemRowList[tmpInItemNum - 1] - tmpItemRowList[0] + 1;
				}

				/***** 해당 테이블에 해당하는 아이템 타입 리스트를 구함 끝 *****/

				String[] subDataList = new String[tableColNumList[i]];
				int stData = 0;
				int stTotData = 0;
				for (int c = 0; c < i; c++) {
					stData = stData + (tableColNumList[c] * tableRowNumList[c]);
					stTotData = stTotData + tableColNumList[c];
				}

				MTableAnalysis tbAnalysis = new MTableAnalysis(subForm[i], tmpItemTypeList);

				titleForms[i] = tbAnalysis.getTitleForm();
				dataForms[i] = tbAnalysis.getDataForm();
				tailForms[i] = tbAnalysis.getTailForm();

				reportBuff.append(titleForms[i]);
				for (int r = 0; r < tableRowNumList[i]; r++) {
					for (int cp = 0; cp < (tableColNumList[i]); cp++) {
						subDataList[cp] = dataList[stData + cp];
					}
					reportBuff.append(MReportMaker.reportMake(dataForms[i], subDataList, tmpItemNumList,
							tmpItemNameList, tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum,
							tmpTitleRowCount, tableRowNumList[i], Integer.parseInt(tableTypesList[i]), super.getOutType()));
					stData = stData + tableColNumList[i];
				}

				/*
				 * 건설교통평가원 요청으로 합계를 한글에서 안보이게 하기 위에 여기를 주석처리
				 * for(int cp=0; cp<(tableColNumList[i]);cp++){
				 * subTotDataList[cp]=totDataList[stTotData+cp];
				 * }
				 * //reportBuff.append(MReportMaker.reportMake(dataForms[i], subDataList, tmpItemNumList,
				 * tmpItemNameList, tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum, tmpTitleRowCount,
				 * tableRowNumList[i], Integer.parseInt(tableTypesList[i]), super.outType));
				 * 
				 * if(tableRowNumList[i] > 1) reportBuff.append(MReportMaker.reportMake(dataForms[i], subTotDataList,
				 * tmpItemNumList, tmpItemNameList, tmpItemBNWList, tmpItemTypeList, tmpItemExprList, tmpInItemNum,
				 * tmpTitleRowCount, tableRowNumList[i], MTable.DYNATOTLINE, super.outType));
				 */
				reportBuff.append(tailForms[i]);
				if ("view".equals(actionType)) {
					if (i < (tableRowNumList.length - 1)) {
						reportBuff.append("<br>");
					}
				}
				tablePosition = tp;
			}
			reportList[d] = reportBuff.toString();
		}
		return reportList;
	}

	public String[] getTitleFormsList() { // 타이틀만 가져오는 메소드사용(X)
		StringBuffer reportBuff = null;
		String[] reportList = new String[getReportsView().getCount()];

		for (int d = 0; d < getReportsView().getCount(); d++) {
			if (getReportsView().getPrssList()[d] < MReport.COMP || getReportsView().getPrssList()[d] == MReport.NOT) {

				reportList[d] = MReport.prss[getReportsView().getPrssList()[d]];
				reportList[d] = "<table><tr><td>&nbsp;</td></tr></table>";
				continue;
			}

			int[] tableRowNumList = MReportData.getRowNumList(getReportsView().getDatasList()[d]);

			String[] titleForms = new String[tableRowNumList.length];
			String[] subForm = new String[tableRowNumList.length];

			String Form = super.getForm();
			int here = 0;

			for (int i = 0; i < tableRowNumList.length; i++) {
				if (Form.indexOf("</table>", here) < 0)
					break;
				subForm[i] = MReport.geteExtractTable(Form, here);
				here = Form.indexOf("</table>", here) + 8;
			}

			int tablePosition = 0;
			int tp = 0;
			reportBuff = new StringBuffer();

			for (int i = 0; i < tableRowNumList.length; i++) {
				/****** 해당 테이블에 해당하는 아이템 타입 리스트를 구함 ******/
				for (tp = tablePosition; tp < super.getItemList().getTableNumList().length; tp++) {
					if ((i + 1) < super.getItemList().getTableNumList()[tp])
						break;
				}

				int[] tmpItemTypeList = new int[tp - tablePosition];

				for (int in = 0, t = tablePosition; t < tp; in++, t++) {
					tmpItemTypeList[in] = super.getItemList().getTypeList()[t];
				}

				for (int a = 0; a < tmpItemTypeList.length; a++) {
					if (tmpItemTypeList[a] != 0) { // MItem.TITLE == 0
						break;
					}
				}

				MTableAnalysis tbAnalysis = new MTableAnalysis(subForm[i], tmpItemTypeList);
				titleForms[i] = tbAnalysis.getTitleForm();
				reportBuff.append(titleForms[i]);
				reportBuff.append("</table>");
				tablePosition = tp;
			}
			reportList[d] = reportBuff.toString();
		}
		return reportList;
	}

	private String[] getDataList(int d) {

		String[] dataList = MReportData.getDataArray(getReportsView().getDatasList()[d]);

		for (int i = 0; i < dataList.length; i++) {
			dataList[i] = dataList[i].replaceAll(" ", "&nbsp;");
			dataList[i] = dataList[i].replaceAll("<", "&lt");
			dataList[i] = dataList[i].replaceAll(">", "&gt");
		}
		return dataList;
	}
}
