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

org.ow2.bonita.definition.activity.SubFlow Maven / Gradle / Ivy

/**
 * Copyright (C) 2006  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.definition.activity;

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

import org.ow2.bonita.pvm.Execution;
import org.ow2.bonita.pvm.activity.ActivityExecution;
import org.ow2.bonita.definition.ActivityType;
import org.ow2.bonita.definition.Performer;
import org.ow2.bonita.definition.XpdlProcess;
import org.ow2.bonita.facade.uuid.ProcessInstanceUUID;
import org.ow2.bonita.runtime.XpdlExecution;
import org.ow2.bonita.runtime.XpdlInstance;
import org.ow2.bonita.services.Recorder;
import org.ow2.bonita.services.Repository;
import org.ow2.bonita.util.BonitaRuntimeException;
import org.ow2.bonita.util.EngineEnvTool;

/**
 * @author Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes, Pierre Vigneras
 */
public class SubFlow extends AbstractActivity {

  /**
   * 
   */
  private static final long serialVersionUID = 477565487347215726L;

  protected static final Logger LOG = Logger.getLogger(SubFlow.class.getName());

  public static final String SUBFLOW_SIGNAL = "end_of_subflow";
  protected String subProcessId;
  protected boolean isAsync;
  // MAP: key = parameter name, value = name of the variable that contains the value.
  protected Map inParameters;
  protected Map outParameters;

  protected  SubFlow() {
    super();
  }

  public SubFlow(final String activityName, final JoinType joinType,
      final SplitType splitType,
      final Performer performer,
      final String subProcessId,
      final boolean isAsync,
      final Map inParameters,
      final Map outParameters) {

    super(activityName, joinType,
        splitType,
        ActivityType.subFlow,
        performer
        );
    this.subProcessId = subProcessId;
    this.isAsync = isAsync;
    this.inParameters = inParameters;
    this.outParameters = outParameters;
  }

  @Override
  protected boolean bodyStartAutomatically() {
    return true;
  }

  @Override
  protected boolean executeBusinessLogic(final Execution execution) {
    final XpdlExecution xpdlExecution = (XpdlExecution) execution;

    final Repository repository = EngineEnvTool.getRepository();
    final XpdlProcess process = repository.findLatestProcessById(this.subProcessId);
    if (process == null) {
      throw new BonitaRuntimeException("SubFlow : process not found " + this.subProcessId);
    }
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine("Starting a new instance of process (as a subProcess) : " + this.subProcessId);
    }
    final Map parameterValues = new HashMap();
    if (this.inParameters != null) {
      for (final Map.Entry parameter : this.inParameters.entrySet()) {
        parameterValues.put(parameter.getKey(), xpdlExecution.getVariable(parameter.getValue()));
      }
    }
    final XpdlExecution rootExecution = (XpdlExecution) process.beginProcessInstance();
    rootExecution.setVariables(parameterValues);
    final XpdlInstance instance = rootExecution.getXpdlInstance();

    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine("Started subprocess instance : " + instance);
    }
    if (!this.isAsync) {
      instance.setExecutionToSignal(xpdlExecution);
      xpdlExecution.setSubflowXpdlInstance(instance);
    }
    rootExecution.signal();
    if (this.isAsync) {
      return true;
    }
    return false;
  }

  @Override
  public void signal(final ActivityExecution execution, final String signal, final Map signalParameters) {
    final XpdlExecution xpdlExecution = (XpdlExecution) execution;
    xpdlExecution.setSubflowXpdlInstance(null);
    if (SUBFLOW_SIGNAL.equals(signal)) {
      if (this.outParameters != null) {
        final Recorder recorder = EngineEnvTool.getRecorder();
        final ProcessInstanceUUID instanceUUID = xpdlExecution.getXpdlInstance().getUUID();

        for (final Map.Entry parameter : this.outParameters.entrySet()) {
          final String variableId = parameter.getValue();
          final Object variableValue = signalParameters.get(parameter.getKey());
          execution.setVariable(variableId, variableValue);

          if (xpdlExecution.getXpdlInstance().getRootExecution().hasVariable(variableId)) {
            recorder.recordInstanceVariableUpdated(variableId, variableValue, instanceUUID, EngineEnvTool.getUserId());
          } else {
            recorder.recordActivityVariableUpdated(variableId, variableValue, xpdlExecution.getCurrentActivityInstanceUUID(), EngineEnvTool.getUserId());
          }
        }
      }
      super.signal(xpdlExecution, BODY_FINISHED, null);
    } else {
      super.signal(xpdlExecution, signal, signalParameters);
    }
  }

}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy