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

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * The Class DataSourceContextHolder.
 */
public class DataSourceContextHolder {

    /** The Constant contextHolder. */
    private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<DataSourceType>();

    /**
     * Sets the data source type.
     * 
     * @param dataSourceType
     *            the new data source type
     */
    public static void setDataSourceType(DataSourceType dataSourceType) {
    	contextHolder.set(dataSourceType);
    }

    /**
     * Gets the data source type.
     * 
     * @return the data source type
     */
    public static DataSourceType getDataSourceType() {
        return (DataSourceType) contextHolder.get();
    }

    /**
     * Clear data source type.
     */
    public static void clearDataSourceType() {
        contextHolder.remove();
    }

    /**
     * Gets the data source type.
     * 
     * @param request
     *            the request
     * 
     * @return the data source type
     * @throws Exception 
     */
    public static DataSourceType getDataSourceType(HttpServletRequest request) throws Exception {
    	String serviceType = request.getParameter("serviceType");
    	DataSourceType dataSourceType = null;
    	if (serviceType == null){
    		//throw new Exception("service type 오류");
    		//dataSourceType = DataSourceTypeManager.getDataSourceType(DataSourceTypeManager.RMS_DEFAULT);
    	}else{
    		dataSourceType = DataSourceTypeManager.getDataSourceType(serviceType);
    	}
    	return dataSourceType;
    }
    public static List<String> getRender(HttpServletRequest request) throws Exception {
    	//return getDataSourceType(request).getRender();
    	//취약점소스 수정 2020.08.24
    	DataSourceType tempNullableVar = getDataSourceType(request);

		if(tempNullableVar != null) {
			return tempNullableVar.getRender();
		}else{
			return null;
		}
    }    
}
