package com.eactive.eai.rms.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DataSourceTypeInterceptor extends HandlerInterceptorAdapter {
	
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    	//DataSourceType d =DataSourceContextHolder.getDataSourceType();
        DataSourceContextHolder.clearDataSourceType();
    }

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
    	
		DataSourceType dataSourceType = DataSourceContextHolder.getDataSourceType(request);
		DataSourceContextHolder.setDataSourceType(dataSourceType);
		log.trace("dataSourceType : {}", dataSourceType);
		return true;
    }
}
