package idc.service.command;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utility.StringToDate;
import idc.model.*;

public class IdcCSInfoUpdateHandleCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	public void execute(HttpServletRequest request, HttpServletResponse response){
		
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request)){
			request.setAttribute("reError", dao.reError);
			return;
		}else{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
				
		String vpc_num = request.getParameter("vpc_num");
		String service_line = request.getParameter("service_line");
		String client_name = request.getParameter("client_name");
		String vpc_code = request.getParameter("vpc_code");
		String vpc_csip = request.getParameter("vpc_csip");
		String service_ip = request.getParameter("service_ip");
		String server_code = request.getParameter("server_code");
		String vpc_connect_domain = request.getParameter("vpc_connect_domain");
		String vpc_connect_domain_dn = request.getParameter("vpc_connect_domain_dn");
		String vpc_connect_domain_my = request.getParameter("vpc_connect_domain_my");
		String vpc_connect_domain_etc = request.getParameter("vpc_connect_domain_etc");
		String vpc_fireWall = request.getParameter("vpc_fireWall");
		String last_date = request.getParameter("vpc_last_date");
		
		Date vpc_last_date;
		StringToDate st = new StringToDate();
		if(last_date == ""){
			vpc_last_date = null;
		}else{
			vpc_last_date = st.stringToDate(last_date);
		}		
		String vpc_note = request.getParameter("vpc_note");
		
		vpcUpdate( vpc_num,  service_line,  client_name,  vpc_code, vpc_csip,  service_ip,  server_code,  vpc_connect_domain,vpc_connect_domain_dn,vpc_connect_domain_my,vpc_connect_domain_etc,
				 vpc_fireWall,  vpc_last_date,  vpc_note);
				
	}
	
	//서비스 수정 기능 수행
	public void vpcUpdate(String vpc_num, String service_line, String client_name, String vpc_code,
			String vpc_csip, String service_ip, String server_code, String vpc_connect_domain,String vpc_connect_domain_dn,String vpc_connect_domain_my,String vpc_connect_domain_etc,
			String vpc_fireWall, Date vpc_last_date, String vpc_note){		
		
		Connection conn = null;
		PreparedStatement pstmt = null;		
		String sql;
						
		try {
			conn = dao.getConn();			
			sql = "UPDATE vpcDB SET vpc_csip=?, vpc_connect_domain=?,vpc_connect_domain_dn=?,vpc_connect_domain_my=?,vpc_connect_domain_etc=?, vpc_fireWall=?, ";			
			sql += " vpc_last_date=?, vpc_note=? ";
			sql += " WHERE vpc_code=?";
			pstmt = conn.prepareStatement(sql);
						
			pstmt.setString(1, vpc_csip);
			pstmt.setString(2, vpc_connect_domain);
			pstmt.setString(3, vpc_connect_domain_dn);
			pstmt.setString(4, vpc_connect_domain_my);
			pstmt.setString(5, vpc_connect_domain_etc);
			pstmt.setString(6, vpc_fireWall);
			pstmt.setDate(7, vpc_last_date);
			pstmt.setString(8, vpc_note);			
			pstmt.setString(9, vpc_code);
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}