Skip to content

Commit

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

@WithAccessId(user = "admin")
@TestFactory
Stream<DynamicTest> should_CreateTransferredHistoryEvent_When_TaskIsTransferredWithOwner() {
List<Quadruple<String, String, String, Consumer<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.
*/
Quadruple.of(
"Using WorkbasketId; Task doesn't have an Attachment"
+ " or any secondary Object References",
"TKI:000000000000000000000000000000000005",
"WBI:100000000000000000000000000000000001",
wrap(
(String taskId) ->
taskService.transferWithOwner(
taskId, "WBI:100000000000000000000000000000000007", "USER-1-2"))),
Quadruple.of(
"Using WorkbasketId; Task has Attachment and secondary Object Reference",
"TKI:000000000000000000000000000000000001",
"WBI:100000000000000000000000000000000006",
wrap(
(String taskId) ->
taskService.transferWithOwner(
taskId, "WBI:100000000000000000000000000000000007", "USER-1-2"))),
Quadruple.of(
"Using WorkbasketKey and Domain",
"TKI:000000000000000000000000000000000006",
"WBI:100000000000000000000000000000000001",
wrap(
(String taskId) ->
taskService.transferWithOwner(
taskId, "USER-1-2", "DOMAIN_A", "USER-1-2"))));
ThrowingConsumer<Quadruple<String, String, String, Consumer<String>>> test =
q -> {
String taskId = q.getSecond();
Consumer<String> transferMethod = q.getFourth();

TaskHistoryQueryMapper taskHistoryQueryMapper = getHistoryQueryMapper();

List<TaskHistoryEvent> events =
taskHistoryQueryMapper.queryHistoryEvents(
(TaskHistoryQueryImpl) historyService.createTaskHistoryQuery().taskIdIn(taskId));

assertThat(events).isEmpty();

transferMethod.accept(taskId);

events =
taskHistoryQueryMapper.queryHistoryEvents(
(TaskHistoryQueryImpl) historyService.createTaskHistoryQuery().taskIdIn(taskId));

assertThat(events).hasSize(1);
String sourceWorkbasketId = q.getThird();
assertTransferHistoryEvent(
events.get(0).getId(),
sourceWorkbasketId,
"WBI:100000000000000000000000000000000007",
"admin");
};
return DynamicTest.stream(testCases.iterator(), Quadruple::getFirst, test);
}

private void assertTransferHistoryEvent(
String eventId, String expectedOldValue, String expectedNewValue, String expectedUser)
throws Exception {
Expand Down
117 changes: 117 additions & 0 deletions lib/taskana-core/src/main/java/pro/taskana/task/api/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ Task terminateTask(String taskId)
* Transfers a {@linkplain Task} to another {@linkplain Workbasket} while always setting
* {@linkplain Task#isTransferred() isTransferred} to true.
*
* @param taskId the {@linkplain Task#getId() id} of the {@linkplain Task} which should be
* transferred
* @param destinationWorkbasketId the {@linkplain Workbasket#getId() id} of the target {@linkplain
* Workbasket}
* @return the transferred {@linkplain Task}
* @see #transfer(String, String, boolean)
*/
@SuppressWarnings("checkstyle:JavadocMethod")
Expand Down Expand Up @@ -513,6 +518,13 @@ Task transfer(String taskId, String destinationWorkbasketId, boolean setTransfer
* Transfers a {@linkplain Task} to another {@linkplain Workbasket} while always setting
* {@linkplain Task#isTransferred isTransferred} .
*
* @param taskId the {@linkplain Task#getId() id} of the {@linkplain Task} which should be
* transferred
* @param workbasketKey the {@linkplain Workbasket#getKey() key} of the target {@linkplain
* Workbasket}
* @param domain the {@linkplain Workbasket#getDomain() domain} of the target {@linkplain
* Workbasket}
* @return the transferred {@linkplain Task}
* @see #transfer(String, String, String, boolean)
*/
@SuppressWarnings("checkstyle:JavadocMethod")
Expand Down Expand Up @@ -553,6 +565,111 @@ Task transfer(String taskId, String workbasketKey, String domain, boolean setTra
NotAuthorizedOnWorkbasketException,
InvalidTaskStateException;

/**
* Transfers a {@linkplain Task} to another {@linkplain Workbasket} and set owner of {@linkplain
* Task} in the new {@linkplain Workbasket} to {@param owner} while always setting {@linkplain
* Task#isTransferred() isTransferred} to true.
*
* @param taskId the {@linkplain Task#getId() id} of the {@linkplain Task} which should be
* transferred
* @param destinationWorkbasketId the {@linkplain Workbasket#getId() id} of the target {@linkplain
* Workbasket}
* @param owner the owner of the transferred {@linkplain Task} in the new workbasket
* @return the transferred {@linkplain Task}
* @see #transferWithOwner(String, String, String, boolean)
*/
@SuppressWarnings("checkstyle:JavadocMethod")
default Task transferWithOwner(String taskId, String destinationWorkbasketId, String owner)
throws TaskNotFoundException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException,
InvalidTaskStateException {
return transferWithOwner(taskId, destinationWorkbasketId, owner, true);
}

/**
* Transfers a {@linkplain Task} 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.
*
* @param taskId the {@linkplain Task#getId() id} of the {@linkplain Task} which should be
* transferred
* @param destinationWorkbasketId the {@linkplain Workbasket#getId() id} of the target {@linkplain
* Workbasket}
* @param owner the owner of the transferred {@linkplain Task} in the new workbasket
* @param setTransferFlag controls whether to set {@linkplain Task#isTransferred() isTransferred}
* to true or not
* @return the transferred {@linkplain Task}
* @throws TaskNotFoundException if the {@linkplain Task} with taskId wasn't found
* @throws WorkbasketNotFoundException if the target {@linkplain Workbasket} was not found
* @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 InvalidTaskStateException if the {@linkplain Task} is in one of the {@linkplain
* TaskState#END_STATES}
*/
Task transferWithOwner(
String taskId, String destinationWorkbasketId, String owner, boolean setTransferFlag)
throws TaskNotFoundException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException,
InvalidTaskStateException;

/**
* Transfers a {@linkplain Task} to another {@linkplain Workbasket} and set owner of {@linkplain
* Task} in new {@linkplain Workbasket} to {@param owner} while always setting {@linkplain
* Task#isTransferred isTransferred} .
*
* @param taskId the {@linkplain Task#getId() id} of the {@linkplain Task} which should be
* transferred
* @param workbasketKey the {@linkplain Workbasket#getKey() key} of the target {@linkplain
* Workbasket}
* @param domain the {@linkplain Workbasket#getDomain() domain} of the target {@linkplain
* Workbasket}
* @param owner the owner of the transferred {@linkplain Task} in the new workbasket
* @see #transferWithOwner(String, String, String, String, boolean)
*/
@SuppressWarnings("checkstyle:JavadocMethod")
default Task transferWithOwner(String taskId, String workbasketKey, String domain, String owner)
throws TaskNotFoundException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException,
InvalidTaskStateException {
return transferWithOwner(taskId, workbasketKey, domain, owner, true);
}

/**
* Transfers a {@linkplain Task} 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.
*
* @param taskId the {@linkplain Task#getId() id} of the {@linkplain Task} which should be
* transferred
* @param workbasketKey the {@linkplain Workbasket#getKey() key} of the target {@linkplain
* Workbasket}
* @param domain the {@linkplain Workbasket#getDomain() domain} of the target {@linkplain
* Workbasket}
* @param owner the owner of the transferred {@linkplain Task} in the new workbasket
* @param setTransferFlag controls whether to set {@linkplain Task#isTransferred() isTransferred}
* or not
* @return the transferred {@linkplain Task}
* @throws TaskNotFoundException if the {@linkplain Task} with taskId was not found
* @throws WorkbasketNotFoundException if the target {@linkplain Workbasket} was not found
* @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 InvalidTaskStateException if the {@linkplain Task} is in one of the {@linkplain
* TaskState#END_STATES}
*/
Task transferWithOwner(
String taskId, String workbasketKey, String domain, String owner, boolean setTransferFlag)
throws TaskNotFoundException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException,
InvalidTaskStateException;

/**
* Transfers a List of {@linkplain Task Tasks} to another {@linkplain Workbasket} while always
* setting {@linkplain Task#isTransferred isTransferred} to true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,27 @@ public Task transfer(String taskId, String workbasketKey, String domain, boolean
return taskTransferrer.transfer(taskId, workbasketKey, domain, setTransferFlag);
}

@Override
public Task transferWithOwner(
String taskId, String destinationWorkbasketId, String owner, boolean setTransferFlag)
throws TaskNotFoundException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException,
InvalidTaskStateException {
return taskTransferrer.transferWithOwner(
taskId, destinationWorkbasketId, owner, setTransferFlag);
}

@Override
public Task transferWithOwner(
String taskId, String workbasketKey, String domain, String owner, boolean setTransferFlag)
throws TaskNotFoundException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException,
InvalidTaskStateException {
return taskTransferrer.transferWithOwner(taskId, workbasketKey, domain, owner, setTransferFlag);
}

@Override
public Task setTaskRead(String taskId, boolean isRead)
throws TaskNotFoundException, NotAuthorizedOnWorkbasketException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Task transfer(String taskId, String destinationWorkbasketId, boolean setTransfer
InvalidTaskStateException {
WorkbasketSummary destinationWorkbasket =
workbasketService.getWorkbasket(destinationWorkbasketId).asSummary();
return transferSingleTask(taskId, destinationWorkbasket, setTransferFlag);
return transferSingleTask(taskId, destinationWorkbasket, null, setTransferFlag);
}

Task transfer(
Expand All @@ -74,7 +74,7 @@ Task transfer(
InvalidTaskStateException {
WorkbasketSummary destinationWorkbasket =
workbasketService.getWorkbasket(destinationWorkbasketKey, destinationDomain).asSummary();
return transferSingleTask(taskId, destinationWorkbasket, setTransferFlag);
return transferSingleTask(taskId, destinationWorkbasket, null, setTransferFlag);
}

BulkOperationResults<String, TaskanaException> transfer(
Expand Down Expand Up @@ -104,8 +104,34 @@ BulkOperationResults<String, TaskanaException> transfer(
return transferMultipleTasks(taskIds, destinationWorkbasket, setTransferFlag);
}

Task transferWithOwner(
String taskId, String destinationWorkbasketId, String owner, boolean setTransferFlag)
throws TaskNotFoundException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException,
InvalidTaskStateException {
WorkbasketSummary destinationWorkbasket =
workbasketService.getWorkbasket(destinationWorkbasketId).asSummary();
return transferSingleTask(taskId, destinationWorkbasket, owner, setTransferFlag);
}

Task transferWithOwner(
String taskId,
String destinationWorkbasketKey,
String destinationDomain,
String owner,
boolean setTransferFlag)
throws TaskNotFoundException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException,
InvalidTaskStateException {
WorkbasketSummary destinationWorkbasket =
workbasketService.getWorkbasket(destinationWorkbasketKey, destinationDomain).asSummary();
return transferSingleTask(taskId, destinationWorkbasket, owner, setTransferFlag);
}

private Task transferSingleTask(
String taskId, WorkbasketSummary destinationWorkbasket, boolean setTransferFlag)
String taskId, WorkbasketSummary destinationWorkbasket, String owner, boolean setTransferFlag)
throws TaskNotFoundException,
WorkbasketNotFoundException,
NotAuthorizedOnWorkbasketException,
Expand All @@ -120,7 +146,7 @@ private Task transferSingleTask(
WorkbasketSummary originWorkbasket = task.getWorkbasketSummary();
checkPreconditionsForTransferTask(task, destinationWorkbasket, originWorkbasket);

applyTransferValuesForTask(task, destinationWorkbasket, setTransferFlag);
applyTransferValuesForTask(task, destinationWorkbasket, owner, setTransferFlag);
taskMapper.update(task);
if (historyEventManager.isEnabled()) {
createTransferredEvent(
Expand Down Expand Up @@ -262,7 +288,7 @@ private void updateTransferableTasks(
if (!taskSummariesWithSameGoalState.isEmpty()) {
TaskImpl updateObject = new TaskImpl();
updateObject.setState(goalState);
applyTransferValuesForTask(updateObject, destinationWorkbasket, setTransferFlag);
applyTransferValuesForTask(updateObject, destinationWorkbasket, null, setTransferFlag);
taskMapper.updateTransfered(
taskSummariesWithSameGoalState.stream()
.map(TaskSummary::getId)
Expand All @@ -275,7 +301,8 @@ private void updateTransferableTasks(
TaskSummaryImpl newSummary = (TaskSummaryImpl) oldSummary.copy();
newSummary.setId(oldSummary.getId());
newSummary.setExternalId(oldSummary.getExternalId());
applyTransferValuesForTask(newSummary, destinationWorkbasket, setTransferFlag);
applyTransferValuesForTask(
newSummary, destinationWorkbasket, null, setTransferFlag);

createTransferredEvent(
oldSummary,
Expand All @@ -289,11 +316,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 34ddd61

Please sign in to comment.