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

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

/**
 * Copyright (C) 2007  Bull S. A. S.
 * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 **/
package org.ow2.bonita.runtime;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ow2.bonita.definition.XpdlProcess;
import org.ow2.bonita.definition.activity.AbstractActivity;
import org.ow2.bonita.facade.runtime.InstanceState;
import org.ow2.bonita.facade.uuid.ActivityInstanceUUID;
import org.ow2.bonita.pvm.Execution;
import org.ow2.bonita.pvm.internal.model.ExecutionImpl;
import org.ow2.bonita.pvm.internal.type.Variable;
import org.ow2.bonita.pvm.model.OpenExecution;
import org.ow2.bonita.services.Recorder;
import org.ow2.bonita.services.info.ActivityInstanceCurrentInfo;
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;

/**
 * @author Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes, Pierre Vigneras
 */

/**
 *  Variables in XpdlExecution are handled this way:
 *  - Process (global) variables are all in one VariableScope. This variable Scope always exists,
 *  even if it is empty.
 *  - Activity (local) variables are in a second variableScope. This variable scope only exists
 *  when there are local variables.
 *
 *  Methods getLocalVariables and getGlobalVariables are provided to have access to global and
 *  local variables of this execution.
 */
public class XpdlExecution extends ExecutionImpl {

  private static final long serialVersionUID = 1L;

  private static final Logger LOG = Logger.getLogger(XpdlExecution.class.getName());

  public static final String MAIN_INSTANCE_NAME = "mainInstance";
  protected XpdlInstance xpdlInstance;

  protected XpdlInstance subflowXpdlInstance;

  protected String iterationId;
  protected String activityInstanceId = MAIN_INSTANCE_NAME;
  protected int waitingForActivityInstanceNb;


  protected ActivityInstanceUUID currentActivityInstanceUUID = null;

  // mandatory for persistence
  protected XpdlExecution() {
    super();
  }

  @Override
  public XpdlExecution getParent() {
    return (XpdlExecution) this.parent;
  }

  @Override
  protected XpdlExecution newChildExecution() {
    final XpdlExecution child = new XpdlExecution();
    child.xpdlInstance = this.getXpdlInstance();
    child.iterationId = this.getIterationId();
    return child;
  }

  @Override
  public XpdlExecution getProcessInstance() {
    return (XpdlExecution) super.getProcessInstance();
  }

  @Override
  public XpdlProcess getProcessDefinition() {
    return (XpdlProcess) this.processDefinition;
  }

  @Override
  public void begin() {
    final XpdlInstance instance = getXpdlInstance();
    instance.setInstanceState(InstanceState.INITIAL);
    setIterationId(Misc.getUniqueId("it"));
    super.begin();
  }

  /* ITERATORS */

  public XpdlInstance getXpdlInstance() {
    return this.xpdlInstance;
  }

  public String getIterationId() {
    return this.iterationId;
  }

  public void setIterationId(final String iterationId) {
    this.iterationId = iterationId;
  }

  @SuppressWarnings("unchecked")
  public Map getScopeVariables() {
    if (this.variables == null) {
      return Collections.EMPTY_MAP;
    }
    final Map values = new HashMap();
    for (final Map.Entry entry : this.variables.entrySet()) {
      final String name = entry.getKey();
      final Variable variable = entry.getValue();
      final Object value = variable.getValue();
      values.put(name, value);
    }
    return values;
  }

  public String getActivityInstanceId() {
    return this.activityInstanceId;
  }
  public void setActivityInstanceId(final String activityInstanceId) {
    this.activityInstanceId = activityInstanceId;
  }
  public int getWaitingForActivityInstanceNb() {
    return this.waitingForActivityInstanceNb;
  }
  public void setWaitingForActivityInstanceNb(final int waitingFor) {
    this.waitingForActivityInstanceNb = waitingFor;
  }
  public ActivityInstanceUUID getCurrentActivityInstanceUUID() {
    return this.currentActivityInstanceUUID;
  }
  public void setCurrentActivityInstanceUUID(final ActivityInstanceUUID currentActivityInstanceUUID) {
    this.currentActivityInstanceUUID = currentActivityInstanceUUID;
  }


  public XpdlInstance getSubflowXpdlInstance() {
    return this.subflowXpdlInstance;
  }


  public void setSubflowXpdlInstance(final XpdlInstance subflowXpdlInstance) {
    this.subflowXpdlInstance = subflowXpdlInstance;
  }

  public void abort() {
    if (this.getExecutions() != null) {
      for (final OpenExecution child : new ArrayList(this.getExecutions())) {
        ((XpdlExecution) child).abort();
      }
    }
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine(this + " aborted.");
    }

    if (this.getCurrentActivityInstanceUUID() != null) {
      final AbstractActivity activity = (AbstractActivity) this.getNode().getBehaviour();

      switch (activity.getActivityType()) {
        case task:
          final TaskRunTime taskToAbort = EngineEnvTool.getRepository().getTask(this);
          if (taskToAbort != null) {
            taskToAbort.abort();
          }
          break;
        case subFlow:
          final XpdlInstance childInstance = this.getSubflowXpdlInstance();
          childInstance.getRootExecution().abort();
          childInstance.setInstanceState(InstanceState.ABORTED);
          childInstance.setExecutionToSignal(null);
          final Recorder recorder = ServiceEnvTool.getRecorder();

          recorder.recordInstanceAborted(new ProcessInstanceCurrentInfo(
              childInstance.getPackageDefinitionUUID(),
              childInstance.getProcessDefinitionUUID(),
              childInstance.getUUID(),
              childInstance.getParentInstance().getUUID(),
              childInstance.getRootExecution().getVariables(),
              ServiceEnvTool.getUserId())
          );
          break;
        default:
      }
      final ActivityInstanceCurrentInfo activityInstanceCurrentInfo = new ActivityInstanceCurrentInfo(
          this.getXpdlInstance().getPackageDefinitionUUID(),
          this.getXpdlInstance().getProcessDefinitionUUID(),
          this.getXpdlInstance().getUUID(),
          this.getCurrentActivityInstanceUUID(),
          activity.getActivityType(),
          activity.getActivityId(),
          this.getIterationId(),
          this.getActivityInstanceId(),
          this.getScopeVariables());

      ServiceEnvTool.getRecorder().recordBodyAborted(activityInstanceCurrentInfo);
    }

    end(Execution.STATE_CANCELLED);
    final XpdlExecution parent = this.getParent();
    if (parent != null) {
      parent.removeExecution(this);
    }
  }

  @Override
  public void cancel() {
    if (this.getExecutions() != null) {
      for (final OpenExecution child : new ArrayList(this.getExecutions())) {
        ((XpdlExecution) child).cancel();
      }
    }
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine(this + " cancelled.");
    }

    if (this.getCurrentActivityInstanceUUID() != null) {
      final AbstractActivity activity = (AbstractActivity) this.getNode().getBehaviour();

      switch (activity.getActivityType()) {
        case task:
          final TaskRunTime taskToCancel = EngineEnvTool.getRepository().getTask(this);
          if (taskToCancel != null) {
            taskToCancel.cancel();
          }
          break;
        case subFlow:
          final XpdlInstance childInstance = this.getSubflowXpdlInstance();
          childInstance.cancel();
          break;
        default:
      }
      final ActivityInstanceCurrentInfo activityInstanceCurrentInfo = new ActivityInstanceCurrentInfo(
          this.getXpdlInstance().getPackageDefinitionUUID(),
          this.getXpdlInstance().getProcessDefinitionUUID(),
          this.getXpdlInstance().getUUID(),
          this.getCurrentActivityInstanceUUID(),
          activity.getActivityType(),
          activity.getActivityId(),
          this.getIterationId(),
          this.getActivityInstanceId(),
          this.getScopeVariables());

      ServiceEnvTool.getRecorder().recordBodyCancelled(activityInstanceCurrentInfo);
    }

    end(Execution.STATE_CANCELLED);
    final XpdlExecution parent = this.getParent();
    if (parent != null) {
      parent.removeExecution(this);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy