package com.leanwo.activiti.util;

/**
 * 线程变量
 * @author YangZhiJie
 *
 */
public class LoginContextUtil {
	
private static ThreadLocal<LoginContext> loginContextHolder = new ThreadLocal<LoginContext>();
	
	private static ThreadLocal<String> uuidHolder = new ThreadLocal<String>();
	
	/**
	 * 从线程获取LoginContext
	 * @return
	 */
	public static LoginContext getLoginContext(){
		LoginContext loginContext = loginContextHolder.get();
		return loginContext;
	}
	
	/**
	 * 设置线程的LoginContext
	 * @param loginContext
	 */
	public static void setLoginContext(LoginContext loginContext){
		loginContextHolder.set(loginContext);
	}
	
	/**
	 * 从线程获取UUID
	 * @return
	 */
	public static String getUuid(){
		String uuid = uuidHolder.get();
		return uuid;
	}
	
	/**
	 * 设置线程的UUID
	 * @param loginContext
	 */
	public static void setUuid(String uuid){
		uuidHolder.set(uuid);
	}
	
	/**
	 * 设置使用 accoutId 账户<br>
	 * 一般用在工作流中
	 * Add by jack 20151010
	 */
	public static void setUseApplicationAccount(Long accountId){
		LoginContext loginContext = getLoginContext();
		if(loginContext == null){
			loginContext = new LoginContext();
			LoginContextUtil.setLoginContext(loginContext);
		}
		loginContext.setAccountId(accountId);
	}

	
	
	/**
	 * 清空LoginContext
	 */
	public static void clear(){
		loginContextHolder.set(null);
	}
}
