package cn.hmsoft.application;

import java.io.File;
import java.time.LocalDate;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;

import cn.hmsoft.helper.LocalDateHelper;
import cn.hmsoft.jdbc.core.DatabaseDao;

public class SpringHelper {

	/*************************
	 * 全局的WebServletContext，系统启动时设置该变量
	 */
	public static ServletContext WebServletContext = null;
	/*************************
	 * Spring容器
	 */
	public static ApplicationContext SpringContext = null;

	/*******************
	 * 通用数据库类
	 */

	public static DatabaseDao AppGobalDao = null;

	public static <T> T getBean(Class<T> clazz) {
		return SpringContext.getBean(clazz);
	}

	/************************
	 * 获取根目录
	 */
	public static String getRootPath() {
		if (WebServletContext != null)
			return SpringHelper.WebServletContext.getRealPath("/");
		else
			return new File("").getAbsolutePath() + File.separator;
	}

	
	/************************
	 * 获取临时目录
	 */
	public static String getWebTemporaryPath(LocalDate date) {
		String path =   "temporary" + File.separator + LocalDateHelper.formatNoSplit(LocalDate.now())+ File.separator;
		File file = new File(SpringHelper.getRootPath() + path);
		if(!file.exists())
			file.mkdirs();
		return path;
	}
	
	public static String getWebTemporaryPath() {
		return getWebTemporaryPath(LocalDate.now());
	}
	/************************
	 * getPath
	 */
	public static String getFilePath(String path) {
		return getRootPath() + path;
	}
	
	/************************
	 * getFile
	 */
	public static File getFile(String path) {
		return new File(getFilePath(path));
	}
}
