package com.eactive.eai.adapter.wca.servlet;

import com.eactive.eai.adapter.wca.exception.WCAException;
import com.eactive.eai.adapter.wca.global.WCADefine;
import com.eactive.eai.adapter.wca.jdbc.CommonJDBC;
import com.eactive.eai.adapter.wca.jdbc.JDBCFactory;
import com.eactive.eai.adapter.wca.jdbc.SQLData;
import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import org.apache.log4j.Logger;

/** 
* 1. 기능 : JSP에서 표시될 select box 데이타 생성
* 2. 처리 개요 : 
* 3. 주의사항 
*
* @author  : 
* @version : 
* @see : 
* @since   :
*/
public class SelectBoxData extends BaseDAO
{
    Logger logger = Logger.getLogger(SelectBoxData.class);
	private static SelectBoxData instance = new SelectBoxData();	
    public static String ALL_SEARCH = "ALL";
    public static String LU_GRP_GUBUNJA  = "_";
    
    /** 
    * 1. 기능 : Constructor
    * 2. 처리 개요 : 
    * 3. 주의사항 
    **/
	private SelectBoxData()
	{
	}
			
    /** 
    * 1. 기능 :  SelectBoxData Singleton Object 반환
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @return   SelectBoxData Singleton object
    **/	  
	public static SelectBoxData getInstance()  
	{			
		return instance;
	}
	
   /** 
    * 1. 기능 :  Node Name 반환
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @return   selectbox script 데이타 (String)반환
    **/	  
    public String getNodeName(String value) throws DAOException {        
		StringBuffer sb_query = new StringBuffer();
		StringBuffer sb_result = new StringBuffer();
        
        int     nRsltCnt ;
        String sNodeName = "";
        String sSelected = "";
        
        if (value == null) value ="";
		try {
			// 20080808 Start : SNA Host 참조 테이블 변경
			/*
            sb_query.append("SELECT DISTINCT \"HostName\" FROM " +  TableInfoEx.T_EAI_SVR + 
                            " WHERE \"HostName\" is not null " +
                            " ORDER BY \"HostName\"");
            */
            sb_query.append("SELECT DISTINCT \"SNAAdptrSevrName\" FROM " +  TableInfoEx.T_LU_HOST + 
                    " WHERE \"SNAAdptrSevrName\" is not null " +
                    " ORDER BY \"SNAAdptrSevrName\"");
			// 20080808 End            
            JDBCFactory factory   = JDBCFactory.newInstance();
            CommonJDBC  common = (CommonJDBC)factory.create(CommonJDBC.class);
            SQLData     data = common.select(sb_query.toString());
	        nRsltCnt 	 = data.getDispDatasCount();
            
            for (int nIndx = 0 ; nIndx < nRsltCnt; nIndx++)
            {
            	// 20080808 Start : SNA Host 참조 테이블 변경
                // sNodeName = data.getSQLStringData("HostName" + nIndx);
            	sNodeName = data.getSQLStringData("SNAAdptrSevrName" + nIndx);
    			// 20080808 End
            	
                if (sNodeName.trim().equals("")) continue;
                
                if (sNodeName.equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sb_result.append("<option value =\"" + sNodeName + "\" " + sSelected + " > " + sNodeName + "</option>" );
            }
            
	        return sb_result.toString();
	    } catch(Exception e) {
	        logger.error("getNodeName error", e);
	        throw new DAOException("SelectBoxData.getNodeName Error. - " + e.getMessage());
	    } finally {
	        this.disconnect();
        }
	}     
    
//     public String getBackupNodeName(String value) throws DAOException {        
//		StringBuffer sb_query = new StringBuffer();
//		StringBuffer sb_result = new StringBuffer();
//        
//        int     nRsltCnt ;
//        String sNodeName = "";
//        String sSelected = "";
//        
//        if (value == null) value ="";
//		try {
//            sb_query.append("SELECT DISTINCT \"EAISevrIP\" FROM " +  TableInfoEx.T_EAI_SVR + 
//                            " WHERE \"EAISevrInstncName\" != '" + ALL_SEARCH +"'" + 
//                            " ORDER BY \"EAISevrIP\" ");
//            
//            JDBCFactory factory   = JDBCFactory.newInstance();
//            CommonJDBC  common = (CommonJDBC)factory.create(CommonJDBC.class);
//            SQLData     data = common.select(sb_query.toString());
//	        nRsltCnt 	 = data.getDispDatasCount();
//            
//            for (int nIndx = 0 ; nIndx < nRsltCnt; nIndx++)
//            {
//                sNodeName = data.getSQLStringData("EAISevrIP" + nIndx);
//                
//                sNodeName = getHostNameByIP(sNodeName);
//                if (sNodeName != null && sNodeName.equals(value))
//                    sSelected = "selected";
//                else sSelected ="";
//                sb_result.append("<option value =\"" + sNodeName + "\" " + sSelected + " > " + sNodeName + "</option>" );
//            }
//            
//	        return sb_result.toString();
//	    } catch(Exception e) {
//	    	logger.error("SelectBoxData.getBackupNodeName Error", e);
//	        throw new DAOException("SelectBoxData.getBackupNodeName Error. - " + e.getMessage());
//	    } finally {
//	        this.disconnect();
//        }
//	}     
    
//    public String getHostNameByIP(String sIP)
//    {
//        try{
//            InetAddress 	inet1	 	=	null;
//            inet1 = InetAddress.getByName(sIP);
//            return inet1.getHostName();
//        }
//        catch (UnknownHostException host){
//        	logger.error("getHostNameByIP error", host);
//            return sIP;
//        }
//    }    
    
   /** 
    * 1. 기능 : EAI instance 이름 반환
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @return   selectbox script 데이타 (String)반환
    **/	  
	public String getEaiSystemName(String value) throws DAOException {        
		StringBuffer sb_query = new StringBuffer();
		StringBuffer sb_result = new StringBuffer();
        
        int     nRsltCnt ;
        String sEaiSvrName = "";
        String sSelected = "";
        
        if (value == null) value ="";
		try {
            
			sb_query.append("SELECT \"EAISevrInstncName\" FROM " +  TableInfoEx.T_EAI_SVR + " ORDER BY \"EAISevrInstncName\"");
            JDBCFactory factory   = JDBCFactory.newInstance();
            CommonJDBC  common = (CommonJDBC)factory.create(CommonJDBC.class);
            SQLData     data = common.select(sb_query.toString());            
            
	        nRsltCnt 	 = data.getDispDatasCount();
            sEaiSvrName = "";
            
            for (int nIndx = 0 ; nIndx < nRsltCnt; nIndx++)
            {            
                sEaiSvrName = data.getSQLStringData("EAISevrInstncName" + nIndx);
                if (sEaiSvrName.equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sb_result.append("<option value =\"" + sEaiSvrName + "\" " + sSelected + " > " + sEaiSvrName + "</option>" );
            }
            
	        return sb_result.toString();
	    } catch(Exception e) {
	        logger.error("SelectBoxData.getEaiSystemName Error. - ", e);
	        throw new DAOException("SelectBoxData.getEaiSystemName Error. - " + e.getMessage());
	    } finally {
	        this.disconnect();
	    }	    
	}
    
   /** 
    * 1. 기능 : 업무 이름 반환
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @return   selectbox script 데이타 (String)반환
    **/	  
    public String getUpmuName(String value) throws DAOException {        
		StringBuffer sb_query = new StringBuffer();
		StringBuffer sb_result = new StringBuffer();
        
        int    nRsltCnt ;
        String sUpmuName = "";
        String sSelected = "";
        
        if (value == null) value ="";
		try {
            /*
			sb_query.append("SELECT distinct ADPTGRPNAME " + 
                            " FROM "+ 
                            "( " + 
                            "    SELECT decode(pos1, 0, \"AdptrBzwkGroupName\", SUBSTR(\"AdptrBzwkGroupName\",pos1+1, pos2-pos1-1)) as ADPTGRPNAME " + 
                            "    FROM " + 
                            "        (SELECT INSTR(\"AdptrBzwkGroupName\",'_', 1, 1) as pos1,  " +
                            "                INSTR(\"AdptrBzwkGroupName\",'_', 1, 2) as pos2, " + 
                            "                \"AdptrBzwkGroupName\" from " + TableInfo.T_ADAPTER_GROUP  +
                            "        )   "+
                            ") ORDER BY ADPTGRPNAME");
            */
            sb_query.append("SELECT distinct ADPTGRPNAME " + 
                            " FROM "+ 
                            "( " + 
                            "    SELECT CASE pos1 WHEN 0 THEN \"AdptrBzwkGroupName\" ELSE SUBSTR(\"AdptrBzwkGroupName\",pos1+1, pos2-pos1-1) END as ADPTGRPNAME " + 
                            "    FROM " + 
                            "        (SELECT LOCATE('_',\"AdptrBzwkGroupName\", 1) as pos1,  " +
                            "                LOCATE('_',\"AdptrBzwkGroupName\", LOCATE('_',\"AdptrBzwkGroupName\", 1) + 1) as pos2, " + 
                            "                \"AdptrBzwkGroupName\" from " + TableInfoEx.T_ADAPTER_GROUP  +
                            "        WHERE \"AdptrCd\" = 'SNA')  TMP1 "+
                            ") TMP2 ORDER BY ADPTGRPNAME");
            JDBCFactory factory   = JDBCFactory.newInstance();
            CommonJDBC  common = (CommonJDBC)factory.create(CommonJDBC.class);
            SQLData     data = common.select(sb_query.toString());            
            
	        nRsltCnt 	 = data.getDispDatasCount();
            sUpmuName = "";
            
            for (int nIndx = 0 ; nIndx < nRsltCnt; nIndx++)
            {            
                sUpmuName = data.getSQLStringData("ADPTGRPNAME" + nIndx);
                                
                if (sUpmuName.equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sb_result.append("<option value =\"" + sUpmuName + "\" " + sSelected + " > " + sUpmuName + "</option>" );
            }
            
	        return sb_result.toString();
	    } catch(Exception e) {
	        logger.error("SelectBoxData.getUpmuName Error", e);
	        throw new DAOException("SelectBoxData.getUpmuName Error. - " + e.getMessage());
	    } finally {
	        this.disconnect();
	    }	    
	}
    
    /** 
    * 1. 기능 : 어댑터 그룹 이름 반환
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @return   selectbox script 데이타 (String)반환
    **/	  
	public String getAdapterGrpName(String value) throws DAOException {
		StringBuffer sb_query = new StringBuffer();
		StringBuffer sb_result = new StringBuffer();
        
        int     nRsltCnt ;
        String sAdpaterGrpName = "";
        String sSelected = "";
        
        if (value == null) value ="";
		try {
			sb_query.append("SELECT \"AdptrBzwkGroupName\" FROM " + TableInfoEx.T_ADAPTER_GROUP + " WHERE \"AdptrBzwkGroupName\" LIKE '%SNA%' ORDER BY \"AdptrBzwkGroupName\"");
            JDBCFactory factory   = JDBCFactory.newInstance();
            CommonJDBC  common = (CommonJDBC)factory.create(CommonJDBC.class);
            SQLData     data = common.select(sb_query.toString());           
            
	        nRsltCnt 	 = data.getDispDatasCount();
            sAdpaterGrpName = "";
            
            //sb_result.append("<option value =\"ALL\" " + sSelected + " >전체</option>" );
            for (int nIndx = 0 ; nIndx < nRsltCnt; nIndx++)
            {
                sAdpaterGrpName = data.getSQLStringData("AdptrBzwkGroupName" + nIndx);
                if (sAdpaterGrpName.equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sb_result.append("<option value =\"" + sAdpaterGrpName + "\" " + sSelected + " > " + sAdpaterGrpName + "</option>" );
            }
            
	        return sb_result.toString();
	    } catch(Exception e) {
	    	logger.error("SelectBoxData.getAdapterGrpName Error", e);
	        throw new DAOException("SelectBoxData.getAdapterGrpName Error. - " + e.getMessage());
	    } finally {
	        this.disconnect();
	    }	    
	}
    
   /** 
    * 1. 기능 : 수동 백업 여부 전환 selectbox  반환
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @return   selectbox script 데이타 (String)반환
    **/
     public String getBackupAutoCvs(String value) throws WCAException {
		StringBuffer sb_result = new StringBuffer();    
        String sSelected ="";    
        String[][] saBackupAutoCvs = {{WCADefine.WCA_AUTO_BACKUP_CVS_MODE_NO, "수동"},
                              {WCADefine.WCA_AUTO_BACKUP_CVS_MODE_YES, "자동"}
                              };
                            
        if (value == null) value ="";
		try {     
            for (int nIndx = 0 ; nIndx < saBackupAutoCvs.length; nIndx++)
            {
                if (saBackupAutoCvs[nIndx][0].equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sb_result.append("<option value =\"" + saBackupAutoCvs[nIndx][0] + "\" " + sSelected + " > " + saBackupAutoCvs[nIndx][1] + "</option>" );
            }                        
	        return sb_result.toString();
	    } catch(Exception e) {
	        logger.error("SelectBoxData.getBackupAutoCvs Error", e);
			WCAException exception = new WCAException();
            exception.setMessage("SelectBoxData.getBackupAutoCvs Error. - " + e.getMessage());
            throw exception;
        }
	}
   
   /** 
    * 1. 기능 : LU 그룹 이름 selectbox  반환
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @return   selectbox script 데이타 (String)반환
    **/
	public String getLUGrpName(String sNodeName, String sAdpaterGrpName, String value) throws DAOException {
		StringBuffer sbQuery = new StringBuffer();
		StringBuffer sbResult = new StringBuffer();
        
        int     nRsltCnt ;
        String sSelected = "";
        String sLUGrpName = "";
        String sDispLUGrpName = "";
        
//        HashMap result = new HashMap();
        
        if (sNodeName == null) sNodeName = "";
        if (value == null) value ="";
        if (sAdpaterGrpName == null) sAdpaterGrpName ="";
        
		try {
			sbQuery.append("SELECT DISTINCT \"LUGroupName\" FROM " + TableInfoEx.T_LU_GROUP + " WHERE 1=1 " );
            
            if (!sNodeName.equals("") && !sNodeName.equals("ALL")){
                sbQuery.append(" AND \"AdptrGstatSevrName\" = '" + sNodeName + "' ");
            }            
            if (!sAdpaterGrpName.equals("") && !sAdpaterGrpName.equals("ALL")){
                sbQuery.append(" AND \"AdptrGroupName\" = '" + sAdpaterGrpName + "' ");
            }
            sbQuery.append(" ORDER BY \"LUGroupName\" ");
            
            logger.debug(sbQuery.toString());
            
            JDBCFactory factory   = JDBCFactory.newInstance();
            CommonJDBC  common = (CommonJDBC)factory.create(CommonJDBC.class);
            SQLData     data = common.select(sbQuery.toString());                       
	        nRsltCnt 	 = data.getDispDatasCount();
            
            if (nRsltCnt < 0 ) nRsltCnt = 0;
            String[] saRslt = new String[nRsltCnt];   
            int nRealRsltIndx = 0;     
            for (int nIndx = 0 ; nIndx < nRsltCnt; nIndx++)
            {
                sLUGrpName = data.getSQLStringData("LUGroupName" + nIndx);                
                sDispLUGrpName = getDispLUGrpName(sLUGrpName);             
                
                if (CheckDuplicate(saRslt, sDispLUGrpName, nRealRsltIndx))
                    continue;
                saRslt[nRealRsltIndx++] = sDispLUGrpName;
                
                if (sDispLUGrpName.equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sbResult.append("<option value =\"" + sDispLUGrpName + "\" " + sSelected + " > " + sDispLUGrpName + "</option>" );
            }
	        return sbResult.toString(); 
	    } catch(Exception e) {
	    	logger.error("SelectBoxData.getLUGrpName Error. - ", e);
	        throw new DAOException("SelectBoxData.getLUGrpName Error. - " + e.getMessage());
	    } finally {
	        this.disconnect();
        }
	}
  
   /** 
    * 1. 기능 : orgin 배열에서 cnt까지 compare 가 있는지 확인     
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @param   orgin       비교할 배열
    * @param   compare     비교 문자
    * @param   cnt         비교할 배열의 index 
    **/
    private boolean CheckDuplicate(String[] orgin, String compare, int cnt)
    {
        if (compare == null) return false;
        for(int nIndx = 0; nIndx < cnt; nIndx++)
        {
            if (orgin[nIndx].equals(compare))
                return true;
        }
        return false;
    }
    
    /** 
    * 1. 기능 : 표시할  LU 그룹 이름 반환
    * 2. 처리 개요 : LU 그룹 이름은 <어댑터 그룹 이름 + _ + 실제 LU 그룹 이름>
    *               실제 LU 그룹 이름만 반환
    * 3. 주의사항 
    *
    * @param   sName       전체 LU 그룹 이름
    * @return  표시할  LU 그룹 이름
    **/
    public String getDispLUGrpName(String sName)
    {
        int iIndx = 0;
        if(sName == null) return "";
        if ((iIndx=sName.lastIndexOf(LU_GRP_GUBUNJA)) == -1)
            return sName;
        
        return sName.substring(iIndx + 1);
    }
    
   /** 
    * 1. 기능 : 전체 LU 그룹 이름 반환
    * 2. 처리 개요 : LU 그룹 이름은 <어댑터 그룹 이름 + _ + 실제 LU 그룹 이름>
    * 3. 주의사항 
    *
    * @param   sAdapterGrpName       어댑터 그룹 이름
    * @param   sDispLUGrpName        실제 LU 그룹 이름
    * @return  전체 LU 그룹 이름
    **/
    public String getRealLUGrpName(String sAdapterGrpName, String sDispLUGrpName)
    {
        return sAdapterGrpName + LU_GRP_GUBUNJA + sDispLUGrpName;
    }
    
    /** 
    * 1. 기능 : 로그 기록 여부 selectbox  반환
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @return   selectbox script 데이타 (String)반환
    **/
    public String getLogYN(String value) throws WCAException {
		StringBuffer sb_result = new StringBuffer();    
        String sSelected ="";    
        String[][] saLogYN = {{WCADefine.WCA_LOG_YES, "예"},
                              {WCADefine.WCA_LOG_NO, "아니오"}
                              };
                            
        if (value == null) value ="";
		try {     
            for (int nIndx = 0 ; nIndx < saLogYN.length; nIndx++)
            {
                if (saLogYN[nIndx][0].equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sb_result.append("<option value =\"" + saLogYN[nIndx][0] + "\" " + sSelected + " > " + saLogYN[nIndx][1] + "</option>" );
            }                        
	        return sb_result.toString();
	    } catch(Exception e) {
	    	logger.error("SelectBoxData.getLogYN Error", e);
			WCAException exception = new WCAException();
            exception.setMessage("SelectBoxData.getLogYN Error. - " + e.getMessage());
            throw exception;
	    }	    
	}    
    
    /** 
    * 1. 기능 : 로그 레벨  selectbox  반환
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @return   selectbox script 데이타 (String)반환
    **/
    public String getLogLevel(String value) throws WCAException {
		StringBuffer sb_result = new StringBuffer();    
        String sSelected ="";    
        String[][] saLogLevel = {{"DEBUG", "DEBUG"},
                                {"INFO", "INFO"},
                                {"WARN", "WARN"}
                              };
                            
        if (value == null) value ="";
		try {     
            for (int nIndx = 0 ; nIndx < saLogLevel.length; nIndx++)
            {
                if (saLogLevel[nIndx][0].equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sb_result.append("<option value =\"" + saLogLevel[nIndx][0] + "\" " + sSelected + " > " + saLogLevel[nIndx][1] + "</option>" );
            }
                        
	        return sb_result.toString();
	    } catch(Exception e) {
	    	logger.error("SelectBoxData.getLogLevel Error", e);
			WCAException exception = new WCAException();
            exception.setMessage("SelectBoxData.getLogLevel Error. - " + e.getMessage());
            throw exception;
	    }	    
	}
    
   
    
    /**
     *  LU  TranMOde select box
     */
    public String getLUGTranMode(String value) throws WCAException {
		StringBuffer sb_result = new StringBuffer();    
        String sSelected ="";    
        String[][] saTranMode = WCADefine.LU_TRAN_MODE;
                            
        if (value == null) value ="";
		try {     
            for (int nIndx = 0 ; nIndx < saTranMode.length; nIndx++)
            {
                if (saTranMode[nIndx][0].equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sb_result.append("<option value =\"" + saTranMode[nIndx][0] + "\" " + sSelected + " > " + saTranMode[nIndx][1] + "</option>" );
            }
                        
	        return sb_result.toString();
	    } catch(Exception e) {
	        logger.error("SelectBoxData.getLUGTranMode Error", e);
			WCAException exception = new WCAException();
            exception.setMessage("SelectBoxData.getLUGTranMode Error. - " + e.getMessage());
            throw exception;
        }
	}
    
    /**
     *  Bdackup LU 여부
     */
    public String getBackupLUYN(String value) throws WCAException {
		StringBuffer sb_result = new StringBuffer();    
        String sSelected ="";    
        String[][] saBackupLUYN = WCADefine.BACKUP_LU_YN;
                            
        if (value == null) value ="";
		try {     
            for (int nIndx = 0 ; nIndx < saBackupLUYN.length; nIndx++)
            {
                if (saBackupLUYN[nIndx][0].equals(value))
                    sSelected = "selected";
                else sSelected ="";
                sb_result.append("<option value =\"" + saBackupLUYN[nIndx][0] + "\" " + sSelected + " > " + saBackupLUYN[nIndx][1] + "</option>" );
            }
                        
	        return sb_result.toString();
	    } catch(Exception e) {
	        logger.error("SelectBoxData.getBackupLUYN Error.", e);
			WCAException exception = new WCAException();
            exception.setMessage("SelectBoxData.getBackupLUYN Error. - " + e.getMessage());
            throw exception;
        }
	}
    
    /* 20080526 Start */
    /**
     *  Bdackup LU 여부
     */
    public String getRemoteLUYN(String value) throws WCAException {
		StringBuffer sb_result = new StringBuffer();    
        String sSelected = "";    
        String[][] saRemoteLUYN = WCADefine.REMOTE_LU_YN;
                            
        if(value == null) value = "";
		try {     
            for (int nIndx = 0 ; nIndx < saRemoteLUYN.length; nIndx++)
            {
                if(saRemoteLUYN[nIndx][0].equals(value))
                    sSelected = "selected";
                else 
                	sSelected = "";
                sb_result.append("<option value =\"" + saRemoteLUYN[nIndx][0] + "\" " + sSelected + " > " + saRemoteLUYN[nIndx][1] + "</option>" );
            }
                        
	        return sb_result.toString();
	    } catch(Exception e) {
	        WCAException exception = new WCAException();
            exception.setMessage("SelectBoxData.getBackupLUYN Error. - " + e.getMessage());
            throw exception;
        }
	}
    /* 20080526 End */
}