Skip to content

Commit

Permalink
Closes Taskana#2496 : Set Owner of Tasks when Transferring
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrdi committed Feb 21, 2024
1 parent 6ddb86f commit d814f70
Show file tree
Hide file tree
Showing 10 changed files with 489 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,90 @@ Stream<DynamicTest> should_CreateTransferredHistoryEvents_When_TaskBulkTransfer(
return DynamicTest.stream(testCases.iterator(), Triplet::getLeft, test);
}

@WithAccessId(user = "admin")
@TestFactory
Stream<DynamicTest> should_CreateTransferredHistoryEvents_When_TaskBulkTransferWithOwner() {
List<Triplet<String, Map<String, String>, Consumer<List<String>>>> testCases =
List.of(
/*
The workbasketId of the source Workbasket is parametrized. Putting the tested Tasks
into the same Workbasket would result in changes to the test data. This would require
changing tests that already use the tested Tasks. That's why workbasketId is
parametrized.
*/
Triplet.of(
"Using WorkbasketId",
Map.ofEntries(
Map.entry(
"TKI:000000000000000000000000000000000010",
"WBI:100000000000000000000000000000000001"),
Map.entry(
"TKI:000000000000000000000000000000000011",
"WBI:100000000000000000000000000000000001"),
Map.entry(
"TKI:000000000000000000000000000000000012",
"WBI:100000000000000000000000000000000001")),
wrap(
(List<String> taskIds) ->
taskService.transferTasksWithOwner(
"WBI:100000000000000000000000000000000007", taskIds, "user-1-2"))),
Triplet.of(
"Using WorkbasketKey and Domain",
Map.ofEntries(
Map.entry(
"TKI:000000000000000000000000000000000013",
"WBI:100000000000000000000000000000000001"),
Map.entry(
"TKI:000000000000000000000000000000000014",
"WBI:100000000000000000000000000000000001"),
Map.entry(
"TKI:000000000000000000000000000000000015",
"WBI:100000000000000000000000000000000001")),
wrap(
(List<String> taskIds) ->
taskService.transferTasksWithOwner(
"USER-1-2", "DOMAIN_A", taskIds, "USER-1-2"))));
ThrowingConsumer<Triplet<String, Map<String, String>, Consumer<List<String>>>> test =
t -> {
Map<String, String> taskIds = t.getMiddle();
Consumer<List<String>> transferMethod = t.getRight();

TaskHistoryQueryMapper taskHistoryQueryMapper = getHistoryQueryMapper();

List<TaskHistoryEvent> events =
taskHistoryQueryMapper.queryHistoryEvents(
(TaskHistoryQueryImpl)
historyService
.createTaskHistoryQuery()
.taskIdIn(taskIds.keySet().toArray(new String[0])));

assertThat(events).isEmpty();

transferMethod.accept(new ArrayList<>(taskIds.keySet()));

events =
taskHistoryQueryMapper.queryHistoryEvents(
(TaskHistoryQueryImpl)
historyService
.createTaskHistoryQuery()
.taskIdIn(taskIds.keySet().toArray(new String[0])));

assertThat(events)
.extracting(TaskHistoryEvent::getTaskId)
.containsExactlyInAnyOrderElementsOf(taskIds.keySet());

for (TaskHistoryEvent event : events) {
assertTransferHistoryEvent(
event.getId(),
taskIds.get(event.getTaskId()),
"WBI:100000000000000000000000000000000007",
"admin");
}
};

return DynamicTest.stream(testCases.iterator(), Triplet::getLeft, test);
}

private void assertTransferHistoryEvent(
String eventId, String expectedOldValue, String expectedNewValue, String expectedUser)
throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,100 @@ BulkOperationResults<String, TaskanaException> transferTasks(
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException;

/**
* Transfers a List of {@linkplain Task Tasks} to another {@linkplain Workbasket} and set owner to
* {@param owner} while always setting {@linkplain Task#isTransferred isTransferred} to true.
*
* @see #transferTasksWithOwner(String, List, String, boolean)
*/
@SuppressWarnings("checkstyle:JavadocMethod")
default BulkOperationResults<String, TaskanaException> transferTasksWithOwner(
String destinationWorkbasketId, List<String> taskIds, String owner)
throws InvalidArgumentException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException {
return transferTasksWithOwner(destinationWorkbasketId, taskIds, owner, true);
}

/**
* Transfers a List of {@linkplain Task Tasks} to another {@linkplain Workbasket} and set the
* owner.
*
* <p>The transfer resets {@linkplain Task#isRead() isRead} and sets {@linkplain
* Task#isTransferred() isTransferred} if setTransferFlag is true. Exceptions will be thrown if
* the caller got no {@linkplain WorkbasketPermission} on the target or if the target {@linkplain
* Workbasket} doesn't exist. Other Exceptions will be stored and returned in the end.
*
* @param destinationWorkbasketId {@linkplain Workbasket#getId() id} of the target {@linkplain
* Workbasket}
* @param taskIds List of source {@linkplain Task Tasks} which will be moved
* @param owner the new owner of the transferred tasks
* @param setTransferFlag controls whether to set {@linkplain Task#isTransferred() isTransferred}
* or not
* @return Bulkresult with {@linkplain Task#getId() ids} and Error for each failed transactions
* @throws NotAuthorizedOnWorkbasketException if the current user has no {@linkplain
* WorkbasketPermission#READ} for the source {@linkplain Workbasket} or no {@linkplain
* WorkbasketPermission#TRANSFER} for the target {@linkplain Workbasket}
* @throws InvalidArgumentException if the method parameters are empty or NULL
* @throws WorkbasketNotFoundException if the target {@linkplain Workbasket} can't be found
*/
BulkOperationResults<String, TaskanaException> transferTasksWithOwner(
String destinationWorkbasketId, List<String> taskIds, String owner, boolean setTransferFlag)
throws InvalidArgumentException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException;

/**
* Transfers a List of {@linkplain Task Tasks} to another {@linkplain Workbasket} and set owner
* while always setting {@linkplain Task#isTransferred() isTransferred} to true.
*
* @see #transferTasksWithOwner(String, String, List, String, boolean)
*/
@SuppressWarnings("checkstyle:JavadocMethod")
default BulkOperationResults<String, TaskanaException> transferTasksWithOwner(
String destinationWorkbasketKey,
String destinationWorkbasketDomain,
List<String> taskIds,
String owner)
throws InvalidArgumentException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException {
return transferTasksWithOwner(
destinationWorkbasketKey, destinationWorkbasketDomain, taskIds, owner, true);
}

/**
* Transfers a List of {@linkplain Task Tasks} to another {@linkplain Workbasket} and set owner.
*
* <p>The transfer resets {@linkplain Task#isRead() isRead} and sets {@linkplain
* Task#isTransferred() isTransferred} if setTransferFlag is true. Exceptions will be thrown if
* the caller got no {@linkplain WorkbasketPermission} on the target {@linkplain Workbasket} or if
* it doesn't exist. Other Exceptions will be stored and returned in the end.
*
* @param destinationWorkbasketKey target {@linkplain Workbasket#getKey() key}
* @param destinationWorkbasketDomain target {@linkplain Workbasket#getDomain() domain}
* @param taskIds List of {@linkplain Task#getId() ids} of source {@linkplain Task Tasks} which
* will be moved
* @param owner the new owner of the transferred tasks
* @param setTransferFlag controls whether to set {@linkplain Task#isTransferred() isTransferred}
* or not
* @return BulkResult with {@linkplain Task#getId() ids} and Error for each failed transactions
* @throws NotAuthorizedOnWorkbasketException if the current user has no {@linkplain
* WorkbasketPermission#READ} for the source {@linkplain Workbasket} or no {@linkplain
* WorkbasketPermission#TRANSFER} for the target {@linkplain Workbasket}
* @throws InvalidArgumentException if the method parameters are empty or NULL
* @throws WorkbasketNotFoundException if the target {@linkplain Workbasket} can't be found
*/
BulkOperationResults<String, TaskanaException> transferTasksWithOwner(
String destinationWorkbasketKey,
String destinationWorkbasketDomain,
List<String> taskIds,
String owner,
boolean setTransferFlag)
throws InvalidArgumentException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException;

/**
* Update a {@linkplain Task}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,30 @@ public BulkOperationResults<String, TaskanaException> transferTasks(
taskIds, destinationWorkbasketKey, destinationWorkbasketDomain, setTransferFlag);
}

@Override
public BulkOperationResults<String, TaskanaException> transferTasksWithOwner(
String destinationWorkbasketId, List<String> taskIds, String owner, boolean setTransferFlag)
throws InvalidArgumentException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException {
return taskTransferrer.transferTasksWithOwner(
taskIds, destinationWorkbasketId, owner, setTransferFlag);
}

@Override
public BulkOperationResults<String, TaskanaException> transferTasksWithOwner(
String destinationWorkbasketKey,
String destinationWorkbasketDomain,
List<String> taskIds,
String owner,
boolean setTransferFlag)
throws InvalidArgumentException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException {
return taskTransferrer.transferTasksWithOwner(
taskIds, destinationWorkbasketKey, destinationWorkbasketDomain, owner, setTransferFlag);
}

@Override
public void deleteTask(String taskId)
throws TaskNotFoundException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ BulkOperationResults<String, TaskanaException> transfer(
workbasketService.getWorkbasket(destinationWorkbasketId).asSummary();
checkDestinationWorkbasket(destinationWorkbasket);

return transferMultipleTasks(taskIds, destinationWorkbasket, setTransferFlag);
return transferMultipleTasks(taskIds, destinationWorkbasket, null, setTransferFlag);
}

BulkOperationResults<String, TaskanaException> transfer(
Expand All @@ -101,7 +101,35 @@ BulkOperationResults<String, TaskanaException> transfer(
workbasketService.getWorkbasket(destinationWorkbasketKey, destinationDomain).asSummary();
checkDestinationWorkbasket(destinationWorkbasket);

return transferMultipleTasks(taskIds, destinationWorkbasket, setTransferFlag);
return transferMultipleTasks(taskIds, destinationWorkbasket, null, setTransferFlag);
}

BulkOperationResults<String, TaskanaException> transferTasksWithOwner(
List<String> taskIds, String destinationWorkbasketId, String owner, boolean setTransferFlag)
throws WorkbasketNotFoundException,
InvalidArgumentException,
NotAuthorizedOnWorkbasketException {
WorkbasketSummary destinationWorkbasket =
workbasketService.getWorkbasket(destinationWorkbasketId).asSummary();
checkDestinationWorkbasket(destinationWorkbasket);

return transferMultipleTasks(taskIds, destinationWorkbasket, owner, setTransferFlag);
}

BulkOperationResults<String, TaskanaException> transferTasksWithOwner(
List<String> taskIds,
String destinationWorkbasketKey,
String destinationDomain,
String owner,
boolean setTransferFlag)
throws WorkbasketNotFoundException,
InvalidArgumentException,
NotAuthorizedOnWorkbasketException {
WorkbasketSummary destinationWorkbasket =
workbasketService.getWorkbasket(destinationWorkbasketKey, destinationDomain).asSummary();
checkDestinationWorkbasket(destinationWorkbasket);

return transferMultipleTasks(taskIds, destinationWorkbasket, owner, setTransferFlag);
}

private Task transferSingleTask(
Expand All @@ -120,7 +148,7 @@ private Task transferSingleTask(
WorkbasketSummary originWorkbasket = task.getWorkbasketSummary();
checkPreconditionsForTransferTask(task, destinationWorkbasket, originWorkbasket);

applyTransferValuesForTask(task, destinationWorkbasket, setTransferFlag);
applyTransferValuesForTask(task, destinationWorkbasket, null, setTransferFlag);
taskMapper.update(task);
if (historyEventManager.isEnabled()) {
createTransferredEvent(
Expand All @@ -136,6 +164,7 @@ private Task transferSingleTask(
private BulkOperationResults<String, TaskanaException> transferMultipleTasks(
List<String> taskToBeTransferred,
WorkbasketSummary destinationWorkbasket,
String owner,
boolean setTransferFlag)
throws InvalidArgumentException {
if (taskToBeTransferred == null || taskToBeTransferred.isEmpty()) {
Expand All @@ -154,7 +183,7 @@ private BulkOperationResults<String, TaskanaException> transferMultipleTasks(
() -> taskService.createTaskQuery().idIn(taskIds.toArray(new String[0])).list());
taskSummaries =
filterOutTasksWhichDoNotMatchTransferCriteria(taskIds, taskSummaries, bulkLog);
updateTransferableTasks(taskSummaries, destinationWorkbasket, setTransferFlag);
updateTransferableTasks(taskSummaries, destinationWorkbasket, owner, setTransferFlag);

return bulkLog;
} finally {
Expand Down Expand Up @@ -254,6 +283,7 @@ private Set<String> getSourceWorkbasketIdsWithTransferPermission(
private void updateTransferableTasks(
List<TaskSummary> taskSummaries,
WorkbasketSummary destinationWorkbasket,
String owner,
boolean setTransferFlag) {
Map<TaskState, List<TaskSummary>> summariesByState = groupTasksByState(taskSummaries);
for (Map.Entry<TaskState, List<TaskSummary>> entry : summariesByState.entrySet()) {
Expand All @@ -262,7 +292,7 @@ private void updateTransferableTasks(
if (!taskSummariesWithSameGoalState.isEmpty()) {
TaskImpl updateObject = new TaskImpl();
updateObject.setState(goalState);
applyTransferValuesForTask(updateObject, destinationWorkbasket, setTransferFlag);
applyTransferValuesForTask(updateObject, destinationWorkbasket, owner, setTransferFlag);
taskMapper.updateTransfered(
taskSummariesWithSameGoalState.stream()
.map(TaskSummary::getId)
Expand All @@ -275,7 +305,8 @@ private void updateTransferableTasks(
TaskSummaryImpl newSummary = (TaskSummaryImpl) oldSummary.copy();
newSummary.setId(oldSummary.getId());
newSummary.setExternalId(oldSummary.getExternalId());
applyTransferValuesForTask(newSummary, destinationWorkbasket, setTransferFlag);
applyTransferValuesForTask(
newSummary, destinationWorkbasket, owner, setTransferFlag);

createTransferredEvent(
oldSummary,
Expand All @@ -289,11 +320,11 @@ private void updateTransferableTasks(
}

private void applyTransferValuesForTask(
TaskSummaryImpl task, WorkbasketSummary workbasket, boolean setTransferFlag) {
TaskSummaryImpl task, WorkbasketSummary workbasket, String owner, boolean setTransferFlag) {
task.setRead(false);
task.setTransferred(setTransferFlag);
task.setState(getStateAfterTransfer(task));
task.setOwner(null);
task.setOwner(owner);
task.setWorkbasketSummary(workbasket);
task.setDomain(workbasket.getDomain());
task.setModified(Instant.now());
Expand Down
Loading

0 comments on commit d814f70

Please sign in to comment.