package com.eactive.eai.adapter;

import java.io.Serializable;

import com.eactive.eai.adapter.listener.AdapterException;
import com.eactive.eai.adapter.listener.AdapterListener;
import com.eactive.eai.adapter.socket.config.ConfigurationContext;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.lifecycle.Lifecycle;
import com.eactive.eai.common.lifecycle.LifecycleException;
import com.eactive.eai.common.lifecycle.LifecycleListener;
import com.eactive.eai.common.lifecycle.LifecycleSupport;
import com.eactive.eai.common.util.Logger;

/**
* 1. 기능 : 어댑터 정보를 표현하는 Java Value Object 클래스.
* 2. 처리 개요 : 어댑터 정보를 정의한다.
* 3. 주의사항
*
* @author  :
* @version : v 1.0.0
* @see : Serializable, Lifecycle
* @since   :
*/
public class AdapterVO implements Serializable, Lifecycle
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * Default Logger
     */
    static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);

    private AdapterGroupVO adapterGroupVO;  //어댑터 그룹

    private String adapterGroupName;       //어뎁터업무그룹명[TSEAIAD02.AdptrBzwkGroupName]
    private String name;                   //어뎁터업무명[TSEAIAD02.AdptrBzwkName]
//    private String eaiSevrInstncName;      //서버인스탄스명[TSEAIAD02.EAISevrInstncName] - migration added.
    private String listenerClass;          //리스너클래스명[TSEAIAD02.LstnerClsName]
//    private String testCallClass;          //테스터클래스명[TSEAIAD02.TestClsName]
    private String propGroupName;          //프라퍼티그룹명[TSEAIAD02.PrptyGroupName]
    private String description;            //어댑터설명[TSEAIAD02.AdptrDesc]
    private String propGroupDesc;          //프라퍼티그룹설명

    private String batchSystemConnCode;    //BATCH작업업무구분코드[TSEAIBJ05.SysLnkgDstCd] [JUNGFUX] 추가  2006/02/20
    private String batchProcessCode;       //BATCH작업업무구분코드[TSEAIBJ05.BjobBzwkDstcd] [JUNGFUX] 추가  2006/02/20
    private String batchProcessName;       //BATCH작업업무구분코드[TSEAIBJ05.BjobBzwkName] [JUNGFUX] 추가  2006/11/17
    private String batchInstitutionCode;   //BATCH작업업무구분코드[TSEAIBJ05.OsidInstiDstCd] [JUNGFUX] 추가  2006/02/20
    private String batchServerIP;          //BATCH작업업무구분코드[TSEAIBJ05.LnkgIPInfoName] [JUNGFUX] 추가  2006/02/20
    private String batchServerPortNo;      //BATCH작업업무구분코드[TSEAIBJ05.LnkgPortInfoName] [JUNGFUX] 추가  2006/02/20
    private int    batchInterMsgTimeout;   //BATCH소켓서버전문간Timeout[TSEAIBJ05.ToutVal] [JUNGFUX] 추가 2006/02/20
    private String batchRcvFlowRuleCode;   //BATCH수신처리규칙코드[TSEAIBJ02.SysLnkgPortNo] [JUNGFUX] 추가  2006/02/20
    //20060328 add by khs
    private String batchInstitutionName;   // 대외기관 이름
    //20060329 add by khs
    private String lnkID;
    private String lnkPwd;
    //20060331 add by khs
//    private String batchInstitutionType;    // 퇴직연금 전문조립에 사용되는 대외기관 구분 : 1, 2,3,4,5 등 가능
    //20060411 add by khs
    private int    batchPacketSize;
    //20060425 add by khs
    private String rqstRspnsDstcd;      // 응답수신('A'), 응답송신('C'), 요구('R') 구분

    private ConfigurationContext context;

    /**
     * 어댑터 상태
     */
    private boolean status;

    /**
     * AdapterListener instance
     */
    transient private AdapterListener adapterListener;
    //private AdapterListener adapterListener;


    /**
     * 기동여부
     */
    private boolean started;
    /**
     * LifecycleSupport instance
     */
    private LifecycleSupport lifecycle = new LifecycleSupport(this);

    /**
    * 1. 기능 :  Defualt Constructor
    * 2. 처리 개요 :
    *     -
    * 3. 주의사항
    *
    **/
    public AdapterVO() {
    }

    /**
    * 1. 기능 :  어댑터를 초기화하는 생성자 함수
    * 2. 처리 개요 : 어댑터를 초기화한다.
    * 3. 주의사항
    *
    * @param name   어뎁터업무 이름
    **/
    public AdapterVO(String name) {
        this.name = name;
    }

    /**
    * 1. 기능 :  초기화 여부를 반환하는 getter 메서드
    * 2. 처리 개요 : 초기화 여부를 반환한다.
    * 3. 주의사항
    *
    * @return   초기화 여부
    **/
    public boolean isStarted() {
        return this.started;
    }

    public void setAdapterGroupName(String adapterGroupName) {
        this.adapterGroupName = adapterGroupName;
    }

    public String getAdapterGroupName() {
        return this.adapterGroupName;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setListenerClass(String listenerClass) {
        this.listenerClass = listenerClass;
    }

    public String getListenerClass() {
        return this.listenerClass;
    }

//    public void setTestCallClass(String testCallClass) {
//        this.testCallClass = testCallClass;
//    }
//
//    public String getTestCallClass() {
//        return this.testCallClass;
//    }

    public void setPropGroupName(String propGroupName) {
        this.propGroupName = propGroupName;
    }

    public String getPropGroupName() {
        return this.propGroupName;
    }

    public void setAdapterGroupVO(AdapterGroupVO group) {
        this.adapterGroupVO = group;
    }

    public AdapterGroupVO getAdapterGroupVO() {
        return this.adapterGroupVO;
    }

    /**
    * 1. 기능 :  TestCallManager에 어댑터 정보를 설정하는 메서드
    * 2. 처리 개요 : 어댑터 상태 설정
    * 3. 주의사항
    *
    * @param    status  어댑터 상태
    *
    **/
    public synchronized void setStatus(boolean status) {
        this.status = status;
        
//        if (this.status==false && this.testCallClass!=null && !this.testCallClass.equals("")) {
//            TestCallManager tmanager = TestCallManager.getInstance();
//            try {
//                tmanager.addTester(this);
//                logger.info(ExceptionUtil.make("BICEAIMAD034", new String[] {this.toString()}));
//                
//            } catch(Exception e) {
//                throw new RuntimeException(ExceptionUtil.getErrorCode(e, "BICEAIMAD035"));
//            }
//        }

        if(this.status) {
            this.adapterGroupVO.active(this);
        } else {
            this.adapterGroupVO.disable(this);
        }
    }

    public boolean isStatus() {
        return this.status;
    }

    /**
    * 1. 기능 :  Lifecycle의 start 메서드로 AdapterVO 초기화하는 메서드
    * 2. 처리 개요 : AdapterVO를 초기화한다.
    * 3. 주의사항
    *
    * @exception    LifecycleException  이미 시작되었을 경우
    *
    **/
     public void start() throws LifecycleException {
        if (started)
            throw new LifecycleException("BECEAIMAD036");

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(STARTING_EVENT, this);

        if(listenerClass!=null && !"".equals(listenerClass)) {
            try {
                adapterListener = (AdapterListener)Class.forName(listenerClass).newInstance();
                adapterListener.setAdapterInfo(this);
                adapterListener.start();
            } catch(Exception e) {
                throw new LifecycleException(ExceptionUtil.getErrorCode(e, "BECEAIMAD037"));
            }
        }

        started = true;
        status = true;
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(STARTED_EVENT, this);

    }

    /**
    * 1. 기능 :  Lifecycle의 stop 메서드로 어댑터 정보를 clear 하는 메서드
    * 2. 처리 개요 : 멤버에 캐싱한 어댑터 정보를 clear한다.
    * 3. 주의사항
    *
    * @exception    LifecycleException  이미 종료된 경우 발생
    **/
    public void stop() throws LifecycleException {
        // Validate and update our current component state
        if (!started)
            throw new LifecycleException("BECEAIMAD038");

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(STOPING_EVENT, this);

        try {
            if(adapterListener!=null) adapterListener.stop();
        } catch(AdapterException e) {
            throw new LifecycleException(ExceptionUtil.getErrorCode(e, "BECEAIMAD039"));
        }

        started = false;
        status = false;
        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(STOPPED_EVENT, this);
    }

    /**
    * 1. 기능 :  LifecycleListener를 등록하는 메서드
    * 2. 처리 개요 : LifecycleListener를 등록한다.
    * 3. 주의사항
    *
    * @param    listener    LifecycleEvent를 수신한 LifecycleListener
    **/
    public void addLifecycleListener(LifecycleListener listener)
    {
        lifecycle.addLifecycleListener(listener);
    }

    /**
    * 1. 기능 :  등록된 LifecycleListener 리스트를 반환하는 메서드
    * 2. 처리 개요 : 등록된 LifecycleListener 리스트를 반환하다.
    * 3. 주의사항
    *
    * @return   등록된 LifecycleListener 리스트
    **/
    public LifecycleListener[] findLifecycleListeners()
    {
        return lifecycle.findLifecycleListeners();
    }

    /**
    * 1. 기능 :  등록된 LifecycleListener를 삭제하는 메서드
    * 2. 처리 개요 : 파라미터의 LifecycleListener를 삭제한다.
    * 3. 주의사항
    *
    * @param    listener    삭제할 LifecycleListener
    **/
    public void removeLifecycleListener(LifecycleListener listener)
    {
        lifecycle.removeLifecycleListener(listener);
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return this.description;
    }

//    public void setEaiSevrInstncName(String eaiSevrInstncName) {
//        this.eaiSevrInstncName = eaiSevrInstncName;
//    }
//
//    public String getEaiSevrInstncName() {
//        return this.eaiSevrInstncName;
//    }

    public void setPropGroupDesc(String propGroupDesc) {
        this.propGroupDesc = propGroupDesc;
    }

    public String getPropGroupDesc() {
        return this.propGroupDesc;
    }

    public void setContext(ConfigurationContext context) {
        this.context = context;
    }

	public ConfigurationContext getContext() {
		return this.context;
	}

    /**
    * 1. 기능 :  어댑터 정보를 String 형태로 반환하는 메서드
    * 2. 처리 개요 : String 형태로 어댑터 정보를 반환한다.
    * 3. 주의사항
    *
    * @return    String    어댑터 정보
    **/
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("\n     AdapterVO [ name=").append(name);
        sb.append(", adapterGroupName=").append(adapterGroupName);
//        sb.append(", eaiSevrInstncName=").append(eaiSevrInstncName);
        sb.append(", listenerClass=").append(listenerClass);
//        sb.append(", testCallClass=").append(testCallClass);
        sb.append(", propGroupName=").append(propGroupName);
        sb.append(", description=").append(description);
        sb.append(", status=").append(status);
        sb.append(", context=").append(context);
        sb.append(", batchSystemConnCode=").append(batchSystemConnCode);
        sb.append(", batchProcessCode=").append(batchProcessCode);
        sb.append(", batchProcessName=").append(batchProcessName);
        sb.append(", batchInstitutionCode=").append(batchInstitutionCode);
        sb.append(", batchServerIP=").append(batchServerIP);
        sb.append(", batchServerPortNo=").append(batchServerPortNo);
        sb.append(", batchInterMsgTimeout=").append(batchInterMsgTimeout);
        sb.append(", batchRcvFlowRuleCode=").append(batchRcvFlowRuleCode);
        sb.append(", batchInstitutionCode=").append(this.batchInstitutionName);
//        sb.append(", batchInstitutionType=").append(this.batchInstitutionType);
        sb.append(" ]");

        return sb.toString();
    }

    public void setBatchSystemConnCode(String batchSystemConnCode) {
        this.batchSystemConnCode = batchSystemConnCode;
    }

    public String getBatchSystemConnCode() {
        return this.batchSystemConnCode;
    }

    public void setBatchProcessCode(String batchProcessCode) {
        this.batchProcessCode = batchProcessCode;
    }

    public String getBatchProcessCode() {
        return this.batchProcessCode;
    }

    public void setBatchProcessName(String batchProcessName) {
        this.batchProcessName = batchProcessName;
    }

    public String getBatchProcessName() {
        return this.batchProcessName;
    }

    public void setBatchInstitutionCode(String batchInstitutionCode) {
        this.batchInstitutionCode = batchInstitutionCode;
    }

    public String getBatchInstitutionCode() {
        return this.batchInstitutionCode;
    }

    public void setBatchInstitutionName(String batchInstitutionName) {
        this.batchInstitutionName = batchInstitutionName;
    }

    public String getBatchInstitutionName() {
        return this.batchInstitutionName;
    }
//    public void setBatchInsitutionType(String batchInstitutionType) {
//        this.batchInstitutionType = batchInstitutionType;
//    }
//    public String getBatchInstitutionType() {
//        return this.batchInstitutionType;
//    }


    public void setBatchServerIP(String batchServerIP) {
        this.batchServerIP = batchServerIP;
    }

    public String getBatchServerIP() {
        return this.batchServerIP;
    }

    public void setBatchServerPortNo(String batchServerPortNo) {
        this.batchServerPortNo = batchServerPortNo;
    }

    public String getBatchServerPortNo() {
        return this.batchServerPortNo;
    }

    public void setBatchInterMsgTimeout(int batchInterMsgTimeout) {
        this.batchInterMsgTimeout = batchInterMsgTimeout;
    }

    public int getBatchInterMsgTimeout() {
        return this.batchInterMsgTimeout;
    }

    public void setBatchRcvFlowRuleCode(String batchRcvFlowRuleCode) {
        this.batchRcvFlowRuleCode = batchRcvFlowRuleCode;
    }

    public String getBatchRcvFlowRuleCode() {
        return this.batchRcvFlowRuleCode;
    }

    public void setLnkID(String lnkID) {
        this.lnkID = lnkID;
    }

    public String getLnkID() {
        return this.lnkID;
    }

    public void setLnkPwd(String lnkPwd) {
        this.lnkPwd = lnkPwd;
    }

    public String getLnkPwd() {
        return this.lnkPwd;
    }

    public int getPacketSize() {
        return this.batchPacketSize;
    }

    public void setPacketSize(int packetSize) {
        this.batchPacketSize = packetSize;
    }

    //20060425 add by khs for 응답송신
    public String getRqstRspnsDstcd() {
        return this.rqstRspnsDstcd;
    }
    //20060425 add by khs for 응답송신
    public void setRqstRspnsDstcd(String rqstRspnsDstcd) {
        this.rqstRspnsDstcd = rqstRspnsDstcd;
    }
}
