
<%
/************************************************************@@
  * Program Name	: subtree.jsp
  * Description		: 하위트리 ajax
  * Author				: 배정환
  * Create Date		: 2009-02-03
  * History				:
@@************************************************************/
%>
<%@ page contentType="text/html; charset=UTF-8"
	import="kr.co.kihyun.lang.MLong" import="kr.co.kihyun.beans.user.Part"
	import="kr.co.kihyun.beans.user.Dept"
	import="kr.co.kihyun.beans.user.DeptList"%>
<%@page import="kr.co.kihyun.lang.MString"%>
<%@page import="javax.jdo.Query"%>
<%@page import="kr.co.kihyun.beans.entity.MUser"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="kr.co.kihyun.beans.entity.MDept"%>
<%@page import="kr.co.kihyun.beans.entity.util.*"%>
<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="javax.jdo.Transaction"%>
<%@page import="org.slf4j.Logger"%>
<%@page import="org.slf4j.LoggerFactory"%>

<%
Logger LOG = LoggerFactory.getLogger(this.getClass());
PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
Transaction tx = pm.currentTransaction();
try{
	response.setHeader("Cache-Control","no-cache");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires",0);
	/********** session내의 userId **********/
	String upperID = request.getParameter("id");
	boolean hideUser = MString.isNull(request.getParameter("hideUser")) ? true : new Boolean(request.getParameter("hideUser"));
	LOG.debug("hideUser: {}", hideUser);

	/************ PartList **************/
	int deptCount = 0;
	String[] deptIDList;
	String[] deptNameList;
	String[] deptOrganList;
	boolean[] deptSelTypeList;
	int userCount = 0;
	
	MDept mDept = null;
	Query q = null;
	List<MUser> mUsers = new ArrayList<MUser>();
	
	DeptList deptList  = new DeptList();
	deptList.lowIDList(upperID);
	deptCount = deptList.getCount();
	deptIDList = deptList.getIDList();
	deptNameList = deptList.getNameList();
	deptOrganList = deptList.getOrganList();
	deptSelTypeList = deptList.getSelTypeList();

	tx.begin();

	mDept = (MDept)pm.getObjectById(MDept.class, upperID);
	q = pm.newQuery(MUser.class);
	q.setFilter("isDeleted == false && mDept == dept");
	q.declareParameters("String dept");
 	if (!hideUser) {
                //v2. 13.SQL 삽입 : JDO형식이므로 해결책에 따른 prepare SQL 문으로 변경할 수 없음
		mUsers = (List<MUser>) q.execute(mDept.getId());
                //=================
		userCount = mUsers.size();
	}

	StringBuffer stringTree = new StringBuffer();
	Dept dept = new Dept();
	Part part = new Part();

	String dtName = dept.getName(upperID);
	String ptName = part.getName(dept.getPartID(upperID));




	if(deptCount>0 || userCount>0){
		stringTree.append("<ul class=\"mktree_b\">");
		for(int u=0; !hideUser && u<userCount; u++){
			String uname=ptName+"-"+dtName+"-"+mUsers.get(u).getName(pm);
			stringTree.append("<li id=\""+mUsers.get(u).getId()+"\"><span id=\"span_"+mUsers.get(u).getId()+"\" style=\"cursor:pointer;\" onclick=\"parent.userList('"+mUsers.get(u).getId()+"/"+upperID+"','"+uname+"', 'user'); selectItem('"+mUsers.get(u).getId()+"', 'user');\"><img src=\"/totsys/common/images/bullet.gif\"></span>&nbsp;<span id=\"spanA_"+mUsers.get(u).getId()+"\" style=\"cursor:pointer;\" onclick=\"parent.userList('"+mUsers.get(u).getId()+"/"+upperID+"','"+uname+"', 'user'); selectItem('"+mUsers.get(u).getId()+"', 'user');\">"+mUsers.get(u).getName(pm)+"  ("+mUsers.get(u).getId()+")"+"&nbsp;"+MString.checkNull(mUsers.get(u).getDutyName())+"</span></li>");
		}


		for(int d=0; d<deptCount; d++){
            
			String dname=deptNameList[d];
            
			if(!deptOrganList[d].equals(deptIDList[d])){                
				dname=part.getName(deptOrganList[d])+"-"+dname;
			}
            
			if(deptSelTypeList[d]){
				stringTree.append("<li id=\""+deptIDList[d]+"\"><span id=\"span_"+deptIDList[d]+"\" style=\"cursor:pointer;\" onclick=\"toggleF('"+deptIDList[d]+"', '"+deptSelTypeList[d]+"'); selectItem('"+deptIDList[d]+"', 'dept'); parent.userList('"+deptIDList[d]+"','"+dname+"', 'dept');\"><img src=\"/totsys/common/images/open_plus.gif\" id='i_"+deptIDList[d]+"'>&nbsp;<img src=\"/totsys/common/images/plus.gif\" id='k_"+deptIDList[d]+"'></span><span id=\"spanA_"+deptIDList[d]+"\" style=\"cursor:pointer;\" onclick=\"selectItem('"+deptIDList[d]+"', 'dept'); parent.userList('"+deptIDList[d]+"','"+dname+"', 'dept');\">&nbsp;"+deptNameList[d]+"</span><input id='h_"+deptIDList[d]+"' type='hidden' value='close'/><div id=\"t_"+deptIDList[d]+"\" style='display:none'></div></li>");
			}else{
				stringTree.append("<li id=\""+deptIDList[d]+"\"><span id=\"span_"+deptIDList[d]+"\" style=\"cursor:pointer;\" onclick=\"toggleF('"+deptIDList[d]+"', '"+deptSelTypeList[d]+"'); selectItem('"+deptIDList[d]+"', 'dept'); parent.userList('"+deptIDList[d]+"','"+dname+"', '');\"><img src=\"/totsys/common/images/open_plus.gif\" id='i_"+deptIDList[d]+"'>&nbsp;<img src=\"/totsys/common/images/uplus.gif\" id='k_"+deptIDList[d]+"'></span><span id=\"spanA_"+deptIDList[d]+"\" style=\"cursor:pointer;\" onclick=\"selectItem('"+deptIDList[d]+"', 'dept'); parent.userList('"+deptIDList[d]+"','"+dname+"', '');\">&nbsp;"+deptNameList[d]+"</span><input id='h_"+deptIDList[d]+"' type='hidden' value='close'/><div id=\"t_"+deptIDList[d]+"\" style='display:none'></div></li>");
			}
            
		}

		stringTree.append("</ul>");
	}
	else{

		stringTree.append("");
	}

	tx.rollback();

	if(deptCount>0 || userCount>0){
%>
<%=stringTree.toString()%>
<%
	}
}catch(Exception ex){
	ex.printStackTrace();
	out.println(kr.co.kihyun.text.html.ServletUtil.getJavaScript("location='/servlet/kr.co.kihyun.beans.user.HttpSSOLogin?mode=logout';"));
}finally{
	if(tx.isActive())
		tx.rollback();
	pm.close();
}
%>
