Skip to content

Commit

Permalink
fix(taskAttachment): prevent NPE for deletion of attachments with emp…
Browse files Browse the repository at this point in the history
…ty or nonexisting taskId

related to #3545
  • Loading branch information
PHWaechtler committed Nov 27, 2024
1 parent 119cbf8 commit 3859a8a
Show file tree
Hide file tree
Showing 3 changed files with 2,892 additions and 2,848 deletions.
4 changes: 4 additions & 0 deletions engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.history.UserOperationLogEntry;
import org.camunda.bpm.engine.impl.interceptor.Command;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
Expand Down Expand Up @@ -49,39 +50,45 @@ public DeleteAttachmentCmd(String taskId, String attachmentId) {

public Object execute(CommandContext commandContext) {
AttachmentEntity attachment = null;
if (taskId != null) {
if (StringUtils.isNotEmpty(taskId)) {
attachment = (AttachmentEntity) commandContext
.getAttachmentManager()
.findAttachmentByTaskIdAndAttachmentId(taskId, attachmentId);
ensureNotNull("No attachment exist for task id '" + taskId + " and attachmentId '" + attachmentId + "'.", "attachment", attachment);
ensureNotNull("No attachment exists for task id '" + taskId + " and attachmentId '" + attachmentId + "'.", "attachment", attachment);
} else {
attachment = commandContext
.getDbEntityManager()
.selectById(AttachmentEntity.class, attachmentId);
ensureNotNull("No attachment exist with attachmentId '" + attachmentId + "'.", "attachment", attachment);
ensureNotNull("No attachment exists with attachmentId '" + attachmentId + "'.", "attachment", attachment);
}

commandContext
.getDbEntityManager()
.delete(attachment);
.getDbEntityManager()
.delete(attachment);

if (attachment.getContentId() != null) {
commandContext
.getByteArrayManager()
.deleteByteArrayById(attachment.getContentId());
.getByteArrayManager()
.deleteByteArrayById(attachment.getContentId());
}

if (attachment.getTaskId()!=null) {
if (StringUtils.isNotEmpty(attachment.getTaskId())) {
TaskEntity task = commandContext
.getTaskManager()
.findTaskById(attachment.getTaskId());

if (task != null) {
PropertyChange propertyChange = new PropertyChange("name", null, attachment.getName());

commandContext.getOperationLogManager()
.logAttachmentOperation(UserOperationLogEntry.OPERATION_TYPE_DELETE_ATTACHMENT, task, propertyChange);

task.triggerUpdateEvent();
} else {
ensureNotNull(
"No task exists for attachment '" + attachmentId + "' with taskId '" + attachment.getTaskId() + "'.",
"task", task);
}
}

return null;
Expand Down
Loading

0 comments on commit 3859a8a

Please sign in to comment.