package idc.line.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcIPDBDTO;

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;

public class IdcIPModifyFormCmd implements IdcCmd
{
	private IdcDAO dao;
	public IdcIPModifyFormCmd()
	{
		dao = new IdcDAO();
	}
	
	//ip수정 창
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String ip_address = request.getParameter("ip_address");
		
		IdcIPDBDTO dto = new IdcIPDBDTO();
		getIP(ip_address, dto);
		
		request.setAttribute("ipdb", dto);
	}
	
	//해당 ip 관련 정보 얻어온다.
	private void getIP(String ip_address, IdcIPDBDTO dto)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM ipdb WHERE ip_address=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, ip_address);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				dto.setLine_name(rs.getString("line_name"));
				dto.setIp_case2(rs.getString("ip_case2"));
				dto.setIp_user(rs.getString("ip_user"));
				dto.setIp_able(rs.getInt("ip_able"));
				dto.setIp_address(rs.getString("ip_address"));
				dto.setIp_before_user(rs.getString("ip_before_user"));
				dto.setCancel_reason(rs.getString("cancel_reason"));
				dto.setBlock(rs.getString("block"));
				dto.setBlock_endday(rs.getDate("block_endday"));
				dto.setIp_note(rs.getString("ip_note"));
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcIPModifyFormCmd - getIP 에서의 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("IdcIPModifyFormCmd - getIP 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
