package idc.service.command;

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

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

import utility.IdcGetClientNameCode;
import idc.model.*;

public class IdcDeffenceWriteHandleCmd 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 service_code = request.getParameter("service_code");		
		String deffence_case = request.getParameter("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 server_code = request.getParameter("server_code");
		String service_ip = request.getParameter("service_ip");
		String deffence_domain = request.getParameter("deffence_domain");
		String deffence_note = request.getParameter("deffence_note");
		
		insertDeffenceDB1(deffence_zone, deffence_acl, deffence_real, deffence_port, client_name, vpc_code, server_code, 
				service_ip, deffence_domain, deffence_note, service_code, deffence_case);
	}
	
	//deffence1DB에 추가
	private void insertDeffenceDB1(String deffence_zone, String deffence_acl, String deffence_real, String deffence_port,
			 String client_name, String vpc_code, String server_code, String service_ip, String deffence_domain, String deffence_note, 
			 String service_code, String deffence_case){
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		String sql;
		String vpc_csip = "";
		
		try {
			conn = dao.getConn();
			
			sql = "INSERT INTO deffence1DB (deffence_zone, deffence_acl, deffence_real, deffence_port, client_name, vpc_code, ";
			sql +=  "vpc_csip, server_code, service_ip, deffence_domain, service_code,deffence_request_date, ";
			sql += " deffence_request_time, deffence_note, deffence_case, client_code) ";
			sql += "values(?,?,?,?,?,?,?,?,?,?,?,curdate(),curTime(),?,?,?)";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, deffence_zone);
			pstmt.setString(2, deffence_acl);
			pstmt.setString(3, deffence_real);
			pstmt.setString(4, deffence_port);
			pstmt.setString(5, client_name);
			pstmt.setString(6, vpc_code);
			pstmt.setString(7, vpc_csip);
			pstmt.setString(8, server_code);
			pstmt.setString(9, service_ip);
			pstmt.setString(10, deffence_domain);			
			pstmt.setString(11, service_code);
			pstmt.setString(12, deffence_note);
			pstmt.setString(13, deffence_case);
			pstmt.setString(14, IdcGetClientNameCode.getClientCode(client_name));
			
			pstmt.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
