package com.leanwo.activiti.listener.base;

import java.util.List;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.reflect.TypeToken;
import com.leanwo.activiti.exception.WorkflowException;
import com.leanwo.activiti.util.FormatUtil;
import com.leanwo.activiti.util.StringUtil;


/**
 * 多实例任务（用户组）创建监听器
 * <ul>
 * 		<li>分配用户</li>
 * </ul>.
 *
 * @author YangZhiJie
 */
public class MultiInstanceUserTaskByUserIdsExecutionListener implements ExecutionListener{
	
	/**  */
	private static Logger logger = LoggerFactory.getLogger(MultiInstanceUserTaskByUserIdsExecutionListener.class);

	/**  任务接受人(String). */
	private Expression assignListVariableName;
	
	/**  任务接受人集合(List<String>). */
	private Expression assigneeList;

	/** 节点名称. */
	private Expression nodeName;

	@Override
	public void notify(DelegateExecution execution) {
		String assignListName = assignListVariableName.getExpressionText();
		String assignListText = assigneeList.getExpressionText();
		String node = nodeName.getExpressionText();
		
		if(StringUtil.isEmpty(assignListName) || StringUtil.isEmpty(assignListText)) {
			throw new WorkflowException(String.format("多实例任务未定义变量assignListVariableName和assigneeList。", node));
		}
		
		List<String> assigneeList = FormatUtil.getGson().fromJson(assignListText,  new TypeToken<List<String>>() {}.getType());
		if (assigneeList != null && assigneeList.size() > 0) {
			execution.setVariable(assignListName, assigneeList);
		}
	}


	/**
	 * Gets the 任务接受人(String).
	 *
	 * @return the 任务接受人(String)
	 */
	public Expression getAssignListVariableName() {
		return assignListVariableName;
	}


	/**
	 * Sets the 任务接受人(String).
	 *
	 * @param assignListVariableName the new 任务接受人(String)
	 */
	public void setAssignListVariableName(Expression assignListVariableName) {
		this.assignListVariableName = assignListVariableName;
	}


	/**
	 * Gets the 任务接受人集合(List<String>).
	 *
	 * @return the 任务接受人集合(List<String>)
	 */
	public Expression getAssigneeList() {
		return assigneeList;
	}


	/**
	 * Sets the 任务接受人集合(List<String>).
	 *
	 * @param assigneeList the new 任务接受人集合(List<String>)
	 */
	public void setAssigneeList(Expression assigneeList) {
		this.assigneeList = assigneeList;
	}

	/**
	 * Gets the 节点名称.
	 *
	 * @return the 节点名称
	 */
	public Expression getNodeName() {
		return nodeName;
	}

	/**
	 * Sets the 节点名称.
	 *
	 * @param nodeName the new 节点名称
	 */
	public void setNodeName(Expression nodeName) {
		this.nodeName = nodeName;
	}



}
