package idc.server.command;

import java.sql.*;

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

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


public class IdcServerUpdateCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	public void execute(HttpServletRequest request, HttpServletResponse response){
		
		if(dao.checkToken(request)){
			request.setAttribute("reError", dao.reError);
			return;
		}else{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
		
		String server_num = request.getParameter("server_num");
		String server_use_status = request.getParameter("server_use_status");						
		String server_code = request.getParameter("server_code");
		String server_process = request.getParameter("server_process");
		String server_spec = request.getParameter("server_spec");
		String server_cpuname = request.getParameter("server_cpuname");
				
		String server_ram1 = request.getParameter("server_ram1");
		String server_ram2 = request.getParameter("server_ram2");
		String server_ram3 = request.getParameter("server_ram3");
		String server_ram4 = request.getParameter("server_ram4");
		String server_ram5 = request.getParameter("server_ram5");
		
		String server_hdd1 = request.getParameter("server_hdd1");
		String server_hdd2 = request.getParameter("server_hdd2");
		String server_hdd3 = request.getParameter("server_hdd3");
		String server_hdd4 = request.getParameter("server_hdd4");
		String server_hdd5 = request.getParameter("server_hdd5");
				
		String server_ram = server_ram1 + " " + server_ram2 + " " + server_ram3 + " " + server_ram4 + " " + server_ram5;  
		String server_hdd = server_hdd1 + " " + server_hdd2 + " " + server_hdd3 + " " + server_hdd4 + " " + server_hdd5;
		
		String server_brand = request.getParameter("server_brand");
		String server_location = request.getParameter("server_location");
		String server_fomat_date = request.getParameter("server_fomat_date");
		String server_recieve_date = request.getParameter("server_recieve_date");
		String server_release_date = request.getParameter("server_release_date");
		String server_note = request.getParameter("server_note");
		String server_disuse_date = request.getParameter("server_disuse_date");
		String server_before_user = request.getParameter("server_before_user");
		String server_now_user = request.getParameter("server_now_user");
		
		//String을 Date형으로 형변환
		StringToDate st = new StringToDate();
		
		Date fomat; 		
		if(server_fomat_date == "" || server_fomat_date == null){
			fomat= null;			
		}else{
			fomat = st.stringToDate(server_fomat_date);
		}
		
		Date recieve;
		if(server_recieve_date == "" || server_recieve_date == null){
			recieve = null;			
		}else{
			recieve = st.stringToDate(server_recieve_date);			
		}
		
		Date release;		
		if(server_release_date == "" || server_release_date == null){
			release = null;			
		}else{
			release = st.stringToDate(server_release_date);			
		}
		
		Date disuse;		
		if(server_disuse_date == "" || server_disuse_date == null){
			disuse = null;			
		}else{
			disuse = st.stringToDate(server_disuse_date);			
		}		
		
		//serverDB 업데이트
		idcServerUpdate(server_num, server_use_status, server_code,
				server_process, server_spec, server_cpuname, server_ram, 
				server_hdd, server_brand, server_location, fomat, recieve, release, server_note, disuse, 
				server_before_user, server_now_user,
				server_ram1,  server_ram2,  server_ram3,  server_ram4, server_ram5,
				server_hdd1,  server_hdd2,  server_hdd3,  server_hdd4, server_hdd5
				);		
				
	}
	
	
	//서버 수정 기능 수행
	public void idcServerUpdate(String server_num, String server_use_status, String server_code,
			String server_process, String server_spec, String server_cpuname, String server_ram, 
			String server_hdd, String server_brand, String server_location, Date server_fomat_date, Date server_recieve_date,
			Date server_release_date, String server_note, Date server_disuse_date, String server_before_user, String server_now_user,
			String server_ram1, String server_ram2, String server_ram3, String server_ram4, String server_ram5,
			String server_hdd1, String server_hdd2, String server_hdd3, String server_hdd4, String server_hdd5){
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			conn = dao.getConn();
			String sql = "UPDATE serverDB SET server_use_status=?, server_process=?, server_spec=?, server_cpuname=?, server_ram=?, server_hdd=?, server_brand=?, ";
			sql += "server_location=?, server_fomat_date=?, server_recieve_date=?, server_release_date=?, server_note=?, ";
			sql += "server_disuse_date=?, server_before_user=?, server_now_user=?,";
			sql += "server_ram1=?, server_ram2=?, server_ram3=?, server_ram4=?, server_ram5=?,";
			sql += "server_hdd1=?, server_hdd2=?, server_hdd3=?, server_hdd4=?, server_hdd5=? ";
			sql += " WHERE server_code=?";
			pstmt = conn.prepareStatement(sql);
									
			pstmt.setString(1, server_use_status);
			pstmt.setString(2, server_process);
			pstmt.setString(3, server_spec);
			pstmt.setString(4, server_cpuname);
			pstmt.setString(5, server_ram);
			pstmt.setString(6, server_hdd);
			pstmt.setString(7, server_brand);
			pstmt.setString(8, server_location);
			pstmt.setDate(9, server_fomat_date);
			pstmt.setDate(10, server_recieve_date);
			pstmt.setDate(11, server_release_date);
			pstmt.setString(12, server_note);
			pstmt.setDate(13, server_disuse_date);
			pstmt.setString(14, server_before_user);
			pstmt.setString(15, server_now_user);
			pstmt.setString(16, server_ram1);
			pstmt.setString(17, server_ram2);
			pstmt.setString(18, server_ram3);
			pstmt.setString(19, server_ram4);
			pstmt.setString(20, server_ram5);
			pstmt.setString(21, server_hdd1);
			pstmt.setString(22, server_hdd2);
			pstmt.setString(23, server_hdd3);
			pstmt.setString(24, server_hdd4);
			pstmt.setString(25, server_hdd5);
			pstmt.setString(26, server_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();
			}
		}
		
	}

}