package idc.service.command;

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

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

import utility.StringToDate;
import idc.model.*;

public class IdcKtCSInfoUpdateHandleCmd 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 kcs_num = request.getParameter("kcs_num");
		String service_line = request.getParameter("service_line");
		String service_admin = request.getParameter("service_admin");
		String client_name = request.getParameter("client_name");
		String kcs_code = request.getParameter("kcs_code");
		String kcs_ip = request.getParameter("kcs_ip");
		String service_ip = request.getParameter("service_ip");
		String server_code = request.getParameter("server_code");
		String kcs_use_domain = request.getParameter("kcs_use_domain");
		String kcs_connect_domain = request.getParameter("kcs_connect_domain");
		String last_date = request.getParameter("kcs_last_date");
		
		StringToDate st = new StringToDate();		
		Date kcs_last_date;		
		if(last_date == "" || last_date == null){
			kcs_last_date = null;
		}else{
			kcs_last_date = st.stringToDate(last_date);
		}				
		String kcs_note = request.getParameter("kcs_note");
		
		kcsUpdate(kcs_num,  service_line,  service_admin,  client_name,  kcs_code,
				 kcs_ip,  service_ip,  server_code,  kcs_use_domain,  kcs_connect_domain,
				 kcs_last_date,  kcs_note);		
	}
	
	//kcs업데이트하기
	public void kcsUpdate(String kcs_num, String service_line, String service_admin, String client_name, String kcs_code,
			String kcs_ip, String service_ip, String server_code, String kcs_use_domain, String kcs_connect_domain,
			Date kcs_last_date, String kcs_note){		
		
		Connection conn = null;
		PreparedStatement pstmt = null;		
		String sql;
						
		try {
			conn = dao.getConn();			
			sql = "UPDATE kcsDB SET kcs_ip=?, kcs_use_domain=?, kcs_connect_domain=?, ";			
			sql += " kcs_last_date=?, kcs_note=? , kcs_admin=?";
			sql += " WHERE kcs_code=?";
			pstmt = conn.prepareStatement(sql);
						
			pstmt.setString(1, kcs_ip);
			pstmt.setString(2, kcs_use_domain);
			pstmt.setString(3, kcs_connect_domain);
			pstmt.setDate(4, kcs_last_date);
			pstmt.setString(5, kcs_note);			
			pstmt.setString(6, service_admin);
			pstmt.setString(7, kcs_code);
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
