package com.siginfo.controller;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

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

import org.codehaus.jackson.map.ObjectMapper;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.siginfo.service.SigOpenService;
import com.siginfo.vo.CrossRoadInfoDetailVO;
import com.siginfo.vo.CrossRoadInfoHeaderVO;
import com.siginfo.vo.CrossRoadInfoListVO;
import com.siginfo.vo.PlanCRHDInfoVO;
import com.siginfo.vo.PlanCROPInfoVO;
import com.siginfo.vo.PlanCRRSInfoVO;
import com.siginfo.vo.PlanCRWDInfoVO;
import com.siginfo.controller.HttpsClient;

/**
 * @author OpenValue
 *
 */
@Controller
public class SigOpenController {

	//기관코드
	private static final String govCode = "1320000";
	
	@Autowired
	private SigOpenService sigOpenService;
	
	private static CrossRoadInfoHeaderVO headerVo;
	//private static CrossRoadInfoResponse crossRoadInfo;
	
    //교차로기반정보서비스 제공 서비스 111111111111111
	
	@RequestMapping(value = "/"+govCode+"/CrossRoadInfoService/getCrossRoadInfoList", method = RequestMethod.GET)
	@ResponseBody
	public void getCrossRoadInfoList(Locale locale, Model model, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
    	System.out.println("------------getCrossRoadInfoList Start------------");
    	
    	String type     = (req.getParameter("type") == null) ? "xml" : req.getParameter("type").toString();
    	String apimKey  = (req.getParameter("SG_APIM") == null) ? "" : req.getParameter("SG_APIM").toString(); // 게이트웨이 인증키 값 저장
    	String srchCTId = (req.getParameter("srchCTId") == null) ? "" : req.getParameter("srchCTId").toString();
    	String srchCRNm = (req.getParameter("srchCRNm") == null) ? "" : req.getParameter("srchCRNm").toString();
    	String pageNo  = (req.getParameter("pageNo") == null) ? "1" : req.getParameter("pageNo").toString();
    	String numOfRows = (req.getParameter("numOfRows") == null) ? "100" : req.getParameter("numOfRows").toString();
    	
    	if(srchCRNm != null)
    	{
    		srchCRNm = new String(srchCRNm.getBytes("8859_1"),"UTF-8"); // getBytes("8859_1")
    	}
    	
    	int eRow = Integer.parseInt(numOfRows);
    	if(eRow > 100 ) eRow = 100;
    	String [] calcPageReturn = calcPageStartEndNo(pageNo, eRow);

    	checkHttpAndKey("CrossRoadInfoService/getCrossRoadInfoList",apimKey);
    	
    	Map<String,Object> paramMap = new HashMap<String,Object>();
    	paramMap.put("queryTotId", "getTotalCount");
		paramMap.put("queryId", "getCrossRoadInfoList");
		paramMap.put("tableId", "TB_INT");
		paramMap.put("numOfRows", numOfRows);
		paramMap.put("srchCTId", srchCTId);
		paramMap.put("srchCRNm", srchCRNm);
		paramMap.put("sRow", calcPageReturn[0]);
		paramMap.put("eRow", calcPageReturn[1]);
		
		List<CrossRoadInfoHeaderVO> totCount = sigOpenService.getTotalCount(paramMap);
		List<CrossRoadInfoListVO> crilList = sigOpenService.listList(paramMap);
		
    	if(type.equals("xml"))
    	{
    		res.setContentType("application/xml; charset=UTF-8");
    		Format format = Format.getPrettyFormat();
        	format.setEncoding("UTF-8");
    		
    		Element data = new Element("CrossRoadInfoResponse");
        	Element header = new Element("Header");
        	
        	if(headerVo.getResultCode().equals("0"))
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",numOfRows);
        		addElement(header,"totPage",totCount.get(0).getTotPage());
        		addElement(header,"totCount",totCount.get(0).getTotCount());
        		addElement(header,"pageNo",pageNo);
        		
        		data.addContent(header);
        		
        		for(CrossRoadInfoListVO crilVO : crilList)
        		{
        			String regionCd = crilVO.getREGION_CD();
        			String intNo = crilVO.getINT_NO();
        			String intNm = crilVO.getINT_NM();
        			String xCoord = crilVO.getX_COORD();
        			String yCoord = crilVO.getY_COORD();
        			String updDtime = crilVO.getUPD_DTIME();
        			
        			Element element = new Element("CrossRoadInfoList");
        			addElement(element,"REGION_CD",regionCd);
        			addElement(element,"INT_NO",intNo);
        			addElement(element,"INT_NM",intNm);
        			addElement(element,"X_COORD",xCoord);
        			addElement(element,"Y_COORD",yCoord);
        			addElement(element,"UPD_DTIME",updDtime);
        		    data.addContent(element);
        		}
        	}
        	else
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",headerVo.getNumOfRows());
        		addElement(header,"totPage",headerVo.getTotPage());
        		addElement(header,"totCount",headerVo.getTotCount());
        		addElement(header,"pageNo",headerVo.getPageNo());
        		
        		data.addContent(header);
        	}
        	
        	Document document = new Document(data);
        	
        	XMLOutputter outputter = new XMLOutputter();
        	outputter.setFormat(format);
        	outputter.output(document, res.getWriter());
    	}
    	else
    	{
    		ObjectMapper jsonMapper = new ObjectMapper();
            res.setContentType("application/json; charset=UTF-8");
            res.setCharacterEncoding("UTF-8");
            PrintWriter out = res.getWriter();
            
            List<Map<String,Object>> jsonList = new ArrayList<Map<String,Object>>();
            Map<String,Object> headerMap = new HashMap<String,Object>();
    		headerMap.put("resultCode", headerVo.getResultCode());
    		headerMap.put("resultMsg", headerVo.getResultMsg());
    		headerMap.put("numOfRows", numOfRows);
    		headerMap.put("totCnt", totCount.get(0).getTotCount());
    		headerMap.put("totPage", totCount.get(0).getTotPage());
    		headerMap.put("pageNo", pageNo);
    		
    		jsonList.add(headerMap);
            
    		if(headerVo.getResultCode().equals("0"))
        	{
	            for(CrossRoadInfoListVO crilVO : crilList)
	    		{
	            	String regionCd = crilVO.getREGION_CD();
        			String intNo = crilVO.getINT_NO();
        			String intNm = crilVO.getINT_NM();
        			String xCoord = crilVO.getX_COORD();
        			String yCoord = crilVO.getY_COORD();
        			String updDtime = crilVO.getUPD_DTIME();
        			
        			Map<String,Object> jsonMap = new HashMap<String,Object>();
        			jsonMap.put("REGION_CD", regionCd);
        			jsonMap.put("INT_NO", intNo);
        			jsonMap.put("INT_NM", intNm);
        			jsonMap.put("X_COORD", xCoord);
        			jsonMap.put("Y_COORD", yCoord);
        			jsonMap.put("UPD_DTIME", updDtime);
	            	jsonList.add(jsonMap);
	    		}
        	}
            String json = jsonMapper.writeValueAsString(jsonList);
            out.print(json);
            out.close();
    	}
    	
	}
	
	//교차로디테일정보서비스 제공 서비스 22222222222222
	
	@RequestMapping(value = "/"+govCode+"/CrossRoadInfoService/getCrossRoadInfoDetail", method = RequestMethod.GET)
	@ResponseBody
	public void getCrossRoadInfoDetail(Locale locale, Model model, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
    	System.out.println("------------getCrossRoadInfoDetail Start------------");
    	
    	String type     = (req.getParameter("type") == null) ? "xml" : req.getParameter("type").toString();
    	String apimKey  = (req.getParameter("SG_APIM") == null) ? "" : req.getParameter("SG_APIM").toString(); // 게이트웨이 인증키 값 저장
    	String srchCTId = (req.getParameter("srchCTId") == null) ? "" : req.getParameter("srchCTId").toString();
    	String srchCRNm = (req.getParameter("srchCRNm") == null) ? "" : req.getParameter("srchCRNm").toString();
    	String pageNo  = (req.getParameter("pageNo") == null) ? "1" : req.getParameter("pageNo").toString();
    	String numOfRows = (req.getParameter("numOfRows") == null) ? "100" : req.getParameter("numOfRows").toString();
    	
    	if(srchCRNm != null)
    	{
    		srchCRNm = new String(srchCRNm.getBytes("8859_1"),"UTF-8"); // getBytes("8859_1")
    	}
    	
    	int eRow = Integer.parseInt(numOfRows);
    	if(eRow > 100 ) eRow = 100;
    	String [] calcPageReturn = calcPageStartEndNo(pageNo, eRow);

    	checkHttpAndKey("CrossRoadInfoService/getCrossRoadInfoDetail",apimKey);
    	
    	Map<String,Object> paramMap = new HashMap<String,Object>();
    	paramMap.put("queryTotId", "getTotalCount");
		paramMap.put("queryId", "getCrossRoadDetailList");
		paramMap.put("tableId", "TB_INT_PHASE");
		paramMap.put("numOfRows", numOfRows);
		paramMap.put("srchCTId", srchCTId);
		paramMap.put("srchCRNm", srchCRNm);
		paramMap.put("sRow", calcPageReturn[0]);
		paramMap.put("eRow", calcPageReturn[1]);
    	
    	List<CrossRoadInfoHeaderVO> totCount = sigOpenService.getTotalCount(paramMap);
		List<CrossRoadInfoDetailVO> cridList = sigOpenService.detailList(paramMap);
		
		if(type.equals("xml"))
    	{
    		res.setContentType("application/xml; charset=UTF-8");
    		Format format = Format.getPrettyFormat();
        	format.setEncoding("UTF-8");
    		
    		Element data = new Element("CrossRoadInfoResponse");
        	Element header = new Element("Header");
        	
    		if(headerVo.getResultCode().equals("0"))
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",numOfRows);
        		addElement(header,"totPage",totCount.get(0).getTotPage());
        		addElement(header,"totCount",totCount.get(0).getTotCount());
        		addElement(header,"pageNo",pageNo);
        		
        		data.addContent(header);
        		
        		for(CrossRoadInfoDetailVO cridVO : cridList)
        		{
        			String regionCd = cridVO.getREGION_CD();
        			String intNo = cridVO.getINT_NO();
        			String intNm = cridVO.getINT_NM();
        			String mapNo = cridVO.getMAP_NO();
        			String intMainphase = cridVO.getINT_MAINPHASE();
        			String aRing1PhaseConfCd = cridVO.getA_RING_1_PHASE_CONF_CD();
        			String aRing2PhaseConfCd = cridVO.getA_RING_2_PHASE_CONF_CD();
        			String aRing3PhaseConfCd = cridVO.getA_RING_3_PHASE_CONF_CD();
        			String aRing4PhaseConfCd = cridVO.getA_RING_4_PHASE_CONF_CD();
        			String aRing5PhaseConfCd = cridVO.getA_RING_5_PHASE_CONF_CD();
        			String aRing6PhaseConfCd = cridVO.getA_RING_6_PHASE_CONF_CD();
        			String aRing7PhaseConfCd = cridVO.getA_RING_7_PHASE_CONF_CD();
        			String aRing8PhaseConfCd = cridVO.getA_RING_8_PHASE_CONF_CD();
        			String bRing1PhaseConfCd = cridVO.getB_RING_1_PHASE_CONF_CD();
        			String bRing2PhaseConfCd = cridVO.getB_RING_2_PHASE_CONF_CD();
        			String bRing3PhaseConfCd = cridVO.getB_RING_3_PHASE_CONF_CD();
        			String bRing4PhaseConfCd = cridVO.getB_RING_4_PHASE_CONF_CD();
        			String bRing5PhaseConfCd = cridVO.getB_RING_5_PHASE_CONF_CD();
        			String bRing6PhaseConfCd = cridVO.getB_RING_6_PHASE_CONF_CD();
        			String bRing7PhaseConfCd = cridVO.getB_RING_7_PHASE_CONF_CD();
        			String bRing8PhaseConfCd = cridVO.getB_RING_8_PHASE_CONF_CD();
        			
        			Element element = new Element("CrossRoadInfoDetail");
        			addElement(element,"REGION_CD",regionCd);
        			addElement(element,"INT_NO",intNo);
        			addElement(element,"INT_NM",intNm);
        			addElement(element,"MAP_NO",mapNo);
        			addElement(element,"INT_MAINPHASE",intMainphase);
        			addElement(element,"A_RING_1_PHASE_CONF_CD",aRing1PhaseConfCd);
        			addElement(element,"A_RING_2_PHASE_CONF_CD",aRing2PhaseConfCd);
        			addElement(element,"A_RING_3_PHASE_CONF_CD",aRing3PhaseConfCd);
        			addElement(element,"A_RING_4_PHASE_CONF_CD",aRing4PhaseConfCd);
        			addElement(element,"A_RING_5_PHASE_CONF_CD",aRing5PhaseConfCd);
        			addElement(element,"A_RING_6_PHASE_CONF_CD",aRing6PhaseConfCd);
        			addElement(element,"A_RING_7_PHASE_CONF_CD",aRing7PhaseConfCd);
        			addElement(element,"A_RING_8_PHASE_CONF_CD",aRing8PhaseConfCd);
        			addElement(element,"B_RING_1_PHASE_CONF_CD",bRing1PhaseConfCd);
        			addElement(element,"B_RING_2_PHASE_CONF_CD",bRing2PhaseConfCd);
        			addElement(element,"B_RING_3_PHASE_CONF_CD",bRing3PhaseConfCd);
        			addElement(element,"B_RING_4_PHASE_CONF_CD",bRing4PhaseConfCd);
        			addElement(element,"B_RING_5_PHASE_CONF_CD",bRing5PhaseConfCd);
        			addElement(element,"B_RING_6_PHASE_CONF_CD",bRing6PhaseConfCd);
        			addElement(element,"B_RING_7_PHASE_CONF_CD",bRing7PhaseConfCd);
        			addElement(element,"B_RING_8_PHASE_CONF_CD",bRing8PhaseConfCd);
        			
        		    data.addContent(element);
        		}
        	}
        	else
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",headerVo.getNumOfRows());
        		addElement(header,"totPage",headerVo.getTotPage());
        		addElement(header,"totCount",headerVo.getTotCount());
        		addElement(header,"pageNo",headerVo.getPageNo());
        		
        		data.addContent(header);
        	}
        	
        	Document document = new Document(data);
        	
        	XMLOutputter outputter = new XMLOutputter();
        	outputter.setFormat(format);
        	outputter.output(document, res.getWriter());
    	}
    	else
    	{
    		ObjectMapper jsonMapper = new ObjectMapper();
            res.setContentType("application/json; charset=UTF-8");
            res.setCharacterEncoding("UTF-8");
            PrintWriter out = res.getWriter();
            
            List<Map<String,Object>> jsonList = new ArrayList<Map<String,Object>>();
            Map<String,Object> headerMap = new HashMap<String,Object>();
    		headerMap.put("resultCode", headerVo.getResultCode());
    		headerMap.put("resultMsg", headerVo.getResultMsg());
    		headerMap.put("numOfRows", numOfRows);
    		headerMap.put("totCnt", totCount.get(0).getTotCount());
    		headerMap.put("totPage", totCount.get(0).getTotPage());
    		headerMap.put("pageNo", pageNo);
    		
    		jsonList.add(headerMap);
            
    		if(headerVo.getResultCode().equals("0"))
        	{
	            for(CrossRoadInfoDetailVO cridVO : cridList)
	    		{
        			String regionCd = cridVO.getREGION_CD();
        			String intNo = cridVO.getINT_NO();
        			String intNm = cridVO.getINT_NM();
        			String mapNo = cridVO.getMAP_NO();
        			String intMainphase = cridVO.getINT_MAINPHASE();
        			String aRing1PhaseConfCd = cridVO.getA_RING_1_PHASE_CONF_CD();
        			String aRing2PhaseConfCd = cridVO.getA_RING_2_PHASE_CONF_CD();
        			String aRing3PhaseConfCd = cridVO.getA_RING_3_PHASE_CONF_CD();
        			String aRing4PhaseConfCd = cridVO.getA_RING_4_PHASE_CONF_CD();
        			String aRing5PhaseConfCd = cridVO.getA_RING_5_PHASE_CONF_CD();
        			String aRing6PhaseConfCd = cridVO.getA_RING_6_PHASE_CONF_CD();
        			String aRing7PhaseConfCd = cridVO.getA_RING_7_PHASE_CONF_CD();
        			String aRing8PhaseConfCd = cridVO.getA_RING_8_PHASE_CONF_CD();
        			String bRing1PhaseConfCd = cridVO.getB_RING_1_PHASE_CONF_CD();
        			String bRing2PhaseConfCd = cridVO.getB_RING_2_PHASE_CONF_CD();
        			String bRing3PhaseConfCd = cridVO.getB_RING_3_PHASE_CONF_CD();
        			String bRing4PhaseConfCd = cridVO.getB_RING_4_PHASE_CONF_CD();
        			String bRing5PhaseConfCd = cridVO.getB_RING_5_PHASE_CONF_CD();
        			String bRing6PhaseConfCd = cridVO.getB_RING_6_PHASE_CONF_CD();
        			String bRing7PhaseConfCd = cridVO.getB_RING_7_PHASE_CONF_CD();
        			String bRing8PhaseConfCd = cridVO.getB_RING_8_PHASE_CONF_CD();
        			
        			Map<String,Object> jsonMap = new HashMap<String,Object>();
        			jsonMap.put("REGION_CD", regionCd);
        			jsonMap.put("INT_NO", intNo);
        			jsonMap.put("INT_NM", intNm);
        			jsonMap.put("MAP_NO", mapNo);
        			jsonMap.put("INT_MAINPHASE", intMainphase);
        			jsonMap.put("A_RING_1_PHASE_CONF_CD", aRing1PhaseConfCd);
        			jsonMap.put("A_RING_2_PHASE_CONF_CD", aRing2PhaseConfCd);
        			jsonMap.put("A_RING_3_PHASE_CONF_CD", aRing3PhaseConfCd);
        			jsonMap.put("A_RING_4_PHASE_CONF_CD", aRing4PhaseConfCd);
        			jsonMap.put("A_RING_5_PHASE_CONF_CD", aRing5PhaseConfCd);
        			jsonMap.put("A_RING_6_PHASE_CONF_CD", aRing6PhaseConfCd);
        			jsonMap.put("A_RING_7_PHASE_CONF_CD", aRing7PhaseConfCd);
        			jsonMap.put("A_RING_8_PHASE_CONF_CD", aRing8PhaseConfCd);
        			jsonMap.put("B_RING_1_PHASE_CONF_CD", bRing1PhaseConfCd);
        			jsonMap.put("B_RING_2_PHASE_CONF_CD", bRing2PhaseConfCd);
        			jsonMap.put("B_RING_3_PHASE_CONF_CD", bRing3PhaseConfCd);
        			jsonMap.put("B_RING_4_PHASE_CONF_CD", bRing4PhaseConfCd);
        			jsonMap.put("B_RING_5_PHASE_CONF_CD", bRing5PhaseConfCd);
        			jsonMap.put("B_RING_6_PHASE_CONF_CD", bRing6PhaseConfCd);
        			jsonMap.put("B_RING_7_PHASE_CONF_CD", bRing7PhaseConfCd);
        			jsonMap.put("B_RING_8_PHASE_CONF_CD", bRing8PhaseConfCd);
        			
	            	jsonList.add(jsonMap);
	    		}
        	}
            String json = jsonMapper.writeValueAsString(jsonList);
            out.print(json);
            out.close();
    	}
    	
	}
	
	//교차로특수일계획정보 제공 서비스 333333333333333333 
	
	@RequestMapping(value = "/"+govCode+"/PlanCrossRoadInfoService/getPlanCRHDInfo", method = RequestMethod.GET)
	@ResponseBody
	public void getPlanCRHDInfo(Locale locale, Model model, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
    	System.out.println("------------getPlanCRHDInfo Start------------");
    	
    	String type     = (req.getParameter("type") == null) ? "xml" : req.getParameter("type").toString();
    	String apimKey  = (req.getParameter("SG_APIM") == null) ? "" : req.getParameter("SG_APIM").toString(); // 게이트웨이 인증키 값 저장
    	String srchCTId = (req.getParameter("srchCTId") == null) ? "" : req.getParameter("srchCTId").toString();
    	String srchCRNm = (req.getParameter("srchCRNm") == null) ? "" : req.getParameter("srchCRNm").toString();
    	String pageNo  = (req.getParameter("pageNo") == null) ? "1" : req.getParameter("pageNo").toString();
    	String numOfRows = (req.getParameter("numOfRows") == null) ? "100" : req.getParameter("numOfRows").toString();
    	
    	if(srchCRNm != null)
    	{
    		srchCRNm = new String(srchCRNm.getBytes("8859_1"),"UTF-8"); // getBytes("8859_1")
    	}
    	
    	int eRow = Integer.parseInt(numOfRows);
    	if(eRow > 100 ) eRow = 100;
    	String [] calcPageReturn = calcPageStartEndNo(pageNo, eRow);

    	checkHttpAndKey("PlanCrossRoadInfoService/getPlanCRHDInfo",apimKey);
    	
    	Map<String,Object> paramMap = new HashMap<String,Object>();
    	paramMap.put("queryTotId", "getTotalCount");
		paramMap.put("queryId", "getPlanCRHDInfoList");
		paramMap.put("tableId", "TB_INT_HOLYDD_PLAN");
		paramMap.put("numOfRows", numOfRows);
		paramMap.put("srchCTId", srchCTId);
		paramMap.put("srchCRNm", srchCRNm);
		paramMap.put("sRow", calcPageReturn[0]);
		paramMap.put("eRow", calcPageReturn[1]);
    	
    	List<CrossRoadInfoHeaderVO> totCount = sigOpenService.getTotalCount(paramMap);
		List<PlanCRHDInfoVO> crhdList = sigOpenService.crhdList(paramMap);
		
		if(type.equals("xml"))
    	{
    		res.setContentType("application/xml; charset=UTF-8");
    		Format format = Format.getPrettyFormat();
        	format.setEncoding("UTF-8");
    		
    		Element data = new Element("PlanCrossRoadInfoService");
        	Element header = new Element("Header");
        	
    		if(headerVo.getResultCode().equals("0"))
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",numOfRows);
        		addElement(header,"totPage",totCount.get(0).getTotPage());
        		addElement(header,"totCount",totCount.get(0).getTotCount());
        		addElement(header,"pageNo",pageNo);
        		
        		data.addContent(header);
        		
        		for(PlanCRHDInfoVO crhdVO : crhdList)
        		{
        			String regionCd = crhdVO.getREGION_CD();
        			String intNo = crhdVO.getINT_NO();
        			String intNm = crhdVO.getINT_NM();
        			String planSn = crhdVO.getPLAN_SN();
        			String holyPlanMm = crhdVO.getHOLYDD_PLAN_MM();
        			String holyPlanDd = crhdVO.getHOLYDD_PLAN_DD();
        			String intPlanNo  = crhdVO.getINT_PLAN_NO();
        			String collctDtime = crhdVO.getCOLLCT_DTIME();
        			
        			Element element = new Element("PlanCRHDInfo");
        			addElement(element,"REGION_CD",regionCd);
        			addElement(element,"INT_NO",intNo);
        			addElement(element,"INT_NM",intNm);
        			addElement(element,"PLAN_SN",planSn);
        			addElement(element,"HOLY_PLAN_MM",holyPlanMm);
        			addElement(element,"HOLY_PLAN_DD",holyPlanDd);
        			addElement(element,"INT_PLAN_NO",intPlanNo);
        			addElement(element,"COLLCT_DTIME",collctDtime);
        			
        		    data.addContent(element);
        		}
        	}
        	else
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",headerVo.getNumOfRows());
        		addElement(header,"totPage",headerVo.getTotPage());
        		addElement(header,"totCount",headerVo.getTotCount());
        		addElement(header,"pageNo",headerVo.getPageNo());
        		
        		data.addContent(header);
        	}
        	
        	Document document = new Document(data);
        	
        	XMLOutputter outputter = new XMLOutputter();
        	outputter.setFormat(format);
        	outputter.output(document, res.getWriter());
    	}
    	else
    	{
    		ObjectMapper jsonMapper = new ObjectMapper();
            res.setContentType("application/json; charset=UTF-8");
            res.setCharacterEncoding("UTF-8");
            PrintWriter out = res.getWriter();
            
            List<Map<String,Object>> jsonList = new ArrayList<Map<String,Object>>();
            Map<String,Object> headerMap = new HashMap<String,Object>();
    		headerMap.put("resultCode", headerVo.getResultCode());
    		headerMap.put("resultMsg", headerVo.getResultMsg());
    		headerMap.put("numOfRows", numOfRows);
    		headerMap.put("totCnt", totCount.get(0).getTotCount());
    		headerMap.put("totPage", totCount.get(0).getTotPage());
    		headerMap.put("pageNo", pageNo);
    		
    		jsonList.add(headerMap);
            
    		if(headerVo.getResultCode().equals("0"))
        	{
	            for(PlanCRHDInfoVO crhdVO : crhdList)
	    		{
        			String regionCd = crhdVO.getREGION_CD();
        			String intNo = crhdVO.getINT_NO();
        			String intNm = crhdVO.getINT_NM();
        			String planSn = crhdVO.getPLAN_SN();
        			String holyPlanMm = crhdVO.getHOLYDD_PLAN_MM();
        			String holyPlanDd = crhdVO.getHOLYDD_PLAN_DD();
        			String intPlanNo  = crhdVO.getINT_PLAN_NO();
        			String collctDtime = crhdVO.getCOLLCT_DTIME();
        			
        			Map<String,Object> jsonMap = new HashMap<String,Object>();
        			jsonMap.put("REGION_CD", regionCd);
        			jsonMap.put("INT_NO", intNo);
        			jsonMap.put("INT_NM", intNm);
        			jsonMap.put("PLAN_SN", planSn);
        			jsonMap.put("HOLY_PLAN_MM", holyPlanMm);
        			jsonMap.put("HOLY_PLAN_DD", holyPlanDd);
        			jsonMap.put("INT_PLAN_NO", intPlanNo);
        			jsonMap.put("COLLCT_DTIME", collctDtime);
        			
	            	jsonList.add(jsonMap);
	    		}
        	}
            String json = jsonMapper.writeValueAsString(jsonList);
            out.print(json);
            out.close();
    	}
		
	}
	
	//교차로요일계획정보 제공 서비스 444444444444444444444444
	
	@RequestMapping(value = "/"+govCode+"/PlanCrossRoadInfoService/getPlanCRWDInfo", method = RequestMethod.GET)
	@ResponseBody
	public void getPlanCRWDInfo(Locale locale, Model model, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
    	System.out.println("------------getPlanCRWDInfo Start------------");
    	
    	String type     = (req.getParameter("type") == null) ? "xml" : req.getParameter("type").toString();
    	String apimKey  = (req.getParameter("SG_APIM") == null) ? "" : req.getParameter("SG_APIM").toString(); // 게이트웨이 인증키 값 저장
    	String srchCTId = (req.getParameter("srchCTId") == null) ? "" : req.getParameter("srchCTId").toString();
    	String srchCRNm = (req.getParameter("srchCRNm") == null) ? "" : req.getParameter("srchCRNm").toString();
    	String pageNo  = (req.getParameter("pageNo") == null) ? "1" : req.getParameter("pageNo").toString();
    	String numOfRows = (req.getParameter("numOfRows") == null) ? "100" : req.getParameter("numOfRows").toString();
    	
    	if(srchCRNm != null)
    	{
    		srchCRNm = new String(srchCRNm.getBytes("8859_1"),"UTF-8"); // getBytes("8859_1")
    	}
    	
    	int eRow = Integer.parseInt(numOfRows);
    	if(eRow > 100 ) eRow = 100;
    	String [] calcPageReturn = calcPageStartEndNo(pageNo, eRow);

    	checkHttpAndKey("PlanCrossRoadInfoService/getPlanCRWDInfo",apimKey);
    	
    	Map<String,Object> paramMap = new HashMap<String,Object>();
    	paramMap.put("queryTotId", "getTotalCount");
		paramMap.put("queryId", "getPlanCRWDInfoList");
		paramMap.put("tableId", "TB_INT_DY_PLAN");
		paramMap.put("numOfRows", numOfRows);
		paramMap.put("srchCTId", srchCTId);
		paramMap.put("srchCRNm", srchCRNm);
		paramMap.put("sRow", calcPageReturn[0]);
		paramMap.put("eRow", calcPageReturn[1]);
    	
    	List<CrossRoadInfoHeaderVO> totCount = sigOpenService.getTotalCount(paramMap);
		List<PlanCRWDInfoVO> crwdList = sigOpenService.crwdList(paramMap);
		
		if(type.equals("xml"))
    	{
    		res.setContentType("application/xml; charset=UTF-8");
    		Format format = Format.getPrettyFormat();
        	format.setEncoding("UTF-8");
    		
    		Element data = new Element("PlanCrossRoadInfoService");
        	Element header = new Element("Header");
        	
    		if(headerVo.getResultCode().equals("0"))
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",numOfRows);
        		addElement(header,"totPage",totCount.get(0).getTotPage());
        		addElement(header,"totCount",totCount.get(0).getTotCount());
        		addElement(header,"pageNo",pageNo);
        		
        		data.addContent(header);
        		
        		for(PlanCRWDInfoVO crwdVO : crwdList)
        		{
        			String regionCd = crwdVO.getREGION_CD();
        			String intNo = crwdVO.getINT_NO();
        			String intNm = crwdVO.getINT_NM();
        			String planDy = crwdVO.getPLAN_DY();
        			String intPlanNo  = crwdVO.getINT_PLAN_NO();
        			String collctDtime = crwdVO.getCOLLCT_DTIME();
        			
        			Element element = new Element("PlanCRWDInfo");
        			addElement(element,"REGION_CD",regionCd);
        			addElement(element,"INT_NO",intNo);
        			addElement(element,"INT_NM",intNm);
        			addElement(element,"PLAN_DY",planDy);
        			addElement(element,"INT_PLAN_NO",intPlanNo);
        			addElement(element,"COLLCT_DTIME",collctDtime);
        			
        		    data.addContent(element);
        		}
        	}
        	else
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",headerVo.getNumOfRows());
        		addElement(header,"totPage",headerVo.getTotPage());
        		addElement(header,"totCount",headerVo.getTotCount());
        		addElement(header,"pageNo",headerVo.getPageNo());
        		
        		data.addContent(header);
        	}
        	
        	Document document = new Document(data);
        	
        	XMLOutputter outputter = new XMLOutputter();
        	outputter.setFormat(format);
        	outputter.output(document, res.getWriter());
    	}
    	else
    	{
    		ObjectMapper jsonMapper = new ObjectMapper();
            res.setContentType("application/json; charset=UTF-8");
            res.setCharacterEncoding("UTF-8");
            PrintWriter out = res.getWriter();
            
            List<Map<String,Object>> jsonList = new ArrayList<Map<String,Object>>();
            Map<String,Object> headerMap = new HashMap<String,Object>();
    		headerMap.put("resultCode", headerVo.getResultCode());
    		headerMap.put("resultMsg", headerVo.getResultMsg());
    		headerMap.put("numOfRows", numOfRows);
    		headerMap.put("totCnt", totCount.get(0).getTotCount());
    		headerMap.put("totPage", totCount.get(0).getTotPage());
    		headerMap.put("pageNo", pageNo);
    		
    		jsonList.add(headerMap);
            
    		if(headerVo.getResultCode().equals("0"))
        	{
	            for(PlanCRWDInfoVO crwdVO : crwdList)
	    		{
        			String regionCd = crwdVO.getREGION_CD();
        			String intNo = crwdVO.getINT_NO();
        			String intNm = crwdVO.getINT_NM();
        			String planDy = crwdVO.getPLAN_DY();
        			String intPlanNo  = crwdVO.getINT_PLAN_NO();
        			String collctDtime = crwdVO.getCOLLCT_DTIME();
        			
        			Map<String,Object> jsonMap = new HashMap<String,Object>();
        			jsonMap.put("REGION_CD", regionCd);
        			jsonMap.put("INT_NO", intNo);
        			jsonMap.put("INT_NM", intNm);
        			jsonMap.put("PLAN_DY", planDy);
        			jsonMap.put("INT_PLAN_NO", intPlanNo);
        			jsonMap.put("COLLCT_DTIME", collctDtime);
        			
	            	jsonList.add(jsonMap);
	    		}
        	}
            String json = jsonMapper.writeValueAsString(jsonList);
            out.print(json);
            out.close();
    	}
	}
	
	//교차로예약계획정보 제공 서비스 5555555555555555555555555
	
	@RequestMapping(value = "/"+govCode+"/PlanCrossRoadInfoService/getPlanCRRSInfo", method = RequestMethod.GET)
	@ResponseBody
	public void getPlanCRRSInfo(Locale locale, Model model, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
    	System.out.println("------------getPlanCRRSInfo Start------------");
    	
    	String type     = (req.getParameter("type") == null) ? "xml" : req.getParameter("type").toString();
    	String apimKey  = (req.getParameter("SG_APIM") == null) ? "" : req.getParameter("SG_APIM").toString(); // 게이트웨이 인증키 값 저장
    	String srchCTId = (req.getParameter("srchCTId") == null) ? "" : req.getParameter("srchCTId").toString();
    	String srchCRNm = (req.getParameter("srchCRNm") == null) ? "" : req.getParameter("srchCRNm").toString();
    	String pageNo  = (req.getParameter("pageNo") == null) ? "1" : req.getParameter("pageNo").toString();
    	String numOfRows = (req.getParameter("numOfRows") == null) ? "100" : req.getParameter("numOfRows").toString();
    	
    	if(srchCRNm != null)
    	{
    		srchCRNm = new String(srchCRNm.getBytes("8859_1"),"UTF-8"); // getBytes("8859_1")
    	}
    	
    	int eRow = Integer.parseInt(numOfRows);
    	if(eRow > 100 ) eRow = 100;
    	String [] calcPageReturn = calcPageStartEndNo(pageNo, eRow);
    	
    	checkHttpAndKey("PlanCrossRoadInfoService/getPlanCRRSInfo",apimKey);
    	
    	Map<String,Object> paramMap = new HashMap<String,Object>();
    	paramMap.put("queryTotId", "getTotalCount");
		paramMap.put("queryId", "getPlanCRRSInfoList");
		paramMap.put("tableId", "TB_INT_RESRV_PLAN");
		paramMap.put("numOfRows", numOfRows);
		paramMap.put("srchCTId", srchCTId);
		paramMap.put("srchCRNm", srchCRNm);
		paramMap.put("sRow", calcPageReturn[0]);
		paramMap.put("eRow", calcPageReturn[1]);
    	
    	List<CrossRoadInfoHeaderVO> totCount = sigOpenService.getTotalCount(paramMap);
		List<PlanCRRSInfoVO> crrsList = sigOpenService.crrsList(paramMap);
		
		if(type.equals("xml"))
    	{
    		res.setContentType("application/xml; charset=UTF-8");
    		Format format = Format.getPrettyFormat();
        	format.setEncoding("UTF-8");
    		
    		Element data = new Element("PlanCrossRoadInfoService");
        	Element header = new Element("Header");
        	
    		if(headerVo.getResultCode().equals("0"))
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",numOfRows);
        		addElement(header,"totPage",totCount.get(0).getTotPage());
        		addElement(header,"totCount",totCount.get(0).getTotCount());
        		addElement(header,"pageNo",pageNo);
        		
        		data.addContent(header);
        		
        		for(PlanCRRSInfoVO crrsVO : crrsList)
        		{
        			String regionCd = crrsVO.getREGION_CD();
        			String intNo = crrsVO.getINT_NO();
        			String intNm = crrsVO.getINT_NM();
        			String resrvNo = crrsVO.getRESRV_NO();
        			String resrvMm = crrsVO.getRESRV_MM();
        			String resrvDd = crrsVO.getRESRV_DD();
        			String resrvDy = crrsVO.getRESRV_DY();
        			String resrvStrtHh = crrsVO.getRESRV_STRT_HH();
        			String resrvStrtMi = crrsVO.getRESRV_STRT_MI();
        			String resrvEndHh = crrsVO.getRESRV_END_HH();
        			String resrvEndMi = crrsVO.getRESRV_END_MI();
        			String resrvContrlCd = crrsVO.getRESRV_CONTRL_CD();
        			String updDtime = crrsVO.getUPD_DTIME();
        			
        			Element element = new Element("PlanCRRSInfo");
        			addElement(element,"REGION_CD",regionCd);
        			addElement(element,"INT_NO",intNo);
        			addElement(element,"INT_NM",intNm);
        			addElement(element,"RESRV_NO",resrvNo);
        			addElement(element,"RESRV_MM",resrvMm);
        			addElement(element,"RESRV_DD",resrvDd);
        			addElement(element,"RESRV_DY",resrvDy);
        			addElement(element,"RESRV_STRT_HH",resrvStrtHh);
        			addElement(element,"RESRV_STRT_MI",resrvStrtMi);
        			addElement(element,"RESRV_END_HH",resrvEndHh);
        			addElement(element,"RESRV_END_MI",resrvEndMi);
        			addElement(element,"RESRV_CONTRL_CD",resrvContrlCd);
        			addElement(element,"COLLCT_DTIME",updDtime);
        			
        		    data.addContent(element);
        		}
        	}
        	else
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",headerVo.getNumOfRows());
        		addElement(header,"totPage",headerVo.getTotPage());
        		addElement(header,"totCount",headerVo.getTotCount());
        		addElement(header,"pageNo",headerVo.getPageNo());
        		
        		data.addContent(header);
        	}
        	
        	Document document = new Document(data);
        	
        	XMLOutputter outputter = new XMLOutputter();
        	outputter.setFormat(format);
        	outputter.output(document, res.getWriter());
    	}
    	else
    	{
    		ObjectMapper jsonMapper = new ObjectMapper();
            res.setContentType("application/json; charset=UTF-8");
            res.setCharacterEncoding("UTF-8");
            PrintWriter out = res.getWriter();
            
            List<Map<String,Object>> jsonList = new ArrayList<Map<String,Object>>();
            Map<String,Object> headerMap = new HashMap<String,Object>();
    		headerMap.put("resultCode", headerVo.getResultCode());
    		headerMap.put("resultMsg", headerVo.getResultMsg());
    		headerMap.put("numOfRows", numOfRows);
    		headerMap.put("totCnt", totCount.get(0).getTotCount());
    		headerMap.put("totPage", totCount.get(0).getTotPage());
    		headerMap.put("pageNo", pageNo);
    		
    		jsonList.add(headerMap);
            
    		if(headerVo.getResultCode().equals("0"))
        	{
	            for(PlanCRRSInfoVO crrsVO : crrsList)
	    		{
	            	String regionCd = crrsVO.getREGION_CD();
        			String intNo = crrsVO.getINT_NO();
        			String intNm = crrsVO.getINT_NM();
        			String resrvNo = crrsVO.getRESRV_NO();
        			String resrvMm = crrsVO.getRESRV_MM();
        			String resrvDd = crrsVO.getRESRV_DD();
        			String resrvDy = crrsVO.getRESRV_DY();
        			String resrvStrtHh = crrsVO.getRESRV_STRT_HH();
        			String resrvStrtMi = crrsVO.getRESRV_STRT_MI();
        			String resrvEndHh = crrsVO.getRESRV_END_HH();
        			String resrvEndMi = crrsVO.getRESRV_END_MI();
        			String resrvContrlCd = crrsVO.getRESRV_CONTRL_CD();
        			String updDtime = crrsVO.getUPD_DTIME();
        			
        			Map<String,Object> jsonMap = new HashMap<String,Object>();
        			jsonMap.put("REGION_CD", regionCd);
        			jsonMap.put("INT_NO", intNo);
        			jsonMap.put("INT_NM", intNm);
        			jsonMap.put("RESRV_NO", resrvNo);
        			jsonMap.put("RESRV_MM", resrvMm);
        			jsonMap.put("RESRV_DD", resrvDd);
        			jsonMap.put("RESRV_DY", resrvDy);
        			jsonMap.put("RESRV_STRT_HH", resrvStrtHh);
        			jsonMap.put("RESRV_STRT_MI", resrvStrtMi);
        			jsonMap.put("RESRV_END_HH", resrvEndHh);
        			jsonMap.put("RESRV_END_MI", resrvEndMi);
        			jsonMap.put("RESRV_CONTRL_CD", resrvContrlCd);
        			jsonMap.put("COLLCT_DTIME", updDtime);
        			
	            	jsonList.add(jsonMap);
	    		}
        	}
            String json = jsonMapper.writeValueAsString(jsonList);
            out.print(json);
            out.close();
    	}

	}
	
	//교차로운영계획정보 제공 서비스 6666666666666666666
	
	@RequestMapping(value = "/"+govCode+"/PlanCrossRoadInfoService/getPlanCROPInfo", method = RequestMethod.GET)
	@ResponseBody
	public void getPlanCROPInfo(Locale locale, Model model, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
    	System.out.println("------------getPlanCROPInfo Start------------");
    	
    	String type     = (req.getParameter("type") == null) ? "xml" : req.getParameter("type").toString();
    	String apimKey  = (req.getParameter("SG_APIM") == null) ? "" : req.getParameter("SG_APIM").toString(); // 게이트웨이 인증키 값 저장
    	String srchCTId = (req.getParameter("srchCTId") == null) ? "" : req.getParameter("srchCTId").toString();
    	String srchCRNm = (req.getParameter("srchCRNm") == null) ? "" : req.getParameter("srchCRNm").toString();
    	String pageNo  = (req.getParameter("pageNo") == null) ? "1" : req.getParameter("pageNo").toString();
    	String numOfRows = (req.getParameter("numOfRows") == null) ? "100" : req.getParameter("numOfRows").toString();
    	
    	if(srchCRNm != null)
    	{
    		srchCRNm = new String(srchCRNm.getBytes("8859_1"),"UTF-8"); // getBytes("8859_1")
    	}
    	
    	int eRow = Integer.parseInt(numOfRows);
    	if(eRow > 100 ) eRow = 100;

    	String [] calcPageReturn = calcPageStartEndNo(pageNo, eRow);
    	
    	checkHttpAndKey("PlanCrossRoadInfoService/getPlanCROPInfo",apimKey);
    	
    	Map<String,Object> paramMap = new HashMap<String,Object>();
    	paramMap.put("queryTotId", "getTotalCount");
		paramMap.put("queryId", "getPlanCROPInfoList");
		paramMap.put("tableId", "TB_INT_OPER_PLAN");
		paramMap.put("numOfRows", numOfRows);
		paramMap.put("srchCTId", srchCTId);
		paramMap.put("srchCRNm", srchCRNm);
		paramMap.put("sRow", calcPageReturn[0]);
		paramMap.put("eRow", calcPageReturn[1]);

    	List<CrossRoadInfoHeaderVO> totCount = sigOpenService.getTotalCount(paramMap);
		List<PlanCROPInfoVO> cropList = sigOpenService.cropList(paramMap);

		if(type.equals("xml"))
    	{
    		res.setContentType("application/xml; charset=UTF-8");
    		Format format = Format.getPrettyFormat();
        	format.setEncoding("UTF-8");
    		
    		Element data = new Element("PlanCrossRoadInfoService");
        	Element header = new Element("Header");
        	
    		if(headerVo.getResultCode().equals("0"))
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",numOfRows);
        		addElement(header,"totPage",totCount.get(0).getTotPage());
        		addElement(header,"totCount",totCount.get(0).getTotCount());
        		addElement(header,"pageNo",pageNo);
        		
        		data.addContent(header);
        		
        		for(PlanCROPInfoVO cropVO : cropList)
        		{
        			String regionCd = cropVO.getREGION_CD();
        			String intNo = cropVO.getINT_NO();
        			String intNm = cropVO.getINT_NM();
        			String intPlanNo = cropVO.getINT_PLAN_NO();
        			String intPlanIdxNo = cropVO.getINT_PLAN_IDX_NO();
        			String operPlanHh = cropVO.getOPER_PLAN_HH();
        			String operPlanMi = cropVO.getOPER_PLAN_MI();
        			String intOperCycleVal = cropVO.getINT_OPER_CYCLE_VAL();
        			String intOperOffsetVal = cropVO.getINT_OPER_OFFSET_VAL();
        			String aRing1PhaseVal = cropVO.getA_RING_1_PHASE_VAL();
        			String aRing2PhaseVal = cropVO.getA_RING_2_PHASE_VAL();
        			String aRing3PhaseVal = cropVO.getA_RING_3_PHASE_VAL();
        			String aRing4PhaseVal = cropVO.getA_RING_4_PHASE_VAL();
        			String aRing5PhaseVal = cropVO.getA_RING_5_PHASE_VAL();
        			String aRing6PhaseVal = cropVO.getA_RING_6_PHASE_VAL();
        			String aRing7PhaseVal = cropVO.getA_RING_7_PHASE_VAL();
        			String aRing8PhaseVal = cropVO.getA_RING_8_PHASE_VAL();
        			String bRing1PhaseVal = cropVO.getB_RING_1_PHASE_VAL();
        			String bRing2PhaseVal = cropVO.getB_RING_2_PHASE_VAL();
        			String bRing3PhaseVal = cropVO.getB_RING_3_PHASE_VAL();
        			String bRing4PhaseVal = cropVO.getB_RING_4_PHASE_VAL();
        			String bRing5PhaseVal = cropVO.getB_RING_5_PHASE_VAL();
        			String bRing6PhaseVal = cropVO.getB_RING_6_PHASE_VAL();
        			String bRing7PhaseVal = cropVO.getB_RING_7_PHASE_VAL();
        			String bRing8PhaseVal = cropVO.getB_RING_8_PHASE_VAL();
        			
        			Element element = new Element("PlanCROPInfo");
        			addElement(element,"REGION_CD",regionCd);
        			addElement(element,"INT_NO",intNo);
        			addElement(element,"INT_NM",intNm);
        			addElement(element,"INT_PLAN_NO",intPlanNo);
        			addElement(element,"INT_PLAN_IDX_NO",intPlanIdxNo);
        			addElement(element,"OPER_PLAN_HH",operPlanHh);
        			addElement(element,"OPER_PLAN_MI",operPlanMi);
        			addElement(element,"INT_OPER_CYCLE_VAL",intOperCycleVal);
        			addElement(element,"INT_OPER_OFFSET_VAL",intOperOffsetVal);
        			addElement(element,"A_RING_1_PHASE_VAL",aRing1PhaseVal);
        			addElement(element,"A_RING_2_PHASE_VAL",aRing2PhaseVal);
        			addElement(element,"A_RING_3_PHASE_VAL",aRing3PhaseVal);
        			addElement(element,"A_RING_4_PHASE_VAL",aRing4PhaseVal);
        			addElement(element,"A_RING_5_PHASE_VAL",aRing5PhaseVal);
        			addElement(element,"A_RING_6_PHASE_VAL",aRing6PhaseVal);
        			addElement(element,"A_RING_7_PHASE_VAL",aRing7PhaseVal);
        			addElement(element,"A_RING_8_PHASE_VAL",aRing8PhaseVal);
        			addElement(element,"B_RING_1_PHASE_VAL",bRing1PhaseVal);
        			addElement(element,"B_RING_2_PHASE_VAL",bRing2PhaseVal);
        			addElement(element,"B_RING_3_PHASE_VAL",bRing3PhaseVal);
        			addElement(element,"B_RING_4_PHASE_VAL",bRing4PhaseVal);
        			addElement(element,"B_RING_5_PHASE_VAL",bRing5PhaseVal);
        			addElement(element,"B_RING_6_PHASE_VAL",bRing6PhaseVal);
        			addElement(element,"B_RING_7_PHASE_VAL",bRing7PhaseVal);
        			addElement(element,"B_RING_8_PHASE_VAL",bRing8PhaseVal);
        			
        		    data.addContent(element);
        		}
        	}
        	else
        	{
        		addElement(header,"resultCode",headerVo.getResultCode());
        		addElement(header,"resultMsg",headerVo.getResultMsg());
        		addElement(header,"numOfRows",headerVo.getNumOfRows());
        		addElement(header,"totPage",headerVo.getTotPage());
        		addElement(header,"totCount",headerVo.getTotCount());
        		addElement(header,"pageNo",headerVo.getPageNo());
        		
        		data.addContent(header);
        	}
        	
        	Document document = new Document(data);
        	
        	XMLOutputter outputter = new XMLOutputter();
        	outputter.setFormat(format);
        	outputter.output(document, res.getWriter());
    	}
    	else
    	{
    		ObjectMapper jsonMapper = new ObjectMapper();
            res.setContentType("application/json; charset=UTF-8");
            res.setCharacterEncoding("UTF-8");
            PrintWriter out = res.getWriter();
            
            List<Map<String,Object>> jsonList = new ArrayList<Map<String,Object>>();
            Map<String,Object> headerMap = new HashMap<String,Object>();
    		headerMap.put("resultCode", headerVo.getResultCode());
    		headerMap.put("resultMsg", headerVo.getResultMsg());
    		headerMap.put("numOfRows", numOfRows);
    		headerMap.put("totCnt", totCount.get(0).getTotCount());
    		headerMap.put("totPage", totCount.get(0).getTotPage());
    		headerMap.put("pageNo", pageNo);
    		
    		jsonList.add(headerMap);
            
    		if(headerVo.getResultCode().equals("0"))
        	{
	            for(PlanCROPInfoVO cropVO : cropList)
	    		{
	            	String regionCd = cropVO.getREGION_CD();
        			String intNo = cropVO.getINT_NO();
        			String intNm = cropVO.getINT_NM();
        			String intPlanNo = cropVO.getINT_PLAN_NO();
        			String intPlanIdxNo = cropVO.getINT_PLAN_IDX_NO();
        			String operPlanHh = cropVO.getOPER_PLAN_HH();
        			String operPlanMi = cropVO.getOPER_PLAN_MI();
        			String intOperCycleVal = cropVO.getINT_OPER_CYCLE_VAL();
        			String intOperOffsetVal = cropVO.getINT_OPER_OFFSET_VAL();
        			String aRing1PhaseVal = cropVO.getA_RING_1_PHASE_VAL();
        			String aRing2PhaseVal = cropVO.getA_RING_2_PHASE_VAL();
        			String aRing3PhaseVal = cropVO.getA_RING_3_PHASE_VAL();
        			String aRing4PhaseVal = cropVO.getA_RING_4_PHASE_VAL();
        			String aRing5PhaseVal = cropVO.getA_RING_5_PHASE_VAL();
        			String aRing6PhaseVal = cropVO.getA_RING_6_PHASE_VAL();
        			String aRing7PhaseVal = cropVO.getA_RING_7_PHASE_VAL();
        			String aRing8PhaseVal = cropVO.getA_RING_8_PHASE_VAL();
        			String bRing1PhaseVal = cropVO.getB_RING_1_PHASE_VAL();
        			String bRing2PhaseVal = cropVO.getB_RING_2_PHASE_VAL();
        			String bRing3PhaseVal = cropVO.getB_RING_3_PHASE_VAL();
        			String bRing4PhaseVal = cropVO.getB_RING_4_PHASE_VAL();
        			String bRing5PhaseVal = cropVO.getB_RING_5_PHASE_VAL();
        			String bRing6PhaseVal = cropVO.getB_RING_6_PHASE_VAL();
        			String bRing7PhaseVal = cropVO.getB_RING_7_PHASE_VAL();
        			String bRing8PhaseVal = cropVO.getB_RING_8_PHASE_VAL();
        			
        			Map<String,Object> jsonMap = new HashMap<String,Object>();
        			jsonMap.put("REGION_CD", regionCd);
        			jsonMap.put("INT_NO", intNo);
        			jsonMap.put("INT_NM", intNm);
        			jsonMap.put("INT_PLAN_NO", intPlanNo);
        			jsonMap.put("INT_PLAN_IDX_NO", intPlanIdxNo);
        			jsonMap.put("OPER_PLAN_HH", operPlanHh);
        			jsonMap.put("OPER_PLAN_MI", operPlanMi);
        			jsonMap.put("INT_OPER_CYCLE_VAL", intOperCycleVal);
        			jsonMap.put("INT_OPER_OFFSET_VAL", intOperOffsetVal);
        			jsonMap.put("A_RING_1_PHASE_VAL", aRing1PhaseVal);
        			jsonMap.put("A_RING_2_PHASE_VAL", aRing2PhaseVal);
        			jsonMap.put("A_RING_3_PHASE_VAL", aRing3PhaseVal);
        			jsonMap.put("A_RING_4_PHASE_VAL", aRing4PhaseVal);
        			jsonMap.put("A_RING_5_PHASE_VAL", aRing5PhaseVal);
        			jsonMap.put("A_RING_6_PHASE_VAL", aRing6PhaseVal);
        			jsonMap.put("A_RING_7_PHASE_VAL", aRing7PhaseVal);
        			jsonMap.put("A_RING_8_PHASE_VAL", aRing8PhaseVal);
        			jsonMap.put("B_RING_1_PHASE_VAL", bRing1PhaseVal);
        			jsonMap.put("B_RING_2_PHASE_VAL", bRing2PhaseVal);
        			jsonMap.put("B_RING_3_PHASE_VAL", bRing3PhaseVal);
        			jsonMap.put("B_RING_4_PHASE_VAL", bRing4PhaseVal);
        			jsonMap.put("B_RING_5_PHASE_VAL", bRing5PhaseVal);
        			jsonMap.put("B_RING_6_PHASE_VAL", bRing6PhaseVal);
        			jsonMap.put("B_RING_7_PHASE_VAL", bRing7PhaseVal);
        			jsonMap.put("B_RING_8_PHASE_VAL", bRing8PhaseVal);
        			
	            	jsonList.add(jsonMap);
	    		}
        	}
            String json = jsonMapper.writeValueAsString(jsonList);
            out.print(json);
            out.close();
    	}
    	
	}
		
		
	//페이지 시작, 끝 번호 추출...
	public String [] calcPageStartEndNo(String pageNo, int numOfRows) {
		
    	int sPageNo = Integer.parseInt(pageNo) - 1;
    	int ePageNo = Integer.parseInt(pageNo);
    	int sRow = 1;
    	int eRow = 100;
    	
		sRow = (sPageNo * numOfRows) + 1;
		eRow = ePageNo * numOfRows;
    	
    	String [] result = {Integer.toString(sRow),Integer.toString(eRow)};
    	
    	return result;
	}
		
    
    //통신장애 및 인증키 확인 프로세스...
    public void checkHttpAndKey(String svcId, String apimKey) {
    	
    	HttpsClient httpsClient = new HttpsClient();

		try {
			
			headerVo = httpsClient.pingWeb(svcId,apimKey);
			
		} catch (SQLException e) {
			
		} catch (Exception e) {
			
		}
    }
    
    // 엘리먼트 생성
 	public Element addElement(Element parent, String name, String value) {
 		Element element = new Element(name);
 		element.setText(value);
 		parent.addContent(element);
     return parent;
     }
  
     // 애트리뷰트 생성
     public void addAttribute(Element element, String name, String value){
     	Attribute attribute = new Attribute(name,value);
     	element.setAttribute(attribute);
     }
    
    /*public void createCrossRoadInfo(String type, String bodyName, Map<String,Object> paramMap) throws Exception {
    	int listLen = 0;
    	
    	List<CrossRoadInfoHeaderVO> totCount = sigOpenService.getTotalCount(paramMap);
    	headerVo.setTotCount(totCount.get(0).getTotCount());
    	headerVo.setTotPage(totCount.get(0).getTotPage());
    	
    	if("list".equals(bodyName)) 
    	{
    		List<CrossRoadInfoListVO> crilList = null;
    		crilList = sigOpenService.listList(paramMap);
    		listLen = crilList.size();
    		
    		if(listLen > 0)
        	{
    			if(type.equals("xml")) 
        		{
    				for(CrossRoadInfoListVO crixVo : crilList) 
            		{
            			//crossRoadInfo.getCrossRoadInfoListVO().add(crixVo);
    					
            		}
        		}
    			else if(type.equals("json")) 
    			{
    				ObjectMapper jsonMapper = new ObjectMapper();
    	            String json = jsonMapper.writeValueAsString(crilList);
    	            System.out.println(json);
    			}
        	}
    	}
    	else if("detail".equals(bodyName))
    	{
    		List<CrossRoadInfoDetailVO> cridList = null;
    		cridList = sigOpenService.detailList(paramMap);
    		listLen = cridList.size();
    		
    		if(listLen > 0)
        	{
        		for(CrossRoadInfoDetailVO cridVo : cridList)
        		{
        			crossRoadInfo.getCrossRoadInfoDetailVO().add(cridVo);
        		}
        	}
    	}
    	else if("crhd".equals(bodyName))
    	{
    		List<PlanCRHDInfoVO> crhdList = null;
    		crhdList = sigOpenService.crhdList(paramMap);
    		listLen = crhdList.size();
    		
    		if(listLen > 0)
        	{
        		for(PlanCRHDInfoVO crhdVo : crhdList)
        		{
        			crossRoadInfo.getPlanCRHDInfoVO().add(crhdVo);
        		}
        	}
    	}
    	else if("crwd".equals(bodyName))
    	{
    		List<PlanCRWDInfoVO> crwdList = null;
    		crwdList = sigOpenService.crwdList(paramMap);
    		listLen = crwdList.size();
    		
    		if(listLen > 0)
        	{
        		for(PlanCRWDInfoVO crwdVo : crwdList)
        		{
        			crossRoadInfo.getPlanCRWDInfoVO().add(crwdVo);
        		}
        	}
    	}
    	else if("crrs".equals(bodyName))
    	{
    		List<PlanCRRSInfoVO> crrsList = null;
    		crrsList = sigOpenService.crrsList(paramMap);
    		listLen = crrsList.size();
    		
    		if(listLen > 0)
        	{
        		for(PlanCRRSInfoVO crrsVo : crrsList)
        		{
        			crossRoadInfo.getPlanCRRSInfoVO().add(crrsVo);
        		}
        	}
    	}
    	else if("crop".equals(bodyName))
    	{
    		List<PlanCROPInfoVO> cropList = null;
    		cropList = sigOpenService.cropList(paramMap);
    		listLen = cropList.size();
    		
    		if(listLen > 0)
        	{
        		for(PlanCROPInfoVO cropVo : cropList)
        		{
        			crossRoadInfo.getPlanCROPInfoVO().add(cropVo);
        		}
        	}
    	}
    	
	}*/
		
}
