package com.eactive.eai.rms.onl.dashboard.controller;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.onl.dashboard.service.DashboardErrorService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Vector;

@Controller
public class DashboardErrorController {
    private static final Logger logger = Logger.getLogger(DashboardErrorController.class);

    @Autowired
    @Qualifier("dashboardErrorService")
    private DashboardErrorService dashboardErrorService;
    
    @RequestMapping("/dashboard2/popupError.do")
    public String mainProc(ModelMap modelMap, HttpServletRequest request,
            HttpServletResponse response, String cmd, String type) {        
        try {
        	response.setHeader("Pragma", "No-cache");
    		response.setHeader("Cache-Control", "no-cache");
    		response.setHeader("Expires", "0");
    		request.setCharacterEncoding("utf-8");

            HttpSession session = request.getSession();
            
    		String logTime    = request.getParameter("logTime");
    		String domainType = request.getParameter("domainType");
    		String nodeIndex  = request.getParameter("nodeIndex");
    		String errorType  = request.getParameter("errorType");
    		String commDiv    = request.getParameter("commDiv");
    		String subPage    = request.getParameter("subPage");
    		
    		if( commDiv == null   ) commDiv   = "0";
    		if( nodeIndex == null ) nodeIndex = "";
    		if( errorType == null ) errorType = "";
    		
    		boolean isFromMainPage = true;
    		if( "yes".equals(subPage) ) {
    			// 상세페이지에서 호출된 경우에는 TARGET DB 전체를 조회한다.
    			isFromMainPage = false ;
    		}

    		@SuppressWarnings("rawtypes")
			Vector logList = null;

    		if (logTime == null) {
    			logTime = CommonUtil.getThisTime();
    		}

    		if (domainType == null) {
    			logger.error("domainType os null");
//    			domainType = DataSourceTypeManager.NSG;
    		}

    		// 테스트
    		logger.debug( "********************************" );
    		logger.debug("isFromMainPage = " + isFromMainPage);
    		logger.debug("logTime = " + logTime);
    		logger.debug("domainType = " + domainType);
    		logger.debug("nodeIndex = " + nodeIndex);
    		logger.debug("errorType = " + errorType);
    		logger.debug("commDiv = " + commDiv);


    		//취약점코드 수정 2019.08.22 SW
    		String owner = "";
			DataSourceType dt;
			if(domainType != null) {
				dt = DataSourceTypeManager.getDataSourceType(domainType);
				if(dt != null){
					DataSourceContextHolder.setDataSourceType(dt);
					owner = dt.getSchema();
				}
			}
        	
    		if( isFromMainPage == false ) {
    			// 상세페이지 에서의 조회는 서버구분코드를 고려하지 않는다.
    			logList = dashboardErrorService.getLogList(owner, logTime, nodeIndex, errorType, "");
    		} else if (DataSourceTypeManager.MCI.equals(domainType) 
    				|| DataSourceTypeManager.MCU.equals(domainType)
    				|| DataSourceTypeManager.EAI.equals(domainType)
    				|| DataSourceTypeManager.FEP.equals(domainType)) {
    			logList = dashboardErrorService.getLogListBranchTest(owner, logTime, nodeIndex, errorType, commDiv);
    		} else {
    			logList = dashboardErrorService.getLogList(owner, logTime, nodeIndex, errorType, "");
    		}
    		
    		session.setAttribute("logTime", logTime);
    		session.setAttribute("domainType", domainType);
    		session.setAttribute("errorType", errorType);
    		session.setAttribute("commDiv", commDiv);
    		session.setAttribute("nodeIndex", nodeIndex);
    		session.setAttribute("logList", logList);
        } catch ( Exception e ) {
        	//e.printStackTrace();
        	//취약점코드 수정 2019.08.22 SW
			logger.error(e.getMessage());
        }
        
        if("new".equals(request.getParameter("cmd"))){        
        	return "redirect:/dashboard2/dashpopup2.jsp";
        }else{
        	return "redirect:/dashboard2/dashpopup.jsp";
        }
    }

    @RequestMapping(value= "/dashboard2/timeoutError.json")
	public ModelAndView timeoutError(HttpServletRequest request, HttpServletResponse response, String adt, String domainType) throws Exception {
    	
    	HashMap<String,Object> map = new HashMap<String,Object>();
    	
    	if (adt != null && domainType != null) {
    		DataSourceType dt = DataSourceTypeManager.getDataSourceType(domainType);
    		DataSourceContextHolder.setDataSourceType(dt);
    		
    		//취약점코드 수정 2019.08.22 SW
    		String owner = "";
    		if(dt != null){
    			owner = dt.getSchema();
    		}
    		
    		map = dashboardErrorService.getDSGTelno(owner, adt);
    	}
    	
		ModelAndView modelAndView = new ModelAndView("jsonView",map);
		return modelAndView;
	}

}
