package com.style24.front.support.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.style24.core.support.annotation.WmsDs;
import com.style24.core.support.env.TscConstants;

/**
 * wmsDs용 Mybatis Configuration
 * 
 * @author gagamel
 * @since 2020. 11. 26
 */
@Configuration
@MapperScan(basePackages = TscConstants.BASE_PACKAGE, annotationClass = WmsDs.class, sqlSessionFactoryRef = "wmsSqlSessionFactory")
public class TsfMybatisWmsConfig {

	@Autowired
	private ApplicationContext applicationContext;

	@Bean(name = "wmsSqlSessionFactory")
	public SqlSessionFactory wmsSqlSessionFactory(@Qualifier("wmsDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();

		sessionFactoryBean.setDataSource(dataSource);
		sessionFactoryBean.setTypeAliasesPackage(TscConstants.DOMAIN_PACKAGE);
		sessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:persistence/mybatis-wms-config.xml"));
		sessionFactoryBean.setMapperLocations(applicationContext.getResources(TscConstants.MAPPER_LOCATION_PATH + "/wms/*.xml"));

		return sessionFactoryBean.getObject();
	}

	@Bean(name = "wmsSqlSessionTemplate")
	public SqlSessionTemplate wmsSqlSessionTemplate(@Qualifier("wmsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

}
