Skip to content

Commit

Permalink
Handle deleting task candidates through DelegateTask (#3998)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacekMiszczak authored Jan 21, 2025
1 parent 8b3b6d5 commit 1785529
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,34 @@
*/
package org.flowable.engine.test.api.task;

import java.util.HashSet;
import java.util.Set;

import org.flowable.engine.delegate.TaskListener;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.task.service.delegate.DelegateTask;

public class DeleteCandidateTaskListener implements TaskListener {
public static final String VARNAME_CANDIDATE_USERS = "candidateUsers";
public static final String VARNAME_CANDIDATE_GROUPS = "candidateGroups";

@Override
public void notify(DelegateTask delegateTask) {
delegateTask.deleteCandidateUser("admin");
delegateTask.deleteCandidateGroup("admins");

Set<IdentityLink> candidates = delegateTask.getCandidates();
Set<String> candidateUsers = new HashSet<>();
Set<String> candidateGroups = new HashSet<>();
for (IdentityLink candidate : candidates) {
if (candidate.getUserId() != null) {
candidateUsers.add(candidate.getUserId());
} else if (candidate.getGroupId() != null) {
candidateGroups.add(candidate.getGroupId());
}
}
delegateTask.setVariable(VARNAME_CANDIDATE_USERS, candidateUsers);
delegateTask.setVariable(VARNAME_CANDIDATE_GROUPS, candidateGroups);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.impl.history.HistoryLevel;
Expand Down Expand Up @@ -321,16 +322,46 @@ public void testDeleteOwner() {
}

@Test
@Deployment(resources = "org/flowable/engine/test/api/task/TaskIdentityLinksTest.testDeleteCandidateUser.bpmn20.xml")
public void testDeleteCandidateUser() {
@Deployment(resources = "org/flowable/engine/test/api/task/TaskIdentityLinksTest.testDeleteCandidates.bpmn20.xml")
public void testDeleteCandidates() {
runtimeService.startProcessInstanceByKey("TaskIdentityLinks");

String taskId = taskService.createTaskQuery().singleResult().getId();

List<IdentityLink> identityLinks = taskService.getIdentityLinksForTask(taskId);
assertThat(identityLinks)
.filteredOn(identityLink -> identityLink.getUserId() != null)
.extracting(IdentityLink::getUserId)
.containsExactly("user");

assertThat(identityLinks)
.filteredOn(identityLink -> identityLink.getGroupId() != null)
.extracting(IdentityLink::getGroupId)
.containsExactly("users");

if (HistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.AUDIT, processEngineConfiguration)) {
List<HistoricIdentityLink> historicIdentityLinks = historyService.getHistoricIdentityLinksForTask(taskId);

assertThat(historicIdentityLinks)
.filteredOn(identityLink -> identityLink.getUserId() != null)
.extracting(HistoricIdentityLink::getUserId)
.containsExactly("user");

assertThat(historicIdentityLinks)
.filteredOn(identityLink -> identityLink.getGroupId() != null)
.extracting(HistoricIdentityLink::getGroupId)
.containsExactly("users");
}

@SuppressWarnings("unchecked")
Set<String> candidateUsers = (Set<String>) taskService.getVariable(taskId, DeleteCandidateTaskListener.VARNAME_CANDIDATE_USERS);
assertThat(candidateUsers)
.containsExactly("user");

@SuppressWarnings("unchecked")
Set<String> candidateGroups = (Set<String>) taskService.getVariable(taskId, DeleteCandidateTaskListener.VARNAME_CANDIDATE_GROUPS);
assertThat(candidateGroups)
.containsExactly("users");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</extensionElements>
<potentialOwner>
<resourceAssignmentExpression>
<formalExpression>user(user),user(admin)</formalExpression>
<formalExpression>user(user),user(admin),group(users),group(admins)</formalExpression>
</resourceAssignmentExpression>
</potentialOwner>
</userTask>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,27 @@ public void deleteCandidateGroup(String groupId) {
@Override
public void deleteUserIdentityLink(String userId, String identityLinkType) {
IdentityLinkEntityManager identityLinkEntityManager = getIdentityLinkServiceConfiguration().getIdentityLinkEntityManager();
identityLinkEntityManager.deleteTaskIdentityLink(this.id, getIdentityLinks(), userId, null, identityLinkType);
List<IdentityLinkEntity> identityLinkEntities = identityLinkEntityManager.deleteTaskIdentityLink(this.id,
getIdentityLinks(), userId, null, identityLinkType);
InternalTaskAssignmentManager taskAssignmentManager = getTaskAssignmentManager();
if (taskAssignmentManager != null) {
for (IdentityLinkEntity identityLink : identityLinkEntities) {
taskAssignmentManager.deleteUserIdentityLink(this, identityLink);
}
}
}

@Override
public void deleteGroupIdentityLink(String groupId, String identityLinkType) {
IdentityLinkEntityManager identityLinkEntityManager = getIdentityLinkServiceConfiguration().getIdentityLinkEntityManager();
identityLinkEntityManager.deleteTaskIdentityLink(this.id, getIdentityLinks(), null, groupId,identityLinkType);
List<IdentityLinkEntity> identityLinkEntities = identityLinkEntityManager.deleteTaskIdentityLink(this.id,
getIdentityLinks(), null, groupId, identityLinkType);
InternalTaskAssignmentManager taskAssignmentManager = getTaskAssignmentManager();
if (taskAssignmentManager != null) {
for (IdentityLinkEntity identityLink : identityLinkEntities) {
taskAssignmentManager.deleteGroupIdentityLink(this, identityLink);
}
}
}

@Override
Expand Down

0 comments on commit 1785529

Please sign in to comment.