/* 
    IntraPMS Project
    사용자의 개인 정보를 변경하는 서블릿

    
*/
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;

import java.util.*;

import java.sql.DriverManager ;
import java.sql.Connection ;
import java.sql.Statement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;

import com.etri.groupware.*;
import com.etri.groupware.organization.*;

public class PersonInformationChangeServlet extends HttpServlet
{
	GroupwareControllerInterface gc = null ;
	OrganizationManagementControllerInterface  omc = null ;
	Properties myPro = null ;

	public void init ( ServletConfig config ) throws ServletException {
		super.init ( config ) ;
		System.out.println ( "PersonInformationChangeServlet init () Call..........." ) ;

		// 그룹웨어 컨트롤러를 생성한다.
		// 먼저 프라퍼티를 생성한다.
		myPro = new Properties ( ) ;
		// DB매니저 구현 클래스 이름 설정
		myPro.put ( "OrganizationDBManagerClassName" , "PandaSoftOrganizationDBManager" ) ;
		myPro.put ( "BBSDBManagerClassName" , "PandaSoftBBSDBManager" ) ;
		myPro.put ( "EDADBManagerClassName" , "PandaSoftEDADBManager" ) ;
		myPro.put ( "MailDBManagerClassName" , "PandaSoftMailDBManager" ) ;
		////////////////////////////////////////
		System.out.println ( "모든 사원을 보여주는 서블릿의 프라퍼티 설정을 무사히 마쳤습니다." ) ;
		
		try {
			gc = GroupwareController.init ( myPro ) ;
			/////////////////////////////////////////////////
			System.out.println ( "모든 사원을 보여주는 서블릿에서 사용하는 그룹웨어 컨트롤러 생성을 무사히 마쳤습니다." ) ;
			
			// 조직도 관리 컨트롤러를 가져온다.
			omc = gc.createOrganizationManagementController ( ) ;
			///////////////////////////////////////////////////////////
			System.out.println ( "모든 사원을 보여주는 서블릿에서 사용하는 조직도 관리 컨트롤러 생성도 무사히 마쳤습니다." ) ;
		} catch ( Exception e ) { }
	}
	
	public void destroy ( ) {
		super.destroy();
		DBConnectionManager.instance.release() ;
		System.out.println ( "PersonInformationChangeServlet destroy () Call..........." ) ;
	}

	/*----------------------------------------------------------------- */
	/**
	 [메소드이름]: doPost<br>
	 [기능]: 사용자의 개인 정보를 변경한다.<br>
	 [파라미터]: HttpServletRequest request, HttpServletResponse response<br>
	 [호출자] : 사용자<br>
	 */
	/*--------------------------------------------------------------- */	
	public void doPost (
		HttpServletRequest request ,
		HttpServletResponse response ) throws ServletException , IOException
	{
		PrintWriter out ;
		String head = " 팬더 소프트 " ;
		// Set content type and other response header fields first
		response.setContentType ( "text/html;charset=euc-kr" ) ;
		// then write the data of the response
		out = new PrintWriter ( new OutputStreamWriter ( response.getOutputStream ( ) , "EUC-KR" ) ) ;
		
		/////////////////////////////////////////////
		// 세션 확인 부분 - 인증되지 않은 사용자의 경우 Login.html 로 되돌려 보낸다.
		String authenKind = null ;
		String authenId = null ;
		String authenName = null ;
		String authenLoginStatus = null;
		
		HttpSession session = request.getSession ();
		authenLoginStatus =  (String)session.getValue("Authentication");
		
		if( (authenLoginStatus == null) || ( !(authenLoginStatus.equals("true")) ) ){
			session.invalidate();
			response.sendRedirect("../Login.html");
		} else {
			authenKind = (String)session.getValue("Kind");
			authenId = (String)session.getValue("Id");
			authenName = (String)session.getValue("Name");   
		}
		/////////////////////////////////////////////
		
		// 파일을 서버에 저장하기 위해서 InputStream을 PareseRequestData로 바꾸어 준다. 
		ParseRequestData pD = new ParseRequestData ( request.getInputStream() ) ;
		
		// 데이타베이스에서 얻어들 모든 사원의 내용으로 String의 배열형태이다.
		Official[] allOfficial = null ;
		
		// 변경의 경우에 실행되는 메서드이므로 파라미터를 순서대로 받는다. 
		String id = pD.getParameter("id") ;		
		id = new String ( id.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		String password = null ;
		String residentRegistrationNum = null ;			
		password = pD.getParameter("password") ;
		password = new String ( password.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		residentRegistrationNum = pD.getParameter("residentRegistrationNum") ;
		residentRegistrationNum = new String ( residentRegistrationNum.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		int sex = Integer.parseInt ( pD.getParameter("sex") ) ;
		String familyName = null ;
		familyName = pD.getParameter("familyName") ;
		familyName = new String ( familyName.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		String firstName = pD.getParameter("firstName") ;
		firstName = new String ( firstName.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		String orders = pD.getParameter("orders") ;
		orders = new String ( orders.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		int age = 0 ;
		age = Integer.parseInt ( pD.getParameter("age") ) ;		
	  	String upDep = pD.getParameter ( "department" ) ;
		upDep = new String ( upDep.getBytes("ISO-8859-1") , "EUC-KR" ) ;			
	  	String o2 = upDep ;
  		String name = pD.getParameter ( "team" ) ;
		name = new String ( name.getBytes("ISO-8859-1") , "EUC-KR" ) ;	
	  	String o1 = name ;
		String department = upDep ;
		String team = name ;
		String phoneNumber = null ;
		phoneNumber = pD.getParameter("phoneNumber") ;
		phoneNumber = new String ( phoneNumber.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		String faxNumber = null ;
		faxNumber = pD.getParameter("faxNumber") ;
		faxNumber = new String ( faxNumber.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		String personalPhone = null ;
		personalPhone = pD.getParameter("personalPhone") ;
		personalPhone = new String ( personalPhone.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		String homePhoneNumber = null ;
		homePhoneNumber = pD.getParameter("homePhoneNumber") ;
		homePhoneNumber = new String ( homePhoneNumber.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		String address = pD.getParameter("address") ;
		address = new String ( address.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		String zipCode = null ;
		zipCode = pD.getParameter("zipCode") ;
		zipCode = new String ( zipCode.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		int userKind = Integer.parseInt ( pD.getParameter ( "userKind") ) ;
		int year = Integer.parseInt ( pD.getParameter("year") ) ;
		int month = Integer.parseInt ( pD.getParameter("month") ) ;
		int day = Integer.parseInt ( pD.getParameter("day") ) ;
		String weightWork = pD.getParameter("weightWork") ;		
		weightWork = new String ( weightWork.getBytes("ISO-8859-1") , "EUC-KR" ) ;
		String others = pD.getParameter("others") ;		
		others = new String ( others.getBytes("ISO-8859-1") , "EUC-KR" ) ;
			
		String filePath = "c:/groupware/file/org" ;
		String fileName = id ;
			
		Vector attachedFiles = pD.getAttachedFileNames();
		String fName = null ;
		
		if(!attachedFiles.isEmpty()){
			for(int i=0; i<attachedFiles.size() ; i++){
				try {
					//pD.saveFile((String)attachedFiles.elementAt(i), filePath, fileName);
					pD.saveFile( filePath, fileName);
				} catch ( Exception e ) { }
				fName = fileName + "-" + attachedFiles.elementAt(i) ;
			}  
		}
     	
		Official newOfficial = new Official ( ) ;
			
		newOfficial.setId ( id ) ;
		newOfficial.setPassword ( password ) ;
		newOfficial.setResidentRegistrationNum ( residentRegistrationNum ) ;
		newOfficial.setFirstName ( firstName ) ;
		newOfficial.setFamilyName ( familyName ) ;
		if ( sex == 1 ) newOfficial.setSex ( true ) ;
		else newOfficial.setSex ( false ) ;
		newOfficial.setAge ( age ) ;
		newOfficial.setAddress ( address ) ;
		newOfficial.setZipCode ( zipCode ) ;
		newOfficial.setPhoneNumber ( phoneNumber ) ;
		newOfficial.setPersonalPhone ( personalPhone ) ;
		newOfficial.setWeightWork ( weightWork ) ;
		newOfficial.setDepartment ( department ) ;
		newOfficial.setTeam ( team ) ;
		newOfficial.setOrders ( orders ) ;
		Calendar enterCompanyDay = Calendar.getInstance ( ) ;
		enterCompanyDay.set ( Calendar.YEAR , year ) ;
		enterCompanyDay.set ( Calendar.MONTH , month ) ;
		enterCompanyDay.set ( Calendar.DATE , day ) ;
		newOfficial.setEnterCompanyDay ( enterCompanyDay ) ;
		newOfficial.setHomePhoneNumber ( homePhoneNumber ) ;
		newOfficial.setFaxNumber ( faxNumber ) ;
		newOfficial.setOthers ( others ) ;
		if ( userKind == 0 ) newOfficial.setUserKind ( 0 ) ;
		else newOfficial.setUserKind ( 1 ) ;
		newOfficial.setImagePath ( fName ) ;
				
		// 변경하기 버튼을 눌렀을 경우....
		OrganizationResultStatus ors = omc.updateOfficial ( newOfficial ) ;
		if ( ors.getResultStatus() == 1 ) {
			// 변경하는것에 실패했을 경우
			out.println ( "<html><head><title>에러</title></head>" ) ;
			out.println ( "<body><p align=\"center\">에러가 발생했습니다.</p>" ) ;
			out.println ( "<p align=\"center\">" + ors.getReason() + "</p>" ) ;
			out.println ( "<p align=\"center\"><input type=\"button\" name=\"update\" value=\"되돌아 가기\" onClick=\"history.back();\"></p>" ) ;
			out.println ( "</body></html>" ) ;
			out.close ( ) ;
		}
	
		out.println ( "<html><head><title>팬더 소프트" + upDep + name + "</title>" ) ;

		out.println ( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=euc-kr\">" ) ;
		out.println ( "<style type=\"text/css\">" ) ;
		out.println ( "BODY {font-size:9pt; color:\"#000000\"; line-height:14px;}" ) ;
		out.println ( "A:Link {font-size:9pt; color:\"#000033\"; line-height:14px; text-decoration:none}" ) ;
		out.println ( "A:Visited {font-size:9pt; color:\"#330033\"; line-height:14px; text-decoration:none}" ) ;
		out.println ( "A:Hover {font-size:9pt; color:\"#0000ff\"; line-height:14px; text-decoration:underline}" ) ;
		out.println ( "td {font-size:9pt;}" ) ;
		out.println ( "td.red {font-size:9pt; color:\"#ff0000\"}" ) ;
		out.println ( "td.black {font-size:9pt; color:\"#000000\"}" ) ;
		out.println ( "td.navy {font-size:9pt; color:\"#000066\"}" ) ;
		out.println ( "td.space {font-size:1pt;}" ) ;
		out.println ( "</style>  </HEAD>" ) ;

		out.println ( "<body>" ) ;
		out.println ( "<p align=right><img src=\"/image/우측_개인정보.gif\" alt=\"개인 정보 변경\"></p>" ) ;
		out.println ( "<br><br><p align=center>사용자의 정보가 변경되었습니다.</p>" ) ;
		out.println ( "<br><br><p align=center><img src=\"/image/home_line.jpg\"></p>" ) ;
		out.println ( "<br><br><p align=center><a href=\"/servlet/ShowStartPageServlet\"><img src=\"/image/home.gif\" alt=\"처음 페이지로\"></a></p>" ) ;
		out.println ( "<br><br><p align=center><img src=\"/image/home_line.jpg\"></p>" ) ;
		
		out.println ( "</body></html>" ) ;
				
		out.close ( ) ;
	}	
}