package idc.service.command;

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

import idc.model.*;
import java.sql.*;

public class IdcServiceEtcManageCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	//1회성 서비스 추가
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		IdcServiceETCManageDTO ise;
		
		String service_code = request.getParameter("service_code");
		String client_name = request.getParameter("client_name");
		String vpnFlag = request.getParameter("vpnFlag");
		
		if(vpnFlag == null || vpnFlag == "") vpnFlag = "false";			
		
		ise = getETCManageDTO(service_code, client_name);
		request.setAttribute("ise", ise);
		request.setAttribute("vpnFlag", vpnFlag);
		
	}
	
	public IdcServiceETCManageDTO getETCManageDTO(String service_code, String client_name){
		
		IdcServiceETCManageDTO ise = new IdcServiceETCManageDTO();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT client_code, server_code, service_ip, curdate() As curdate FROM serviceDB WHERE service_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				String client_code = rs.getString("client_code");
				String server_code = rs.getString("server_code");
				String service_ip = rs.getString("service_ip");
				Date onetime_curdate = rs.getDate("curdate");
				
				ise.setService_code(service_code);
				ise.setClient_code(client_code);
				ise.setClient_name(client_name);
				ise.setServer_code(server_code);
				ise.setService_ip(service_ip);
				ise.setOnetime_curdate(onetime_curdate);
			}
			
			
		} 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 ise;
	}		

}
