package idc.service.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import idc.model.*;

import java.sql.*;

public class IdcAddDadd1HandleCmd 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_num = request.getParameter("deffence_num");
		String service_code = "";
		String dadd_port = request.getParameter("dadd_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 vpc_csip = request.getParameter("vpc_csip");
		String dadd_domain = request.getParameter("dadd_domain");
		String dadd_note = request.getParameter("dadd_note");
				
		insertDadd1(deffence_num, service_code, dadd_port, client_name,vpc_code,server_code,service_ip, vpc_csip, dadd_domain,dadd_note);
	}
	
	//deffence1add에 추가
	private void insertDadd1(String deffence_num, String service_code, String dadd_port, String client_name, String vpc_code,
		String server_code, String service_ip, String vpc_csip, String dadd_domain, String dadd_note){
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql;
				
		try{			
			conn = dao.getConn();							
			sql = "INSERT INTO deffence1add (deffence_num, service_code, dadd_port, client_name, vpc_code, vpc_csip, server_code, service_ip, dadd_domain, dadd_date,dadd_time,dadd_note) ";			
			sql += "values(?, ?, ?, ?, ?, ?, ?, ?, ?, curdate(), curtime(), ?)";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, Integer.parseInt(deffence_num));
			pstmt.setString(2, service_code);
			pstmt.setString(3, dadd_port);
			pstmt.setString(4, client_name);
			pstmt.setString(5, vpc_code);
			pstmt.setString(6, vpc_csip);
			pstmt.setString(7, server_code);
			pstmt.setString(8, service_ip);
			pstmt.setString(9, dadd_domain);
			pstmt.setString(10, dadd_note);			
			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();
			}
		}
	}
		
}