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

import org.activiti.bpmn.constants.BpmnXMLConstants;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.pvm.ReadOnlyProcessDefinition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueland.bpm.engine.core.model.ActivityInstanceModel;
import com.blueland.bpm.engine.core.model.ProcessInstanceModel;
import com.blueland.bpm.engine.core.service.ProcessInstanceService;
import com.blueland.bpm.engine.util.ProcessDefinitionUtils;
/**
 * 驳回至历史环节操作权限处理类
 * @version:  1.0
 * @author lw 
 */
@Service
public class RejectButActivityOperatePower{
	@Autowired
	ProcessInstanceService processInstanceService;
	@Autowired
	RepositoryServiceImpl repositoryService;

	/**
	 * 判断curActivityInstance当前节点是否允许驳回
	 * @param processInstance
	 * @param curActivityInstance
	 * @param rpd
	 * @return
	 */
	public boolean isCurActivityInstanceRejectPower(ProcessInstanceModel processInstance,ActivityInstanceModel curActivityInstance,ReadOnlyProcessDefinition rpd ) {
		boolean isDestActivityPower = false;
		if (curActivityInstance == null) {
			return isDestActivityPower;
		}
		ActivityImpl activity = (ActivityImpl) rpd
				.findActivity(curActivityInstance.getActDefId());
		String destActivityType = (String) activity
				.getProperty(BpmnXMLConstants.ATTRIBUTE_TYPE);
		isDestActivityPower = !(ProcessDefinitionUtils
				.isSubProcessStartEvent(activity)//是否内嵌子流开始节点
				|| ProcessDefinitionUtils.isSubProcessEndEvent(activity)//是否内嵌子流结束节点
				|| ProcessDefinitionUtils.isCallSubProcessEndEvent(
						processInstance, activity)//是否外部子流结束节点
				|| ProcessDefinitionUtils.isMultiInstance(activity)//是否多实例节点
				|| ProcessDefinitionUtils.isEndEvent(activity)//是否流程结束节点点
				|| BpmnXMLConstants.ELEMENT_GATEWAY_PARALLEL
						.equals(destActivityType) || BpmnXMLConstants.ELEMENT_GATEWAY_INCLUSIVE
				.equals(destActivityType));//是否并行网关、包容网关
		return isDestActivityPower;
	}
	
	/**
	 * 判断historicTaskInstance历史节点是否允许驳回
	 * @param processInstance
	 * @param curActivityInstance
	 * @param historicTaskInstance
	 * @param rpd
	 * @return
	 */
	public boolean isHisActivityInstanceRejectPower(ProcessInstanceModel processInstance,
			ActivityInstanceModel curActivityInstance,
			HistoricTaskInstance historicTaskInstance,ReadOnlyProcessDefinition rpd) {
		boolean isHisActivityPower = false;
		if (historicTaskInstance == null) {
			return isHisActivityPower;
		}
		ActivityImpl hisActivity = (ActivityImpl) rpd
				.findActivity(historicTaskInstance.getTaskDefinitionKey());
		if (hisActivity == null) {
			return isHisActivityPower;
		}
		String hisActivityType = (String) hisActivity
				.getProperty(BpmnXMLConstants.ATTRIBUTE_TYPE);
		isHisActivityPower = !(ProcessDefinitionUtils
				.isSubProcessStartEvent(hisActivity)//是否内嵌子流开始节点
				|| ProcessDefinitionUtils.isSubProcessEndEvent(hisActivity)//是否内嵌子流结束节点
				|| ProcessDefinitionUtils.isCallSubProcessEndEvent(
						processInstance, hisActivity)//是否外部子流结束节点
				|| ProcessDefinitionUtils.isMultiInstance(hisActivity)//是否多实例节点
				|| ProcessDefinitionUtils.isEndEvent(hisActivity)//是否流程结束节点点
				|| BpmnXMLConstants.ELEMENT_GATEWAY_PARALLEL
						.equals(hisActivityType)
				|| BpmnXMLConstants.ELEMENT_GATEWAY_INCLUSIVE
						.equals(hisActivityType)
				|| BpmnXMLConstants.ELEMENT_TASK_SERVICE
						.equals(hisActivityType) || BpmnXMLConstants.ELEMENT_TASK_SCRIPT
				.equals(hisActivityType));//是否并行网关、包容网关、服务任务、脚本任务
		if (isHisActivityPower) {
			//判断当前任务与待驳回的任务执行路径是否一致 
			if (!(historicTaskInstance.getExecutionId()
					.equals(curActivityInstance.getExecutionId()))) {
				isHisActivityPower = false;
			}
		}

		return isHisActivityPower;
	}

}
