package com.eactive.eai.flowcontrol.jms;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.flowcontrol.jms.QueueMonitor;
import com.eactive.eai.flowcontrol.jms.loader.QueueMonitorLoader;
import com.eactive.eai.flowcontrol.jms.mapper.QueueMonitorMapper;

@Service
@Transactional
public class QueueMonitorDAO extends BaseDAO {

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

	@Autowired
	QueueMonitorLoader queueMonitorLoader;

	@Autowired
	QueueMonitorMapper queueMonitorMapper;

	public Map<String, QueueMonitorVO> getAllQueue() throws DAOException {
		boolean isScalable = BooleanUtils.toBoolean(System.getProperty("scalable"));
		String serverName ="";
		if(isScalable) {
			serverName ="ALL";
		}else {
			serverName = EAIServerManager.getInstance().getLocalServerName();
		}
		try {
			Map<String, QueueMonitorVO> queueMonitorMap = new HashMap<>();
			logger.info("[ @@ Load QueueMonitor Configuration - starting >>>>>>>>>>>>>>>>> ]");

			List<QueueMonitor> queueMonitors = queueMonitorLoader
					.findByIdEaisevrinstncname(serverName);

			for (QueueMonitor queueMonitor : queueMonitors) {
				QueueMonitorVO queueMonitorVO = queueMonitorMapper.toVo(queueMonitor);
				queueMonitorMap.put(queueMonitorVO.getQueueName() + queueMonitorVO.getSevrInstncName(), queueMonitorVO);
				logger.info("@ " + queueMonitorVO.toString());
			}
			logger.info("[>>Load QueueMonitor getInflowMap Configuration - ended ]");
			return queueMonitorMap;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICCM101"));
		}
	}
}
