package kr.co.kihyun.tree;

import java.io.IOException;
import java.sql.ResultSet;
//import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.co.kihyun.beans.entity.SysAuth;
//import kr.co.kihyun.lang.Encoder;
//import kr.co.kihyun.lang.MInteger;
//import kr.co.kihyun.lang.MLong;
//import kr.co.kihyun.moumi.MoumiConfig;
import kr.co.kihyun.db.DBManager;
import kr.co.kihyun.lang.MString;
import kr.co.kihyun.beans.user.HttpSSOLogin;
//import java.util.Calendar;

public class MakeJSon extends DBManager {

    public int dataCnt = 0, sysAuth;
    public String userId, sql = null;
    public PrintWriter out = null;
    public StringBuffer sbuf = new StringBuffer();
    public String grade="";

    public MakeJSon(HttpServletRequest req, HttpServletResponse res) {
        try {
            res.setContentType("text/html;charset=UTF-8");
            sysAuth = HttpSSOLogin.getSysAuth(req);
            userId = HttpSSOLogin.getLoginID(req);
            out = res.getWriter();
            String id = req.getParameter("id");
            //String rootId = req.getParameter("rootId");
            String rootId = MString.checkNull(req.getParameter("rootId"), null);
            String deptId = req.getParameter("rel");

            String userYN = MString.checkNull(req.getParameter("userYN"), "N");
            String gubun = MString.checkNull(req.getParameter("gubun"), "ORGAN");
            grade = MString.checkNull(req.getParameter("grade"), "");
            if (rootId == null || rootId.equals("null") || rootId.equals("ROOT")) {
                rootId = null;
            }
            sbuf.append("[");
            if (gubun.equals("COMMON")) {
                makeGroupCommonSql(id, userId);
                group2JSon(id);
            } else if (gubun.equals("AREA")) {
                makeGroupAreaSql(id, userId);
                group2JSon(id);
            } else if (gubun.equals("DEPT")) {
                makeGroupDeptSql(id, userId, deptId);
                group2JSon(id);
            } else {
                makeTreeSql(id, rootId);
                dept2JSon();
                if (userYN.equals("Y")) {
                    user2JSon(id);
                }
            }
            sbuf.append("]");
            if (dataCnt > 0) {
                sbuf.deleteCharAt(sbuf.lastIndexOf(","));
            }
            out.println(sbuf.toString());
        //44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 Add by YOUNGJUN,CHO
        } catch(IOException ioex) {
            ioex.printStackTrace();
        //++++++++++++++++++++++++++++++++++++++++++++++++
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public void makeGroupCommonSql(String id, String userId) {
        sql = null;
        if (id.equals("ROOT")) {
            sql  = "SELECT UP.ID      AS PART_ID    \n";
            sql += "     , UP.NAME   AS DEPT_NAME   \n";
            sql += "     , UP.USER_ID               \n";
            sql += "     , UP.ID     AS DEPT_ID     \n";
            sql += "     , 'closed'  AS STATE       \n";
            sql += "     , ''       AS ORGAN_DEPT_NAME       \n";     //2014.09.17 Add by KWON,HAN
            sql += "     , ''       AS UPPER_DEPT_NAME       \n";     //2014.09.17 Add by KWON,HAN
            sql += "     , COUNT(UD.PART_ID) AS PART_ID_COUNT      \n";
            sql += "  FROM MOUMI_USER_DEPT  UD         \n";
            sql += "     , MOUMI_USER_PART  UP     \n";
            sql += "     , MOUMI_DEPT  DT          \n";
            sql += " WHERE UD.PART_ID = UP.ID  \n";
            sql += "   AND UP.USE_YN = 'Y'  \n";
            sql += "   AND UD.ID = DT.ID  \n";
            sql += "   AND DT.SEL_TYPE='Y'          \n";  // 2014.12.19 YOUNGJUN.CHO (공식부서만 조회되도록 조건 추가, SEL_TYPE = 'Y')
            sql += "   AND DT.IS_OUT='N'        \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += "   AND UP.USER_ID IN (SELECT ID FROM MOUMI_MUSER WHERE SYS_AUTH=9)  \n";
            sql += " GROUP BY UP.ID, UP.NAME, UP.USER_ID  \n";
            sql += " ORDER BY UP.NAME  \n";
//System.out.println("MakeJSon.makeGroupCommonSql 1:::id.equals(ROOT)::\n" + sql);
            return;
        } else {
            //2014.09.17 Update by KWON.HAN
            sql  = "SELECT A.PART_ID    \n";
            sql += "     , A.NAME       AS DEPT_NAME    \n";
            sql += "     , ''           AS USER_ID    \n";
            sql += "     , A.ID         AS DEPT_ID    \n";
            sql += "     , 'opened'     AS STATE    \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = ( CASE WHEN dt.id = dt.organ AND dt.upper_dept IS NOT NULL   \n";
            sql += "                                                       THEN ( SELECT organ FROM moumi_dept where id = dt.upper_dept )    \n";
            sql += "                                                       ELSE dt.organ END )    \n";
            sql += "                                                              ) AS ORGAN_DEPT_NAME    \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = dt.upper_dept ) AS UPPER_DEPT_NAME    \n";
            sql += "  FROM MOUMI_USER_DEPT A    \n";
            sql += "     , MOUMI_DEPT      DT    \n";
            sql += " WHERE A.PART_ID = '" + id + "'     \n";
            sql += "   AND A.ID = DT.ID    \n";
            sql += "   AND DT.SEL_TYPE='Y'          \n";  // 2014.12.19 YOUNGJUN.CHO (공식부서만 조회되도록 조건 추가, SEL_TYPE = 'Y')
            sql += "   AND DT.IS_OUT='N'        \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += " ORDER BY A.NAME    \n";

//System.out.println("MakeJSon.makeGroupCommonSql 2:::::\n" + sql);
            return;
        }
    }

    public void makeGroupAreaSql(String id, String userId) {
        sql = null;
        if (id.equals("ROOT")) {
            sql = " SELECT UP.ID    AS PART_ID       \n";
            sql += "      , UP.NAME  AS DEPT_NAME     \n";
            sql += "      , UP.USER_ID                \n";
            sql += "      , UP.ID    AS DEPT_ID       \n";
            sql += "      , 'closed' AS STATE         \n";
            sql += "     , ''       AS ORGAN_DEPT_NAME       \n";     //2014.09.17 Add by KWON,HAN
            sql += "     , ''       AS UPPER_DEPT_NAME       \n";     //2014.09.17 Add by KWON,HAN
            sql += "     , COUNT(UD.PART_ID) AS PART_ID_COUNT      \n";
            sql += " FROM MOUMI_USER_DEPT UD      \n";
            sql += "      , MOUMI_USER_PART UP  \n";
            sql += "      , MOUMI_DEPT DT       \n";
            sql += " WHERE UD.PART_ID = UP.ID   \n";
            sql += "   AND UP.USE_YN = 'Y'    \n";
            sql += "   AND UD.ID=DT.ID   \n";
            sql += "   AND DT.SEL_TYPE='Y'          \n";  // 2014.12.19 YOUNGJUN.CHO (공식부서만 조회되도록 조건 추가, SEL_TYPE = 'Y')
            sql += "   AND DT.IS_OUT='N'        \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += "   AND UP.USER_ID IN (    \n";
            sql += "                      SELECT US.ID   \n";
            sql += "                        FROM MOUMI_MUSER US  \n";
            sql += "                           , MOUMI_DEPT DT   \n";
            sql += "                       WHERE US.SYS_AUTH=7   \n";
            sql += "                         AND US.DEPT_ID = DT.ID   \n";
            sql += "                         AND DT.SEL_TYPE='Y'          \n";  // 2014.12.19 YOUNGJUN.CHO (공식부서만 조회되도록 조건 추가, SEL_TYPE = 'Y')
            sql += "                         AND DT.IS_OUT='N'        \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += "                         AND DT.ORGAN = ( SELECT DT.ORGAN   \n";
            sql += "                                            FROM MOUMI_MUSER US   \n";
            sql += "                                               , MOUMI_DEPT DT    \n";
            sql += "                                           WHERE US.ID='" + userId + "'   \n";
            sql += "                                             AND US.DEPT_ID = DT.ID   \n";
            sql += "                                             AND DT.SEL_TYPE='Y'          \n";  // 2014.12.19 YOUNGJUN.CHO (공식부서만 조회되도록 조건 추가, SEL_TYPE = 'Y')
            sql += "                                             AND DT.IS_OUT='N'        \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += "                                        )   \n";
            sql += "                     )    \n";
            sql += " GROUP BY UP.ID, UP.NAME, UP.USER_ID  \n";
            sql += " ORDER BY UP.NAME   \n";

//System.out.println("MakeJSon.makeGroupAreaSql:::id.equals(ROOT)::\n" + sql);
            return;
        } else {
            //2014.09.17 Update by KWON.HAN
            sql  = "SELECT A.PART_ID    \n";
            sql += "     , A.NAME       AS DEPT_NAME    \n";
            sql += "     , ''           AS USER_ID    \n";
            sql += "     , A.ID         AS DEPT_ID    \n";
            sql += "     , 'opened'     AS STATE    \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = ( CASE WHEN dt.id = dt.organ AND dt.upper_dept IS NOT NULL   \n";
            sql += "                                                       THEN ( SELECT organ FROM moumi_dept where id = dt.upper_dept )    \n";
            sql += "                                                       ELSE dt.organ END )    \n";
            sql += "                                                              ) AS ORGAN_DEPT_NAME    \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = dt.upper_dept ) AS UPPER_DEPT_NAME    \n";
            sql += "  FROM MOUMI_USER_DEPT A    \n";
            sql += "     , MOUMI_DEPT      DT    \n";
            sql += " WHERE A.PART_ID = '" + id + "'     \n";
            sql += "   AND A.ID = DT.ID    \n";
            sql += "   AND DT.SEL_TYPE='Y'          \n";  // 2014.12.19 YOUNGJUN.CHO (공식부서만 조회되도록 조건 추가, SEL_TYPE = 'Y')
            sql += "   AND DT.IS_OUT='N'        \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += " ORDER BY A.NAME    \n";

//System.out.println("MakeJSon.makeGroupAreaSql:::::\n" + sql);
            return;
        }
    }

    public void makeGroupDeptSql(String id, String userId, String deptId) {
        sql = null;
        if (id.equals("ROOT")) {
            sql =  " SELECT UP.ID    AS PART_ID       \n";
            sql += "     , UP.NAME  AS DEPT_NAME     \n";
            sql += "     , UP.USER_ID                \n";
            sql += "     , UP.ID    AS DEPT_ID       \n";
            sql += "     , 'closed' AS STATE         \n";
            sql += "     , ''       AS ORGAN_DEPT_NAME       \n";     //2014.09.17 Add by KWON,HAN
            sql += "     , ''       AS UPPER_DEPT_NAME       \n";     //2014.09.17 Add by KWON,HAN
            sql += "     , COUNT(UD.PART_ID) AS PART_ID_COUNT       \n";
            sql += "  FROM         \n";
            sql += "       MOUMI_USER_DEPT UD         \n";
            sql += "     , MOUMI_USER_PART UP         \n";
            sql += "     , MOUMI_DEPT DT              \n";
            sql += " WHERE    \n";
            sql += "       UD.PART_ID = UP.ID          \n";  // 2014.12.19 YOUNGJUN.CHO (공식부서만 조회되도록 조건 추가, SEL_TYPE = 'Y')
            sql += "   AND UP.USE_YN = 'Y'           \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += "   AND UD.ID = DT.ID         \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += "   AND DT.SEL_TYPE='Y'        \n";
            sql += "   AND DT.IS_OUT='N'        \n";
            sql += "   AND UP.USER_ID IN (        \n";
            sql += "                      select us.id    \n";
            sql += "                        from moumi_muser us   \n";
            sql += "                       where us.dept_id =    \n";
            sql += "                          ( SELECT US.DEPT_ID    \n";
            sql += "                           FROM MOUMI_MUSER US   \n";
            sql += "                                        WHERE US.ID='" + userId + "'   \n";
            sql += "                                     )   \n";
            sql += "                     )    \n";
            sql += " GROUP BY UP.ID, UP.NAME, UP.USER_ID         \n";
            sql += " ORDER BY UP.NAME    \n";

//System.out.println("MakeJSon.makeGroupDeptSql:::id.equals(ROOT)::\n" + sql);
            return;
        } else {
            //2014.09.17 Update by KWON.HAN
            sql  = "SELECT A.PART_ID    \n";
            sql += "     , A.NAME       AS DEPT_NAME    \n";
            sql += "     , ''           AS USER_ID    \n";
            sql += "     , A.ID         AS DEPT_ID    \n";
            sql += "     , 'opened'     AS STATE    \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = ( CASE WHEN dt.id = dt.organ AND dt.upper_dept IS NOT NULL   \n";
            sql += "                                                       THEN ( SELECT organ FROM moumi_dept where id = dt.upper_dept )    \n";
            sql += "                                                       ELSE dt.organ END )    \n";
            sql += "                                                              ) AS ORGAN_DEPT_NAME    \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = dt.upper_dept ) AS UPPER_DEPT_NAME    \n";
            sql += "  FROM MOUMI_USER_DEPT A    \n";
            sql += "     , MOUMI_DEPT      DT    \n";
            sql += " WHERE A.PART_ID = '" + id + "'     \n";
            sql += "   AND A.ID = DT.ID    \n";
            sql += "   AND DT.SEL_TYPE='Y'          \n";  // 2014.12.19 YOUNGJUN.CHO (공식부서만 조회되도록 조건 추가, SEL_TYPE = 'Y')
            sql += "   AND DT.IS_OUT='N'        \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += " ORDER BY A.NAME    \n";

//System.out.println("MakeJSon.makeGroupDeptSql:::::\n" + sql);
            return;
        }
    }

    public void group2JSon(String id) {
        int ii = 0;
        ResultSet rs = null;
        String partId, deptName, userId, deptId, upper, organ, seltype, pri, state = "closed";
        partId = deptName = userId = deptId = upper = organ = seltype = pri = "";

        //2014.09.17 Add by KWON,HAN  +++
        String organdeptname;
        String upperdeptname;
        organdeptname = "";
        upperdeptname = "";
        //+++++++++++++++++++++++++++++++

        try {
            rs = execQuery(sql);
            if (rs == null) {
                return;
            }
            while (rs.next()) {
                partId = MString.checkNull(rs.getString(1));
                deptName = MString.checkNull(rs.getString(2));
                if (id.equals("ROOT")) {
                    //deptName = new GroupCount().getCount(partId, deptName);
                    deptName = deptName + "(" + MString.checkNull(rs.getString(8), "0") + ")";
                }
                userId = MString.checkNull(rs.getString(3));
                deptId = MString.checkNull(rs.getString(4));
                state = MString.checkNull(rs.getString(5));

                organdeptname = MString.checkNull(rs.getString(6));      //2014.09.17 Add by KWON,HAN
                upperdeptname = MString.checkNull(rs.getString(7));      //2014.09.17 Add by KWON,HAN

                sbuf.append("{ \"data\":\"" + deptName + "\",\"attr\": { \"ID\":\"" + deptId + "\", ");
                sbuf.append("\"UPPER\":\"" + upper + "\",\"ORGAN\":\"" + organ + "\", \"SELTYPE\":\"" + seltype + "\", ");
                sbuf.append("\"deptname\":\"" + deptName + "\",");           //2014.09.17 Add by KWON,HAN
                sbuf.append("\"organdeptname\":\"" + organdeptname + "\","); //2014.09.17 Add by KWON,HAN
                sbuf.append("\"upperdeptname\":\"" + upperdeptname + "\","); //2014.09.17 Add by KWON,HAN
                sbuf.append("\"PRI\":\"" + pri + "\",\"rel\":\"GROUP\"},\"state\" : \"" + state + "\"}");
                sbuf.append(",");
                dataCnt++;

            }
            close(rs);
            pstmt.close();
        //44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 Add by YOUNGJUN,CHO
        } catch(SQLException sqlex) {
            sqlex.printStackTrace();
        //++++++++++++++++++++++++++++++++++++++++++++++++
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (rs != null) {
                close(rs);
            }
            execClose();
        }
    }

    public void makeTreeSql(String id, String rootId) {
        sql = null;
        if (rootId != null) {
            sql = "SELECT dt.ID, dt.NAME, dt.UPPER_DEPT, dt.ORGAN,                                                   \n";
            sql += "  ( CASE WHEN dt.SEL_TYPE='Y' AND dt.SEL_TYPE<>'0' THEN 1 ELSE 0 END ) AS SEL_TYPE, dt.PRIORITY   \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = ( CASE WHEN dt.id = dt.organ AND dt.upper_dept IS NOT NULL   \n";
            sql += "                                                       THEN ( SELECT organ FROM moumi_dept where id = dt.upper_dept )    \n";
            sql += "                                                       ELSE dt.organ END )    \n";
            sql += "                                                              ) AS ORGAN_DEPT_NAME    \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = dt.upper_dept ) AS UPPER_DEPT_NAME    \n";
            sql += "FROM MOUMI_DEPT dt                                                                 \n";
            sql += "WHERE dt.ORGAN IS NOT NULL AND dt.ID='" + rootId + "'                                       \n";
            sql += "   AND DT.IS_OUT='N'        \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)

                // 매일 2회 batch 실행시, 직속기관인경우 직속기관의 ORGAN 컬럼을 ID 로 UPDATE 하는 경우에 or 조건은 주석처리한다.
            // 사용자가 속한 부서의 상위기관까지만 조회되도록 한다.
            // 2014.09.16 by youngjun cho.
            //sql+="  OR (UPPER_DEPT='"+rootId+"' AND ID=ORGAN)                                       \n";
            sql += "ORDER BY dt.NAME                                                                     \n";

          //System.out.println("::makeTreeSql:::rootId!=null:id::::"+id+"::::::::rootId::::::::"+rootId);

            return;
        }
        if (id.equals("ROOT")) {
            sql = "SELECT dt.ID, dt.NAME, dt.UPPER_DEPT, dt.ORGAN,                                                   \n";
            sql += "  ( CASE WHEN SEL_TYPE='Y' AND SEL_TYPE<>'0' THEN 1 ELSE 0 END ) AS SEL_TYPE, dt.PRIORITY   \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = ( CASE WHEN dt.id = dt.organ AND dt.upper_dept IS NOT NULL   \n";
            sql += "                                                       THEN ( SELECT organ FROM moumi_dept where id = dt.upper_dept )    \n";
            sql += "                                                       ELSE dt.organ END )    \n";
            sql += "                                                              ) AS ORGAN_DEPT_NAME    \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = dt.upper_dept ) AS UPPER_DEPT_NAME    \n";
            sql += "FROM MOUMI_DEPT  dt                                                                 \n";
            sql += "WHERE dt.ORGAN IS NOT NULL AND dt.UPPER_DEPT IS NULL                                    \n";
            sql += "  AND dt.SYS_AUTH<3                                                                  \n";
            sql += "   AND DT.IS_OUT='N'        \n";   // 2014.09.22 BY YOUNGJUN CHO(폐교 제외조건 추가)
            sql += "ORDER BY dt.NAME                                                                     \n";

//System.out.println("::makeTreeSql:::id.equals(ROOT):::id::::"+id+"::::::::rootId::::::::"+rootId);
            System.out.println(sql);
            return;
        }

            sql = "SELECT dt.ID, dt.NAME, dt.UPPER_DEPT, dt.ORGAN,                                                   \n";
            sql += "  ( CASE WHEN dt.SEL_TYPE='Y' AND dt.SEL_TYPE<>'0' THEN 1 ELSE 0 END ) AS SEL_TYPE, dt.PRIORITY   \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = ( CASE WHEN dt.id = dt.organ AND dt.upper_dept IS NOT NULL   \n";
            sql += "                                                       THEN ( SELECT organ FROM moumi_dept where id = dt.upper_dept )    \n";
            sql += "                                                       ELSE dt.organ END )    \n";
            sql += "                                                              ) AS ORGAN_DEPT_NAME    \n";
            sql += "     , ( SELECT name FROM moumi_dept where id = dt.upper_dept ) AS UPPER_DEPT_NAME    \n";
            sql += "FROM MOUMI_DEPT  dt                                                                  \n";
            sql += "WHERE dt.UPPER_DEPT='" + id + "' AND dt.ID <> dt.UPPER_DEPT AND dt.SYS_AUTH < 6                       \n";

            // 제출기관지정시 폐교된 학교는 조회되지 않도록 한다.(IS_OUT : 'Y'= 폐교됨 / 'N'= 폐교안됨)
        // 매일 2회 batch 실행시, TC_SYMOCMOC000 테이블의 ORG_UON 컬럼값으로 체크하여 MOUMI_DEPT(IS_OUT) 을 UPDATE 한다.
        // (ORG_UON : 'Y' = 폐교안됨 / 'N' = 폐교됨)
        // 2014.09.16 by youngjun cho.
        sql += "  AND dt.IS_OUT = 'N'                                                                \n";

        //기관관리자일때 유초중고직속사용자는  제외 by wonseok Lee 20171108
        if(grade.equals("N")) {
            sql += " AND dt.SEL_TYPE='Y'                                                                \n";
        }
        sql += "ORDER BY dt.NAME                                                                     \n";

//System.out.println("makeTreeSql:::::\n"+sql);
        return;
    }


    public void dept2JSon() {
        int ii = 0;
        ResultSet rs = null;
        String deptId, name, upper, organ, seltype, pri;
        deptId = name = upper = organ = seltype = pri = null;

        //2014.09.17 Add by KWON,HAN  +++
        String organdeptname;
        String upperdeptname;
        organdeptname = "";
        upperdeptname = "";
        //+++++++++++++++++++++++++++++++

//System.out.println("dept2JSon:::::\n"+sql);
        try {
            rs = execQuery(sql);
            if (rs == null) {
                return;
            }
            while (rs.next()) {
                deptId = MString.checkNull(rs.getString(1));
                name = MString.checkNull(rs.getString(2));
                upper = MString.checkNull(rs.getString(3));
                organ = MString.checkNull(rs.getString(4));
                seltype = MString.checkNull(rs.getString(5));
                pri = MString.checkNull(rs.getString(6));

                organdeptname = MString.checkNull(rs.getString(7));      //2014.09.17 Add by KWON,HAN
                upperdeptname = MString.checkNull(rs.getString(8));      //2014.09.17 Add by KWON,HAN

                sbuf.append("{ \"data\":\"" + name + "\",\"attr\": { \"ID\":\"" + deptId + "\", ");
                sbuf.append("\"UPPER\":\"" + upper + "\",\"ORGAN\":\"" + organ + "\", \"SELTYPE\":\"" + seltype + "\", ");
                sbuf.append("\"deptname\":\"" + name + "\",");               //2014.09.17 Add by KWON,HAN
                sbuf.append("\"organdeptname\":\"" + organdeptname + "\","); //2014.09.17 Add by KWON,HAN
                sbuf.append("\"upperdeptname\":\"" + upperdeptname + "\","); //2014.09.17 Add by KWON,HAN
                sbuf.append("\"PRI\":\"" + pri + "\",\"rel\":\"DEPT\"},\"state\" : \"closed\"}");
                sbuf.append(",");
                dataCnt++;

            }
            close(rs);
            pstmt.close();
        //44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 Add by YOUNGJUN,CHO
        } catch(SQLException sqlex) {
            sqlex.printStackTrace();
        //++++++++++++++++++++++++++++++++++++++++++++++++
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (rs != null) {
                close(rs);
            }
            execClose();
        }
    }

    public void user2JSon(String id) {

    	//System.out.println("GRADE>>>>>>>>"+grade);
        int ii = 0;
        ResultSet rs = null;
        String userId, name, sysAuth;
        String subSysadm = Integer.toString(SysAuth.SUB_SYSADM.ordinal());  //2014.10.15 Add by KWON,HAN
        String deptName;              //2014.08.26 Add by KWON,HAN
        //2014.08.26 Update by KWON,HAN
        //sql="SELECT ID,sp_cm_dec_fu(NAME),sys_auth, DEPT_ID FROM MOUMI_MUSER WHERE DEL_TYPE='N' AND DEPT_ID='"+id+"'";
        //기관관리자일때 유초중고직속사용자는 제외 by wonseok Lee 20171108
        if(grade.equals("N")){
        	sql = "SELECT * FROM ( SELECT ID,sp_cm_dec_fu(NAME),sys_auth, (SELECT X.NAME FROM MOUMI_DEPT X WHERE X.ID = DEPT_ID AND ROWNUM = 1), (SELECT X.GRADE FROM MOUMI_DEPT X WHERE X.ID = DEPT_ID AND ROWNUM = 1) FROM MOUMI_MUSER WHERE DEL_TYPE='N' AND DEPT_ID='" + id + "'  ORDER BY NAME_GENCPT ) A WHERE A.GRADE IS NULL";
        }
        else {
        	sql = "SELECT ID,sp_cm_dec_fu(NAME),sys_auth, (SELECT X.NAME FROM MOUMI_DEPT X WHERE X.ID = DEPT_ID AND ROWNUM = 1) FROM MOUMI_MUSER WHERE DEL_TYPE='N' AND DEPT_ID='" + id + "'  ORDER BY NAME";
        }

     	//System.out.println("sql:::::"+sql);
        try {
            rs = execQuery(sql);
            if (rs == null) {
                return;
            }
            while (rs.next()) {
                userId = MString.checkNull(rs.getString(1));
                name = MString.checkNull(rs.getString(2));
                sysAuth = MString.checkNull(rs.getString(3));
                deptName = MString.checkNull(rs.getString(4));    //2014.08.26 Add by KWON,HAN

            //2014.10.15 Update by KWON,HAN
                sbuf.append("{ \"data\":\"" + name + "(" + userId + ")");
            if(sysAuth.equals("9")) {
                sbuf.append(" [시스템관리자]");
            }else if(sysAuth.equals("7")){
            	sbuf.append(" [기관관리자]");
            }else if(sysAuth.equals("6")){
            	sbuf.append(" [부서관리자]");
            }else if(sysAuth.equals("5")){
            	sbuf.append(" [학교관리자]");
            }
                sbuf.append("\",");
                sbuf.append("\"attr\": { \"ID\":\"" + userId + "\", \"SYSAUTH\":\"" + sysAuth + "\",");
            //=============================
                sbuf.append("\"name\":\"" + name + "\",");         //2014.08.26 Add by KWON,HAN
                sbuf.append("\"deptid\":\"" + id + "\",");         //2014.08.26 Add by KWON,HAN
                sbuf.append("\"deptname\":\"" + deptName + "\","); //2014.08.26 Add by KWON,HAN
                sbuf.append("\"rel\":\"USER\"},\"state\" : \"opened\"}");
                sbuf.append(",");
                dataCnt++;
            }
            close(rs);
            pstmt.close();

        //44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 Add by YOUNGJUN,CHO
        } catch(SQLException sqlex) {
            sqlex.printStackTrace();
        //++++++++++++++++++++++++++++++++++++++++++++++++
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (rs != null) {
                close(rs);
            }
            execClose();
        }
    }
}
