package idc.service.command;

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

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

import java.sql.*;

public class IdcDeffenceInfoUpdateHandleCmd 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 curPage = request.getParameter("curPage");
		request.setAttribute("curPage", curPage);		
		
		String deffence_num = request.getParameter("deffence_num");
		String deffence_case = request.getParameter("deffence_case");
		request.setAttribute("deffence_case", deffence_case);
		
		String deffence_zone = request.getParameter("deffence_zone");
		String deffence_acl = request.getParameter("deffence_acl");
		String deffence_real = request.getParameter("deffence_real");
		String deffence_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 deffence_domain = request.getParameter("deffence_domain");
		String request_date = request.getParameter("deffence_request_date");
		String request_time = request.getParameter("deffence_request_time");
		String deffence_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); 
		}
		
		updateDeffenceDB( deffence_num,  deffence_case,  deffence_zone,  deffence_acl,
				 deffence_real,  deffence_port,  client_name,  vpc_code,  vpc_csip,  server_code,
				 service_ip,  deffence_domain,  deffence_request_date,  deffence_request_time,  deffence_note);
				
	}
	
	//deffence1DB 업데이트
	public void updateDeffenceDB(String deffence_num, String deffence_case, String deffence_zone, String deffence_acl,
			String deffence_real, String deffence_port, String client_name, String vpc_code, String vpc_csip, String server_code,
			String service_ip, String deffence_domain, Date deffence_request_date, Time deffence_request_time, String deffence_note){

		Connection conn = null;
		PreparedStatement pstmt = null;		
		String sql;
						
		try {
			conn = dao.getConn();			
			sql = "UPDATE deffence1DB SET deffence_case=?, deffence_zone=?, deffence_acl=?, ";			
			sql += " deffence_real=?, deffence_port=?, vpc_code=?, vpc_csip=?, deffence_domain=?, deffence_request_date=?,  ";
			sql += " deffence_request_time=?, deffence_note=?, server_code=?, client_name=?, service_ip=? ";
			sql += " WHERE deffence_num=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, deffence_case);
			pstmt.setString(2, deffence_zone);
			pstmt.setString(3, deffence_acl);
			pstmt.setString(4, deffence_real);
			pstmt.setString(5, deffence_port);			
			pstmt.setString(6, vpc_code);
			pstmt.setString(7, vpc_csip);
			pstmt.setString(8, deffence_domain);
			pstmt.setDate(9, deffence_request_date);
			pstmt.setTime(10, deffence_request_time);
			pstmt.setString(11, deffence_note);
			pstmt.setString(12, server_code);
			pstmt.setString(13, client_name);
			pstmt.setString(14, service_ip);
			pstmt.setInt(15, Integer.parseInt(deffence_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();
			}
		}
	}

}