package com.eactive.eai.common.util;

import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.dao.DAOFactory;
import com.eactive.eai.common.errorcode.ErrorCodeManager;
import com.eactive.eai.inbound.error.InboundErrorInfoDAO;
import com.eactive.eai.inbound.error.InboundErrorInfoVO;

public class InboundErrorLogger {

	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	public static void error(String uuid, String adapterGroupName, String adapterName, String errCode, String errMsg,
			long errTm, String svcInstNm, String errDstCd, Object message) {
		String msg = null;

		if (errDstCd == null || errDstCd.length() == 0) {
			errDstCd = "ND";
		}

		InboundErrorInfoVO errorInfoVO = new InboundErrorInfoVO();
		errorInfoVO.setEaiSvcSno(uuid); // EAI서비스일련번호
		errorInfoVO.setAdptBwkGrpNm(adapterGroupName); // 어댑터업무그룹명
		errorInfoVO.setErrCd(errCode); // 에러코드
		errorInfoVO.setErrTxt(errMsg); // 에러내용
		errorInfoVO.setErrTm(DatetimeUtil.getCurrentTime(errTm)); // 에러발생시각
		errorInfoVO.setErrDstcd(errDstCd);

		if (message == null) {
			msg = "null";
		} else if (message instanceof byte[]) {
			msg = new String((byte[]) message);
		} else if (message instanceof String) {
			msg = (String) message;
		} else {
			msg = "invaild message [" + message.getClass().getName() + "] " + message.toString();
		}

		errorInfoVO.setBwkDataTxt(msg); // 업무데이터내용
		errorInfoVO.setEaiSvrInstNm(svcInstNm); // EAI서버인스턴스명

		error(errorInfoVO);
	}

	public static void error(InboundErrorInfoVO errorInfoVO) {
		InboundErrorInfoDAO dao = null;
		try {
			dao = ApplicationContextProvider.getContext().getBean(InboundErrorInfoDAO.class);
			dao.addInboundErrorInfo(errorInfoVO);
		} catch (DAOException e) {
			logger.error("InboundErrorLogger] Inbound Unknown Message Logging Error", e);
		}
	}
}