/*********************************************************************************************************
	프로그램명		: HttpSaveExcel.java
	프로그램설명	: 시스템 관리자 사용차 엑셀저장
	작성자			: 
	작성일			: 
	변경일			:	
**********************************************************************************************************/

package kr.co.kihyun.beans.totsys.sysadm.user;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import kr.co.kihyun.lang.MString;
import kr.co.kihyun.lang.Encoder;

import kr.co.kihyun.moumi.Moumi;
import kr.co.kihyun.beans.user.HttpSSOLogin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebServlet("/servlet/kr.co.kihyun.beans.totsys.sysadm.user.HttpSaveExcel")
public class HttpSaveExcel extends HttpServlet{
        private static final Logger LOG = LoggerFactory.getLogger(HttpSaveExcel.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

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

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
		res.setContentType("application/x-msexcel;charset=UTF-8");

		PrintWriter out = res.getWriter();

		/************ Parameter ************/		
		String findOption = MString.checkNull(req.getParameter("findOption"));
		String findWord = Encoder.toJava(req.getParameter("findWord"));	

		/************ user list values ************/		
		UserList userList = new UserList();
              //v2. 5.static final 필드 변조 가능성 : Update by KWON,HAN      
		//if(HttpSSOLogin.getSysAuth(req) == Moumi.SYSADM){
                if(HttpSSOLogin.getSysAuth(req) == Moumi.SYSADM){
                    LOG.debug("v2. 5.static final 필드 변조 가능성 : HttpSaveExcel.doGet() Not Test / Moumi.SYSADM = {}", Moumi.SYSADM);
              //==========================================================    
                        //v2. 6.SQL 삽입 : JDO형식이므로 해결책에 따른 prepare SQL 문으로 변경할 수 없음
			userList.executeQuery(findOption, findWord);
                        //===============
		}else{
                        //v2. 6.SQL 삽입 : JDO형식이므로 해결책에 따른 prepare SQL 문으로 변경할 수 없음
			userList.executeQuery(HttpSSOLogin.getOrganID(req), findOption, findWord);
                        //===============
		}

		int count = userList.getCount();
		String[] idList = userList.getIDList();
		String[] nameList = userList.getNameList();
		String[] passwdList = userList.getPasswdList();
		String[] sysAuthList = userList.getSysAuthList();
		String[] deptNameList = userList.getDeptNameList();

		String saveFileName = "cybermoumi-UserList";
		
		res.setHeader("Accept-Ranges", "bytes");
		res.setHeader("Content-Disposition","attachment; filename="+saveFileName+".xls;");		
		out.println(Excel.getHeader());
		
		StringBuffer excelSaveBuf = new StringBuffer();

		excelSaveBuf.append("<CENTER><B>사용자 리스트</B></CENTER>");
		excelSaveBuf.append("<TABLE cellSpacing=1 cellPadding=3 width=800 align=center border=1>");
		excelSaveBuf.append("	<TBODY>");
		excelSaveBuf.append("		<TR>");
		excelSaveBuf.append("			<TD align=middle bgColor=#dddddd><b>아이디</b></TD>");
		excelSaveBuf.append("				<TD class=class66 align=middle bgColor=#dddddd><b>비밀번호</b></TD>");
		excelSaveBuf.append("				<TD class=class66 align=middle bgColor=#dddddd><b>성명</b></TD>");
		excelSaveBuf.append("				<TD class=class66 align=middle bgColor=#dddddd><b>권한</b></TD>");
		excelSaveBuf.append("				<TD class=class66 align=middle bgColor=#dddddd><b>소속기관/부서</b></TD>");
		excelSaveBuf.append("		</TR>");
	
	for(int i = 0; i < count; i++){	
		excelSaveBuf.append("		<TR onmouseover=\"this.style.backgroundColor='eeeeee'\" onmouseout=\"this.style.backgroundColor='white'\" bgColor=\"white\">");
		excelSaveBuf.append("			<TD class=class00 align=middle>"+idList[i]+"</TD>");
		excelSaveBuf.append("			<TD class=class00 align=middle>"+passwdList[i]+"</TD>");
		excelSaveBuf.append("			<TD class=class00 align=middle>"+nameList[i]+"</TD");
		excelSaveBuf.append("			<TD class=class00 align=middle>"+sysAuthList[i]+"</TD");
		excelSaveBuf.append("			<TD class=class00 align=middle>"+deptNameList[i]+"</TD");
		excelSaveBuf.append("		</TR>");
	}
	
		excelSaveBuf.append("	</TBODY>");
		excelSaveBuf.append("</TABLE>");		

		out.println(excelSaveBuf);
		out.println(Excel.getTail());
		out.close();
	}
}
