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

import com.eactive.eai.adapter.wca.exception.WCAException;
import com.eactive.eai.adapter.wca.jdbc.BaseJDBC;
import com.eactive.eai.adapter.wca.jdbc.JDBCException;
import com.eactive.eai.adapter.wca.util.DateUtil;
import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.util.Logger;

import java.sql.SQLException;

public class InsertErrorLog extends BaseJDBC implements ErrorLogQuery
{ 
    Logger trc_logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
   /** 
    * 1. 기능 :  에러 로그 추가
    * 2. 처리 개요 : 
    * 3. 주의사항 
    *
    * @param        data    SessionGroupData Object
    * @return       추가된 레코드 수 
    * @exception    JDBCException   DB 작업시 발행하는 예외 발생 정보 (SQLException 에러코드와 메세지 포함)
    **/
    public int insertErrorLog(String adapterGroupName, String luGroupName, String luName,
                              String dataccd, String svcsno, String errcd) throws JDBCException {
        int nExeCnt = 0;
        StringBuffer sbLog = new StringBuffer();
        
        if (luGroupName == null || luGroupName.equals("")) { luGroupName = " "; }
        if (luName == null || luName.equals("")) { luName = " "; }
        String sFrtimMsgDpstHMS = " ";
		String sLogPrcssSerno = "100";
        if (errcd.equals(WCAException.E_SOFTLINK_RECV_SESSION_CLOSE)) { sLogPrcssSerno = "400"; }
        
        try {
            DateUtil dateUtil = new DateUtil();
            String sDate = dateUtil.getDateMilliTime();
            getDsConnection();         
            setStatement(INSERT_ERROR_LOG);
            //MsgDpstHMS, AdptrGstatSevrName, AdptrGroupName, LUGroupName, LUName,
            //LogPrcssSerno, EAISvcSerno, DataDstcd, EAIErrCtnt, FrtimMsgDpstHMS
            this.preparedStatement.setString(1, sDate);
            this.preparedStatement.setString(2, EAIServerManager.getInstance().getLocalServerHostName());
            this.preparedStatement.setString(3, adapterGroupName);
            this.preparedStatement.setString(4, luGroupName);
            this.preparedStatement.setString(5, luName);
            this.preparedStatement.setString(6, sLogPrcssSerno);
            this.preparedStatement.setString(7, svcsno);
            this.preparedStatement.setString(8, dataccd);
            this.preparedStatement.setString(9, errcd);
            this.preparedStatement.setString(10, sFrtimMsgDpstHMS);
            
            sbLog.append(sDate + ",")
                 .append(EAIServerManager.getInstance().getLocalServerHostName() + ",")
                 .append(adapterGroupName + ",")
                 .append(luGroupName + ",")
                 .append(luName + ",")
                 .append(sLogPrcssSerno + ",")
                 .append(svcsno + ",")
                 .append(dataccd + ",")
                 .append(errcd + ",")                                  
                 .append(sFrtimMsgDpstHMS);
            if (trc_logger.isDebug()) { trc_logger.debug("Error Log = " + sbLog.toString()); }
            
            nExeCnt =executeUpdate();
            return nExeCnt;
        } 
        catch (SQLException e) {
            throw new JDBCException(e, svcsno);
        }
        catch (java.net.UnknownHostException host) {
            throw new JDBCException("InsertErrorLog", "insertErrorLog", host);
        } 
        finally {
            this.close();   
        }
    }
} 
