Search in sources :

Example 1 with InvalidOwnerException

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

the class TaskServiceImpl method cancelClaim.

@Override
public Task cancelClaim(String taskId, boolean forceUnclaim) throws TaskNotFoundException, InvalidStateException, InvalidOwnerException, NotAuthorizedException {
    String userId = CurrentUserContext.getUserid();
    LOGGER.debug("entry to cancelClaim(taskId = {}) with userId = {}, forceFlag = {}", taskId, userId, forceUnclaim);
    TaskImpl task = null;
    try {
        taskanaEngine.openConnection();
        task = (TaskImpl) getTask(taskId);
        TaskState state = task.getState();
        if (state == TaskState.COMPLETED) {
            LOGGER.warn("Method cancelClaim() found that task {} is already completed. Throwing InvalidStateException", taskId);
            throw new InvalidStateException("Task is already completed");
        }
        if (state == TaskState.CLAIMED && !forceUnclaim && !userId.equals(task.getOwner())) {
            LOGGER.warn("Method cancelClaim() found that task {} is claimed by {} and forceClaim is false. Throwing InvalidOwnerException", taskId, task.getOwner());
            throw new InvalidOwnerException("Task is already claimed by an other user = " + task.getOwner());
        }
        Instant now = Instant.now();
        task.setOwner(null);
        task.setModified(now);
        task.setClaimed(null);
        task.setRead(true);
        task.setState(TaskState.READY);
        taskMapper.update(task);
        LOGGER.debug("Method cancelClaim() unclaimed task '{}' for user '{}'.", taskId, userId);
    } finally {
        taskanaEngine.returnConnection();
        LOGGER.debug("exit from cancelClaim(taskId = {}) with userId = {}, forceFlag = {}", taskId, userId, forceUnclaim);
    }
    return task;
}
Also used : Instant(java.time.Instant) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException) InvalidStateException(pro.taskana.exceptions.InvalidStateException) TaskState(pro.taskana.TaskState)

Example 2 with InvalidOwnerException

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

the class TaskServiceImplTest method testCompleteTaskNotForcedInvalidOwnerException.

@Test(expected = InvalidOwnerException.class)
public void testCompleteTaskNotForcedInvalidOwnerException() throws TaskNotFoundException, InvalidStateException, InvalidOwnerException, ClassificationNotFoundException, NotAuthorizedException {
    final boolean isForced = false;
    TaskServiceImpl cutSpy = Mockito.spy(cut);
    Classification dummyClassification = createDummyClassification();
    TaskImpl task = createUnitTestTask("1", "Unit Test Task 1", "1", dummyClassification);
    task.setOwner("Dummy-Owner-ID: 10");
    task.setState(TaskState.CLAIMED);
    task.setClaimed(Instant.now());
    doReturn(task).when(cutSpy).getTask(task.getId());
    try {
        cutSpy.completeTask(task.getId(), isForced);
    } catch (InvalidOwnerException e) {
        verify(taskanaEngineMock, times(1)).openConnection();
        verify(cutSpy, times(1)).getTask(task.getId());
        verify(taskanaEngineMock, times(1)).returnConnection();
        verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, classificationQueryImplMock);
        throw e;
    }
}
Also used : Classification(pro.taskana.Classification) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with InvalidOwnerException

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

the class TaskServiceImpl method claim.

@Override
public Task claim(String taskId, boolean forceClaim) throws TaskNotFoundException, InvalidStateException, InvalidOwnerException, NotAuthorizedException {
    String userId = CurrentUserContext.getUserid();
    LOGGER.debug("entry to claim(id = {}, forceClaim = {}, userId = {})", taskId, forceClaim, userId);
    TaskImpl task = null;
    try {
        taskanaEngine.openConnection();
        task = (TaskImpl) getTask(taskId);
        TaskState state = task.getState();
        if (state == TaskState.COMPLETED) {
            LOGGER.warn("Method claim() found that task {} is already completed. Throwing InvalidStateException", taskId);
            throw new InvalidStateException("Task is already completed");
        }
        if (state == TaskState.CLAIMED && !forceClaim && !task.getOwner().equals(userId)) {
            LOGGER.warn("Method claim() found that task {} is claimed by {} and forceClaim is false. Throwing InvalidOwnerException", taskId, task.getOwner());
            throw new InvalidOwnerException("You´re not the owner of this task and it is already claimed by other user " + task.getOwner());
        }
        Instant now = Instant.now();
        task.setOwner(userId);
        task.setModified(now);
        task.setClaimed(now);
        task.setRead(true);
        task.setState(TaskState.CLAIMED);
        taskMapper.update(task);
        LOGGER.debug("Method claim() claimed task '{}' for user '{}'.", taskId, userId);
    } finally {
        taskanaEngine.returnConnection();
        LOGGER.debug("exit from claim()");
    }
    return task;
}
Also used : Instant(java.time.Instant) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException) InvalidStateException(pro.taskana.exceptions.InvalidStateException) TaskState(pro.taskana.TaskState)

Example 4 with InvalidOwnerException

use of pro.taskana.exceptions.InvalidOwnerException 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;
}
Also used : Instant(java.time.Instant) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException) InvalidStateException(pro.taskana.exceptions.InvalidStateException)

Example 5 with InvalidOwnerException

use of pro.taskana.exceptions.InvalidOwnerException 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);
    }
}
Also used : Arrays(java.util.Arrays) PersistenceException(org.apache.ibatis.exceptions.PersistenceException) IdGenerator(pro.taskana.impl.util.IdGenerator) LoggerFactory(org.slf4j.LoggerFactory) WorkbasketService(pro.taskana.WorkbasketService) ArrayList(java.util.ArrayList) CurrentUserContext(pro.taskana.security.CurrentUserContext) HashSet(java.util.HashSet) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) SystemException(pro.taskana.exceptions.SystemException) CustomPropertySelector(pro.taskana.mappings.CustomPropertySelector) Task(pro.taskana.Task) InvalidStateException(pro.taskana.exceptions.InvalidStateException) Duration(java.time.Duration) Map(java.util.Map) TaskState(pro.taskana.TaskState) WorkbasketPermission(pro.taskana.WorkbasketPermission) WorkbasketSummary(pro.taskana.WorkbasketSummary) TimeIntervalColumnHeader(pro.taskana.impl.report.impl.TimeIntervalColumnHeader) ClassificationSummary(pro.taskana.ClassificationSummary) TaskNotFoundException(pro.taskana.exceptions.TaskNotFoundException) TaskanaEngine(pro.taskana.TaskanaEngine) Attachment(pro.taskana.Attachment) TaskAlreadyExistException(pro.taskana.exceptions.TaskAlreadyExistException) TaskSummary(pro.taskana.TaskSummary) ClassificationNotFoundException(pro.taskana.exceptions.ClassificationNotFoundException) ConcurrencyException(pro.taskana.exceptions.ConcurrencyException) Logger(org.slf4j.Logger) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException) Iterator(java.util.Iterator) Set(java.util.Set) InvalidWorkbasketException(pro.taskana.exceptions.InvalidWorkbasketException) AttachmentMapper(pro.taskana.mappings.AttachmentMapper) Classification(pro.taskana.Classification) LoggerUtils(pro.taskana.impl.util.LoggerUtils) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) AttachmentPersistenceException(pro.taskana.exceptions.AttachmentPersistenceException) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskService(pro.taskana.TaskService) TaskMapper(pro.taskana.mappings.TaskMapper) List(java.util.List) Workbasket(pro.taskana.Workbasket) TaskanaRole(pro.taskana.TaskanaRole) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) TaskanaException(pro.taskana.exceptions.TaskanaException) TaskQuery(pro.taskana.TaskQuery) Collections(java.util.Collections) Instant(java.time.Instant) InvalidStateException(pro.taskana.exceptions.InvalidStateException) TaskanaException(pro.taskana.exceptions.TaskanaException) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskNotFoundException(pro.taskana.exceptions.TaskNotFoundException) TaskSummary(pro.taskana.TaskSummary) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException)

Aggregations

InvalidOwnerException (pro.taskana.exceptions.InvalidOwnerException)5 Instant (java.time.Instant)4 InvalidStateException (pro.taskana.exceptions.InvalidStateException)4 TaskState (pro.taskana.TaskState)3 Classification (pro.taskana.Classification)2 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 PersistenceException (org.apache.ibatis.exceptions.PersistenceException)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1