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

org.camunda.bpm.engine.impl.cmmn.CaseExecutionCommandBuilderImpl Maven / Gradle / Ivy

There is a newer version: 7.23.0-alpha2
Show newest version
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.camunda.bpm.engine.impl.cmmn;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.camunda.bpm.engine.ProcessEngineException;
import org.camunda.bpm.engine.impl.cmmn.cmd.*;
import org.camunda.bpm.engine.impl.interceptor.Command;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
import org.camunda.bpm.engine.impl.interceptor.CommandExecutor;
import org.camunda.bpm.engine.runtime.CaseExecutionCommandBuilder;

import static org.camunda.bpm.engine.impl.util.EnsureUtil.ensureNotNull;

/**
 * @author Roman Smirnov
 *
 */
public class CaseExecutionCommandBuilderImpl implements CaseExecutionCommandBuilder {

  protected CommandExecutor commandExecutor;
  protected CommandContext commandContext;

  protected String caseExecutionId;
  protected Map variables;
  protected Map variablesLocal;
  protected Collection variableDeletions;
  protected Collection variableLocalDeletions;

  public CaseExecutionCommandBuilderImpl(CommandExecutor commandExecutor, String caseExecutionId) {
    this(caseExecutionId);
    ensureNotNull("commandExecutor", commandExecutor);
    this.commandExecutor = commandExecutor;
  }

  public CaseExecutionCommandBuilderImpl(CommandContext commandContext, String caseExecutionId) {
    this(caseExecutionId);
    ensureNotNull("commandContext", commandContext);
    this.commandContext = commandContext;
  }

  private CaseExecutionCommandBuilderImpl(String caseExecutionId) {
    this.caseExecutionId = caseExecutionId;
  }

  public CaseExecutionCommandBuilder setVariable(String variableName, Object variableValue) {
    ensureNotNull("variableName", variableName);
    ensureVariableShouldNotBeRemoved(variableName);
    if (variables == null) {
      variables = new HashMap();
    }
    variables.put(variableName, variableValue);
    return this;
  }

  public CaseExecutionCommandBuilder setVariables(Map variables) {
    if (variables != null) {
      ensureVariablesShouldNotBeRemoved(variables.keySet());
      if (this.variables == null) {
        this.variables = new HashMap();
      }
      this.variables.putAll(variables);
    }
    return this;
  }

  public CaseExecutionCommandBuilder setVariableLocal(String localVariableName, Object localVariableValue) {
    ensureNotNull("localVariableName", localVariableName);
    ensureVariableShouldNotBeRemoved(localVariableName);
    if (variablesLocal == null) {
      variablesLocal = new HashMap();
    }
    variablesLocal.put(localVariableName, localVariableValue);
    return this;
  }

  public CaseExecutionCommandBuilder setVariablesLocal(Map variablesLocal) {
    if (variablesLocal != null) {
      ensureVariablesShouldNotBeRemoved(variablesLocal.keySet());
      if (this.variablesLocal == null) {
        this.variablesLocal = new HashMap();
      }
      this.variablesLocal.putAll(variablesLocal);
    }
    return this;
  }

  public CaseExecutionCommandBuilder removeVariable(String variableName) {
    ensureVariableShouldNotBeSet(variableName);
    if (variableDeletions == null) {
      variableDeletions = new ArrayList();
    }
    variableDeletions.add(variableName);
    return this;
  }

  public CaseExecutionCommandBuilder removeVariables(Collection variableNames) {
    if (variableNames != null) {
      ensureVariablesShouldNotBeSet(variableNames);
      if (variableDeletions == null) {
        variableDeletions = new ArrayList();
      }
      variableDeletions.addAll(variableNames);
    }
    return this;
  }

  public CaseExecutionCommandBuilder removeVariableLocal(String variableName) {
    ensureVariableShouldNotBeSet(variableName);
    if (variableLocalDeletions == null) {
      variableLocalDeletions = new ArrayList();
    }
    variableLocalDeletions.add(variableName);
    return this;
  }

  public CaseExecutionCommandBuilder removeVariablesLocal(Collection variableNames) {
    if (variableNames != null) {
      ensureVariablesShouldNotBeSet(variableNames);
      if (variableLocalDeletions == null) {
        variableLocalDeletions = new ArrayList();
      }
      variableLocalDeletions.addAll(variableNames);
    }
    return this;
  }

  protected void ensureVariablesShouldNotBeRemoved(Collection variableNames) {
    for (String variableName : variableNames) {
      ensureVariableShouldNotBeRemoved(variableName);
    }
  }

  protected void ensureVariableShouldNotBeRemoved(String variableName) {
    if ((variableDeletions != null && variableDeletions.contains(variableName))
        || (variableLocalDeletions != null && variableLocalDeletions.contains(variableName))) {
      throw new ProcessEngineException("Cannot set and remove a variable with the same variable name: '"+variableName+"' within a command.");
    }
  }

  protected void ensureVariablesShouldNotBeSet(Collection variableNames) {
    for (String variableName : variableNames) {
      ensureVariableShouldNotBeSet(variableName);
    }
  }

  protected void ensureVariableShouldNotBeSet(String variableName) {
    if ((variables != null && variables.keySet().contains(variableName))
        || (variablesLocal != null && variablesLocal.keySet().contains(variableName))) {
      throw new ProcessEngineException("Cannot set and remove a variable with the same variable name: '"+variableName+"' within a command.");
    }
  }

  public void execute() {
    CaseExecutionVariableCmd command = new CaseExecutionVariableCmd(this);
    executeCommand(command);
  }

  public void manualStart() {
    ManualStartCaseExecutionCmd command = new ManualStartCaseExecutionCmd(this);
    executeCommand(command);
  }

  public void disable() {
    DisableCaseExecutionCmd command = new DisableCaseExecutionCmd(this);
    executeCommand(command);
  }

  public void reenable() {
    ReenableCaseExecutionCmd command = new ReenableCaseExecutionCmd(this);
    executeCommand(command);
  }

  public void complete() {
    CompleteCaseExecutionCmd command = new CompleteCaseExecutionCmd(this);
    executeCommand(command);
  }

  public void close() {
    CloseCaseInstanceCmd command = new CloseCaseInstanceCmd(this);
    executeCommand(command);
  }

  protected void executeCommand(Command command) {
    if(commandExecutor != null) {
      commandExecutor.execute(command);
    } else {
      command.execute(commandContext);
    }
  }

  // getters ////////////////////////////////////////////////////////////////////////////////

  public String getCaseExecutionId() {
    return caseExecutionId;
  }

  public Map getVariables() {
    return variables;
  }

  public Map getVariablesLocal() {
    return variablesLocal;
  }

  public Collection getVariableDeletions() {
    return variableDeletions;
  }

  public Collection getVariableLocalDeletions() {
    return variableLocalDeletions;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy