package com.blueland.bpm.unitework;

import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * 统一待办消息同步监听类-流程作废
 * 
 * @version: 1.0
 * @author lw
 */
public class ProcessCancelEvent extends TaskCommonEvent {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void onEntityEvent(ActivitiEvent event) {
		if (!ActivitiEventType.PROCESS_CANCEL.equals(event.getType())) {
			return;
		}
		ExecutionEntity executionEntity = (ExecutionEntity) ((ActivitiEntityEvent) event)
				.getEntity();
		if (executionEntity == null) {
			return;
		}
		try {
			UniteworkService uniteworkService = (UniteworkService) com.blueland.bpm.common.util.ApplicationContextHolder
					.getBean("uniteworkService");
			uniteworkService.changeToCancel(executionEntity.getTopProcessInstanceId());

		} catch (Exception e) {
			throw new WorkFlowException(ExceptionErrorCode.C3006,"消息类型[流程作废],同步统一待办消息失败", e);
		}
	}

	@Override
	public boolean isFailOnException() {
		// The logic in the onEvent method of this listener is not critical,
		// exceptions
		// can be ignored if logging fails...
		return true;
	}

}