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 org.apache.commons.lang3.StringUtils;

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 CallPrevButActivityOperatePower  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");
		boolean isDestActivityPower = false;
		if((activityInstance==null||processInstance==null)||StringUtils.isEmpty(activityInstance.getPrevActDefId())){
			return isDestActivityPower;
		}
		RepositoryServiceImpl repositoryServiceImpl = (org.activiti.engine.impl.RepositoryServiceImpl) repositoryService;
		ReadOnlyProcessDefinition rpd = repositoryServiceImpl
				.getDeployedProcessDefinition(activityInstance.getProcDefId());
		ActivityImpl activity = (ActivityImpl) rpd
				.findActivity(activityInstance.getActivityDefinition()
						.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));
		boolean isPrevActivityPower = false;
		if (isDestActivityPower) {
			ActivityImpl prevActivity = (ActivityImpl) rpd
					.findActivity(activityInstance.getPrevActDefId());
			if(prevActivity==null){
				return isPrevActivityPower;
			}
			String prevActivityType = (String) prevActivity
					.getProperty(BpmnXMLConstants.ATTRIBUTE_TYPE);
			
			isPrevActivityPower = !(ProcessDefinitionUtils
					.isSubProcessStartEvent(prevActivity)
					|| ProcessDefinitionUtils
							.isSubProcessEndEvent(prevActivity)
					|| ProcessDefinitionUtils.isCallSubProcessEndEvent(
							processInstance, prevActivity)
					|| ProcessDefinitionUtils.isMultiInstance(prevActivity)
					|| ProcessDefinitionUtils.isEndEvent(prevActivity)
					|| BpmnXMLConstants.ELEMENT_GATEWAY_PARALLEL
							.equals(prevActivityType)
					|| BpmnXMLConstants.ELEMENT_GATEWAY_INCLUSIVE
							.equals(prevActivityType)
					|| BpmnXMLConstants.ELEMENT_TASK_SERVICE
							.equals(prevActivityType) || BpmnXMLConstants.ELEMENT_TASK_SCRIPT
					.equals(prevActivityType));
			if (isPrevActivityPower) {
				ActivityInstanceModel prevActivityInstance = null;
				try {
					prevActivityInstance = processInstanceService
							.getHistoryTask(activityInstance.getPrevActInstId());
				} catch (Exception e) {
					e.printStackTrace();
					return false;
				}
				if(prevActivityInstance==null){
					return false;
				}else if(!prevActivityInstance.getExecutionId()
								.equals(activityInstance.getExecutionId())) {
					isPrevActivityPower = false;
				}
			}
		}
		return isPrevActivityPower;
	}

}
