package idc.service.command;

import idc.model.*;

import java.sql.*;

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

import utility.StringToDate;

public class IdcDeffenceAddUpdateHandleCmd 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 deffence_case = request.getParameter("deffence_case");
		request.setAttribute("deffence_case", deffence_case);
		
		String dadd_num = request.getParameter("dadd_num");		
		String service_code = request.getParameter("service_code");
		String dadd_port = request.getParameter("deffence_port");
		String client_name = request.getParameter("client_name");
		String vpc_code = request.getParameter("vpc_code");
		String vpc_csip = request.getParameter("vpc_csip");
		String server_code = request.getParameter("server_code");
		String service_ip = request.getParameter("service_ip");
		String dadd_domain = request.getParameter("deffence_domain");
		String request_date = request.getParameter("deffence_request_date");
		String request_time = request.getParameter("deffence_request_time");
		String dadd_note = request.getParameter("deffence_note");
		
		//String을 Date 형으로 형변환
		StringToDate st = new StringToDate();		
		Date deffence_request_date;		
		if(request_date == "" || request_date == null){
			deffence_request_date = null;
		}else{
			deffence_request_date = st.stringToDate(request_date);
		}
		
		//String을 Time 형으로
		Time deffence_request_time = null;		
		if(request_time == "" || request_time == null){
			deffence_request_time = null;
		}else{
			deffence_request_time = Time.valueOf(request_time); 
		}
		
		updateDaddDB( dadd_num,  service_code,  dadd_port,  client_name,  vpc_code,
				 vpc_csip,  server_code,  service_ip,  dadd_domain,  deffence_request_date,  deffence_request_time,  dadd_note);
		
	}
	
	//deffence1add에서 업데이트
	public void updateDaddDB(String dadd_num, String service_code, String dadd_port, String client_name, String vpc_code,
			String vpc_csip, String server_code, String service_ip, String dadd_domain, Date dadd_date, Time dadd_time, String dadd_note){

		Connection conn = null;
		PreparedStatement pstmt = null;		
		String sql;
						
		try {
			conn = dao.getConn();			
			sql = "UPDATE deffence1add SET service_code=?, dadd_port=?, client_name=?, ";			
			sql += " vpc_code=?, vpc_csip=?, server_code=?, service_ip=?, dadd_domain=?, dadd_date=?,  ";
			sql += " dadd_time=?, dadd_note=? ";
			sql += " WHERE dadd_num=?";
			pstmt = conn.prepareStatement(sql);
						
			pstmt.setString(1, service_code);
			pstmt.setString(2, dadd_port);
			pstmt.setString(3, client_name);
			pstmt.setString(4, vpc_code);
			pstmt.setString(5, vpc_csip);			
			pstmt.setString(6, server_code);
			pstmt.setString(7, service_ip);
			pstmt.setString(8, dadd_domain);
			pstmt.setDate(9, dadd_date);
			pstmt.setTime(10, dadd_time);
			pstmt.setString(11, dadd_note);
			pstmt.setInt(12, Integer.parseInt(dadd_num));
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
