package idc.service.command;

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

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

import java.sql.*;
import java.util.ArrayList;

public class IdcDefenceInfoExcelCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	@SuppressWarnings("unchecked")
	public void execute(HttpServletRequest request, HttpServletResponse response) {
				
		String client_code = request.getParameter("client_code");
		request.setAttribute("client_code", client_code);
		
		//방어존1,2,3 선택
		String deffence_case = request.getParameter("deffence_case");
		if(deffence_case == null || deffence_case ==""){
			deffence_case = "1";
		}
		request.setAttribute("deffence_case", deffence_case);
		
		ArrayList<Object> tlist;
		ArrayList<IdcDeffenceDTO> list;
		ArrayList<IdcDeffenceDTO> subList;
		
		String curPage = request.getParameter("curPage");
		
		if(curPage == null || curPage == "") curPage = "1";
		tlist = getIdcDeffenceDTO(curPage, deffence_case);
		
		list = (ArrayList<IdcDeffenceDTO>)tlist.get(0);
		subList = (ArrayList<IdcDeffenceDTO>)tlist.get(1);
		
		request.setAttribute("dlist", list);
		request.setAttribute("curPage", curPage);
		
		request.setAttribute("subList", subList);				
						
	}

	private ArrayList<Object> getIdcDeffenceDTO(String curPage, String d_case){
		ArrayList<IdcDeffenceDTO> list = new ArrayList<IdcDeffenceDTO>();
		ArrayList<IdcDeffenceDTO> subList = new ArrayList<IdcDeffenceDTO>();
		ArrayList<Object> tlist = new ArrayList<Object>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		PreparedStatement pstmt1 = null;
		ResultSet rs = null;
		ResultSet rs1 = null;
		
		String sql;
		String sql1;
		
		try {
			conn = dao.getConn();

			sql = "select * from deffence1db where deffence_case=? order by deffence_num desc ";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, d_case);		
			
			rs = pstmt.executeQuery();			
			
			while(rs.next()){
				int deffence_num = rs.getInt("deffence_num");
				String deffence_zone = rs.getString("deffence_zone");
				String deffence_acl = rs.getString("deffence_acl");
				String deffence_real = rs.getString("deffence_real");
				String deffence_port = rs.getString("deffence_port");
				String client_name = rs.getString("client_name");
				String vpc_code = rs.getString("vpc_code");
				String vpc_csip = rs.getString("vpc_csip");
				String service_code = rs.getString("service_code");
				String server_code = rs.getString("server_code");
				String service_ip = rs.getString("service_ip");
				String deffence_domain = rs.getString("deffence_domain");
				Date deffence_request_date = rs.getDate("deffence_request_date");
				Time deffence_request_time = rs.getTime("deffence_request_time");
				String deffence_note = rs.getString("deffence_note");
				String deffence_case = rs.getString("deffence_case");
				String client_code = rs.getString("client_code");
				
				//특수 문자 치환
				ReplaceHTMLChar rh = new ReplaceHTMLChar();
				if(deffence_zone != null) deffence_zone = rh.replaceHTMLChar(deffence_zone);
				if(client_name != null) client_name = rh.replaceHTMLChar(client_name);
				if(deffence_domain != null) deffence_domain = rh.replaceHTMLChar(deffence_domain);
				if(deffence_note != null ) deffence_note = rh.replaceHTMLChar(deffence_note);
												
				IdcDeffenceDTO writing = new IdcDeffenceDTO();
				writing.setDeffence_num(deffence_num);
				writing.setDeffence_zone(deffence_zone);
				writing.setDeffence_acl(deffence_acl);
				writing.setDeffence_real(deffence_real);
				writing.setDeffence_port(deffence_port);
				writing.setClient_name(client_name);
				writing.setVpc_code(vpc_code);
				writing.setVpc_csip(vpc_csip);
				writing.setService_code(service_code);
				writing.setServer_code(server_code);
				writing.setService_ip(service_ip);
				writing.setDeffence_domain(deffence_domain);
				writing.setDeffence_request_date(deffence_request_date);
				writing.setDeffence_request_time(deffence_request_time);
				writing.setDeffence_note(deffence_note);
				writing.setDeffence_case(deffence_case);
				writing.setClient_code(client_code);								
				list.add(writing);
				
				//연결된 방어존
				sql1 = "SELECT * FROM deffence1add WHERE deffence_num=?";
				pstmt1 = conn.prepareStatement(sql1);
				pstmt1.setInt(1, deffence_num);
				
				rs1 = pstmt1.executeQuery();
				
				while(rs1.next()){
					
					int dadd_num = rs1.getInt("dadd_num");
					int dadd_deffence_num  = rs1.getInt("deffence_num");
					String dadd_port = rs1.getString("dadd_port");					
					String dadd_client_name = rs1.getString("client_name");
					String dadd_vpc_code = rs1.getString("vpc_code");
					String dadd_vpc_csip = rs1.getString("vpc_csip");
					String dadd_server_code = rs1.getString("server_code");
					String dadd_service_ip = rs1.getString("service_ip");
					String dadd_domain = rs1.getString("dadd_domain");
					Date dadd_date = rs1.getDate("dadd_date");
					Time dadd_time = rs1.getTime("dadd_time");
					String dadd_note = rs1.getString("dadd_note");
					
					//줄바꿈, 특수문자 처리
					//port, client_name, connect_ip, domain, note					
					if(dadd_port != null){ 
						dadd_port = rh.replaceHTMLChar(dadd_port);
						dadd_port = dadd_port.replaceAll("\r\n", "<br>");
						dadd_port = dadd_port.replaceAll("\u0020","&nbsp;");
					}
					if(dadd_client_name != null){ 
						dadd_client_name = rh.replaceHTMLChar(dadd_client_name);
						dadd_client_name = dadd_client_name.replaceAll("\r\n", "<br>");
						dadd_client_name = dadd_client_name.replaceAll("\u0020","&nbsp;");
					}
					if(dadd_service_ip != null){ 
						dadd_service_ip = rh.replaceHTMLChar(dadd_service_ip);
						dadd_service_ip = dadd_service_ip.replaceAll("\r\n", "<br>");
						dadd_service_ip = dadd_service_ip.replaceAll("\u0020","&nbsp;");
					}
					if(dadd_domain != null){ 
						dadd_domain = rh.replaceHTMLChar(dadd_domain);
						dadd_domain = dadd_domain.replaceAll("\r\n", "<br>");
						dadd_domain = dadd_domain.replaceAll("\u0020","&nbsp;");
					}
					if(dadd_note != null){ 
						dadd_note = rh.replaceHTMLChar(dadd_note);
						dadd_note = dadd_note.replaceAll("\r\n", "<br>");
						dadd_note = dadd_note.replaceAll("\u0020","&nbsp;");
					}

					IdcDeffenceDTO sub = new IdcDeffenceDTO();
					
					sub.setDadd_num(dadd_num);
					sub.setDeffence_num(dadd_deffence_num);
					sub.setDeffence_port(dadd_port);
					sub.setClient_name(dadd_client_name);
					sub.setVpc_code(dadd_vpc_code);
					sub.setVpc_csip(dadd_vpc_csip);
					sub.setServer_code(dadd_server_code);
					sub.setService_ip(dadd_service_ip);
					sub.setDeffence_domain(dadd_domain);
					sub.setDeffence_request_date(dadd_date);
					sub.setDeffence_request_time(dadd_time);
					sub.setDeffence_note(dadd_note);
					subList.add(sub);
				}
			}
			
						
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(rs1 != null) rs1.close();
				if(pstmt != null) pstmt.close();
				if(pstmt1 != null) pstmt1.close();
				if(conn != null) conn.close();
								
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}			
		
		tlist.add(list);
		tlist.add(subList);
		
		return tlist;
	}
			
}