package com.eactive.eai.rms.onl.common.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.onl.dashboard.service.Dashboard2RepositoryService;
import com.eactive.eai.rms.onl.server.repository.Repository;
import com.eactive.eai.rms.onl.server.repository.TransactionRepositoryService;
import com.eactive.eai.rms.onl.vo.TransactionSendVo;

@Service
@Qualifier("memoryTrInitService")
public class MemoryTrInitService {

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MemoryTrInitService.class);

	@Autowired
	@Qualifier("repository")
	private Repository repository;

	@Autowired
	@Qualifier("dashboard2RepositoryService")
	private Dashboard2RepositoryService repositoryService;

	@Autowired
	@Qualifier("transactionRepositoryService")
	TransactionRepositoryService transactionRepositoryService;

	public void clearTransactionMemoryMap() {
		logger.info("START RESET TRANSACTION Memory map ..... " + CommonUtil.getToday(""));

		// 메모리 TR 정보를 리셋한다.
		repository.cleanupNoExpire();
		repository.checkNoExpire();
		repositoryService.initMemTrRepository();
		repositoryService.checkMemTrRepository();
		transactionRepositoryService.clearRepository();
		transactionRepositoryService.checkRepository();

		logger.info("END RESET TRANSACTION Memory map !" + CommonUtil.getToday(""));
	}

	public void clearTransactionMemoryMap(String host, String inst) {
		try {
			TransactionSendVo vo = new TransactionSendVo();
			repository.removeMonitorData(host, inst, vo.getCategory());
			repositoryService.removeMemTrRepository(host, inst);
			transactionRepositoryService.clearRepository(host, inst);
		} catch (Exception e) {
			logger.error("clearTransactionMemoryMap Error", e);
		}
	}
}
