/***********************************************************************************
 * @@ Program Name : HttpUserShareList.java 
 * @@ Description  : 사용자공유 목록 
 * @@ Author       : KWON,HAN
 * @@ Create Date  : 2014.08.25
 * @@ History      :
 ***********************************************************************************/

package kr.co.kihyun.beans.user;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.co.kihyun.lang.Encoder;
import kr.co.kihyun.moumi.MoumiConfig;
import kr.co.kihyun.lang.MString;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebServlet("/servlet/kr.co.kihyun.beans.user.HttpUserShareList")
public class HttpUserShareList extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private static final Logger LOG = LoggerFactory.getLogger(HttpUserShareList.class);

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, NumberFormatException {
        doPost(req, res);
        return;
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, NumberFormatException {
        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();

        String idList = Encoder.toJava(req.getParameter("idList"));
        String typeList = Encoder.toJava(req.getParameter("typeList"));
        String nameList = Encoder.toJava(req.getParameter("nameList"));
        String deptNameList = Encoder.toJava(req.getParameter("deptNameList"));

        if (!MString.isNull(idList)) {  
            //v2. 9.크로스사이트 스크립트 (PrintWrier) : Update by KWON,HAN
//            out.println(getRightCheckList(idList, typeList, nameList, deptNameList));
            
            // 수정 : 외부 입력값 필터링
            String filtered_idList = idList.replaceAll("<","").replaceAll(">","").replaceAll("&","");
            String filtered_typeList = typeList.replaceAll("<","").replaceAll(">","").replaceAll("&","");
            String filtered_nameList = nameList.replaceAll("<","").replaceAll(">","").replaceAll("&","");
            String filtered_deptNameList = deptNameList.replaceAll("<","").replaceAll(">","").replaceAll("&","");
            //LOG.debug("v2 9.크로스사이트 스크립트 (PrintWrier) : HttpUserShareList.doPost() filtered_idList={}, Test Ok", filtered_idList);
            //LOG.debug("v2 9.크로스사이트 스크립트 (PrintWrier) : HttpUserShareList.doPost() filtered_typeList={}, Test Ok", filtered_typeList);
            //LOG.debug("v2 9.크로스사이트 스크립트 (PrintWrier) : HttpUserShareList.doPost() filtered_nameList={}, Test Ok", filtered_nameList);
            //LOG.debug("v2 9.크로스사이트 스크립트 (PrintWrier) : HttpUserShareList.doPost() filtered_deptNameList={}, Test Ok", filtered_deptNameList);
            out.println(getRightCheckList(filtered_idList, filtered_typeList, filtered_nameList, filtered_deptNameList));   
            //========================================             
        }
    }
    
    
    private String getRightCheckList(String idList, String typeList, String nameList, String deptNameList) {
        StringBuilder strbuf = new StringBuilder(8192);

        int count = 0;
        String[] arrayId = idList.split(",");             
        String[] arrayType = typeList.split(",");
        String[] arrayName = nameList.split(",");
        String[] arrayDeptName = deptNameList.split(",");    
            
        count = arrayId.length;
        
//        System.out.println("HttpUserShareList.getRightCheckList() ============");
//        for (int i = 0; i < arrayId.length; i++) { 
//            System.out.println("부서명 ="+arrayDeptName[i]+"/ 사용자명="+arrayName[i]+"(사용자ID="+arrayId[i]+")");
//        }
//        System.out.println("==================================================");
        
        strbuf.append("<script language=\"javascript\">\n\n");
        strbuf.append("	function setList() {\n");
        strbuf.append("		parent.idList = \"").append(idList).append("\";\n");
        strbuf.append("		parent.typeList = \"").append(typeList).append("\";\n");        
        strbuf.append("		parent.nameList = \"").append(nameList).append("\";\n");
        strbuf.append("		parent.deptNameList = \"").append(deptNameList).append("\";\n");    
        strbuf.append("	}\n\n");
        strbuf.append("	function movelist(){\n");
        strbuf.append("		var id = '';\n");
        strbuf.append("		var type = '';\n");
        strbuf.append("		var name = '';\n");        
        strbuf.append("		var deptname = '';\n");
        strbuf.append("		var addForm = document.AddrList;\n");
      for (int i = 0; i < count; i++) {
        strbuf.append("		if(addForm.name");
        strbuf.append(i);
        strbuf.append(".checked == false){\n");
        strbuf.append("			id += addForm.id");
        strbuf.append(i);
        strbuf.append(".value+',';\n");
        strbuf.append("			type += addForm.type");
        strbuf.append(i);
        strbuf.append(".value+',';\n");        
        strbuf.append("			name += addForm.name");
        strbuf.append(i);
        strbuf.append(".value+',';\n");
        strbuf.append("			deptname += addForm.deptname");
        strbuf.append(i);
        strbuf.append(".value+',';\n");        
        strbuf.append("		}\n\n");
      }
        strbuf.append("		parent.leftFrameDel(name, id, type, deptname);\n");
        strbuf.append("	}\n\n");
        strbuf.append("	function AllSelect() {\n");
        strbuf.append("		var addForm = document.AddrList;\n");
      for (int i = 0; i < count; i++) {
        strbuf.append("		addForm.name");
        strbuf.append(i);
        strbuf.append(".checked = true;\n");
      }
        strbuf.append("	}\n\n");
        strbuf.append("	function AllCancel() {\n");
        strbuf.append("		var addForm = document.AddrList;\n");
      for (int i = 0; i < count; i++) {
        strbuf.append("		addForm.name");
        strbuf.append(i);
        strbuf.append(".checked = false;\n");
      }
        strbuf.append("	}\n\n");
        strbuf.append("</script>\n");
        /******************** HTML 생성 ********************/ 
        strbuf.append("<html>\n");
        strbuf.append("	<head></head>\n");
        strbuf.append("<link rel='stylesheet' href='/totsys/common/css/text.css' type='text/css'>\n");
        strbuf.append("	<body bgcolor=\"#FFFFFF\" text=\"#000000\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" onload=\"setList()\">\n");
        strbuf.append("		<form name=AddrList method=Post>\n");
        strbuf.append("		<table border=0 cellpadding=5 cellspacing=2 width=100%>\n");
        strbuf.append("			<tr><td><font size=2 face=\"돋움\">").append(MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.submit")).append(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.request")).append(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.devision")).append("(").append(count).append(")</font></td></tr>\n");
        strbuf.append("			<tr><td><font size=2 face=\"돋움\"><span style='cursor:hand' onclick='AllSelect()'>").append(MoumiConfig.getMessageBundle().getString("moumi.message.button_small.selectAll")).append("</span> | <span style='cursor:hand' onclick='AllCancel()'>").append(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.all")).append(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.cancel")).append("</span></font></td></tr>\n");
        strbuf.append("			<tr>\n");
        strbuf.append("				<td>\n");
      for (int i = 0; i < count; i++) {
        strbuf.append("		");
        strbuf.append("<font size=2 face=\"돋움\">");
        strbuf.append(i+1);
        strbuf.append(".</font>");
        strbuf.append("&nbsp;<input type=checkbox name=name");
        strbuf.append(i);
        strbuf.append(" value=\"");
        strbuf.append(arrayName[i]);       //사용자명   
        strbuf.append("\" onclick=\"\">&nbsp;<font size=2 color=\"black\" face=\"돋움\">");
        strbuf.append(arrayDeptName[i]);   //부서명 / 사용자명(사용자ID)
        strbuf.append(" / ");   
        strbuf.append(arrayName[i]);   
        strbuf.append("(");   
        strbuf.append(arrayId[i]);    
        strbuf.append(")");         
        strbuf.append("</font><br>\n");
        strbuf.append("					<input type=hidden name=id");
        strbuf.append(i);
        strbuf.append(" value=\"");
        strbuf.append(arrayId[i]);         //사용자ID
        strbuf.append("\">\n");
        strbuf.append("					<input type=hidden name=type");
        strbuf.append(i);
        strbuf.append(" value=\"");
        strbuf.append(arrayType[i]);       //type
        strbuf.append("\">\n");
        strbuf.append("					<input type=hidden name=deptname");
        strbuf.append(i);
        strbuf.append(" value=\"");
        strbuf.append(arrayDeptName[i]);   //부서명
        strbuf.append("\">\n");        
      }
        strbuf.append("				</td>\n");
        strbuf.append("			</tr>\n");
        strbuf.append("		</table>\n");
        strbuf.append("		</form>\n");
        strbuf.append("	</body>\n");
        strbuf.append("</html>\n");

        return strbuf.toString();
    }    
    
}
