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.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ow2.bonita.definition.InternalProcess;
import org.ow2.bonita.facade.def.majorElement.ActivityDefinition;
import org.ow2.bonita.facade.def.majorElement.ProcessDefinition;
import org.ow2.bonita.facade.exception.ProcessNotFoundException;
import org.ow2.bonita.facade.runtime.ActivityInstance;
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.activity.ActivityExecution;
import org.ow2.bonita.pvm.internal.type.Variable;
import org.ow2.bonita.runtime.InternalExecution;
import org.ow2.bonita.runtime.InternalInstance;
import org.ow2.bonita.services.Recorder;
import org.ow2.bonita.services.Repository;
import org.ow2.bonita.util.BonitaRuntimeException;
import org.ow2.bonita.util.EnvTool;
import org.ow2.bonita.util.ExceptionManager;
import org.ow2.bonita.util.ProcessUtil;

/**
 * @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  SubFlow() {
    super();
  }

  public SubFlow(ActivityDefinition activityDef) {
    super(activityDef);
  }

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

  @Override
  protected boolean executeBusinessLogic(final Execution execution) {
    final InternalExecution internalExecution = (InternalExecution) execution;

    final Repository repository = EnvTool.getRepository();
    
    String subflowProcessName = this.activityDef.getSubflowProcessName();
    final InternalProcess subProcess = repository.findLatestProcessByName(subflowProcessName);
    if (subProcess == null) {
    	String message = ExceptionManager.getInstance().getFullMessage(
    			"be_SF_1", subflowProcessName);
      throw new BonitaRuntimeException(message);
    }
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine("Starting a new instance of process (as a subProcess) : " + subflowProcessName);
    }
    
    final InternalInstance instance = ((InternalExecution)execution).getInstance();
    final ProcessInstanceUUID instanceUUID = instance.getUUID();
    final ProcessDefinitionUUID processUUID = instanceUUID.getProcessDefinitionUUID();
    final ProcessDefinition processDef = EnvTool.getJournalQueriers().getProcess(processUUID);
    
    Map activityVariables = ProcessUtil.createVariables(activityDef.getDataFields());
    
    Map processVariables = ProcessUtil.createVariables(processDef.getDataFields());
    
    Map allVariables = new HashMap();
    if (processVariables != null) {
    	allVariables.putAll(processVariables);
    }
    if (activityVariables != null) {
    	allVariables.putAll(activityVariables);
    }
    
    
    final Map parameterValues = new HashMap();
    Map> inParameters = this.activityDef.getSubflowInParameters();
    if (inParameters != null) {
      for (final Map.Entry> parameter : inParameters.entrySet()) {
        //Object parentProcessVar = internalExecution.getVariable(parameter.getKey());
    	Object parentProcessVar = allVariables.get(parameter.getKey()).getValue();
        for (String subProcessVarName : parameter.getValue()) {
          parameterValues.put(subProcessVarName, parentProcessVar);
        }
      }
    }
    try {
    	ProcessDefinitionUUID subProcessUUID = subProcess.getUUID();
      InternalExecution newRootExecution = ProcessUtil.createProcessInstance(subProcessUUID, parameterValues, null, instanceUUID);

      if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Started subprocess instance : " + instance);
      }
      newRootExecution.getInstance().setExecutionToSignal(internalExecution);
      internalExecution.setSubflowInstance(newRootExecution.getInstance());
      newRootExecution.signal();
    } catch (ProcessNotFoundException e) {
      throw new BonitaRuntimeException(e);
    }
    return false;
  }

  @Override
  public void signal(final ActivityExecution execution, final String signal, final Map signalParameters) {
    final InternalExecution internalExecution = (InternalExecution) execution;
    internalExecution.setSubflowInstance(null);
    if (SUBFLOW_SIGNAL.equals(signal)) {
      Map> outParameters = this.activityDef.getSubflowOutParameters();
      if (outParameters != null) {
        final Recorder recorder = EnvTool.getRecorder();
        final ProcessInstanceUUID instanceUUID = internalExecution.getInstance().getUUID();
        
        for (final Map.Entry> parameter : outParameters.entrySet()) {
          final Object variableValue = signalParameters.get(parameter.getKey());
          for (String parentProcessVarName : parameter.getValue()) {
            //execution.setVariable(parentProcessVarName, variableValue);
            final ActivityInstance activityInstance = EnvTool.getJournalQueriers().getActivityInstance(internalExecution.getCurrentActivityInstanceUUID());
            if (activityInstance.getLastKnownVariableValues().containsKey(parentProcessVarName)) {
            	recorder.recordActivityVariableUpdated(parentProcessVarName, variableValue, internalExecution.getCurrentActivityInstanceUUID(), EnvTool.getUserId());
            } else {
            	recorder.recordInstanceVariableUpdated(parentProcessVarName, variableValue, instanceUUID, EnvTool.getUserId());
            }
            /*
            if (internalExecution.getInstance().getRootExecution().hasVariable(parentProcessVarName)) {
              recorder.recordInstanceVariableUpdated(parentProcessVarName, variableValue, instanceUUID, EnvTool.getUserId());
            } else {
              recorder.recordActivityVariableUpdated(parentProcessVarName, variableValue, internalExecution.getCurrentActivityInstanceUUID(), EnvTool.getUserId());
            }
            */
          }

          
        }
      }
      super.signal(internalExecution, BODY_FINISHED, null);
    } else {
      super.signal(internalExecution, signal, signalParameters);
    }
  }

}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy