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

import java.util.Map;

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.model.ProcessPageContext;
import com.blueland.bpm.engine.core.service.ProcessInstanceService;
import com.blueland.bpm.engine.util.ProcessDefinitionUtils;
/**
 * 补发多实例操作权限处理类
 * @version:  1.0
 * @author lw 
 */
public class AddMulltiActivityOperatePower extends AbstractActivityOperatePower {
	ProcessInstanceService processInstanceService = (ProcessInstanceService) ApplicationContextHolder
			.getBean("processInstanceServiceImpl");
	RepositoryServiceImpl repositoryService = (RepositoryServiceImpl) ApplicationContextHolder
			.getBean("repositoryService");

	@Override
	public boolean isActivityOperatePower(Map<String, Object> pageDetailVaribale) {
		boolean isPower = false;
		ProcessPageContext context = (ProcessPageContext) pageDetailVaribale
				.get("processPageContext");
		ActivityInstanceModel curActivityInstance = (ActivityInstanceModel) pageDetailVaribale
				.get("task");
		HistoricProcessInstance processInstance = (HistoricProcessInstance) pageDetailVaribale
				.get("processInstance");
		if (context == null || curActivityInstance == null
				|| processInstance == null) {
			return isPower;
		}
		/*List<Task> tasks=processInstanceService.getProcessTasks(prevActivityInstance.getProcInstId(), prevActivityInstance.getReceiver(), prevActivityInstance.getActInstId());
		if(tasks==null||tasks.isEmpty()){
			return isPower;
		}*/
		//ActivityInstanceModel curActivityInstance=ActivityInstanceModel.buildAndActDef((TaskEntity)tasks.get(0),null);
		RepositoryServiceImpl repositoryServiceImpl = (org.activiti.engine.impl.RepositoryServiceImpl) repositoryService;
		ReadOnlyProcessDefinition rpd = repositoryServiceImpl
				.getDeployedProcessDefinition(processInstance.getProcessDefinitionId());
		ActivityImpl activity = (ActivityImpl) rpd
				.findActivity(curActivityInstance.getActDefId());
		System.out.println(activity.getParentActivity());
		boolean isDestActivityPower = ProcessDefinitionUtils
				.isSubProcessStartTask(activity)
				|| ProcessDefinitionUtils.isMultiInstance(activity)||ProcessDefinitionUtils.isSubProcessMultiInstance(activity);
		return (isDestActivityPower)||!curActivityInstance.getProcInstId().equals(curActivityInstance.getExecutionId());
	}

}
