Search in sources :

Example 21 with InvalidArgumentException

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

the class TaskServiceImpl method transferTasks.

@Override
public BulkOperationResults<String, TaskanaException> transferTasks(String destinationWorkbasketId, List<String> taskIds) throws NotAuthorizedException, InvalidArgumentException, WorkbasketNotFoundException {
    try {
        taskanaEngine.openConnection();
        LOGGER.debug("entry to transferBulk(targetWbId = {}, taskIds = {})", destinationWorkbasketId, taskIds);
        // Check pre-conditions with trowing Exceptions
        if (destinationWorkbasketId == null || taskIds == null) {
            throw new InvalidArgumentException("DestinationWorkbasketId or TaskIds can´t be used as NULL-Parameter.");
        }
        Workbasket destinationWorkbasket = workbasketService.getWorkbasket(destinationWorkbasketId);
        return transferTasks(taskIds, destinationWorkbasket);
    } finally {
        LOGGER.debug("exit from transferBulk(targetWbKey = {}, taskIds = {})", destinationWorkbasketId, taskIds);
        taskanaEngine.returnConnection();
    }
}
Also used : InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) Workbasket(pro.taskana.Workbasket)

Example 22 with InvalidArgumentException

use of pro.taskana.exceptions.InvalidArgumentException 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 23 with InvalidArgumentException

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

the class TaskServiceImpl method createTask.

@Override
public Task createTask(Task taskToCreate) throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, TaskAlreadyExistException, InvalidArgumentException {
    LOGGER.debug("entry to createTask(task = {})", taskToCreate);
    TaskImpl task = (TaskImpl) taskToCreate;
    try {
        taskanaEngine.openConnection();
        if (task.getId() != "" && task.getId() != null) {
            throw new TaskAlreadyExistException(task.getId());
        } else {
            LOGGER.debug("Task {} cannot be be found, so it can be created.", task.getId());
            Workbasket workbasket;
            if (task.getWorkbasketSummary().getId() != null) {
                workbasket = workbasketService.getWorkbasket(task.getWorkbasketSummary().getId());
            } else if (task.getWorkbasketKey() != null) {
                workbasket = workbasketService.getWorkbasket(task.getWorkbasketKey(), task.getDomain());
            } else {
                throw new InvalidArgumentException("Cannot create a task outside a workbasket");
            }
            task.setWorkbasketSummary(workbasket.asSummary());
            task.setDomain(workbasket.getDomain());
            workbasketService.checkAuthorization(task.getWorkbasketSummary().getId(), WorkbasketPermission.APPEND);
            String classificationKey = task.getClassificationKey();
            if (classificationKey == null || classificationKey.length() == 0) {
                throw new InvalidArgumentException("classificationKey of task must not be empty");
            }
            Classification classification = this.classificationService.getClassification(classificationKey, workbasket.getDomain());
            task.setClassificationSummary(classification.asSummary());
            validateObjectReference(task.getPrimaryObjRef(), "primary ObjectReference", "Task");
            PrioDurationHolder prioDurationFromAttachments = handleAttachments(task);
            standardSettings(task, classification, prioDurationFromAttachments);
            this.taskMapper.insert(task);
            LOGGER.debug("Method createTask() created Task '{}'.", task.getId());
        }
        return task;
    } finally {
        taskanaEngine.returnConnection();
        LOGGER.debug("exit from createTask(task = {})", task);
    }
}
Also used : TaskAlreadyExistException(pro.taskana.exceptions.TaskAlreadyExistException) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) Classification(pro.taskana.Classification) Workbasket(pro.taskana.Workbasket)

Example 24 with InvalidArgumentException

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

the class TaskServiceImpl method updateTasks.

@Override
public List<String> updateTasks(ObjectReference selectionCriteria, Map<String, String> customFieldsToUpdate) throws InvalidArgumentException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("entry to updateTasks(selectionCriteria = {}, customFieldsToUpdate = {})", selectionCriteria, customFieldsToUpdate);
    }
    if (customFieldsToUpdate == null || customFieldsToUpdate.isEmpty()) {
        LOGGER.warn("The customFieldsToUpdate argument to updateTasks must not be empty. Throwing InvalidArgumentException.");
        throw new InvalidArgumentException("The customFieldsToUpdate argument to updateTasks must not be empty.");
    }
    validateObjectReference(selectionCriteria, "ObjectReference", "updateTasks call");
    Set<String> allowedKeys = new HashSet<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"));
    try {
        taskanaEngine.openConnection();
        CustomPropertySelector fieldSelector = new CustomPropertySelector();
        TaskImpl newTask = new TaskImpl();
        newTask.setModified(Instant.now());
        for (Map.Entry<String, String> entry : customFieldsToUpdate.entrySet()) {
            String key = entry.getKey();
            if (!allowedKeys.contains(key)) {
                LOGGER.warn("The customFieldsToUpdate argument to updateTasks contains invalid key {}.", key);
                throw new InvalidArgumentException("The customFieldsToUpdate argument to updateTasks contains invalid key " + key);
            } else {
                fieldSelector.setCustomProperty(key, true);
                newTask.setCustomAttribute(key, entry.getValue());
            }
        }
        // use query in order to find only those tasks that are visible to the current user
        List<TaskSummary> taskSummaries = createTaskQuery().primaryObjectReferenceCompanyIn(selectionCriteria.getCompany()).primaryObjectReferenceSystemIn(selectionCriteria.getSystem()).primaryObjectReferenceSystemInstanceIn(selectionCriteria.getSystemInstance()).primaryObjectReferenceTypeIn(selectionCriteria.getType()).primaryObjectReferenceValueIn(selectionCriteria.getValue()).list();
        List<String> taskIds = new ArrayList<>();
        if (!taskSummaries.isEmpty()) {
            taskIds = taskSummaries.stream().map(TaskSummary::getTaskId).collect(Collectors.toList());
            taskMapper.updateTasks(taskIds, newTask, fieldSelector);
            LOGGER.debug("updateTasks() updated the following tasks: {} ", LoggerUtils.listToString(taskIds));
        } else {
            LOGGER.debug("updateTasks() found no tasks for update ");
        }
        return taskIds;
    } finally {
        LOGGER.debug("exit from deleteTasks().");
        taskanaEngine.returnConnection();
    }
}
Also used : InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskSummary(pro.taskana.TaskSummary) ArrayList(java.util.ArrayList) Map(java.util.Map) HashSet(java.util.HashSet) CustomPropertySelector(pro.taskana.mappings.CustomPropertySelector)

Example 25 with InvalidArgumentException

use of pro.taskana.exceptions.InvalidArgumentException 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

InvalidArgumentException (pro.taskana.exceptions.InvalidArgumentException)41 Test (org.junit.Test)24 AbstractAccTest (acceptance.AbstractAccTest)21 WithAccessId (pro.taskana.security.WithAccessId)19 TaskService (pro.taskana.TaskService)18 TaskSummary (pro.taskana.TaskSummary)16 TaskanaEngineProxyForTest (pro.taskana.impl.TaskanaEngineProxyForTest)12 NotAuthorizedException (pro.taskana.exceptions.NotAuthorizedException)11 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Task (pro.taskana.Task)7 Workbasket (pro.taskana.Workbasket)7 Classification (pro.taskana.Classification)6 ClassificationNotFoundException (pro.taskana.exceptions.ClassificationNotFoundException)6 SQLException (java.sql.SQLException)5 ClassificationSummary (pro.taskana.ClassificationSummary)5 WorkbasketAccessItem (pro.taskana.WorkbasketAccessItem)5 WorkbasketService (pro.taskana.WorkbasketService)5 Instant (java.time.Instant)4 Collectors (java.util.stream.Collectors)4