org.camunda.bpm.engine.impl.cmd.ActivityCancellationCmd Maven / Gradle / Ivy
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; 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.cmd;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.camunda.bpm.engine.impl.pvm.process.ScopeImpl;
import org.camunda.bpm.engine.runtime.ActivityInstance;
import org.camunda.bpm.engine.runtime.TransitionInstance;
/**
* @author Thorben Lindhauer
*
*/
public class ActivityCancellationCmd extends AbstractProcessInstanceModificationCommand {
protected String activityId;
protected boolean cancelCurrentActiveActivityInstances;
protected ActivityInstance activityInstanceTree;
public ActivityCancellationCmd(String activityId) {
this(null, activityId);
}
public ActivityCancellationCmd(String processInstanceId, String activityId) {
super(processInstanceId);
this.activityId = activityId;
}
@Override
public Void execute(final CommandContext commandContext) {
ActivityInstance activityInstanceTree = getActivityInstanceTree(commandContext);
List commands = createActivityInstanceCancellations(activityInstanceTree, commandContext);
for (AbstractInstanceCancellationCmd cmd : commands) {
cmd.setSkipCustomListeners(skipCustomListeners);
cmd.setSkipIoMappings(skipIoMappings);
cmd.setExternallyTerminated(true);
cmd.execute(commandContext);
}
return null;
}
protected Set collectParentScopeIdsForActivity(ProcessDefinitionImpl processDefinition, String activityId) {
Set parentScopeIds = new HashSet();
ScopeImpl scope = processDefinition.findActivity(activityId);
while (scope != null) {
parentScopeIds.add(scope.getId());
scope = scope.getFlowScope();
}
return parentScopeIds;
}
protected List getTransitionInstancesForActivity(ActivityInstance tree, Set parentScopeIds) {
// prune all search paths that are not in the scope hierarchy of the activity in question
if (!parentScopeIds.contains(tree.getActivityId())) {
return Collections.emptyList();
}
List instances = new ArrayList();
TransitionInstance[] transitionInstances = tree.getChildTransitionInstances();
for (TransitionInstance transitionInstance : transitionInstances) {
if (activityId.equals(transitionInstance.getActivityId())) {
instances.add(transitionInstance);
}
}
for (ActivityInstance child : tree.getChildActivityInstances()) {
instances.addAll(getTransitionInstancesForActivity(child, parentScopeIds));
}
return instances;
}
protected List getActivityInstancesForActivity(ActivityInstance tree, Set parentScopeIds) {
// prune all search paths that are not in the scope hierarchy of the activity in question
if (!parentScopeIds.contains(tree.getActivityId())) {
return Collections.emptyList();
}
List instances = new ArrayList();
if (activityId.equals(tree.getActivityId())) {
instances.add(tree);
}
for (ActivityInstance child : tree.getChildActivityInstances()) {
instances.addAll(getActivityInstancesForActivity(child, parentScopeIds));
}
return instances;
}
public ActivityInstance getActivityInstanceTree(final CommandContext commandContext) {
return commandContext.runWithoutAuthorization(new GetActivityInstanceCmd(processInstanceId));
}
public String getActivityId() {
return activityId;
}
public void setActivityInstanceTreeToCancel(ActivityInstance activityInstanceTreeToCancel) {
this.activityInstanceTree = activityInstanceTreeToCancel;
}
@Override
protected String describe() {
return "Cancel all instances of activity '" + activityId + "'";
}
public List createActivityInstanceCancellations(ActivityInstance activityInstanceTree, CommandContext commandContext) {
List commands = new ArrayList();
ExecutionEntity processInstance = commandContext.getExecutionManager().findExecutionById(processInstanceId);
ProcessDefinitionImpl processDefinition = processInstance.getProcessDefinition();
Set parentScopeIds = collectParentScopeIdsForActivity(processDefinition, activityId);
List childrenForActivity = getActivityInstancesForActivity(activityInstanceTree, parentScopeIds);
for (ActivityInstance instance : childrenForActivity) {
commands.add(new ActivityInstanceCancellationCmd(processInstanceId, instance.getId()));
}
List transitionInstancesForActivity = getTransitionInstancesForActivity(activityInstanceTree, parentScopeIds);
for (TransitionInstance instance : transitionInstancesForActivity) {
commands.add(new TransitionInstanceCancellationCmd(processInstanceId, instance.getId()));
}
return commands;
}
public boolean isCancelCurrentActiveActivityInstances() {
return cancelCurrentActiveActivityInstances;
}
public void setCancelCurrentActiveActivityInstances(boolean cancelCurrentActiveActivityInstances) {
this.cancelCurrentActiveActivityInstances = cancelCurrentActiveActivityInstances;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy