Java Code Examples for org.activiti.engine.impl.persistence.entity.ExecutionEntity#isProcessInstanceType()

The following examples show how to use org.activiti.engine.impl.persistence.entity.ExecutionEntity#isProcessInstanceType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActivitiProcessStartedEventImpl.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public ActivitiProcessStartedEventImpl(final Object entity, final Map variables, final boolean localScope) {
  super(entity, variables, localScope, ActivitiEventType.PROCESS_STARTED);
  if (entity instanceof ExecutionEntity) {
    ExecutionEntity executionEntity = (ExecutionEntity) entity;
    if (executionEntity.isProcessInstanceType() == false) {
      executionEntity = executionEntity.getParent();
    }
    
    final ExecutionEntity superExecution = executionEntity.getSuperExecution();
    if (superExecution != null) {
      this.nestedProcessDefinitionId = superExecution.getProcessDefinitionId();
      this.nestedProcessInstanceId = superExecution.getProcessInstanceId();
    } else {
      this.nestedProcessDefinitionId = null;
      this.nestedProcessInstanceId = null;
    }
    
  } else {
    this.nestedProcessDefinitionId = null;
    this.nestedProcessInstanceId = null;
  }
}
 
Example 2
Source File: TakeOutgoingSequenceFlowsOperation.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
/**
 * @param executionEntityToIgnore The execution entity which we can ignore to be ended,
 * as it's the execution currently being handled in this operation.
 */
protected ExecutionEntity findNextParentScopeExecutionWithAllEndedChildExecutions(ExecutionEntity executionEntity, ExecutionEntity executionEntityToIgnore) {
  if (executionEntity.getParentId() != null) {
    ExecutionEntity scopeExecutionEntity = executionEntity.getParent();

    // Find next scope
    while (!scopeExecutionEntity.isScope() || !scopeExecutionEntity.isProcessInstanceType()) {
      scopeExecutionEntity = scopeExecutionEntity.getParent();
    }

    // Return when all child executions for it are ended
    if (allChildExecutionsEnded(scopeExecutionEntity, executionEntityToIgnore)) {
      return scopeExecutionEntity;
    }

  }
  return null;
}
 
Example 3
Source File: SetProcessInstanceBusinessKeyCmd.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public Void execute(CommandContext commandContext) {
  ExecutionEntityManager executionManager = commandContext.getExecutionEntityManager();
  ExecutionEntity processInstance = executionManager.findById(processInstanceId);
  if (processInstance == null) {
    throw new ActivitiObjectNotFoundException("No process instance found for id = '" + processInstanceId + "'.", ProcessInstance.class);
  } else if (!processInstance.isProcessInstanceType()) {
    throw new ActivitiIllegalArgumentException("A process instance id is required, but the provided id " + "'" + processInstanceId + "' " + "points to a child execution of process instance " + "'"
        + processInstance.getProcessInstanceId() + "'. " + "Please invoke the " + getClass().getSimpleName() + " with a root execution id.");
  }
  
  if (Activiti5Util.isActiviti5ProcessDefinitionId(commandContext, processInstance.getProcessDefinitionId())) {
    commandContext.getProcessEngineConfiguration().getActiviti5CompatibilityHandler().updateBusinessKey(processInstanceId, businessKey);
    return null;
  }

  executionManager.updateProcessInstanceBusinessKey(processInstance, businessKey);

  return null;
}
 
Example 4
Source File: SetProcessInstanceBusinessKeyCmd.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {
    ExecutionEntityManager executionManager = commandContext.getExecutionEntityManager();
    ExecutionEntity processInstance = executionManager.findExecutionById(processInstanceId);
    if (processInstance == null) {
        throw new ActivitiObjectNotFoundException("No process instance found for id = '" + processInstanceId + "'.", ProcessInstance.class);
    } else if (!processInstance.isProcessInstanceType()) {
        throw new ActivitiIllegalArgumentException(
                "A process instance id is required, but the provided id " +
                        "'" + processInstanceId + "' " +
                        "points to a child execution of process instance " +
                        "'" + processInstance.getProcessInstanceId() + "'. " +
                        "Please invoke the " + getClass().getSimpleName() + " with a root execution id.");
    }

    processInstance.updateProcessBusinessKey(businessKey);

    return null;
}
 
Example 5
Source File: TakeOutgoingSequenceFlowsOperation.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void cleanupCompensation() {

    // The compensation is at the end here. Simply stop the execution.

    commandContext.getHistoryManager().recordActivityEnd(execution, null);
    commandContext.getExecutionEntityManager().deleteExecutionAndRelatedData(execution, null, false);

    ExecutionEntity parentExecutionEntity = execution.getParent();
    if (parentExecutionEntity.isScope() && !parentExecutionEntity.isProcessInstanceType()) {

      if (allChildExecutionsEnded(parentExecutionEntity, null)) {

        // Go up the hierarchy to check if the next scope is ended too.
        // This could happen if only the compensation activity is still active, but the
        // main process is already finished.

        ExecutionEntity executionEntityToEnd = parentExecutionEntity;
        ExecutionEntity scopeExecutionEntity = findNextParentScopeExecutionWithAllEndedChildExecutions(parentExecutionEntity, parentExecutionEntity);
        while (scopeExecutionEntity != null) {
          executionEntityToEnd = scopeExecutionEntity;
          scopeExecutionEntity = findNextParentScopeExecutionWithAllEndedChildExecutions(scopeExecutionEntity, parentExecutionEntity);
        }

        if (executionEntityToEnd.isProcessInstanceType()) {
          Context.getAgenda().planEndExecutionOperation(executionEntityToEnd);
        } else {
          Context.getAgenda().planDestroyScopeOperation(executionEntityToEnd);
        }

      }
    }
  }
 
Example 6
Source File: EndExecutionOperation.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected ExecutionEntity handleRegularExecutionEnd(ExecutionEntityManager executionEntityManager, ExecutionEntity parentExecution) {
  ExecutionEntity executionToContinue = null;

  if (!parentExecution.isProcessInstanceType()
      && !(parentExecution.getCurrentFlowElement() instanceof SubProcess)) {
    parentExecution.setCurrentFlowElement(execution.getCurrentFlowElement());
  }

  if (execution.getCurrentFlowElement() instanceof SubProcess) {
    SubProcess currentSubProcess = (SubProcess) execution.getCurrentFlowElement();
    if (currentSubProcess.getOutgoingFlows().size() > 0) {
      // create a new execution to take the outgoing sequence flows
      executionToContinue = executionEntityManager.createChildExecution(parentExecution);
      executionToContinue.setCurrentFlowElement(execution.getCurrentFlowElement());

    } else {
      if (parentExecution.getId().equals(parentExecution.getProcessInstanceId()) == false) {
        // create a new execution to take the outgoing sequence flows
        executionToContinue = executionEntityManager.createChildExecution(parentExecution.getParent());
        executionToContinue.setCurrentFlowElement(parentExecution.getCurrentFlowElement());

        executionEntityManager.deleteChildExecutions(parentExecution, null, false);
        executionEntityManager.deleteExecutionAndRelatedData(parentExecution, null, false);

      } else {
        executionToContinue = parentExecution;
      }
    }

  } else {
    executionToContinue = parentExecution;
  }
  return executionToContinue;
}
 
Example 7
Source File: SetProcessDefinitionVersionCmd.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public Void execute(CommandContext commandContext) {
  // check that the new process definition is just another version of the same
  // process definition that the process instance is using
  ExecutionEntityManager executionManager = commandContext.getExecutionEntityManager();
  ExecutionEntity processInstance = executionManager.findById(processInstanceId);
  if (processInstance == null) {
    throw new ActivitiObjectNotFoundException("No process instance found for id = '" + processInstanceId + "'.", ProcessInstance.class);
  } else if (!processInstance.isProcessInstanceType()) {
    throw new ActivitiIllegalArgumentException("A process instance id is required, but the provided id " + "'" + processInstanceId + "' " + "points to a child execution of process instance " + "'"
        + processInstance.getProcessInstanceId() + "'. " + "Please invoke the " + getClass().getSimpleName() + " with a root execution id.");
  }
  
  DeploymentManager deploymentCache = commandContext.getProcessEngineConfiguration().getDeploymentManager();
  ProcessDefinition currentProcessDefinition = deploymentCache.findDeployedProcessDefinitionById(processInstance.getProcessDefinitionId());

  ProcessDefinition newProcessDefinition = deploymentCache
      .findDeployedProcessDefinitionByKeyAndVersionAndTenantId(currentProcessDefinition.getKey(), processDefinitionVersion, currentProcessDefinition.getTenantId());

  validateAndSwitchVersionOfExecution(commandContext, processInstance, newProcessDefinition);

  // switch the historic process instance to the new process definition version
  commandContext.getHistoryManager().recordProcessDefinitionChange(processInstanceId, newProcessDefinition.getId());

  // switch all sub-executions of the process instance to the new process definition version
  Collection<ExecutionEntity> childExecutions = executionManager.findChildExecutionsByProcessInstanceId(processInstanceId);
  for (ExecutionEntity executionEntity : childExecutions) {
    validateAndSwitchVersionOfExecution(commandContext, executionEntity, newProcessDefinition);
  }

  return null;
}
 
Example 8
Source File: SetProcessInstanceNameCmd.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {
  if (processInstanceId == null) {
    throw new ActivitiIllegalArgumentException("processInstanceId is null");
  }

  ExecutionEntity execution = commandContext.getExecutionEntityManager().findById(processInstanceId);

  if (execution == null) {
    throw new ActivitiObjectNotFoundException("process instance " + processInstanceId + " doesn't exist", ProcessInstance.class);
  }

  if (!execution.isProcessInstanceType()) {
    throw new ActivitiObjectNotFoundException("process instance " + processInstanceId + " doesn't exist, the given ID references an execution, though", ProcessInstance.class);
  }

  if (execution.isSuspended()) {
    throw new ActivitiException("process instance " + processInstanceId + " is suspended, cannot set name");
  }

  // Actually set the name
  execution.setName(name);

  // Record the change in history
  commandContext.getHistoryManager().recordProcessInstanceNameChange(processInstanceId, name);

  return null;
}
 
Example 9
Source File: SetProcessInstanceNameCmd.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {
    if (processInstanceId == null) {
        throw new ActivitiIllegalArgumentException("processInstanceId is null");
    }

    ExecutionEntity execution = commandContext
            .getExecutionEntityManager()
            .findExecutionById(processInstanceId);

    if (execution == null) {
        throw new ActivitiObjectNotFoundException("process instance " + processInstanceId + " doesn't exist", ProcessInstance.class);
    }

    if (!execution.isProcessInstanceType()) {
        throw new ActivitiObjectNotFoundException("process instance " + processInstanceId +
                " doesn't exist, the given ID references an execution, though", ProcessInstance.class);
    }

    if (execution.isSuspended()) {
        throw new ActivitiException("process instance " + processInstanceId + " is suspended, cannot set name");
    }

    // Actually set the name
    execution.setName(name);

    // Record the change in history
    commandContext.getHistoryManager().recordProcessInstanceNameChange(processInstanceId, name);

    return null;
}
 
Example 10
Source File: EndExecutionOperation.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected void handleRegularExecution() {

    ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();

    // There will be a parent execution (or else we would be in the process instance handling method)
    ExecutionEntity parentExecution = executionEntityManager.findById(execution.getParentId());

    // If the execution is a scope, all the child executions must be deleted first.
    if (execution.isScope()) {
      executionEntityManager.deleteChildExecutions(execution, null, false);
    }

    // Delete current execution
    logger.debug("Ending execution {}", execution.getId());
    executionEntityManager.deleteExecutionAndRelatedData(execution, null, false);

    logger.debug("Parent execution found. Continuing process using execution {}", parentExecution.getId());

    // When ending an execution in a multi instance subprocess , special care is needed
    if (isEndEventInMultiInstanceSubprocess(execution)) {
        handleMultiInstanceSubProcess(executionEntityManager, parentExecution);
        return;
    }

    SubProcess subProcess = execution.getCurrentFlowElement().getSubProcess();

    // If there are no more active child executions, the process can be continued
    // If not (eg an embedded subprocess still has active elements, we cannot continue)
    if (getNumberOfActiveChildExecutionsForExecution(executionEntityManager, parentExecution.getId()) == 0
        || isAllEventScopeExecutions(executionEntityManager, parentExecution)) {

      ExecutionEntity executionToContinue = null;

      if (subProcess != null) {

        // In case of ending a subprocess: go up in the scopes and continue via the parent scope
        // unless its a compensation, then we don't need to do anything and can just end it

        if (subProcess.isForCompensation()) {
          Context.getAgenda().planEndExecutionOperation(parentExecution);
        } else {
          executionToContinue = handleSubProcessEnd(executionEntityManager, parentExecution, subProcess);
        }

      } else {

        // In the 'regular' case (not being in a subprocess), we use the parent execution to
        // continue process instance execution

        executionToContinue = handleRegularExecutionEnd(executionEntityManager, parentExecution);
      }

      if (executionToContinue != null) {
        // only continue with outgoing sequence flows if the execution is
        // not the process instance root execution (otherwise the process instance is finished)
        if (executionToContinue.isProcessInstanceType()) {
          handleProcessInstanceExecution(executionToContinue);

        } else {
          Context.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionToContinue, true);
        }
      }

    }
  }
 
Example 11
Source File: AbstractSetProcessInstanceStateCmd.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public Void execute(CommandContext commandContext) {

    if (processInstanceId == null) {
      throw new ActivitiIllegalArgumentException("ProcessInstanceId cannot be null.");
    }

    ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(processInstanceId);

    if (executionEntity == null) {
      throw new ActivitiObjectNotFoundException("Cannot find processInstance for id '" + processInstanceId + "'.", Execution.class);
    }
    if (!executionEntity.isProcessInstanceType()) {
      throw new ActivitiException("Cannot set suspension state for execution '" + processInstanceId + "': not a process instance.");
    }
    
    if (Activiti5Util.isActiviti5ProcessDefinitionId(commandContext, executionEntity.getProcessDefinitionId())) {
      if (getNewState() == SuspensionState.ACTIVE) {
        commandContext.getProcessEngineConfiguration().getActiviti5CompatibilityHandler().activateProcessInstance(processInstanceId);
      } else {
        commandContext.getProcessEngineConfiguration().getActiviti5CompatibilityHandler().suspendProcessInstance(processInstanceId);
      }
      return null;
    }

    SuspensionStateUtil.setSuspensionState(executionEntity, getNewState());
    commandContext.getExecutionEntityManager().update(executionEntity, false);

    // All child executions are suspended
    Collection<ExecutionEntity> childExecutions = commandContext.getExecutionEntityManager().findChildExecutionsByProcessInstanceId(processInstanceId);
    for (ExecutionEntity childExecution : childExecutions) {
      if (!childExecution.getId().equals(processInstanceId)) {
        SuspensionStateUtil.setSuspensionState(childExecution, getNewState());
        commandContext.getExecutionEntityManager().update(childExecution, false);
      }
    }

    // All tasks are suspended
    List<TaskEntity> tasks = commandContext.getTaskEntityManager().findTasksByProcessInstanceId(processInstanceId);
    for (TaskEntity taskEntity : tasks) {
      SuspensionStateUtil.setSuspensionState(taskEntity, getNewState());
      commandContext.getTaskEntityManager().update(taskEntity, false);
    }
    
    // All jobs are suspended
    if (getNewState() == SuspensionState.ACTIVE) {
      List<SuspendedJobEntity> suspendedJobs = commandContext.getSuspendedJobEntityManager().findJobsByProcessInstanceId(processInstanceId);
      for (SuspendedJobEntity suspendedJob : suspendedJobs) {
        commandContext.getJobManager().activateSuspendedJob(suspendedJob);
      }
      
    } else {
      List<TimerJobEntity> timerJobs = commandContext.getTimerJobEntityManager().findJobsByProcessInstanceId(processInstanceId);
      for (TimerJobEntity timerJob : timerJobs) {
        commandContext.getJobManager().moveJobToSuspendedJob(timerJob);
      }
      
      List<JobEntity> jobs = commandContext.getJobEntityManager().findJobsByProcessInstanceId(processInstanceId);
      for (JobEntity job : jobs) {
        commandContext.getJobManager().moveJobToSuspendedJob(job);
      }
    }

    return null;
  }
 
Example 12
Source File: BoundaryCompensateEventActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(DelegateExecution execution) {
  ExecutionEntity executionEntity = (ExecutionEntity) execution;
  BoundaryEvent boundaryEvent = (BoundaryEvent) execution.getCurrentFlowElement();
  
  Process process = ProcessDefinitionUtil.getProcess(execution.getProcessDefinitionId());
  if (process == null) {
    throw new ActivitiException("Process model (id = " + execution.getId() + ") could not be found");
  }
  
  Activity compensationActivity = null;
  List<Association> associations = process.findAssociationsWithSourceRefRecursive(boundaryEvent.getId());
  for (Association association : associations) {
    FlowElement targetElement = process.getFlowElement(association.getTargetRef(), true);
    if (targetElement instanceof Activity) {
      Activity activity = (Activity) targetElement;
      if (activity.isForCompensation()) {
        compensationActivity = activity;
        break;
      }
    }
  }
  
  if (compensationActivity == null) {
    throw new ActivitiException("Compensation activity could not be found (or it is missing 'isForCompensation=\"true\"'");
  }
  
  // find SubProcess or Process instance execution
  ExecutionEntity scopeExecution = null;
  ExecutionEntity parentExecution = executionEntity.getParent();
  while (scopeExecution == null && parentExecution != null) {
    if (parentExecution.getCurrentFlowElement() instanceof SubProcess) {
      scopeExecution = parentExecution;
      
    } else if (parentExecution.isProcessInstanceType()) {
      scopeExecution = parentExecution;
    } else {
      parentExecution = parentExecution.getParent();
    }
  }
  
  if (scopeExecution == null) {
    throw new ActivitiException("Could not find a scope execution for compensation boundary event " + boundaryEvent.getId());
  }
  
  Context.getCommandContext().getEventSubscriptionEntityManager().insertCompensationEvent(
      scopeExecution, compensationActivity.getId());
}
 
Example 13
Source File: DefaultHistoryManager.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public HistoricActivityInstanceEntity findActivityInstance(ExecutionEntity execution, String activityId, boolean createOnNotFound, boolean endTimeMustBeNull) {
  
  // No use looking for the HistoricActivityInstance when no activityId is provided.
  if (activityId == null) {
    return null;
  }
  
  String executionId = execution.getId();

  // Check the cache
  HistoricActivityInstanceEntity historicActivityInstanceEntityFromCache = 
      getHistoricActivityInstanceFromCache(executionId, activityId, endTimeMustBeNull);
  if (historicActivityInstanceEntityFromCache != null) {
    return historicActivityInstanceEntityFromCache;
  }
  
  // If the execution was freshly created, there is no need to check the database, 
  // there can never be an entry for a historic activity instance with this execution id.
  if (!execution.isInserted() && !execution.isProcessInstanceType()) {

    // Check the database
    List<HistoricActivityInstanceEntity> historicActivityInstances = getHistoricActivityInstanceEntityManager()
        .findUnfinishedHistoricActivityInstancesByExecutionAndActivityId(executionId, activityId); 

    if (historicActivityInstances.size() > 0) {
      return historicActivityInstances.get(0);
    }
    
  }
  
  if (execution.getParentId() != null) {
    HistoricActivityInstanceEntity historicActivityInstanceFromParent 
      = findActivityInstance((ExecutionEntity) execution.getParent(), activityId, false, endTimeMustBeNull); // always false for create, we only check if it can be found
    if (historicActivityInstanceFromParent != null) {
      return historicActivityInstanceFromParent;
    }
  }
  
  if (createOnNotFound 
      && activityId != null
      && ( (execution.getCurrentFlowElement() != null && execution.getCurrentFlowElement() instanceof FlowNode) || execution.getCurrentFlowElement() == null)) {
    return createHistoricActivityInstanceEntity(execution);
  }

  return null;
}
 
Example 14
Source File: UpdateProcessInstanceNameEventListener.java    From lemon with Apache License 2.0 4 votes vote down vote up
protected void onInitialized(ActivitiEvent event) {
    if (!(event instanceof ActivitiEntityEventImpl)) {
        return;
    }

    ActivitiEntityEventImpl activitiEntityEventImpl = (ActivitiEntityEventImpl) event;
    Object entity = activitiEntityEventImpl.getEntity();

    if (!(entity instanceof ExecutionEntity)) {
        return;
    }

    ActivitiEventType activitiEventType = activitiEntityEventImpl.getType();

    if (activitiEventType != ActivitiEventType.ENTITY_INITIALIZED) {
        return;
    }

    ExecutionEntity executionEntity = (ExecutionEntity) entity;

    if (!executionEntity.isProcessInstanceType()) {
        return;
    }

    String processInstanceId = executionEntity.getId();
    String processDefinitionId = executionEntity.getProcessDefinitionId();
    CommandContext commandContext = Context.getCommandContext();
    ProcessDefinitionEntity processDefinition = new GetDeploymentProcessDefinitionCmd(
            processDefinitionId).execute(commandContext);

    // {流程标题:title}-{发起人:startUser}-{发起时间:startTime}
    String processDefinitionName = processDefinition.getName();

    if (processDefinitionName == null) {
        processDefinitionName = processDefinition.getKey();
    }

    String userId = Authentication.getAuthenticatedUserId();
    String displayName = userConnector.findById(userId).getDisplayName();
    String processInstanceName = processDefinitionName + "-" + displayName
            + "-"
            + new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
    // runtime
    executionEntity.setName(processInstanceName);

    // history
    HistoricProcessInstanceEntity historicProcessInstanceEntity = commandContext
            .getHistoricProcessInstanceEntityManager()
            .findHistoricProcessInstance(processInstanceId);
    historicProcessInstanceEntity.setName(processInstanceName);
}