/***********************************************************************************
 * @@ Program Name : Part.java Description : /사용자의 기관에 대한 정보 제공 Author : 강원중 Create Date : 2004-10-25 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.beans.entity.UserDept;

import kr.co.kihyun.db.DBManager;

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

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

	private ResultSet rs = null;

	/******** part name return ********/
	public String getDeptName(String id) {
		StringBuilder sql = new StringBuilder(60);
		String name = "";
		try {
			sql.append("select name from ").append(MoumiEntity.getTableName(MDept.class)).append(" where id=?");

			rs = execQuery(sql, id);
			if (rs.next()) {
				name = rs.getString(1);
			}
			return name;
		} 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 getName(String id) {
		StringBuilder sql = new StringBuilder(70);
		String name = "";
		try {
			sql.append("select name from ").append(MoumiEntity.getTableName(MDept.class))
					.append(" where id=organ and id=?");

			rs = execQuery(sql, id);
			if (rs.next()) {
				name = rs.getString(1);
			}
			return name;
		} 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 getUpperDept(String id) {
		StringBuilder sql = new StringBuilder(70);
		String upperDept = "";
		try {
			sql.append("select ").append(MoumiEntity.getTableName(UserDept.class))
					.append(" from dept where id=organ and id=?");

			rs = execQuery(sql, id);
			if (rs.next()) {
				upperDept = rs.getString(1);
			}
			return upperDept;
		} 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 getOrgan(String id) {
		StringBuilder sql = new StringBuilder(70);
		String organ = "";
		try {
			sql.append("select organ from ").append(MoumiEntity.getTableName(MDept.class))
					.append(" where id=organ and id=?");

			rs = execQuery(sql, id);
			if (rs.next()) {
				organ = rs.getString(1);
			}
			return organ;
		} 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 boolean getSelType(String id) {
		StringBuilder sql = new StringBuilder(150);
		boolean selType = false;
		try {
			sql.append("select case when sel_type = 'Y' and sel_type <> '0' then 1 else 0 end sel_type from ")
					.append(MoumiEntity.getTableName(MDept.class)).append(" where id=organ and id=?");

			rs = execQuery(sql, id);
			if (rs.next()) {
				selType = rs.getBoolean(1);
			}
			return selType;
		} 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();
		}
	}

}
