package com.blueland.bpm.unitework;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blueland.bpm.engine.core.model.ExceptionErrorCode;
import com.blueland.bpm.engine.core.model.ProcessInstanceModel;
import com.blueland.bpm.engine.core.model.TaskEntityDataShare;
import com.blueland.bpm.engine.util.WorkFlowException;
import com.blueland.unitework.model.UniteworkTask;
import com.blueland.unitework.service.UniteworkService;

/**
 * 统一待办消息同步监听类-任务创建（新增待办）
 * 
 * @version: 1.0
 * @author lw
 */
public class TaskCreateListener  extends TaskCommonEvent  implements TaskListener {
	private static final long serialVersionUID = 1L;
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void notify(DelegateTask delegate) {
		if (!delegate.getEventName().equals(TaskListener.EVENTNAME_CREATE))
			return;
		try {
			UniteworkService uniteworkService = (UniteworkService) com.blueland.bpm.common.util.ApplicationContextHolder
					.getBean("uniteworkService");
			TaskEntity delegateTask = (TaskEntity) delegate;
			ProcessInstanceModel processInfo = buildProcessInstanceModel(delegateTask);
			UniteworkTask uniteworkTask = buildUniteworkTaskByTodo(processInfo, 	processInfo.getCurrentActivity());
			uniteworkTask.setCurActLimitTime(delegateTask.getLimitTime());
			uniteworkTask.setFlowState("1");
			uniteworkService.insertTask(uniteworkTask);
			//第一个节点不发送提醒消息
			boolean isSendESB=isSendESB(delegateTask)&&!uniteworkTask.getBatchFlag();
			if(isSendESB){
				TaskEntityDataShare.setTask(uniteworkTask);
			}
		} catch (Exception e) {
			throw new WorkFlowException(ExceptionErrorCode.C3001,"消息类型[新增待办],同步统一待办消息失败", e);
		}
	}

}