Package org.camunda.bpm.engine.impl.cmmn.cmd

Source Code of org.camunda.bpm.engine.impl.cmmn.cmd.CreateCaseInstanceCmd

/* 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.cmd;

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

import java.io.Serializable;
import java.util.Map;

import org.camunda.bpm.engine.exception.cmmn.CaseDefinitionNotFoundException;
import org.camunda.bpm.engine.impl.cmmn.CaseInstanceBuilderImpl;
import org.camunda.bpm.engine.impl.cmmn.entity.repository.CaseDefinitionEntity;
import org.camunda.bpm.engine.impl.cmmn.entity.runtime.CaseExecutionEntity;
import org.camunda.bpm.engine.impl.context.Context;
import org.camunda.bpm.engine.impl.interceptor.Command;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
import org.camunda.bpm.engine.impl.persistence.deploy.DeploymentCache;
import org.camunda.bpm.engine.runtime.CaseInstance;

/**
* @author Roman Smirnov
*
*/
public class CreateCaseInstanceCmd implements Command<CaseInstance>, Serializable {

  private static final long serialVersionUID = 1L;
  protected String caseDefinitionKey;
  protected String caseDefinitionId;
  protected Map<String, Object> variables;
  protected String businessKey;

  public CreateCaseInstanceCmd(CaseInstanceBuilderImpl builder) {
    this.caseDefinitionKey = builder.getCaseDefinitionKey();
    this.caseDefinitionId = builder.getCaseDefinitionId();
    this.businessKey = builder.getBusinessKey();
    this.variables = builder.getVariables();
  }

  public CaseInstance execute(CommandContext commandContext) {
    DeploymentCache deploymentCache = Context
        .getProcessEngineConfiguration()
        .getDeploymentCache();

    // Find the case definition
    CaseDefinitionEntity caseDefinition = null;

    ensureAtLeastOneNotNull("caseDefinition and caseDefinitionKey are null", caseDefinitionId, caseDefinitionKey);

    if (caseDefinitionId!=null) {
      caseDefinition = deploymentCache.findDeployedCaseDefinitionById(caseDefinitionId);

      ensureNotNull(CaseDefinitionNotFoundException.class, "No case definition found for id = '" + caseDefinitionId + "'", "caseDefinition", caseDefinition);

    } else {
      caseDefinition = deploymentCache.findDeployedLatestCaseDefinitionByKey(caseDefinitionKey);

      ensureNotNull(CaseDefinitionNotFoundException.class, "No case definition found for key '" + caseDefinitionKey + "'", "caseDefinition", caseDefinition);
    }

    // Start the case instance
    CaseExecutionEntity caseInstance = (CaseExecutionEntity) caseDefinition.createCaseInstance(businessKey);
    caseInstance.create(variables);
    return caseInstance;
  }

}
TOP

Related Classes of org.camunda.bpm.engine.impl.cmmn.cmd.CreateCaseInstanceCmd

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.