package com.eactive.eai.common.routing;

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

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.routing.loader.RoutingLoader;
import com.eactive.eai.common.routing.mapper.RoutingMapper;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.routing.Routing;

@Service
@Transactional
public class RoutingDAO extends BaseDAO {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	@Autowired
	private RoutingLoader routingLoader;

	@Autowired
	private RoutingMapper routingMapper;

	public Map<String, RoutingVO> getAllRoutings() throws DAOException {
		try {
			Map<String, RoutingVO> routingMap = new HashMap<>();
			List<Routing> routings = routingLoader.findAllByOrderByDefault();
			if(logger.isInfo()) logger.info("[ @@ Load Routing Configuration - starting >>>>>>>>>>>>>>>>> ]");
			for (Routing routing : routings) {
				RoutingVO routingVO = routingMapper.toVo(routing);
				routingMap.put(routingVO.getName(), routingVO);
			}
			if(logger.isInfo()) logger.info("[>>Load Routing Configuration - ended ]");
			return routingMap;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICRT101"));
		}
	}

	public RoutingVO getRouting(String routingName) throws DAOException {
		try {
			Routing routing = routingLoader.getById(routingName);
			RoutingVO routingVO = routingMapper.toVo(routing);
			if(logger.isInfo()) logger.info("@ " + routingVO.toString());
			return routingVO;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICRT101"));
		}
	}
}
