//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.activiti.engine.impl.bpmn.behavior;

import com.blueland.bpm.common.util.ApplicationContextHolder;
import com.blueland.bpm.engine.core.model.ProcessInputModel;
import com.blueland.bpm.engine.core.service.impl.ProcessDefinitionResource;
import com.blueland.bpm.engine.util.WfEngineUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.HistoricTaskInstanceEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InclusiveGatewayActivityBehavior2 extends GatewayActivityBehavior {
    private static final long serialVersionUID = 1L;
    private static Logger log = LoggerFactory.getLogger(InclusiveGatewayActivityBehavior2.class.getName());

    public InclusiveGatewayActivityBehavior2() {
    }

    public void execute(ActivityExecution execution) throws Exception {
        execution.inactivate();
        this.lockConcurrentRoot(execution);
        PvmActivity activity = execution.getActivity();
        if (!this.activeConcurrentExecutionsExist(execution)) {
            if (log.isDebugEnabled()) {
                log.debug("inclusive gateway '{}' activates", activity.getId());
            }

            Context.getCommandContext().getHistoryManager().recordTaskEndExtend(execution);
            Context.getCommandContext().getHistoryManager().recordActivityEnd((ExecutionEntity)execution);
            List<ActivityExecution> joinedExecutions = execution.findInactiveConcurrentExecutions(activity);
            String defaultSequenceFlow = (String)execution.getActivity().getProperty("default");
            List<PvmTransition> transitionsToTake = new ArrayList();
            ProcessDefinitionResource processDefinitionResource = (ProcessDefinitionResource)ApplicationContextHolder.getBean("processDefinitionResource");
            Iterator var8 = execution.getActivity().getOutgoingTransitions().iterator();

            while(true) {
                PvmTransition defaultTransition;
                do {
                    if (!var8.hasNext()) {
                        if (!transitionsToTake.isEmpty()) {
                            execution.takeAll(transitionsToTake, joinedExecutions);
                        } else {
                            if (defaultSequenceFlow == null) {
                                throw new ActivitiException("No outgoing sequence flow of the inclusive gateway '" + execution.getActivity().getId() + "' could be selected for continuing the process");
                            }

                            defaultTransition = execution.getActivity().findOutgoingTransition(defaultSequenceFlow);
                            if (defaultTransition == null) {
                                throw new ActivitiException("Default sequence flow '" + defaultSequenceFlow + "' could not be not found");
                            }

                            execution.take(defaultTransition);
                        }

                        return;
                    }

                    defaultTransition = (PvmTransition)var8.next();
                } while(defaultSequenceFlow != null && defaultTransition.getId().equals(defaultSequenceFlow));

                if (!processDefinitionResource.filterActivity(execution.getProcessDefinitionId(), (ActivityImpl)defaultTransition.getSource(), (ActivityImpl)defaultTransition.getDestination(), execution.getVariables())) {
                    transitionsToTake.add(defaultTransition);
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Inclusive gateway '{}' does not activate", activity.getId());
            }

            try {
                TaskEntity task = TaskEntity.create(Context.getProcessEngineConfiguration().getClock().getCurrentTime());
                WfEngineUtils.buildExtTaskEntity(task, (ExecutionEntity)execution);
                HistoricTaskInstanceEntity historicTaskInstance = new HistoricTaskInstanceEntity(task, (ExecutionEntity)execution);
                ProcessInputModel processInputModel = WfEngineUtils.getWfprocessInputModel(execution.getVariables());
                historicTaskInstance.markEnded("completed");
                Context.getCommandContext().getDbSqlSession().insert(historicTaskInstance);
                Context.getCommandContext().getProcessEngineConfiguration().getTaskService().deleteComments(historicTaskInstance.getPreTaskId(), (String)null);
                Context.getCommandContext().getCommentEntityManager().addCommentNotValidate(historicTaskInstance.getPreTaskId(), historicTaskInstance.getProcessInstanceId(), ((ExecutionEntity)execution).getTopProcessInstanceId(), processInputModel.getWf_curComment(), processInputModel.getWf_commentDisplayArea());
            } catch (Exception var9) {
                var9.printStackTrace();
            }
        }

    }

    List<? extends ActivityExecution> getLeaveExecutions(ActivityExecution parent) {
        List<ActivityExecution> executionlist = new ArrayList();
        List<? extends ActivityExecution> subExecutions = parent.getExecutions();
        if (subExecutions.isEmpty()) {
            executionlist.add(parent);
        } else {
            Iterator var5 = subExecutions.iterator();

            while(var5.hasNext()) {
                ActivityExecution concurrentExecution = (ActivityExecution)var5.next();
                executionlist.addAll(this.getLeaveExecutions(concurrentExecution));
            }
        }

        return executionlist;
    }

    public boolean activeConcurrentExecutionsExist(ActivityExecution execution) {
        PvmActivity activity = execution.getActivity();
        if (execution.isConcurrent()) {
            Iterator var4 = this.getLeaveExecutions(execution.getParent()).iterator();

            while(var4.hasNext()) {
                ActivityExecution concurrentExecution = (ActivityExecution)var4.next();
                if (concurrentExecution.isActive() && !concurrentExecution.getId().equals(execution.getId())) {
                    boolean reachable = false;
                    PvmTransition pvmTransition = ((ExecutionEntity)concurrentExecution).getTransitionBeingTaken();
                    if (pvmTransition != null) {
                        reachable = this.isReachable(pvmTransition.getDestination(), activity, new HashSet());
                    } else {
                        reachable = this.isReachable(concurrentExecution.getActivity(), activity, new HashSet());
                    }

                    if (reachable) {
                        if (log.isDebugEnabled()) {
                            log.debug("an active concurrent execution found: '{}'", concurrentExecution.getActivity());
                        }

                        return true;
                    }
                }
            }
        } else if (execution.isActive()) {
            if (log.isDebugEnabled()) {
                log.debug("an active concurrent execution found: '{}'", execution.getActivity());
            }

            return true;
        }

        return false;
    }

    protected boolean isReachable(PvmActivity srcActivity, PvmActivity targetActivity, Set<PvmActivity> visitedActivities) {
        if (srcActivity.getOutgoingTransitions().isEmpty()) {
            visitedActivities.add(srcActivity);
            if (!(srcActivity.getParent() instanceof PvmActivity)) {
                return false;
            }

            srcActivity = (PvmActivity)srcActivity.getParent();
        }

        if (srcActivity.equals(targetActivity)) {
            return true;
        } else {
            visitedActivities.add(srcActivity);
            List<PvmTransition> transitionList = srcActivity.getOutgoingTransitions();
            if (transitionList != null && !transitionList.isEmpty()) {
                Iterator var6 = transitionList.iterator();

                while(var6.hasNext()) {
                    PvmTransition pvmTransition = (PvmTransition)var6.next();
                    PvmActivity destinationActivity = pvmTransition.getDestination();
                    if (destinationActivity != null && !visitedActivities.contains(destinationActivity)) {
                        boolean reachable = this.isReachable(destinationActivity, targetActivity, visitedActivities);
                        if (reachable) {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
    }
}
