Search in sources :

Example 1 with TaskNotFoundException

use of pro.taskana.exceptions.TaskNotFoundException in project taskana by Taskana.

the class TransferTaskAccTest method testBulkTransferTaskWithExceptions.

@WithAccessId(userName = "teamlead_1", groupNames = { "group_1" })
@Test
public void testBulkTransferTaskWithExceptions() throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException, InvalidStateException, InvalidOwnerException {
    TaskService taskService = taskanaEngine.getTaskService();
    Workbasket wb = taskanaEngine.getWorkbasketService().getWorkbasket("USER_1_1", "DOMAIN_A");
    Instant before = Instant.now();
    ArrayList<String> taskIdList = new ArrayList<>();
    // working
    taskIdList.add("TKI:000000000000000000000000000000000006");
    // NotAuthorized
    taskIdList.add("TKI:000000000000000000000000000000000041");
    // InvalidArgument
    taskIdList.add("");
    // InvalidArgument (added with ""), duplicate
    taskIdList.add(null);
    // TaskNotFound
    taskIdList.add("TKI:000000000000000000000000000000000099");
    BulkOperationResults<String, TaskanaException> results = taskService.transferTasks("WBI:100000000000000000000000000000000006", taskIdList);
    assertTrue(results.containsErrors());
    assertThat(results.getErrorMap().values().size(), equalTo(3));
    // react to result
    for (String taskId : results.getErrorMap().keySet()) {
        TaskanaException ex = results.getErrorForId(taskId);
        if (ex instanceof NotAuthorizedException) {
            System.out.println("NotAuthorizedException on bulkTransfer for taskId=" + taskId);
        } else if (ex instanceof InvalidArgumentException) {
            System.out.println("InvalidArgumentException on bulkTransfer for EMPTY/NULL taskId='" + taskId + "'");
        } else if (ex instanceof TaskNotFoundException) {
            System.out.println("TaskNotFoundException on bulkTransfer for taskId=" + taskId);
        } else {
            fail("Impossible failure Entry registered");
        }
    }
    Task transferredTask = taskService.getTask("TKI:000000000000000000000000000000000006");
    assertNotNull(transferredTask);
    assertTrue(transferredTask.isTransferred());
    assertFalse(transferredTask.isRead());
    assertEquals(TaskState.READY, transferredTask.getState());
    assertThat(transferredTask.getWorkbasketKey(), equalTo(wb.getKey()));
    assertThat(transferredTask.getDomain(), equalTo(wb.getDomain()));
    assertFalse(transferredTask.getModified().isBefore(before));
    assertThat(transferredTask.getOwner(), equalTo(null));
    transferredTask = taskService.getTask("TKI:000000000000000000000000000000000002");
    assertNotNull(transferredTask);
    assertFalse(transferredTask.isTransferred());
    assertEquals("USER_1_1", transferredTask.getWorkbasketKey());
}
Also used : Task(pro.taskana.Task) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskNotFoundException(pro.taskana.exceptions.TaskNotFoundException) TaskService(pro.taskana.TaskService) Instant(java.time.Instant) ArrayList(java.util.ArrayList) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) Workbasket(pro.taskana.Workbasket) TaskanaException(pro.taskana.exceptions.TaskanaException) AbstractAccTest(acceptance.AbstractAccTest) Test(org.junit.Test) WithAccessId(pro.taskana.security.WithAccessId)

Example 2 with TaskNotFoundException

use of pro.taskana.exceptions.TaskNotFoundException in project taskana by Taskana.

the class TaskServiceImpl method transferTasks.

private BulkOperationResults<String, TaskanaException> transferTasks(List<String> taskIds, Workbasket destinationWorkbasket) throws InvalidArgumentException {
    BulkOperationResults<String, TaskanaException> bulkLog = new BulkOperationResults<>();
    // check tasks Ids exist and not empty - log and remove
    Iterator<String> taskIdIterator = taskIds.iterator();
    while (taskIdIterator.hasNext()) {
        String currentTaskId = taskIdIterator.next();
        if (currentTaskId == null || currentTaskId.equals("")) {
            bulkLog.addError("", new InvalidArgumentException("IDs with EMPTY or NULL value are not allowed."));
            taskIdIterator.remove();
        }
    }
    // query for existing tasks. use taskMapper.findExistingTasks because this method
    // returns only the required information.
    List<MinimalTaskSummary> taskSummaries = taskMapper.findExistingTasks(taskIds);
    // check source WB (read)+transfer
    Set<String> workbasketIds = new HashSet<>();
    taskSummaries.stream().forEach(t -> workbasketIds.add(t.getWorkbasketId()));
    WorkbasketQueryImpl query = (WorkbasketQueryImpl) workbasketService.createWorkbasketQuery();
    query.setUsedToAugmentTasks(true);
    List<WorkbasketSummary> sourceWorkbaskets = query.callerHasPermission(WorkbasketPermission.TRANSFER).idIn(workbasketIds.toArray(new String[0])).list();
    taskIdIterator = taskIds.iterator();
    while (taskIdIterator.hasNext()) {
        String currentTaskId = taskIdIterator.next();
        MinimalTaskSummary taskSummary = taskSummaries.stream().filter(t -> currentTaskId.equals(t.getTaskId())).findFirst().orElse(null);
        if (taskSummary == null) {
            bulkLog.addError(currentTaskId, new TaskNotFoundException(currentTaskId, "Task with id " + currentTaskId + " was not found."));
            taskIdIterator.remove();
        } else if (!sourceWorkbaskets.stream().anyMatch(wb -> taskSummary.getWorkbasketId().equals(wb.getId()))) {
            bulkLog.addError(currentTaskId, new NotAuthorizedException("The workbasket of this task got not TRANSFER permissions. TaskId=" + currentTaskId));
            taskIdIterator.remove();
        }
    }
    // filter taskSummaries and update values
    taskSummaries = taskSummaries.stream().filter(ts -> taskIds.contains(ts.getTaskId())).collect(Collectors.toList());
    if (!taskSummaries.isEmpty()) {
        Instant now = Instant.now();
        TaskSummaryImpl updateObject = new TaskSummaryImpl();
        updateObject.setRead(false);
        updateObject.setTransferred(true);
        updateObject.setWorkbasketSummary(destinationWorkbasket.asSummary());
        updateObject.setDomain(destinationWorkbasket.getDomain());
        updateObject.setModified(now);
        updateObject.setState(TaskState.READY);
        updateObject.setOwner(null);
        taskMapper.updateTransfered(taskIds, updateObject);
    }
    return bulkLog;
}
Also used : Instant(java.time.Instant) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) TaskanaException(pro.taskana.exceptions.TaskanaException) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskNotFoundException(pro.taskana.exceptions.TaskNotFoundException) HashSet(java.util.HashSet) WorkbasketSummary(pro.taskana.WorkbasketSummary)

Example 3 with TaskNotFoundException

use of pro.taskana.exceptions.TaskNotFoundException in project taskana by Taskana.

the class TaskServiceImpl method getTask.

@Override
public Task getTask(String id) throws TaskNotFoundException, NotAuthorizedException {
    LOGGER.debug("entry to getTaskById(id = {})", id);
    TaskImpl resultTask = null;
    try {
        taskanaEngine.openConnection();
        resultTask = taskMapper.findById(id);
        if (resultTask != null) {
            WorkbasketQueryImpl query = (WorkbasketQueryImpl) workbasketService.createWorkbasketQuery();
            query.setUsedToAugmentTasks(true);
            String workbasketId = resultTask.getWorkbasketSummary().getId();
            List<WorkbasketSummary> workbaskets = query.idIn(workbasketId).list();
            if (workbaskets.isEmpty()) {
                String currentUser = CurrentUserContext.getUserid();
                LOGGER.error("The current user {} has no read permission for workbasket {}.", currentUser, workbasketId);
                throw new NotAuthorizedException("The current user " + currentUser + " has no read permission for workbasket " + workbasketId);
            } else {
                resultTask.setWorkbasketSummary(workbaskets.get(0));
            }
            List<AttachmentImpl> attachmentImpls = attachmentMapper.findAttachmentsByTaskId(resultTask.getId());
            if (attachmentImpls == null) {
                attachmentImpls = new ArrayList<>();
            }
            List<ClassificationSummary> classifications;
            try {
                classifications = findClassificationForTaskImplAndAttachments(resultTask, attachmentImpls);
            } catch (NotAuthorizedException e1) {
                LOGGER.error("ClassificationQuery unexpectedly returned NotauthorizedException. Throwing SystemException ");
                throw new SystemException("ClassificationQuery unexpectedly returned NotauthorizedException.");
            }
            List<Attachment> attachments = addClassificationSummariesToAttachments(resultTask, attachmentImpls, classifications);
            resultTask.setAttachments(attachments);
            String classificationId = resultTask.getClassificationSummary().getId();
            ClassificationSummary classification = classifications.stream().filter(c -> c.getId().equals(classificationId)).findFirst().orElse(null);
            if (classification == null) {
                LOGGER.error("Could not find a Classification for task {} ", resultTask);
                throw new SystemException("Could not find a Classification for task " + resultTask.getId());
            }
            resultTask.setClassificationSummary(classification);
            return resultTask;
        } else {
            LOGGER.warn("Method getTaskById() didn't find task with id {}. Throwing TaskNotFoundException", id);
            throw new TaskNotFoundException(id, "Task with id " + id + " was not found");
        }
    } finally {
        taskanaEngine.returnConnection();
        LOGGER.debug("exit from getTaskById(). Returning result {} ", resultTask);
    }
}
Also used : Attachment(pro.taskana.Attachment) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) SystemException(pro.taskana.exceptions.SystemException) TaskNotFoundException(pro.taskana.exceptions.TaskNotFoundException) ClassificationSummary(pro.taskana.ClassificationSummary) WorkbasketSummary(pro.taskana.WorkbasketSummary)

Example 4 with TaskNotFoundException

use of pro.taskana.exceptions.TaskNotFoundException in project taskana by Taskana.

the class TaskServiceImpl method classificationChanged.

BulkOperationResults<String, Exception> classificationChanged(String taskId, String classificationId) throws TaskNotFoundException, NotAuthorizedException, ClassificationNotFoundException {
    LOGGER.debug("entry to classificationChanged(taskId = {} , classificationId = {} )", taskId, classificationId);
    TaskImpl task = null;
    BulkOperationResults<String, Exception> bulkLog = new BulkOperationResults<>();
    try {
        taskanaEngine.openConnection();
        if (taskId == null || taskId.isEmpty() || classificationId == null || classificationId.isEmpty()) {
            return bulkLog;
        }
        task = taskMapper.findById(taskId);
        List<AttachmentImpl> attachmentImpls = attachmentMapper.findAttachmentsByTaskId(task.getId());
        if (attachmentImpls == null) {
            attachmentImpls = new ArrayList<>();
        }
        List<Attachment> attachments = augmentAttachmentsByClassification(attachmentImpls, bulkLog);
        task.setAttachments(attachments);
        Classification classification = classificationService.getClassification(classificationId);
        task.setClassificationSummary(classification.asSummary());
        PrioDurationHolder prioDurationFromAttachments = handleAttachmentsOnClassificationUpdate(task);
        updateClassificationRelatedProperties(task, task, prioDurationFromAttachments);
        task.setModified(Instant.now());
        taskMapper.update(task);
        return bulkLog;
    } finally {
        taskanaEngine.returnConnection();
        LOGGER.debug("exit from deleteTask(). ");
    }
}
Also used : Classification(pro.taskana.Classification) Attachment(pro.taskana.Attachment) PersistenceException(org.apache.ibatis.exceptions.PersistenceException) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) SystemException(pro.taskana.exceptions.SystemException) InvalidStateException(pro.taskana.exceptions.InvalidStateException) TaskNotFoundException(pro.taskana.exceptions.TaskNotFoundException) TaskAlreadyExistException(pro.taskana.exceptions.TaskAlreadyExistException) ClassificationNotFoundException(pro.taskana.exceptions.ClassificationNotFoundException) ConcurrencyException(pro.taskana.exceptions.ConcurrencyException) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException) InvalidWorkbasketException(pro.taskana.exceptions.InvalidWorkbasketException) AttachmentPersistenceException(pro.taskana.exceptions.AttachmentPersistenceException) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) TaskanaException(pro.taskana.exceptions.TaskanaException)

Example 5 with TaskNotFoundException

use of pro.taskana.exceptions.TaskNotFoundException in project taskana by Taskana.

the class TaskServiceImpl method deleteTasks.

@Override
public BulkOperationResults<String, TaskanaException> deleteTasks(List<String> taskIds) throws InvalidArgumentException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("entry to deleteTasks(tasks = {})", LoggerUtils.listToString(taskIds));
    }
    try {
        taskanaEngine.openConnection();
        if (taskIds == null) {
            throw new InvalidArgumentException("TaskIds canĀ“t be NULL as parameter for deleteTasks().");
        }
        BulkOperationResults<String, TaskanaException> bulkLog = new BulkOperationResults<>();
        List<MinimalTaskSummary> taskSummaries = taskMapper.findExistingTasks(taskIds);
        Iterator<String> taskIdIterator = taskIds.iterator();
        while (taskIdIterator.hasNext()) {
            String currentTaskId = taskIdIterator.next();
            if (currentTaskId == null || currentTaskId.equals("")) {
                bulkLog.addError("", new InvalidArgumentException("IDs with EMPTY or NULL value are not allowed."));
                taskIdIterator.remove();
            } else {
                MinimalTaskSummary foundSummary = taskSummaries.stream().filter(taskState -> currentTaskId.equals(taskState.getTaskId())).findFirst().orElse(null);
                if (foundSummary == null) {
                    bulkLog.addError(currentTaskId, new TaskNotFoundException(currentTaskId, "Task with id " + currentTaskId + " was not found."));
                    taskIdIterator.remove();
                } else {
                    if (!TaskState.COMPLETED.equals(foundSummary.getTaskState())) {
                        bulkLog.addError(currentTaskId, new InvalidStateException(currentTaskId));
                        taskIdIterator.remove();
                    }
                }
            }
        }
        if (!taskIds.isEmpty()) {
            taskMapper.deleteMultiple(taskIds);
        }
        return bulkLog;
    } finally {
        LOGGER.debug("exit from deleteTasks()");
        taskanaEngine.returnConnection();
    }
}
Also used : InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskNotFoundException(pro.taskana.exceptions.TaskNotFoundException) InvalidStateException(pro.taskana.exceptions.InvalidStateException) TaskanaException(pro.taskana.exceptions.TaskanaException)

Aggregations

TaskNotFoundException (pro.taskana.exceptions.TaskNotFoundException)7 TaskanaException (pro.taskana.exceptions.TaskanaException)6 InvalidArgumentException (pro.taskana.exceptions.InvalidArgumentException)5 NotAuthorizedException (pro.taskana.exceptions.NotAuthorizedException)5 InvalidStateException (pro.taskana.exceptions.InvalidStateException)4 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 Attachment (pro.taskana.Attachment)3 TaskService (pro.taskana.TaskService)3 WorkbasketSummary (pro.taskana.WorkbasketSummary)3 SystemException (pro.taskana.exceptions.SystemException)3 AbstractAccTest (acceptance.AbstractAccTest)2 HashSet (java.util.HashSet)2 PersistenceException (org.apache.ibatis.exceptions.PersistenceException)2 Test (org.junit.Test)2 Classification (pro.taskana.Classification)2 ClassificationSummary (pro.taskana.ClassificationSummary)2 Task (pro.taskana.Task)2 AttachmentPersistenceException (pro.taskana.exceptions.AttachmentPersistenceException)2 ClassificationNotFoundException (pro.taskana.exceptions.ClassificationNotFoundException)2