/*********************************************************************************************************
 * 프로그램명 : HttpFindPasswd.java 프로그램설명 : 프로젝트와 관련된 정보를 얻을수 있는 class 작성자 : 강원중 작성일 : 2003.12.16 변경일 : 2003.12.19
 **********************************************************************************************************/

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 kr.co.kihyun.mail.SendMail;
import kr.co.kihyun.text.javascript.JavaScriptUtil;
@WebServlet("/servlet/kr.co.kihyun.beans.user.HttpFindPasswd")
public class HttpFindPasswd extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException,
			NumberFormatException {

		res.setContentType("text/html;charset=UTF-8");
		PrintWriter out = res.getWriter();

		/************* parameter valeus *************/
		String id = "";
		id = Encoder.toJava(req.getParameter("userID"));

		/************* user info values *************/
		String email = "";
		String passwd = "";
		UserView userView = new UserView();
                //v2. 6.SQL 삽입 : userView.executeQuery에서 prepare SQL 문으로 되어 있다.
		userView.executeQuery(id);
                //================
		email = userView.getEmail();
		passwd = userView.getPasswd();

		if (email.indexOf("@") > 0) {
			String hanmail_Check = email.substring(email.indexOf("@"), email.indexOf("."));
			if (hanmail_Check.equals("@hanmail")) {
				out.println(JavaScriptUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.popup.notmail")));
				out.println(JavaScriptUtil.getJavaScript("window.close();"));
				return;
			}
		}

		if (MString.isNull(email)) {
			out.println(JavaScriptUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.popup.notExistID")));
			String connURL = "location='/totsys/login/passwd/popup_find_passwd.jsp';";
			out.println(JavaScriptUtil.getJavaScript(connURL));
		} else {
			sendPasswd(id, passwd, email);
			// if(isSendMail){
			out.println(JavaScriptUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.popup.passwd") + email + MoumiConfig.getMessageBundle().getString("moumi.message.popup.addressTrans")));
			// }else{
			// out.println(JavaScriptUtil.alert("메일을 발송하지 못하였습니다.\n\n시스템관리자에게 문의 하시요!"));
			// }
			out.println(JavaScriptUtil.getJavaScript("opener.focus();"));
			out.println(JavaScriptUtil.getJavaScript("window.close();"));
		}
	}

	private boolean sendPasswd(String id, String passwd, String receiver) {

		boolean isSendMail = false;
		String sender = "moumi@kihyun.co.kr";
		String topic = id + "("+MoumiConfig.getMessageBundle().getString("moumi.message.popup.idPasswd")+")";
		String mime = "html";
		String content = "id:" + Encoder.toDB(id) + "<br>password:" + Encoder.toDB(passwd) + "<br> "+ MoumiConfig.getMessageBundle().getString("moumi.message.popup.notMailReply");
		content = Encoder.toDB(content);

		SendMail mail = new SendMail(sender, receiver, topic, content, mime);
		// isSendMail= mail.send();
		Thread thread = new Thread(mail);
		thread.start();

		return isSendMail;
	}
}
