TSK-1257: replaced multiple helper methods with abstract helper method
This commit is contained in:
parent
6f12677b74
commit
36adf71b90
|
|
@ -5,8 +5,8 @@ import java.util.function.Consumer;
|
||||||
import pro.taskana.common.api.exceptions.SystemException;
|
import pro.taskana.common.api.exceptions.SystemException;
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface CheckedConsumer<T> {
|
public interface CheckedConsumer<T, E extends Throwable> {
|
||||||
static <T> Consumer<T> wrap(CheckedConsumer<T> checkedConsumer) {
|
static <T, E extends Throwable> Consumer<T> wrap(CheckedConsumer<T, E> checkedConsumer) {
|
||||||
return t -> {
|
return t -> {
|
||||||
try {
|
try {
|
||||||
checkedConsumer.accept(t);
|
checkedConsumer.accept(t);
|
||||||
|
|
@ -16,6 +16,6 @@ public interface CheckedConsumer<T> {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void accept(T t) throws Throwable;
|
void accept(T t) throws E;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ import pro.taskana.common.api.exceptions.TaskanaException;
|
||||||
import pro.taskana.common.internal.CustomPropertySelector;
|
import pro.taskana.common.internal.CustomPropertySelector;
|
||||||
import pro.taskana.common.internal.InternalTaskanaEngine;
|
import pro.taskana.common.internal.InternalTaskanaEngine;
|
||||||
import pro.taskana.common.internal.security.CurrentUserContext;
|
import pro.taskana.common.internal.security.CurrentUserContext;
|
||||||
|
import pro.taskana.common.internal.util.CheckedConsumer;
|
||||||
import pro.taskana.common.internal.util.CheckedFunction;
|
import pro.taskana.common.internal.util.CheckedFunction;
|
||||||
import pro.taskana.common.internal.util.IdGenerator;
|
import pro.taskana.common.internal.util.IdGenerator;
|
||||||
import pro.taskana.common.internal.util.Pair;
|
import pro.taskana.common.internal.util.Pair;
|
||||||
|
|
@ -1020,13 +1021,27 @@ public class TaskServiceImpl implements TaskService {
|
||||||
BulkOperationResults<String, TaskanaException> bulkLog = new BulkOperationResults<>();
|
BulkOperationResults<String, TaskanaException> bulkLog = new BulkOperationResults<>();
|
||||||
|
|
||||||
Instant now = Instant.now();
|
Instant now = Instant.now();
|
||||||
Stream<TaskSummaryImpl> filteredSummaries = filterNotExistingTaskIds(taskIds, bulkLog);
|
Stream<TaskSummaryImpl> filteredSummaries =
|
||||||
filteredSummaries = filterInvalidTaskStates(filteredSummaries, bulkLog);
|
filterNotExistingTaskIds(taskIds, bulkLog)
|
||||||
|
.filter(task -> task.getState() != TaskState.COMPLETED)
|
||||||
|
.filter(
|
||||||
|
addErrorToBulkLog(TaskServiceImpl::checkIfTaskIsTerminatedOrCancelled, bulkLog));
|
||||||
if (!forced) {
|
if (!forced) {
|
||||||
filteredSummaries = filterPreConditionForCompleteTasks(filteredSummaries, bulkLog);
|
filteredSummaries =
|
||||||
|
filteredSummaries.filter(
|
||||||
|
addErrorToBulkLog(TaskServiceImpl::checkPreconditionsForCompleteTask, bulkLog));
|
||||||
} else {
|
} else {
|
||||||
filteredSummaries = claimNotClaimedTasks(filteredSummaries, forced, now, bulkLog);
|
String userId = CurrentUserContext.getUserid();
|
||||||
|
filteredSummaries =
|
||||||
|
filteredSummaries.filter(
|
||||||
|
addErrorToBulkLog(
|
||||||
|
summary -> {
|
||||||
|
if (taskIsNotClaimed(summary)) {
|
||||||
|
checkPreconditionsForClaimTask(summary, true);
|
||||||
|
claimActionsOnTask(summary, userId, now);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
bulkLog));
|
||||||
}
|
}
|
||||||
|
|
||||||
updateTasksToBeCompleted(filteredSummaries, now);
|
updateTasksToBeCompleted(filteredSummaries, now);
|
||||||
|
|
@ -1038,41 +1053,6 @@ public class TaskServiceImpl implements TaskService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<TaskSummaryImpl> filterPreConditionForCompleteTasks(
|
|
||||||
Stream<TaskSummaryImpl> stream, BulkOperationResults<String, TaskanaException> bulkLog) {
|
|
||||||
return stream.filter(
|
|
||||||
summary -> {
|
|
||||||
try {
|
|
||||||
checkPreconditionsForCompleteTask(summary);
|
|
||||||
return true;
|
|
||||||
} catch (TaskanaException e) {
|
|
||||||
bulkLog.addError(summary.getId(), e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Stream<TaskSummaryImpl> claimNotClaimedTasks(
|
|
||||||
Stream<TaskSummaryImpl> stream,
|
|
||||||
boolean forced,
|
|
||||||
Instant now,
|
|
||||||
BulkOperationResults<String, TaskanaException> bulkLog) {
|
|
||||||
String userId = CurrentUserContext.getUserid();
|
|
||||||
return stream.filter(
|
|
||||||
summary -> {
|
|
||||||
try {
|
|
||||||
if (taskIsNotClaimed(summary)) {
|
|
||||||
checkPreconditionsForClaimTask(summary, forced);
|
|
||||||
claimActionsOnTask(summary, userId, now);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
} catch (TaskanaException e) {
|
|
||||||
bulkLog.addError(summary.getId(), e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private Stream<TaskSummaryImpl> filterNotExistingTaskIds(
|
private Stream<TaskSummaryImpl> filterNotExistingTaskIds(
|
||||||
List<String> taskIds, BulkOperationResults<String, TaskanaException> bulkLog) {
|
List<String> taskIds, BulkOperationResults<String, TaskanaException> bulkLog) {
|
||||||
|
|
||||||
|
|
@ -1080,36 +1060,34 @@ public class TaskServiceImpl implements TaskService {
|
||||||
getTasksToChange(taskIds).stream()
|
getTasksToChange(taskIds).stream()
|
||||||
.collect(Collectors.toMap(TaskSummary::getId, e -> (TaskSummaryImpl) e));
|
.collect(Collectors.toMap(TaskSummary::getId, e -> (TaskSummaryImpl) e));
|
||||||
return taskIds.stream()
|
return taskIds.stream()
|
||||||
.map(id -> Pair.of(id, taskSummaryMap.get(id)))
|
.map(id -> Pair.of(id, taskSummaryMap.get(id)))
|
||||||
.filter(
|
.filter(
|
||||||
pair -> {
|
pair -> {
|
||||||
if (pair.getRight() == null) {
|
if (pair.getRight() == null) {
|
||||||
String taskId = pair.getLeft();
|
String taskId = pair.getLeft();
|
||||||
bulkLog.addError(
|
bulkLog.addError(
|
||||||
taskId,
|
taskId,
|
||||||
new TaskNotFoundException(
|
new TaskNotFoundException(
|
||||||
taskId, String.format(TASK_WITH_ID_WAS_NOT_FOUND, taskId)));
|
taskId, String.format(TASK_WITH_ID_WAS_NOT_FOUND, taskId)));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.map(Pair::getRight);
|
.map(Pair::getRight);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<TaskSummaryImpl> filterInvalidTaskStates(
|
private static Predicate<TaskSummaryImpl> addErrorToBulkLog(
|
||||||
Stream<TaskSummaryImpl> stream, BulkOperationResults<String, TaskanaException> bulkLog) {
|
CheckedConsumer<TaskSummaryImpl, TaskanaException> checkedConsumer,
|
||||||
return stream
|
BulkOperationResults<String, TaskanaException> bulkLog) {
|
||||||
.filter(task -> task.getState() != TaskState.COMPLETED)
|
return summary -> {
|
||||||
.filter(
|
try {
|
||||||
summary -> {
|
checkedConsumer.accept(summary);
|
||||||
try {
|
return true;
|
||||||
checkIfTaskIsTerminatedOrCancelled(summary);
|
} catch (TaskanaException e) {
|
||||||
return true;
|
bulkLog.addError(summary.getId(), e);
|
||||||
} catch (TaskanaException e) {
|
return false;
|
||||||
bulkLog.addError(summary.getId(), e);
|
}
|
||||||
return false;
|
};
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkConcurrencyAndSetModified(TaskImpl newTaskImpl, TaskImpl oldTaskImpl)
|
private void checkConcurrencyAndSetModified(TaskImpl newTaskImpl, TaskImpl oldTaskImpl)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue