/***********************************************************************************
 * @@ Program Name : UserList.java Description : 사용자들의 정보 리스트 Author : 배정환 Create Date : 2009-02-11 History :
 * @@
 ***********************************************************************************/

package kr.co.kihyun.beans.user;

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

import kr.co.kihyun.db.DBManager;
import kr.co.kihyun.lang.MString;

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

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

	private ResultSet rs = null;

	private int count = 0;
	private String[] idList = null;
	private String[] nameList = null;
	private Long[] deptIDList = null;
	private String[] dutyNameList = null;

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

		try {
			sql.append("select us.id, sp_cm_dec_fu(us.name), em.dept_id, us.duty_name, us.priority from ")
					.append(MoumiEntity.getTableName(MUser.class)).append(" us, ")
					.append(MoumiEntity.getTableName(Emul.class)).append(" em ")
					.append("where em.user_id=us.id and us.sys_auth<6 and (us.del_type='0' or us.del_type='N')");
			if (deptID != null) {
				sql.append(" and em.dept_id=").append(deptID);
			}
			sql.append(" order by us.priority");
			LOG.debug("userList Query: {}", sql);

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

			idList = new String[count];
			nameList = new String[count];
			deptIDList = new Long[count];
			dutyNameList = new String[count];

			rs = execQuery(sql);

			for (int i = 0; rs.next(); i++) {
				idList[i] = rs.getString(1);
				nameList[i] = rs.getString(2);
				deptIDList[i] = rs.getLong(3);
				dutyNameList[i] = MString.checkNull(rs.getString(4));
			}
		} 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 Long[] getDeptIDList() {
	    //	return deptIDList;
                
            Long[] ret = null;
            if( this.deptIDList != null) {
                ret = new Long[deptIDList.length];
                for (int i=0; i<deptIDList.length; i++) {
                    ret[i] = this.deptIDList[i];
                }
            }
            return ret;                  
	}

	public int getCount() {
		return count;
	}

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