package com.eactive.eai.common.session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.common.property.PropertyLoginKeys;
import com.eactive.eai.common.sessioninfo.SessionHistoryDAO;
import com.eactive.eai.common.sessioninfo.SessionHistoryDTO;
import com.eactive.eai.common.util.ApplicationContextProvider;
import com.eactive.eai.common.util.Logger;

@Component("onlSessionHistoryLogger")
public class SessionHistoryLogger {

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

	@Autowired
	private SessionHistoryDAO sessionHistoryDAO;

//	private static SessionHistoryLogger instance = null;

	private SessionHistoryLogger() {
	}

	public static SessionHistoryLogger getInstance() {
		return ApplicationContextProvider.getContext().getBean(SessionHistoryLogger.class);
	}

	public void log(SessionHistoryDTO sessionHistoryDTO) {
		try {
			if (!"Y".equals(PropManager.getInstance().getProperty(PropertyLoginKeys.LOGIN,
					PropertyLoginKeys.LOGIN_LOGGING_YN, "Y"))) {
				logger.debug("LOGIN_LOGGING_YN option is \"N\". SessionHistory not logged.");
				return;
			}
			sessionHistoryDAO.insertSessionHistory(sessionHistoryDTO.toVo());
		} catch (DAOException de) {
			logger.error("SessionHistoryLogger] Fail logging session history. sessionHistoryDTO : "
					+ sessionHistoryDTO.toString(), de);
		}
	}
}
