package com.eactive.eai.common.stdmessage;

import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.session.SessionManager;
import com.eactive.eai.common.util.ApplicationContextProvider;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.env.ElinkConfig;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.parser.ReaderType;

public class STDMessageMapper {
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	public STDMessageMapper() {
		super();
	}

	public static void storeMessageHeader(String traceKey, StandardMessage standardMessage, String extBizCd, String type)
			throws Exception {
		try {
			if(ElinkConfig.isUseCacheStore()) {
				String key = extBizCd + traceKey;
				String flatMessage = null;
			    if (ReaderType.ASC.equals(type)) {
			    	flatMessage = standardMessage.toFixedString(false);
			    }else if (ReaderType.JSON.equals(type)) {
					flatMessage = standardMessage.toJson(false,false);
				}else if (ReaderType.XML.equals(type)) {
					flatMessage = standardMessage.toXML();
				}
				SessionManager.getInstance().putCacheStore(key, flatMessage);
				if (logger.isWarn()) {
					logger.warn("STDMessageMapper] putCacheStore Key[" + key + "] value[" + flatMessage + "]");
				}
			}
			else {
				STDMessageDAO dao = ApplicationContextProvider.getContext().getBean(STDMessageDAO.class);
				String applCd = extBizCd;
				String flatMessage = null;
			    if (ReaderType.ASC.equals(type)) {
			    	flatMessage = standardMessage.toFixedString(false);
			    }else if (ReaderType.JSON.equals(type)) {
					flatMessage = standardMessage.toJson(false,false);
				}else if (ReaderType.XML.equals(type)) {
					flatMessage = standardMessage.toXML();
				}
	
				boolean duplicate = false;
				try {
					dao.addRestoreMessage(applCd, traceKey, flatMessage);
				} catch (Exception ex) {
					duplicate = true;
					if (logger.isWarn()) {
						logger.warn("STDMessageMapper] Duplicate Key[" + traceKey + "] update STDMessage");
					}
				}
				if (duplicate) {
					dao.updateRestoreMessage(applCd, traceKey, flatMessage);
				}
			}			
		} catch (DAOException e) {
			throw new Exception("STDMessageMapper.storeMessageHeader Error - " + e.getMessage());
		}
	}

	public static String getStoredMessageFixedHeader(String traceKey, String applCd) throws Exception {
		try {
			String message = null;
			if(ElinkConfig.isUseCacheStore()) {
				String key = applCd + traceKey;
				message = SessionManager.getInstance().getCacheStore(key);
				// String 객체를 생성해서 전달되므로 삭제해도 가능함.
				SessionManager.getInstance().removeCacheStore(key);
				if (logger.isWarn()) {
					logger.warn("STDMessageMapper] get & remove CacheStore Key[" + key + "]");
				}
			}
			else {
				STDMessageDAO dao = ApplicationContextProvider.getContext().getBean(STDMessageDAO.class);
				message = dao.getRestoreMessage(applCd, traceKey);
			}
			
			if (message == null) {
				throw new Exception("복원헤더 조회 중 에러(NotFound), Keys (applCd=" + applCd + ",traceKey=" + traceKey + ")");
			}
			return message;
		} catch (DAOException e) {
			throw new Exception("STDMessageMapper.getStoredMessageFixedHeader Error - " + e.getMessage());
		}
	}
}
