ChangeDeploymentTenantIdCmd.java 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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.activiti.engine.impl.cmd;

import java.io.Serializable;
import java.util.List;

import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ActivitiObjectNotFoundException;
20
import org.activiti.engine.compatibility.Activiti5CompatibilityHandler;
21 22 23 24 25 26 27 28 29 30 31
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.DeploymentEntity;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;

/**
 * @author Joram Barrez
 */
public class ChangeDeploymentTenantIdCmd implements Command<Void>, Serializable {

32
  private static final long serialVersionUID = 1L;
T
Tijs Rademakers 已提交
33

34 35
  protected String deploymentId;
  protected String newTenantId;
T
Tijs Rademakers 已提交
36

37 38 39 40
  public ChangeDeploymentTenantIdCmd(String deploymentId, String newTenantId) {
    this.deploymentId = deploymentId;
    this.newTenantId = newTenantId;
  }
T
Tijs Rademakers 已提交
41

42 43 44 45
  public Void execute(CommandContext commandContext) {
    if (deploymentId == null) {
      throw new ActivitiIllegalArgumentException("deploymentId is null");
    }
T
Tijs Rademakers 已提交
46

47
    // Update all entities
T
Tijs Rademakers 已提交
48

49
    DeploymentEntity deployment = commandContext.getDeploymentEntityManager().findById(deploymentId);
50 51 52
    if (deployment == null) {
      throw new ActivitiObjectNotFoundException("Could not find deployment with id " + deploymentId, Deployment.class);
    }
53 54 55 56 57 58 59 60
    
    if (commandContext.getProcessEngineConfiguration().isActiviti5CompatibilityEnabled() && 
        Activiti5CompatibilityHandler.ACTIVITI_5_ENGINE_TAG.equals(deployment.getEngineVersion())) {
      
      commandContext.getProcessEngineConfiguration().getActiviti5CompatibilityHandler().changeDeploymentTenantId(deploymentId, newTenantId);
      return null;
    }
    
61 62
    String oldTenantId = deployment.getTenantId();
    deployment.setTenantId(newTenantId);
T
Tijs Rademakers 已提交
63

64 65 66 67 68 69 70
    // Doing process instances, executions and tasks with direct SQL updates
    // (otherwise would not be performant)
    commandContext.getProcessDefinitionEntityManager().updateProcessDefinitionTenantIdForDeployment(deploymentId, newTenantId);
    commandContext.getExecutionEntityManager().updateExecutionTenantIdForDeployment(deploymentId, newTenantId);
    commandContext.getTaskEntityManager().updateTaskTenantIdForDeployment(deploymentId, newTenantId);
    commandContext.getJobEntityManager().updateJobTenantIdForDeployment(deploymentId, newTenantId);
    commandContext.getEventSubscriptionEntityManager().updateEventSubscriptionTenantId(oldTenantId, newTenantId);
T
Tijs Rademakers 已提交
71

72 73 74 75 76
    // Doing process definitions in memory, cause we need to clear the process definition cache
    List<ProcessDefinition> processDefinitions = commandContext.getDbSqlSession().createProcessDefinitionQuery().deploymentId(deploymentId).list();
    for (ProcessDefinition processDefinition : processDefinitions) {
      commandContext.getProcessEngineConfiguration().getProcessDefinitionCache().remove(processDefinition.getId());
    }
T
Tijs Rademakers 已提交
77

78 79
    // Clear process definition cache
    commandContext.getProcessEngineConfiguration().getProcessDefinitionCache().clear();
T
Tijs Rademakers 已提交
80

81
    return null;
T
Tijs Rademakers 已提交
82

83
  }
84 85

}