package idc.json.command;

import idc.model.IDCserviceListDTO;
import idc.model.IdcCmd;
import idc.model.IdcDAO;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

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

import utility.IdcGetClientNameCode;
import net.sf.json.JSONObject;

public class IdcGetKCSListJSONCmd implements IdcCmd
{
	private IdcDAO dao;
	
	public IdcGetKCSListJSONCmd()
	{
		dao = new IdcDAO();
	}
	
	//입력한 단어 kcsdb에서 리스트 가져오기
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		ArrayList<String> list = new ArrayList<String>();
		String server_code = request.getParameter("server_code");
		IDCserviceListDTO dto = getService(server_code);
		
		getCS(list, server_code);
		
		JSONObject obj = new JSONObject();
		
		obj.put("a", list);
		obj.put("b", dto);
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		
		try
		{
			System.out.println(obj.toString());
			response.getWriter().write(obj.toString());
		}
		catch (IOException e)
		{
			System.out.println("IdcGetCSListJSONCmd - SEND오류");
			e.printStackTrace();
		}
	}
	
	private IDCserviceListDTO getService(String server_code)
	{
		IDCserviceListDTO dto = new IDCserviceListDTO();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM servicedb WHERE server_code = ?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, server_code);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				dto.setService_code(rs.getString("service_code"));
				dto.setService_line(rs.getString("service_line"));
				dto.setClient_name(IdcGetClientNameCode.getClientName(rs.getString("client_code")));
				dto.setService_ip(rs.getString("service_ip"));
				dto.setService_admin(rs.getString("service_admin"));
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcGetCSListJSONCmd - getService 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcGetCSListJSONCmd - getService 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return dto;
	}
	private void getCS(ArrayList<String> list, String server_code)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM csdb WHERE server_code=? AND cs_case='0' AND cs_name like 'KCS%' ORDER BY cs_name ASC";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, server_code);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				list.add(rs.getString("cs_name"));
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcGetCSListJSONCmd 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcGetCSListJSONCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
