
<%
/************************************************************@@
  * Program Name	: dept_list.jsp
  * Description		: 부서
  * Author				: 배정환
  * Create Date		: 2009-03-06
  * History				:
@@************************************************************/
%>

<%@ page contentType="text/html; charset=UTF-8"
	import="kr.co.kihyun.beans.user.HttpSSOLogin"
	import="kr.co.kihyun.moumi.Moumi" import="kr.co.kihyun.beans.user.Part"
	import="kr.co.kihyun.beans.user.Dept"
	import="kr.co.kihyun.beans.user.DeptList"%>
<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="javax.jdo.Transaction"%>
<%@page import="kr.co.kihyun.beans.entity.util.*"%>
<%@page import="kr.co.kihyun.beans.entity.MDept"%>
<%@page import="kr.co.kihyun.beans.entity.MUser"%>
<%@page import="java.util.List"%>
<%@page import="javax.jdo.Query"%>
<%@page import="kr.co.kihyun.lang.MString"%>

<%
PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
Transaction tx = pm.currentTransaction();
try{
	/********** session내의 userId **********/
	String ptID = HttpSSOLogin.getOrganID(request);
	String dtID = HttpSSOLogin.getDeptID(request);
	int sysAuth = HttpSSOLogin.getSysAuth(request);
	MDept mDept = null;
	Query q = null;
	List<MUser> mUsers = null;
	
	/************ PartList **************/
	int partCount;
	String partID=ptID;
	String partName="";
	String partUpperDept="";
	String partOrgan="";
	boolean partSelType=false;

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

	tx.begin();

	if(Moumi.TOTADM == sysAuth || Moumi.TOTPER == sysAuth || Moumi.SYSADM == sysAuth){
		partName=part.getName(ptID);
		partUpperDept=part.getUpperDept(ptID);
		partOrgan=part.getOrgan(ptID);
		partSelType=part.getSelType(ptID);
	}

	tx.rollback();

	DeptList deptList = new DeptList();
	deptList.depthIDList(dtID, ptID);

	String[] depthIDList = deptList.getIDList();

	int depthCount = depthIDList.length;

	StringBuffer stringTree = new StringBuffer();

	stringTree.append("<ul class=\"mktree\" id=\"tree1\">");

	if(partSelType){
		stringTree.append("<li id=\""+partID+"\"><span id=\"span_"+partID+"\" style=\"cursor:pointer;\" onclick=\"toggleF('"+partID+"', '"+partSelType+"'); selectItem('"+partID+"');  parent.userList('"+partID+"','"+partName+"','dept');\"><img src=\"/totsys/common/images/plus.gif\" id='i_"+partID+"'></span>&nbsp;<span id=\"spanA_"+partID+"\" style=\"cursor:pointer;\" onclick=\"toggleF('"+partID+"', '"+partSelType+"'); selectItem('"+partID+"');  parent.userList('"+partID+"','"+partName+"','dept');\">"+partName+"</span><input id='h_"+partID+"' type='hidden' value='close'/><div id=\"t_"+partID+"\" style='display:none'></div></li>");
	}else{
		stringTree.append("<li id=\""+partID+"\"><span id=\"span_"+partID+"\" style=\"cursor:pointer;\" onclick=\"toggleF('"+partID+"', '"+partSelType+"'); selectItem('"+partID+"'); parent.userList('','','');\"><img src=\"/totsys/common/images/uplus.gif\" id='i_"+partID+"'></span>&nbsp;<span id=\"spanA_"+partID+"\" style=\"cursor:pointer;\" onclick=\"selectItem('"+partID+"'); parent.userList('','','');\">"+partName+"</span><input id='h_"+partID+"' type='hidden' value='close'/><div id=\"t_"+partID+"\" style='display:none'></div></li>");
	}

	stringTree.append("</ul>");

	String tree = stringTree.toString();
	
	if(depthCount>0){
		int dp=0;

		StringBuffer subTree = null;
		String[] inDept = new String[depthCount];

		for(int dpt=depthCount; dpt>0; dpt--){
			int deptCount;
			String[] deptIDList;
			String[] deptNameList;
			String[] deptOrganList;
			boolean[] deptSelTypeList;

			int userCount;			
			
			deptList.lowIDList(depthIDList[dpt-1]);
			
			deptCount = deptList.getCount();
			deptIDList = deptList.getIDList();
			deptNameList = deptList.getNameList();
			deptOrganList = deptList.getOrganList();
			deptSelTypeList = deptList.getSelTypeList();

			tx.begin();

			mDept = pm.getObjectById(MDept.class, depthIDList[dpt-1]);
			q = pm.newQuery(MUser.class);
			q.setFilter("isDeleted == false && mDept == dept");
			q.declareParameters("String dept");
			mUsers = (List<MUser>) q.execute(mDept.getId());
			userCount = mUsers.size();
			
			subTree = new StringBuffer();
			
			String dtName = dept.getName(depthIDList[dpt-1]);
			String ptName = part.getName(dept.getPartID(depthIDList[dpt-1]));
			
			if(deptCount>0 || userCount>0){
				subTree.append("<ul class=\"mktree_b\">");

				for(int u=0; u<userCount; u++){
					String uname=ptName+"-"+dtName+"-"+mUsers.get(u).getName(pm);
					subTree.append("<li id=\""+mUsers.get(u).getId()+"\"><span id=\"span_"+mUsers.get(u).getId()+"\" style=\"cursor:pointer;\" onclick=\"parent.userList('"+mUsers.get(u).getId()+"/"+depthIDList[dpt-1]+"','"+uname+"', 'user'); selectItem('"+mUsers.get(u).getId()+"');\"><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()+"/"+depthIDList[dpt-1]+"','"+uname+"', 'user'); selectItem('"+mUsers.get(u).getId()+"');\">"+mUsers.get(u).getName(pm)+"&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]){
						subTree.append("<li id=\""+deptIDList[d]+"\"><span id=\"span_"+deptIDList[d]+"\" style=\"cursor:pointer;\" onclick=\"toggleF('"+deptIDList[d]+"', '"+deptSelTypeList[d]+"'); selectItem('"+deptIDList[d]+"'); 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>&nbsp;<span id=\"spanA_"+deptIDList[d]+"\" style=\"cursor:pointer;\" onclick=\"selectItem('"+deptIDList[d]+"'); parent.userList('"+deptIDList[d]+"','"+dname+"', 'dept');\">"+deptNameList[d]+"</span><input id='h_"+deptIDList[d]+"' type='hidden' value='close'/><div id=\"t_"+deptIDList[d]+"\" style='display:none'></div></li>");
					}else{
						subTree.append("<li id=\""+deptIDList[d]+"\"><span id=\"span_"+deptIDList[d]+"\" style=\"cursor:pointer;\" onclick=\"toggleF('"+deptIDList[d]+"', '"+deptSelTypeList[d]+"'); selectItem('"+deptIDList[d]+"'); parent.userList('', '', '');\"><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>&nbsp;<span id=\"spanA_"+deptIDList[d]+"\" style=\"cursor:pointer;\" onclick=\"selectItem('"+deptIDList[d]+"'); parent.userList('', '', '');\">"+deptNameList[d]+"</span><input id='h_"+deptIDList[d]+"' type='hidden' value='close'/><div id=\"t_"+deptIDList[d]+"\" style='display:none'></div></li>");
					}
				}
				subTree.append("</ul>");
			}else{
				subTree.append("");
			}

			tx.rollback();

			inDept[dp++] = subTree.toString();
		}

		for(int dpt=0; dpt<depthCount; dpt++){
			tree = tree.replaceAll("<div id=\"t_"+depthIDList[depthCount-(1+dpt)]+"\" style='display:none'></div>","<div id=\"t_"+depthIDList[depthCount-(1+dpt)]+"\">"+inDept[dpt]+"</div>");
		}
	}

%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<LINK REL="stylesheet" HREF="/totsys/common/css/mktree.css">
<SCRIPT LANGUAGE="JavaScript" SRC="/totsys/common/js/mktree.js"></SCRIPT>

<script src="/totsys/common/js/rollover.js"></script>

<script language="javascript">
			parent.userGroup=false;
			function toReportFrame(partID, partName, selectType){
				parent.tstate = true;

				var str = '';				
				str = '/servlet/kr.co.kihyun.beans.user.HttpDeptList?partID='+partID+'&selectType='+selectType;
				parent.leftFrame.location.replace(str);
			}

		function init(partID, selectType){
				parent.tstate = true;

				var str = '';				
				str = '/servlet/kr.co.kihyun.beans.user.HttpDeptList?partID='+partID+'&selectType='+selectType;
				parent.leftFrame.location.replace(str);
		}
		</script>


<script type="text/javascript">
			var xhr = null;
			var subID = "";

			var selected_id = "";
			var old_id = "";

		
function getXMLHttpRequest() {
	if (window.ActiveXObject) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");//IE 상위 버젼
        } catch (e1) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");//IE 하위 버젼
            } catch (e2) {
                return null;
            }
        }
    } else if (window.XMLHttpRequest) {
        return new XMLHttpRequest();//IE 이외의 브라우저(FireFox 등)
    } else {
        return null;
    }
}// XMLHttpRequest 객체 얻기

 

function requestTree(id) {
	subID = id;
    URL = "./subtree.jsp?id=" + id;//한글 처리
    xhr = getXMLHttpRequest();//XMLHttpRequest 객체 얻기
    xhr.onreadystatechange = responseTree;//콜백 함수  등록
    xhr.open("GET", URL, true);//연결
    xhr.send(null);//전송
}// 서버에 요청

 

function responseTree() {
    if (xhr.readyState == 4) {//완료
        if (xhr.status == 200) {//오류없이 OK
            var str = xhr.responseText;//서버에서 보낸 내용 받기
			document.getElementById("t_"+subID).innerHTML = str;//보여주기
        } else {
            alert("Fail : " + httpRequest.status);
        }
    }
}// 응답

function toggleF(id, sel) {
	subID = id;
	var hid = "h_"+id;
    if (document.getElementById("h_"+subID).value == "open") {//폴더열려있음
		document.getElementById("h_"+subID).value = "close";
        document.getElementById("t_"+subID).innerHTML = "";
		document.getElementById("t_"+subID).style.display="none";

		document.getElementById("i_"+subID).src = "/totsys/common/images/open_plus.gif";
		if(sel==1){
			document.getElementById("k_"+subID).src = "/totsys/common/images/plus.gif";
		}else{
			document.getElementById("k_"+subID).src = "/totsys/common/images/uplus.gif";
		}
    }
	else{
		document.getElementById("h_"+subID).value = "open";
		document.getElementById("t_"+subID).style.display="";

		document.getElementById("i_"+subID).src = "/totsys/common/images/open_minus.gif";
		if(sel==1){
			document.getElementById("k_"+subID).src = "/totsys/common/images/minus.gif";
		}else{
			document.getElementById("k_"+subID).src = "/totsys/common/images/uminus.gif";
		}
		requestTree(id);
	}
}// 응답

function selectItem(id) {
	old_id = selected_id;
	selected_id = id;
	var old_item = document.getElementById("spanA_"+old_id);
	if(old_item){
		old_item.style.fontWeight="normal";
	}
    document.getElementById("spanA_"+selected_id).style.fontWeight="bold";
}// 응답
</script>

</head>

<body style='' text="#000000" leftmargin="0" topmargin="0"
	marginwidth="0" marginheight="0">
<TABLE width="100%" cellpadding="5" cellspacing="2" border="0"
	style='padding-left: 0px;'>
	<TBODY style='padding-left: 0px;'>
		<TR style='padding-left: 0px;'>
			<TD align="left" style='padding-left: 0px;'>
			<%//=stringTree.toString()%> <%=tree%></TD>
		</TR>
	</TBODY>
</TABLE>
</body>
</html>
<%
}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();	
}
%>
