/*************************************************************************************************
 * 프로그램명 : partList.java 프로그램설명 : 기관 정보 목록 작성자 : 조용준 작성일 : 2004.06.10 변경일 :
 **************************************************************************************************/
package kr.co.kihyun.beans.totsys.sysadm.part;

import java.sql.ResultSet;
import java.sql.SQLException;
import kr.co.kihyun.beans.entity.MDept;
import kr.co.kihyun.beans.entity.MoumiEntity;

import kr.co.kihyun.db.DBManager;
import kr.co.kihyun.moumi.MoumiConfig;

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

public class PartList extends DBManager {
	private static final Logger LOG = LoggerFactory.getLogger(PartList.class);
	private ResultSet rs = null;
	private int count = 0;

	private String[] idList = null;
	private String[] nameList = null;
	private String[] sysAuthList = null;
	private String[] desList = null;
	private String[] upperIDList = null;

	public void executeQuery(String findOption, String findWord) {
		executeQuery(null, findOption, findWord);
	}

	public void executeQuery(String partID, String findOption, String findWord) {
		StringBuilder sql = new StringBuilder(500);

		try {
			sql.append(getSelectClause()).append(getFromClause(partID)).append(getWhereClause(partID))
					.append(getSearchCondition(findOption, findWord))
					.append(getOrderClause());

			rs = execQuery(sql.toString());
			if (rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return;
			}
			setFields(rs);
		} 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();
		}
	}

	private static String getSelectClause() {
		return "select id, name, sys_auth, addr, upper_dept";
	}

	private static String getFromClause(String partID) {
		return new StringBuilder(100).append(" from ").append(MoumiEntity.getTableName(MDept.class)).toString();
	}

	@SuppressWarnings("deprecation")
	private String getWhereClause(String partID) {
		StringBuilder whereString = new StringBuilder(150);

		whereString.append(" where id=organ and sys_auth < ").append(MoumiConfig.PART_REPOADM);
		if (partID != null) {
			whereString.append(" and (id='").append(partID).append("' or upper_dept='").append(partID).append("')");
		}
		return whereString.toString();
	}

	private String getSearchCondition(String findOption, String findWord) {
		StringBuilder result = new StringBuilder(150);

		if ("id".equals(findOption)) {
			return result.append(" and id='").append(findWord).append("'").toString();
		} else if ("partName".equals(findOption)) {
			return result.append(" and name like '%").append(findWord).append("%'").toString();
		} else if ("sys_auth".equals(findOption)) {
			return result.append(" and sys_auth like '%").append(findWord).append("%'").toString();
		} else if ("all".equals(findOption)) {
			return result.append(" and (id like '%").append(findWord).append("%') ")
					.append("or (name like '%").append(findWord).append("%') ")
					.append("or (sys_auth like '%").append(findWord).append("%')").toString();
		} else {
			return "";
		}
	}

	private String getOrderClause() {
		return " order by id";
	}

	private void setFields(ResultSet rs) throws SQLException {

		idList = new String[count];
		nameList = new String[count];
		sysAuthList = new String[count];
		desList = new String[count];
		upperIDList = new String[count];

		for (int i = 0; rs.next(); i++) {
			idList[i] = rs.getString(1);
			nameList[i] = rs.getString(2);
			sysAuthList[i] = rs.getString(3);
			desList[i] = rs.getString(4);
			upperIDList[i] = rs.getString(5);
		}
	}

	public int getCount() {
		return count;
	}

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

	public String[] getNameList() {
	    //	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[] getSysAuthList() {
	    //	return sysAuthList;
                
            String[] ret = null;
            if( this.sysAuthList != null) {
                ret = new String[sysAuthList.length];
                for (int i=0; i<sysAuthList.length; i++) {
                    ret[i] = this.sysAuthList[i];
                }
            }
            return ret;                   
	}

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

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