/***********************************************************************************
 * @@ Program Name : DeptList.java Description : 부서들의 정보 리스트 Author : 강원중 Create Date : 2003-12-19 History :
 * @@
 ***********************************************************************************/

package kr.co.kihyun.beans.user;

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.lang.MLong;

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

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

	private ResultSet rs = null;

	private int count = 0;
	private String[] idList = null;
	private String[] nameList = null;
	private String[] upperDeptIDList = null;
	private String[] organList = null;
	private boolean[] selTypeList = null;

	public void executeQuery(String partID) {
		StringBuilder sql = new StringBuilder(250);

		try {
			sql.append("select dt.id, dt.name, dt.upper_dept, dt.organ, ")
					.append("case when dt.sel_type = 'Y' and dt.sel_type <> '0' then 1 else 0 end sel_type ")
					.append("from ").append(MoumiEntity.getTableName(MDept.class)).append(" dt, ")
					.append(MoumiEntity.getTableName(MDept.class)).append(" pt ")
					.append("where dt.sys_auth<6 and dt.organ=pt.id order by pt.id, dt.id");
//System.out.println("DeptList::::::test1::::"+sql);
			rs = execQuery(sql);
			if (rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return;
			}

			idList = new String[count];
			nameList = new String[count];
			upperDeptIDList = new String[count];
			organList = new String[count];
			selTypeList = new boolean[count];

			for (int i = 0; rs.next(); i++) {
				idList[i] = rs.getString(1);
				nameList[i] = rs.getString(2);
				upperDeptIDList[i] = rs.getString(3);
				organList[i] = rs.getString(4);
				selTypeList[i] = rs.getBoolean(5);
			}
		} 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();
		}
	}

	public void executeIDList(String upperID) {
		StringBuilder sql = new StringBuilder(200);

		try {
			sql.append("select id, name, upper_dept, organ, ")
					.append("case when sel_type = 'Y' and sel_type <> '0' then 1 else 0 end sel_type ")
					.append("from ").append(MoumiEntity.getTableName(MDept.class))
					.append(" where upper_dept=? and id<>organ and sys_auth<6");
//System.out.println("DeptList::::::test2::::"+sql);
			rs = execQuery(sql, upperID);
			if (rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return;
			}

			idList = new String[count];
			nameList = new String[count];
			upperDeptIDList = new String[count];
			organList = new String[count];
			selTypeList = new boolean[count];

			for (int i = 0; rs.next(); i++) {
				idList[i] = rs.getString(1);
				nameList[i] = rs.getString(2);
				upperDeptIDList[i] = rs.getString(3);
				organList[i] = rs.getString(4);
				selTypeList[i] = rs.getBoolean(5);
			}
		} 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();
		}
	}

	public void lowIDList(String upperID) {
		StringBuilder sql = new StringBuilder(250);

		try {
			sql.append("select id, name, upper_dept, organ, ")
					.append("case when sel_type = 'Y' and sel_type <> '0' then 1 else 0 end sel_type ")
					.append("from ").append(MoumiEntity.getTableName(MDept.class))
					.append(" where upper_dept=? and id<>upper_dept and sys_auth<6 order by name");
//System.out.println("DeptList::::::test3::::"+sql);
			rs = execQuery(sql, upperID);
			if (rs != null) {
				rs.last();
				count = rs.getRow();
				rs.beforeFirst();
			} else {
				return;
			}

			idList = new String[count];
			nameList = new String[count];
			upperDeptIDList = new String[count];
			organList = new String[count];
			selTypeList = new boolean[count];

			for (int i = 0; rs.next(); i++) {
				idList[i] = rs.getString(1);
				nameList[i] = rs.getString(2);
				upperDeptIDList[i] = rs.getString(3);
				organList[i] = rs.getString(4);
				selTypeList[i] = rs.getBoolean(5);
			}
		} 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();
		}
	}

	public void depthIDList(String id, String partID) {
		StringBuilder sql = new StringBuilder(60);

		try {
			String ids = id.toString();
			String upperDept = null;

			while (!id.equals(partID)) {
				sql.append("select upper_dept from ").append(MoumiEntity.getTableName(MDept.class))
						.append(" where id=?");
//System.out.println("DeptList::::::test4::::"+sql);
				rs = execQuery(sql, id);
				if (rs.next()) {
					upperDept = rs.getString(1);
				}

				if (upperDept == null || id.equals(upperDept)) {
					break;
				}

				ids = ids + "," + upperDept;
				id = upperDept;
			}

			String[] idsList = ids.split(",");
			idList = new String[idsList.length];
			System.arraycopy(idsList, 0, idList, 0, idList.length);
		} 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();
		}
	}

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

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

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

	public int getCount() {
		return count;
	}
}
