DeleteProcessInstanceCmd.java 3.2 KB
Newer Older
T
tombaeyens 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* 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.
 */
13
package org.activiti.engine.impl.cmd;
T
tombaeyens 已提交
14

15 16
import java.io.Serializable;

17
import org.activiti.engine.ActivitiIllegalArgumentException;
18 19
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.compatibility.Activiti5CompatibilityHandler;
20
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
T
tombaeyens 已提交
21
import org.activiti.engine.impl.interceptor.Command;
22
import org.activiti.engine.impl.interceptor.CommandContext;
23 24 25
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.util.Activiti5Util;
import org.activiti.engine.runtime.ProcessInstance;
T
tombaeyens 已提交
26 27 28 29

/**
 * @author Joram Barrez
 */
M
meyerd 已提交
30
public class DeleteProcessInstanceCmd implements Command<Void>, Serializable {
31

32 33 34 35 36 37 38 39 40 41 42 43 44
  private static final long serialVersionUID = 1L;
  protected String processInstanceId;
  protected String deleteReason;

  public DeleteProcessInstanceCmd(String processInstanceId, String deleteReason) {
    this.processInstanceId = processInstanceId;
    this.deleteReason = deleteReason;
  }

  public Void execute(CommandContext commandContext) {
    if (processInstanceId == null) {
      throw new ActivitiIllegalArgumentException("processInstanceId is null");
    }
45
    
46 47 48
    // fill default reason if none provided
    if (deleteReason == null) {
      deleteReason = "ACTIVITI_DELETED";
49 50
    }

51
    if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
52 53
      commandContext.getProcessEngineConfiguration().getEventDispatcher()
        .dispatchEvent(ActivitiEventBuilder.createCancelledEvent(this.processInstanceId, this.processInstanceId, null, deleteReason));
T
Tijs Rademakers 已提交
54
    }
T
tombaeyens 已提交
55

56 57 58 59 60 61 62
    ExecutionEntity processInstanceEntity = commandContext.getExecutionEntityManager().findExecutionById(processInstanceId);
    
    if (processInstanceEntity == null) {
      throw new ActivitiObjectNotFoundException("No process instance found for id '" + processInstanceId + "'", ProcessInstance.class);
    }
    
    if (Activiti5Util.isActiviti5ProcessDefinitionId(commandContext, processInstanceEntity.getProcessDefinitionId())) {
63
      Activiti5CompatibilityHandler activiti5CompatibilityHandler = Activiti5Util.getActiviti5CompatibilityHandler(); 
64 65
      activiti5CompatibilityHandler.deleteProcessInstance(processInstanceId, deleteReason);
    } else {
66 67
      commandContext.getExecutionEntityManager().deleteProcessInstanceExecutionEntity(processInstanceEntity.getId(), 
          null, deleteReason, false, true, true);
68
    }
69 70 71 72 73 74

    // TODO : remove following line of deleteProcessInstanceExecutionEntity is found to be doing the same as deleteProcessInstance
    // commandContext.getExecutionEntityManager().deleteProcessInstance(processInstanceId, deleteReason);
    return null;
  }

T
tombaeyens 已提交
75
}