package idc.server.command;

import java.sql.Connection;
import java.sql.Date;
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 IdcServerReadCmd implements IdcCmd{
	
	IdcDAO  dao = new IdcDAO();
	
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		
		String inputNum = request.getParameter("server_num");		
				
		IdcServerDTO writing = idcServerRead(inputNum);
		
		request.setAttribute("idcServerRead", writing);
				
	}
	
	//서버 열람 기능 수행
	public IdcServerDTO idcServerRead(String inputNum){
		
		IdcServerDTO writing = new IdcServerDTO();
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null, rs1 = null;
		
		
		try {
			conn = dao.getConn();
			String sql = "SELECT * FROM serverDB WHERE server_num = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, Integer.parseInt(inputNum));
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				int server_num = rs.getInt("server_num");
				String server_use_status = rs.getString("server_use_status");
				String server_code = rs.getString("server_code");
				String server_process = rs.getString("server_process");
				String server_spec = rs.getString("server_spec");				
				String server_cpuname = rs.getString("server_cpuname");
				String server_ram = rs.getString("server_ram");
				String server_hdd = rs.getString("server_hdd");
				String server_brand = rs.getString("server_brand");
				String server_location = rs.getString("server_location");
				Date server_format_date = rs.getDate("server_fomat_date");
				Date server_recieve_date = rs.getDate("server_recieve_date");
				Date server_release_date = rs.getDate("server_release_date");
				String server_note = rs.getString("server_note");
				Date server_disuse_date = rs.getDate("server_disuse_date");
				String server_before_user = rs.getString("server_before_user");
				String server_now_user = rs.getString("server_now_user");
				
				String []server_rams = new String[5]; 
				server_rams[0] = rs.getString("server_ram1");
				server_rams[1] = rs.getString("server_ram2");
				server_rams[2]= rs.getString("server_ram3");
				server_rams[3] = rs.getString("server_ram4");
				server_rams[4] = rs.getString("server_ram5");
				
				String []server_hdds = new String[5];
				server_hdds[0] = rs.getString("server_hdd1");
				server_hdds[1] = rs.getString("server_hdd2");
				server_hdds[2] = rs.getString("server_hdd3");
				server_hdds[3] = rs.getString("server_hdd4");
				server_hdds[4] = rs.getString("server_hdd5");
				
				writing.setServer_num(server_num);
				writing.setServer_use_status(server_use_status);
				writing.setServer_code(server_code);
				writing.setServer_process(server_process);
				writing.setServer_spec(server_spec);
				writing.setServer_cpuname(server_cpuname);
				writing.setServer_ram(server_ram);
				writing.setServer_hdd(server_hdd);
				writing.setServer_brand(server_brand);
				writing.setServer_location(server_location);
				writing.setServer_fomat_date(server_format_date);
				writing.setServer_recieve_date(server_recieve_date);
				writing.setServer_release_date(server_release_date);
				writing.setServer_note(server_note);
				writing.setServer_disuse_date(server_disuse_date);
				writing.setServer_before_user(server_before_user);
				writing.setServer_now_user(server_now_user);
				writing.setServer_rams(server_rams);
				writing.setServer_hdds(server_hdds);
				
				sql = "SELECT count(*) as num FROM serviceDB WHERE server_code=?";
				pstmt1 = conn.prepareStatement(sql);
				pstmt1.setString(1, server_code);
				rs1 = pstmt1.executeQuery();
				
				String server_check = "";
				int num = 0;
				
				if(rs1.next()){
					num = rs1.getInt("num");
				}
				
				if(num == 0){
					server_check = "y";
				}else{
					server_check = "n";
				}				
				writing.setServer_check(server_check);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(rs1 != null) rs1.close();
				if(pstmt1 != null) pstmt1.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return writing;
	}

}