/*************************************************************************************************
 * 프로그램명 : ItemList.java 프로그램설명 : 집계자가 올린 프로젝트 리스트 작성자 : 강원중 작성일 : 2003.12.03 변경일 : 2003.11.30
 **************************************************************************************************/

package kr.co.kihyun.beans.totsys.doc.table.item;

import java.sql.*;
import kr.co.kihyun.beans.entity.MoumiEntity;
import kr.co.kihyun.beans.entity.TotItem;
import kr.co.kihyun.beans.entity.TotItemType;

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

import kr.co.kihyun.db.*;
import kr.co.kihyun.lang.MString;
import kr.co.kihyun.moumi.MoumiUtil;

public class ItemList extends DBManager {
	private static final Logger LOG = LoggerFactory.getLogger(ItemList.class);

	private ResultSet rs = null;
	private int count = 0;

	// num, name, desc, type, expr
	private String[] nameList = null;
	private String[] descList = null;
	private String[] exprList = null;

	private int[] numList = null;
	private int[] typeList = null;
	private int[] sizeList = null;
	private int[] bnwList = null;
	private int[] tableNumList = null;
	private int[] colNumList = null;
	private int[] rowNumList = null;

	private String[] typeNameList = null;
	private boolean isDynaTotLine = false;

	/******** 프로젝트 목록을 배열에 입력 ********/
	public void executeQuery(Long docID) {
		StringBuilder sql = new StringBuilder(150);

		try {
			// DB에서 select해온 data를 미리 생성된 배얼에 입력.
			sql.append(" SELECT table_num, name, des, expr, type, num, input_size, col_num, row_num FROM ")
					.append(MoumiEntity.getTableName(TotItem.class)).append(" WHERE doc_id=? ORDER BY num ASC");

			rs = execQuery(sql.toString(), docID);
			if (rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return;
			}

			// DB에거 가저올 레코드 수 만큼 배열을 생성한다.
			nameList = new String[count];
			descList = new String[count];
			exprList = new String[count];

			typeList = new int[count];
			sizeList = new int[count];
			numList = new int[count];
			tableNumList = new int[count];
			colNumList = new int[count];
			rowNumList = new int[count];
			bnwList = new int[count];

			typeNameList = new String[count];

			for (int i = 0; rs.next(); i++) {
				tableNumList[i] = rs.getInt(1);
				nameList[i] = MString.checkNull(rs.getString(2));
				descList[i] = MString.checkNull(rs.getString(3));
				exprList[i] = MString.checkNull(rs.getString(4));
				typeList[i] = rs.getInt(5);
				numList[i] = rs.getInt(6);
				sizeList[i] = rs.getInt(7);
				colNumList[i] = rs.getInt(8);
				rowNumList[i] = rs.getInt(9);
				bnwList[i] = MoumiUtil.getItemBNW(exprList[i]);
				typeNameList[i] = TotItemType.values()[typeList[i]].name();
				if (typeList[i] == TotItemType.INT.ordinal())
					isDynaTotLine = true;
				// LOG.debug("\ntableNumList - {}\nnameList - {}\ndescList - {}\nexprList - {}\ntypeList - {}\nnumList - {}\nsizeList - {}\ncolNumList - {}\nrowNumList",new
				// Object[] {tableNumList[i], nameList[i], descList[i], exprList[i], typeList[i], numList[i],
				// sizeList[i], colNumList[i], rowNumList[i]});
			}
		} catch (SQLException sqlEx) {
			LOG.error("\nSQLState - {}\nMySQL Error Code - {}\nmessage - {}\nsql - {}",
					new Object[] { sqlEx.getSQLState(), sqlEx.getErrorCode(), sqlEx.getMessage(), sql });
			throw new RuntimeException(sqlEx);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			close(rs);
			execClose();
		}
	}

	/******** 프로젝트의 각 Field값 얻는 Method ********/
	public int getCount() {
		return count;
	}

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

	public String[] getDescList() {
	    //	return descList;
                
            String[] ret = null;
            if( this.descList != null) {
                ret = new String[descList.length];
                for (int i=0; i<descList.length; i++) {
                    ret[i] = this.descList[i];
                }
            }
            return ret;                
	}

	public String[] getExprList() {
	    //	return exprList;
                
            String[] ret = null;
            if( this.exprList != null) {
                ret = new String[exprList.length];
                for (int i=0; i<exprList.length; i++) {
                    ret[i] = this.exprList[i];
                }
            }
            return ret;                
	}

	public int[] getTypeList() {
	    //	return typeList;
                
            int[] ret = null;
            if( this.typeList != null) {
                ret = new int[typeList.length];
                for (int i=0; i<typeList.length; i++) {
                    ret[i] = this.typeList[i];
                }
            }
            return ret;                 
	}

	public int[] getSizeList() {
	    //	return sizeList;
                
            int[] ret = null;
            if( this.sizeList != null) {
                ret = new int[sizeList.length];
                for (int i=0; i<sizeList.length; i++) {
                    ret[i] = this.sizeList[i];
                }
            }
            return ret;                   
	}

	public int[] getNumList() {
	    //	return numList;
                
            int[] ret = null;
            if( this.numList != null) {
                ret = new int[numList.length];
                for (int i=0; i<numList.length; i++) {
                    ret[i] = this.numList[i];
                }
            }
            return ret;                
	}

	public int[] getTableNumList() {
	    //	return tableNumList;
                
            int[] ret = null;
            if( this.tableNumList != null) {
                ret = new int[tableNumList.length];
                for (int i=0; i<tableNumList.length; i++) {
                    ret[i] = this.tableNumList[i];
                }
            }
            return ret;                 
	}

	public int[] getFieldNumList() {
	    //	return colNumList;
                
            int[] ret = null;
            if( this.colNumList != null) {
                ret = new int[colNumList.length];
                for (int i=0; i<colNumList.length; i++) {
                    ret[i] = this.colNumList[i];
                }
            }
            return ret;                
	}

	public int[] getRecordNumList() {
	    //	return rowNumList;
                
            int[] ret = null;
            if( this.rowNumList != null) {
                ret = new int[rowNumList.length];
                for (int i=0; i<rowNumList.length; i++) {
                    ret[i] = this.rowNumList[i];
                }
            }
            return ret;                 
	}

	public int[] getBNWList() {
	    //	return bnwList;
                
            int[] ret = null;
            if( this.bnwList != null) {
                ret = new int[bnwList.length];
                for (int i=0; i<bnwList.length; i++) {
                    ret[i] = this.bnwList[i];
                }
            }
            return ret;                
	}

	public String[] getTypeNameList() {
	    //	return typeNameList;
                
            String[] ret = null;
            if( this.typeNameList != null) {
                ret = new String[typeNameList.length];
                for (int i=0; i<typeNameList.length; i++) {
                    ret[i] = this.typeNameList[i];
                }
            }
            return ret;                
	}

	public boolean isDynaTotLine() {
		return isDynaTotLine;
	}
}
