/***********************************************************************************
 * @@ Program Name : StaticMReportViewer.java Description : 보고한 보고자료와 문서 양식을 조합하여 보고자료 보기 처리 해줌 Author : 강원중 Create Date
 *    : 2004-10-21 History :
 * @@
 ***********************************************************************************/
package kr.co.kihyun.beans.totsys.report;

//import javax.jdo.PersistenceManager;
//import javax.jdo.Transaction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kr.co.kihyun.beans.entity.TotDoc;
import kr.co.kihyun.beans.entity.TotTable;
//import kr.co.kihyun.beans.entity.util.*;
import kr.co.kihyun.beans.totsys.doc.table.item.Item;
import kr.co.kihyun.beans.totsys.doc.table.item.ItemList;
import kr.co.kihyun.moumi.report.MReport;
import kr.co.kihyun.text.html.HtmlUtil;

public class ReportFormElement {

	private static final Logger LOG = LoggerFactory.getLogger(ReportFormElement.class);
	private TotDoc totDoc;
	private Long formID;
	private String docName;
	private int defItemSize;
	private String form;
	private int viewType;
	private int outType;
	private String tableTypes;
	private ItemList itemList;
	private int inItemCount;
	private int strInItemNum;
	private int titleRowCount;
	private String[] titleDataList = null;
	private String[] inItemNameList = null;

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

	public ReportFormElement(TotDoc totDoc, int outType) {
		this(totDoc, MReport.REPORT_OUT, MReport.HTML);
	}

	public ReportFormElement(TotDoc totDoc, int viewType, int outType) {
		this.totDoc = totDoc;
		this.viewType = viewType;
		this.outType = outType;

		setDoc(totDoc, viewType, outType);
		setItem(totDoc);
	}

    public ReportFormElement(TotDoc totDoc, TotDoc slaveDoc,int viewType, int outType) {
		//this.totDoc = totDoc;
        this.totDoc = slaveDoc;
		this.viewType = viewType;
		this.outType = outType;

		setDoc(totDoc, viewType, outType);
		setItem(totDoc);
	}

	protected final void setDoc(TotDoc totDoc, int viewType, int outType) {
		if (MReport.REPORT_OUT == viewType) { // 보고자료출력

			setForm(totDoc.getForm());
            
		} else if (MReport.SIM_REPORTS_OUT == viewType) { // 집계현황출력

			setForm(HtmlUtil.getTables(totDoc.getForm()));
            
		} else if (MReport.TOTAL_OUT == viewType) { // 집계표출력

			setForm(HtmlUtil.getTables(totDoc.getForm()));
		}

		setFormID(totDoc.getMasterTotDoc() == null ? totDoc.getId() : totDoc.getMasterTotDoc().getId());
		LOG.debug("totDoc.getMasterTotDoc(): {}", totDoc.getMasterTotDoc());
		TotTable totTable = totDoc.getTotTables().iterator().next();
		setTableTypes(totTable.getTypes());
		setDefItemSize(totTable.getItemSize());
		setDocName(totDoc.getName());
	}

	/*********** Item values ***********/
	protected final void setItem(TotDoc totDoc) {

		setItemList(new ItemList());
		getItemList().executeQuery(totDoc.getId());
	}

	public String getDocName() {
		return docName;
	}

	protected void itemAnalysis(TotDoc totDoc) {
		Item item = new Item();
		setInItemCount(item.getInCount(totDoc.getId()));
		setStrInItemNum(item.getStartInNum(totDoc.getId()));
		setInItemNameList(item.getInNameList(totDoc.getId()));
		setTitleDataList(item.getTitleDataList(totDoc.getId()));
	}

	public String getForm() {
		return form;
	}

	public ItemList getItemList() {
		return itemList;
	}

	/**
	 * @return the docID
	 */
	public TotDoc getTotDoc() {
		return totDoc;
	}

	/**
	 * @param docID the docID to set
	 */
	public void setTotDoc(TotDoc totDoc) {
		this.totDoc = totDoc;
	}

	/**
	 * @return the formID
	 */
	public Long getFormID() {
		return formID;
	}

	/**
	 * @param formID the formID to set
	 */
	public void setFormID(Long formID) {
		this.formID = formID;
	}

	/**
	 * @param docName the docName to set
	 */
	public void setDocName(String docName) {
		this.docName = docName;
	}

	/**
	 * @return the defItemSize
	 */
	public int getDefItemSize() {
		return defItemSize;
	}

	/**
	 * @param defItemSize the defItemSize to set
	 */
	public void setDefItemSize(int defItemSize) {
		this.defItemSize = defItemSize;
	}

	/**
	 * @param form the form to set
	 */
	public void setForm(String form) {
		this.form = form;
	}

	/**
	 * @return the viewType
	 */
	public int getViewType() {
		return viewType;
	}

	/**
	 * @param viewType the viewType to set
	 */
	public void setViewType(int viewType) {
		this.viewType = viewType;
	}

	/**
	 * @return the outType
	 */
	public int getOutType() {
		return outType;
	}

	/**
	 * @param outType the outType to set
	 */
	public void setOutType(int outType) {
		this.outType = outType;
	}

	/**
	 * @return the tableTypes
	 */
	public String getTableTypes() {
		return tableTypes;
	}

	/**
	 * @param tableTypes the tableTypes to set
	 */
	public void setTableTypes(String tableTypes) {
		this.tableTypes = tableTypes;
	}

	/**
	 * @param itemList the itemList to set
	 */
	public void setItemList(ItemList itemList) {
		this.itemList = itemList;
	}

	/**
	 * @return the inItemCount
	 */
	public int getInItemCount() {
		return inItemCount;
	}

	/**
	 * @param inItemCount the inItemCount to set
	 */
	public void setInItemCount(int inItemCount) {
		this.inItemCount = inItemCount;
	}

	/**
	 * @return the strInItemNum
	 */
	public int getStrInItemNum() {
		return strInItemNum;
	}

	/**
	 * @param strInItemNum the strInItemNum to set
	 */
	public void setStrInItemNum(int strInItemNum) {
		this.strInItemNum = strInItemNum;
	}

	/**
	 * @return the titleRowCount
	 */
	public int getTitleRowCount() {
		return titleRowCount;
	}

	/**
	 * @param titleRowCount the titleRowCount to set
	 */
	public void setTitleRowCount(int titleRowCount) {
		this.titleRowCount = titleRowCount;
	}

	/**
	 * @return the titleDataList
	 */
	public String[] getTitleDataList() {
            //24.Public 메소드로부터 반환된 Private배열_CWE-495 : Update by KWON,HAN
	    //	return titleDataList;
                
            String[] ret = null;
            if( this.titleDataList != null) {
                ret = new String[titleDataList.length];
                for (int i=0; i<titleDataList.length; i++) {
                    ret[i] = this.titleDataList[i];
                }
            }
            return ret;
            //====================================================================                   
	}

	/**
	 * @param titleDataList the titleDataList to set
	 */
	public void setTitleDataList(String[] titleDataList) {
            //01.Private 배열에 Public 데이터 할당_CWE-496 : Update by KWON,HAN
		//this.titleDataList = titleDataList;
            
            this.titleDataList = new String[titleDataList.length];
            for(int i = 0; i < titleDataList.length; ++i) {
                this.titleDataList[i] = titleDataList[i];            
            }
            //===============================================================
	}
        
	/**
	 * @return the inItemNameList
	 */
	public String[] getInItemNameList() {
	    //	return inItemNameList;
                
            String[] ret = null;
            if( this.inItemNameList != null) {
                ret = new String[inItemNameList.length];
                for (int i=0; i<inItemNameList.length; i++) {
                    ret[i] = this.inItemNameList[i];
                }
            }
            return ret;                
	}

	/**
	 * @param inItemNameList the inItemNameList to set
	 */
	public void setInItemNameList(String[] inItemNameList) {
	    //	this.inItemNameList = inItemNameList;
                
            this.inItemNameList = new String[inItemNameList.length];
            for(int i = 0; i < inItemNameList.length; ++i) {
                this.inItemNameList[i] = inItemNameList[i];            
            }                
	}
}
