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.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
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 CallStartButActivityOperatePower  extends AbstractActivityOperatePower{
	ProcessInstanceService processInstanceService=(ProcessInstanceService) ApplicationContextHolder.getBean("processInstanceServiceImpl");
	RepositoryServiceImpl repositoryService=(RepositoryServiceImpl) ApplicationContextHolder.getBean("repositoryService");

	@Override
	public boolean isActivityOperatePower(Map<String, Object> pageDetailVaribale) {
		boolean isDestActivityPower = false;
		ActivityInstanceModel activityInstance = (ActivityInstanceModel) pageDetailVaribale
				.get("task");
		HistoricProcessInstance processInstance = (HistoricProcessInstance) pageDetailVaribale
				.get("processInstance");
		if(activityInstance==null||processInstance==null){
			return isDestActivityPower;
		}
		RepositoryServiceImpl repositoryServiceImpl = (org.activiti.engine.impl.RepositoryServiceImpl) repositoryService;
		ReadOnlyProcessDefinition rpd = repositoryServiceImpl
				.getDeployedProcessDefinition(activityInstance.getProcDefId());
		PvmActivity pvmActivity = rpd.getInitial();
		PvmTransition pvmTransition = pvmActivity.getOutgoingTransitions().get(
				0);
		PvmActivity pvmStartActivity = pvmTransition.getDestination();
		if(activityInstance.getActDefId().equals(pvmStartActivity.getId())){
			return isDestActivityPower;
		}
		ActivityImpl activity = (ActivityImpl) rpd
				.findActivity(activityInstance.getActivityDefinition()
						.getActDefId());
		if(activity==null){
			return isDestActivityPower;
		}
		String destActivityType = (String) activity
				.getProperty(BpmnXMLConstants.ATTRIBUTE_TYPE);
		
	    if(!activityInstance.getExecutionId().equals(processInstance.getId())){
	    	isDestActivityPower=false;
	    }else{
	    	isDestActivityPower = !(ProcessDefinitionUtils
					.isSubProcessStartTask(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);
	}

}
