package net.wicp.tams.app.duckula.controller.service;

import javax.servlet.ServletContext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;

import net.wicp.tams.app.duckula.controller.bean.models.SysGlobal;
import net.wicp.tams.app.duckula.controller.config.constant.ConfigGlobleName;
import net.wicp.tams.app.duckula.controller.dao.SysGlobalMapper;

@Service
public class ContextInit implements ApplicationContextAware, ServletContextAware {

	public static volatile String pagePath;// page页面存放的根目录
	public static volatile String webRootPath;// Webroot所在目录
	
	@Autowired
	private SysGlobalMapper sysGlobalMapper;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		//SysGlobalMapper sysGlobalMapper = applicationContext.getBean(SysGlobalMapper.class);
		SysGlobal sysGlobal = sysGlobalMapper.selectById(1l);
		if (sysGlobal != null) {
			ConfigGlobleName.putAwsConfig(sysGlobal);
		}
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		String realPath = servletContext.getRealPath("/");
		ContextInit.webRootPath = realPath;// 配置根目录
		String rootPath = Thread.currentThread().getContextClassLoader().getResource(".").getPath();
		String appPackage = servletContext.getInitParameter("tapestry.app-package");
		ContextInit.pagePath = String.format("%s%s/pages", rootPath, appPackage.replace(".", "/"));
	}
}
