package com.blueland.bpm.engine.core.model;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blueland.unitework.model.UniteworkTask;

/**
 * 流程待办任务数据全局共享
 * 
 * @version: 1.0
 * @author lw
 */
public class TaskEntityDataShare {
	protected static Logger logger = LoggerFactory.getLogger(TaskEntityDataShare.class);
	//当前线程流程待办共享池
	static final ThreadLocal<List<UniteworkTask>> taskEntityShareThreadLocal = new ThreadLocal<List<UniteworkTask>>();
	//当前线程流程待办共享池
	static final ThreadLocal<List<UniteworkTask>> hisTaskEntityShareThreadLocal = new ThreadLocal<List<UniteworkTask>>();
	
	
	/**
	 * 返回当前线程中流程待办列表
	 * @return
	 */
	public static List<UniteworkTask> getTask() {
		List<UniteworkTask> taskEntitys=getTaskEntitys();
	    return taskEntitys;
	 }
	/**
	 * 返回当前线程中流程已办列表
	 * @return
	 */
	public static List<UniteworkTask> getHisTask() {
		List<UniteworkTask> hisTaskEntitys=getHisTaskEntitys();
	    return hisTaskEntitys;
	 }
	public static void setTask(UniteworkTask taskEntity) {
		logger.info("set待办数据:"+taskEntity);
		getTaskEntitys().add(taskEntity);
	}
	
	public static void setHisTask(UniteworkTask taskEntity) {
		logger.info("set已办数据:"+taskEntity);
		getHisTaskEntitys().add(taskEntity);
	}
	
	
	public static void clear() {
		if(getTaskEntitys()!=null){
			getTaskEntitys().clear();
			taskEntityShareThreadLocal.set(null);
		}
		if(getHisTaskEntitys()!=null){
			getHisTaskEntitys().clear();
			hisTaskEntityShareThreadLocal.set(null);
		}
	}
	
	private static List<UniteworkTask> getTaskEntitys() {
		List<UniteworkTask> taskEntitys=taskEntityShareThreadLocal.get();
		if(taskEntitys==null){
			taskEntitys=new CopyOnWriteArrayList<UniteworkTask>();
			taskEntityShareThreadLocal.set(taskEntitys);
		}
		return taskEntitys;
	}

	private static List<UniteworkTask> getHisTaskEntitys() {
		List<UniteworkTask> hisTaskEntitys = hisTaskEntityShareThreadLocal
				.get();
		if (hisTaskEntitys == null) {
			hisTaskEntitys = new CopyOnWriteArrayList<UniteworkTask>();
			hisTaskEntityShareThreadLocal.set(hisTaskEntitys);
		}
		return hisTaskEntitys;
	}
}
