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

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

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

import com.eactive.eai.rms.client.context.RmsProperties;
import com.eactive.eai.rms.client.util.QueryBuffer;

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

    /** 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 RmsPropertiesDAOImpl() {
        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);
        }
    }

    @SuppressWarnings("unchecked")
    public Properties getProperties() {
        final Properties properties = new Properties();
        QueryBuffer query = new QueryBuffer();

        try {
            query.append(" SELECT Prpty2Val                           ");
            query.append("      , PrptyName                           ");
            query.append("   FROM " + tableOwner + ".TSEAICM03        ");
            query.append("  WHERE PrptyGroupName='RMS_PROPS'          ");

            List commonPropertiesList = this.getJdbcTemplate().queryForList(
                    query.toString());

            if (commonPropertiesList != null) {
                for (Iterator it = commonPropertiesList.iterator(); it
                        .hasNext();) {
                    Map tmp = (Map) it.next();
                    String name = (String) tmp.get("PrptyName");
                    String val = (String) tmp.get("Prpty2Val");
                    properties.setProperty(name, val);
                }
            }

            query.delete(0, query.length());

            query.append(" SELECT PrptyName                                      ");
            query.append("      , Prpty2Val                                      ");
            query.append("   FROM " + tableOwner + ".TSEAICM03                   ");
            query.append("  WHERE PrptyGroupName='RMS_PROPS{"+ instanceName + "}'");

            List propertiesList = this.getJdbcTemplate().queryForList(
                    query.toString());

            if (propertiesList != null) {
                for (Iterator it = propertiesList.iterator(); it.hasNext();) {
                    Map tmp = (Map) it.next();
                    String name = (String) tmp.get("PrptyName");
                    String val = (String) tmp.get("Prpty2Val");
                    properties.setProperty(name, val);
                }
            }
        } catch (DataAccessException e) {
            logger.error(e.toString());
        }
        return properties;
    }

    public String getLocalMBeanServerPort() {
        QueryBuffer buffer = new QueryBuffer();
        buffer.append(" SELECT SevrLsnPortName                             ");
        buffer.append("   FROM " + tableOwner + ".TSEAISY02                ");
        buffer.append("  WHERE EAISevrInstncName    = ?                    ");

        return (String) this.getJdbcTemplate().queryForObject(
                buffer.toString(), new Object[] { instanceName }, String.class);
    }
}
