use of pro.taskana.exceptions.InvalidStateException in project taskana by Taskana.
the class TaskServiceImpl method completeTask.
@Override
public Task completeTask(String taskId, boolean isForced) throws TaskNotFoundException, InvalidOwnerException, InvalidStateException, NotAuthorizedException {
LOGGER.debug("entry to completeTask(id = {}, isForced {})", taskId, isForced);
TaskImpl task = null;
try {
taskanaEngine.openConnection();
task = (TaskImpl) this.getTask(taskId);
// check pre-conditions for non-forced invocation
if (!isForced) {
if (task.getClaimed() == null || task.getState() != TaskState.CLAIMED) {
LOGGER.warn("Method completeTask() does expect a task which need to be CLAIMED before. TaskId={}", taskId);
throw new InvalidStateException(taskId);
} else if (!CurrentUserContext.getAccessIds().contains(task.getOwner())) {
LOGGER.warn("Method completeTask() does expect to be invoced by the task-owner or a administrator. TaskId={}, TaskOwner={}, CurrentUser={}", taskId, task.getOwner(), CurrentUserContext.getUserid());
throw new InvalidOwnerException("TaskOwner is" + task.getOwner() + ", but current User is " + CurrentUserContext.getUserid());
}
} else {
// CLAIM-forced, if task was not already claimed before.
if (task.getClaimed() == null || task.getState() != TaskState.CLAIMED) {
task = (TaskImpl) this.claim(taskId, true);
}
}
Instant now = Instant.now();
task.setCompleted(now);
task.setModified(now);
task.setState(TaskState.COMPLETED);
task.setOwner(CurrentUserContext.getUserid());
taskMapper.update(task);
LOGGER.debug("Method completeTask() completed Task '{}'.", taskId);
} finally {
taskanaEngine.returnConnection();
LOGGER.debug("exit from completeTask()");
}
return task;
}
use of pro.taskana.exceptions.InvalidStateException in project taskana by Taskana.
the class TaskServiceImpl method completeTasks.
@Override
public BulkOperationResults<String, TaskanaException> completeTasks(List<String> taskIds) throws InvalidArgumentException {
try {
LOGGER.debug("entry to completeTasks(taskIds = {})", taskIds);
taskanaEngine.openConnection();
// Check pre-conditions with throwing Exceptions
if (taskIds == null) {
throw new InvalidArgumentException("TaskIds canĀ“t be used as NULL-Parameter.");
}
// process bulk-complete
BulkOperationResults<String, TaskanaException> bulkLog = new BulkOperationResults<>();
if (!taskIds.isEmpty()) {
// remove null/empty taskIds with message
Iterator<String> taskIdIterator = taskIds.iterator();
while (taskIdIterator.hasNext()) {
String currentTaskId = taskIdIterator.next();
if (currentTaskId == null || currentTaskId.isEmpty()) {
bulkLog.addError("", new InvalidArgumentException("IDs with EMPTY or NULL value are not allowed and invalid."));
taskIdIterator.remove();
}
}
// query for existing tasks, modify values and LOG missing ones.
List<TaskSummary> taskSummaries = this.createTaskQuery().idIn(taskIds.toArray(new String[0])).list();
Instant now = Instant.now();
taskIdIterator = taskIds.iterator();
while (taskIdIterator.hasNext()) {
String currentTaskId = taskIdIterator.next();
TaskSummaryImpl taskSummary = (TaskSummaryImpl) taskSummaries.stream().filter(ts -> currentTaskId.equals(ts.getTaskId())).findFirst().orElse(null);
if (taskSummary == null) {
bulkLog.addError(currentTaskId, new TaskNotFoundException(currentTaskId, "task with id " + currentTaskId + " was not found."));
taskIdIterator.remove();
} else if (taskSummary.getClaimed() == null || taskSummary.getState() != TaskState.CLAIMED) {
bulkLog.addError(currentTaskId, new InvalidStateException(currentTaskId));
taskIdIterator.remove();
} else if (!CurrentUserContext.getAccessIds().contains(taskSummary.getOwner())) {
bulkLog.addError(currentTaskId, new InvalidOwnerException("TaskOwner is" + taskSummary.getOwner() + ", but current User is " + CurrentUserContext.getUserid()));
taskIdIterator.remove();
} else {
taskSummary.setCompleted(now);
taskSummary.setModified(now);
taskSummary.setState(TaskState.COMPLETED);
}
}
if (!taskIds.isEmpty() && !taskSummaries.isEmpty()) {
taskMapper.updateCompleted(taskIds, (TaskSummaryImpl) taskSummaries.get(0));
}
}
return bulkLog;
} finally {
taskanaEngine.returnConnection();
LOGGER.debug("exit from to completeTasks(taskIds = {})", taskIds);
}
}
use of pro.taskana.exceptions.InvalidStateException in project taskana by Taskana.
the class WorkOnTaskAccTest method testBulkDeleteTasksWithException.
@WithAccessId(userName = "user_1_2", groupNames = { "group_1" })
@Test
public void testBulkDeleteTasksWithException() throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException, ConcurrencyException, AttachmentPersistenceException {
TaskService taskService = taskanaEngine.getTaskService();
List<String> taskIdList = new ArrayList<>();
taskIdList.add("TKI:000000000000000000000000000000000102");
taskIdList.add("TKI:000000000000000000000000000000003333");
BulkOperationResults<String, TaskanaException> results = taskService.deleteTasks(taskIdList);
assertTrue(results.containsErrors());
assertThat(results.getErrorMap().size(), equalTo(2));
assertTrue(results.getErrorForId("TKI:000000000000000000000000000000003333") instanceof TaskNotFoundException);
assertTrue(results.getErrorForId("TKI:000000000000000000000000000000000102") instanceof InvalidStateException);
}
Aggregations