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

import java.util.Map;

import org.activiti.bpmn.constants.BpmnXMLConstants;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.pvm.ReadOnlyProcessDefinition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;

import com.blueland.bpm.common.util.ApplicationContextHolder;
import com.blueland.bpm.engine.core.model.ActivityInstanceModel;
import com.blueland.bpm.engine.core.service.ProcessInstanceService;
import com.blueland.bpm.engine.util.ProcessDefinitionUtils;
/**
 * 作废流程操作权限处理类
 * @version:  1.0
 * @author lw 
 */
public class CancelButActivityOperatePower  extends AbstractActivityOperatePower{
	ProcessInstanceService processInstanceService=(ProcessInstanceService) ApplicationContextHolder.getBean("processInstanceServiceImpl");
	RepositoryServiceImpl repositoryService=(RepositoryServiceImpl) ApplicationContextHolder.getBean("repositoryService");

	@Override
	public boolean isActivityOperatePower(Map<String, Object> pageDetailVaribale) {
		ActivityInstanceModel activityInstance = (ActivityInstanceModel) pageDetailVaribale
				.get("task");
		HistoricProcessInstance processInstance = (HistoricProcessInstance) pageDetailVaribale
				.get("processInstance");
		RepositoryServiceImpl repositoryServiceImpl = (org.activiti.engine.impl.RepositoryServiceImpl) repositoryService;
		ReadOnlyProcessDefinition rpd = repositoryServiceImpl
				.getDeployedProcessDefinition(processInstance.getProcessDefinitionId());
		ActivityImpl activity = (ActivityImpl) rpd
				.findActivity(activityInstance.getActDefId()!=null?activityInstance.getActDefId():activityInstance.getActivityDefinition()
						.getActDefId());
		String destActivityType = (String) activity
				.getProperty(BpmnXMLConstants.ATTRIBUTE_TYPE);
		boolean isDestActivityPower = false;
	   if(activityInstance.getExecutionId().equals(processInstance.getTopProcessInstanceId())&&!ProcessDefinitionUtils.isMultiInstance(activity)&&!ProcessDefinitionUtils.isExclusiveGateway(activity)){
	    	isDestActivityPower=true;
	    }/*else{
	    	isDestActivityPower = !(ProcessDefinitionUtils
					.isSubProcessStartEvent(activity)
					|| ProcessDefinitionUtils.isSubProcessEndEvent(activity)
					|| ProcessDefinitionUtils.isCallSubProcessEndEvent(
							processInstance, activity)
					|| ProcessDefinitionUtils.isEndEvent(activity)
					|| BpmnXMLConstants.ELEMENT_GATEWAY_PARALLEL
							.equals(destActivityType));
	    }*/
		return (isDestActivityPower);
	}

}
