All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.runtime.XpdlInstance Maven / Gradle / Ivy

package org.ow2.bonita.runtime;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.ow2.bonita.definition.XpdlProcess;
import org.ow2.bonita.facade.runtime.InstanceState;
import org.ow2.bonita.facade.uuid.PackageDefinitionUUID;
import org.ow2.bonita.facade.uuid.ProcessDefinitionUUID;
import org.ow2.bonita.facade.uuid.ProcessInstanceUUID;
import org.ow2.bonita.pvm.Execution;
import org.ow2.bonita.pvm.model.Transition;
import org.ow2.bonita.runtime.TransitionStatus.TransitionState;
import org.ow2.bonita.services.Repository;
import org.ow2.bonita.services.handlers.FinishedInstanceHandler;
import org.ow2.bonita.services.info.ProcessInstanceCurrentInfo;
import org.ow2.bonita.services.util.ServiceEnvTool;
import org.ow2.bonita.util.EngineEnvTool;
import org.ow2.bonita.util.Misc;


public class XpdlInstance {

  protected InstanceState instanceState;
  //processInstanceLevel
  protected ProcessInstanceUUID uuid;
  protected long dbid;
  protected ProcessDefinitionUUID processUUID;
  protected PackageDefinitionUUID packageUUID;
  protected XpdlExecution rootExecution;

  protected XpdlExecution executionToSignal;
  protected Set childInstances;
  protected XpdlInstance parentInstance;

  protected Set transitionStatusSet =  new HashSet();

  protected XpdlInstance() { }

  public XpdlInstance(final XpdlProcess xpdlProcess) {
    this.processUUID = xpdlProcess.getUUID();
    this.packageUUID = xpdlProcess.getPackageDefinitionUUID();
    this.uuid = ServiceEnvTool.getUUIDGenerator().getProcessInstanceUUID(this.processUUID);

    this.rootExecution = new XpdlExecution();
    this.rootExecution.xpdlInstance = this;
    // TODO: process is set in startExecution too, see how we can remove this.
    // (process is needed to assign instance name from the instance repository)
    this.rootExecution.setProcessDefinition(xpdlProcess);

    final Repository repository = EngineEnvTool.getRepository();
    repository.storeXpdlInstance(this);
    this.rootExecution.setName(this.uuid.toString());
  }

  public void setInstanceState(final InstanceState state) {
    this.instanceState = state;
  }

  public boolean isInstanceState(final InstanceState state) {
    return this.instanceState.equals(state);
  }

  public TransitionStatus getTransitionStatus(final Transition transition) {
    for (final TransitionStatus ts : this.transitionStatusSet) {
      if (transition.equals(ts.getTransition())) {
        return ts;
      }
    }
    // create transition status if does not exist
    final TransitionStatus transitionStatus = new TransitionStatus(transition);
    this.transitionStatusSet.add(transitionStatus);
    return transitionStatus;
  }

  public void setTransitionState(final Transition transition, final TransitionState state) {
    final TransitionStatus transitionStatus = this.getTransitionStatus(transition);
    if (transitionStatus == null) {
      Misc.unreachableStatement();
    }
    transitionStatus.setState(state);
  }

  public XpdlExecution getRootExecution() {
    return this.rootExecution;
  }

  public InstanceState getInstanceState() {
    return this.instanceState;
  }

  public ProcessInstanceUUID getUUID() {
    return this.uuid;
  }


  public long getDbid() {
    return this.dbid;
  }


  public ProcessDefinitionUUID getProcessDefinitionUUID() {
    return this.processUUID;
  }

  /**
   * @param execution
   */
  public void setExecutionToSignal(final XpdlExecution execution) {
    this.executionToSignal = execution;
    if (execution != null) {
      // executionToSignal is set to null when the subflow is ended.
      // not setting it to null can create a bug, because the executionTosignal
      // is removed from the db, but still referenced in the subflow instance.
      //
      // When executionToSignal is set to null,
      // the parent process instance has to be kept.
      // (to delete the subflow instance with the main instance)
      this.parentInstance = execution.getXpdlInstance();
      this.parentInstance.addChildInstance(this);
    }
  }

  public XpdlExecution getExecutionToSignal() {
    return this.executionToSignal;
  }

  private void addChildInstance(final XpdlInstance instance) {
    if (this.childInstances == null) {
      this.childInstances = new HashSet();
    }
    this.childInstances.add(instance);
  }

  @Override
  public String toString() {
    String value = "Instance " + this.uuid + "(state:" + this.instanceState;
    if (this.parentInstance != null) {
      value += ", child of " + this.parentInstance;
    }
    value += ")";
    return value;
  }


  public Set getChildInstances() {
    if (this.childInstances == null) {
      return null;
    }
    return Collections.unmodifiableSet(this.childInstances);
  }

  public XpdlInstance getParentInstance() {
    return this.parentInstance;
  }

  public PackageDefinitionUUID getPackageDefinitionUUID() {
    return this.packageUUID;
  }

  public List getExecOnNode(final String nodeName) {
    return this.getExecOnNode(this.getRootExecution(), nodeName);
  }

  private List getExecOnNode(
      final XpdlExecution exec,
      final String nodeName) {
    Misc.checkArgsNotNull(exec, nodeName);
    final List res = new ArrayList();
    if (exec.getExecutions() == null || exec.getExecutions().isEmpty()) {
      if (exec.getNode() != null && exec.getNode().getName().equals(nodeName)) {
        res.add(exec);
      }
    }
    if (exec.getExecutions() != null) {
      for (final Execution child : exec.getExecutions()) {
        res.addAll(this.getExecOnNode((XpdlExecution) child, nodeName));
      }
    }
    return res;
  }

  public void cancel() {
    // cancel execution
    this.getRootExecution().cancel();

    // record cancel
    ServiceEnvTool.getRecorder().recordInstanceCancelled(
      new ProcessInstanceCurrentInfo(
        this.getPackageDefinitionUUID(),
        this.getProcessDefinitionUUID(),
        this.getUUID(),
        this.getParentInstance() != null ? this.getParentInstance().getUUID() : null,
        this.getRootExecution().getVariables(),
        ServiceEnvTool.getUserId())
    );

    // execute finished instance handler
    if (this.getParentInstance() == null) {
      this.finish();
    }
  }

  public void finish() {
    final FinishedInstanceHandler handler = ServiceEnvTool.getFinishedInstanceHandler();
    handler.handleFinishedInstance(this.getUUID());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy