package com.eactive.eai.rms.client.dao;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.eactive.eai.common.dao.Keys;
import com.eactive.eai.rms.client.context.RmsProperties;

/**
 * <pre>
 * 1. 기능 : 모니터링 관리 화면의 프로퍼티 관리 화면에서 입력한 내용들을 로딩 합니다. 
 * 2. 처리 개요 : RMS_PROPS 프로퍼티 그룹과 RMS_PROPS{INSTANCE_NAME} 형태의 프로퍼티 그룹의 모든 프로퍼티를 로딩 합니다. 
 *     -
 * 3. 주의사항 : 
 * </pre>
 * @author  : 
 * @version : $Revision: 1.10 $
 * @since : rts 1.0
 */
public class AgentAdapterDAO extends JdbcDaoSupport {

    /** F/W logger. */
    private static com.eactive.eai.common.util.Logger logger = com.eactive.eai.common.util.Logger.getLogger(com.eactive.eai.common.util.Logger.LOGGER_CLIENT);


    private String tableOwner = null;
    private boolean standalone = false;
    private String instanceName = null;

    public AgentAdapterDAO() {
        tableOwner = System.getProperty("eai.tableowner");
        standalone = Boolean.getBoolean("rms.standalone");

        if (standalone) {
            try {
                instanceName = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                //e.printStackTrace();
            }
        } else {
            instanceName = System
                    .getProperty(RmsProperties.EAI_SERVER_INSTANCE_NAME);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("CURRENT RMS INSTANCE NAME : " + instanceName);
        }
    }
    
    public HashMap getMonitorUseNoAdapterList() throws Exception {
		HashMap<String,String> monitorDisableMap = new HashMap<String,String>();
		
		StringBuffer query = new StringBuffer();
	
		query.append(" select AdptrBzwkGroupName, MoniUseYn \n");
		query.append(" from "+ tableOwner + ".TSEAIAD01 \n");
		query.append(" where MoniUseYn = 'N'  \n");
	
		try	{			
            List adapterList = this.getJdbcTemplate().queryForList(query.toString());

            if (adapterList != null) {
                for (Iterator it = adapterList.iterator(); it.hasNext();) {
                    Map tmp = (Map) it.next();
                    String name = (String) tmp.get("AdptrBzwkGroupName");
                    String val = (String) tmp.get("MoniUseYn");
                    monitorDisableMap.put(name,val);
                }
            }
			return monitorDisableMap;
		}
		catch(Exception e) {
			logger.error(e.toString(),e);
			//e.printStackTrace();	
			throw e;
		}
	}
    public HashMap getMonitorTimeConditionAdapterList() throws Exception {
		HashMap<String,String> monitorDisableMap = new HashMap<String,String>();
		
		StringBuffer query = new StringBuffer();
	
		query.append(" select AdptrBzwkGroupName, MoniCndnCtnt \n");
		query.append(" from "+ tableOwner + ".TSEAIAD01 \n");
		query.append(" where MoniUseYn = 'Y' and MoniCndnCtnt != '' and MoniCndnCtnt != 'N,EVERY,0000,2359'\n");
	
		try	{			
            List adapterList = this.getJdbcTemplate().queryForList(query.toString());

            if (adapterList != null) {
                for (Iterator it = adapterList.iterator(); it.hasNext();) {
                    Map tmp = (Map) it.next();
                    String name = (String) tmp.get("AdptrBzwkGroupName");
                    String val = (String) tmp.get("MoniUseYn");
                    monitorDisableMap.put(name,val);
                }
            }
			return monitorDisableMap;
		}
		catch(Exception e) {
			logger.error(e.toString(),e);
			//e.printStackTrace();	
			throw e;
		}
	}


}
