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 idc.model.*;

public class IdcDeffenceWriteCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();

	public void execute(HttpServletRequest request, HttpServletResponse response) {
		String service_code = request.getParameter("service_code");
		String client_code = request.getParameter("client_code");
		String deffence_case = request.getParameter("deffence_case");
		
		IdcDeffenceDTO idf;
		idf = getIdcDeffenceDTO(service_code,client_code);
		request.setAttribute("dlist", idf);
		request.setAttribute("deffence_case", deffence_case);				
	}
	
	private IdcDeffenceDTO getIdcDeffenceDTO(String service_code, String client_code){
		IdcDeffenceDTO idf = new IdcDeffenceDTO();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT c.client_name, service_ip, s.server_code, c.client_code, s.service_code  ";
			sql += "from serviceDB as s Inner join clientDB as c ";
			sql += "where s.client_code = c.client_code and s.service_code=?";
						
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			rs = pstmt.executeQuery();
			
			if(rs.next()){								
				idf.setClient_name(rs.getString("client_name"));
				idf.setServer_code(rs.getString("server_code"));
				idf.setClient_code(client_code);
				idf.setService_code(service_code);
				idf.setService_ip(rs.getString("service_ip"));								
			}			
			
		} 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();
			}
		}
		
		return idf;
	}

}
