From 9596875310cce1fb3849b9ea2d2010327e3acc25 Mon Sep 17 00:00:00 2001 From: "julian.schallenmueller" Date: Thu, 8 Nov 2018 11:56:48 +0100 Subject: [PATCH] TSK-734: Added new method signature for TaskService.updateTask --- .../main/java/pro/taskana/TaskService.java | 19 ++++++- .../pro/taskana/impl/TaskServiceImpl.java | 54 +++++++++++++++++++ .../acceptance/task/UpdateTaskAccTest.java | 32 +++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/lib/taskana-core/src/main/java/pro/taskana/TaskService.java b/lib/taskana-core/src/main/java/pro/taskana/TaskService.java index d6c8807b4..c3e275f12 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/TaskService.java +++ b/lib/taskana-core/src/main/java/pro/taskana/TaskService.java @@ -373,7 +373,7 @@ public interface TaskService { throws InvalidArgumentException; /** - * Completes tasks with a matching {@link ObjectReference}. + * Updates tasks with a matching {@link ObjectReference}. * * @param selectionCriteria * the {@link ObjectReference} that is used to select the tasks. @@ -388,4 +388,21 @@ public interface TaskService { */ List updateTasks(ObjectReference selectionCriteria, Map customFieldsToUpdate) throws InvalidArgumentException; + + /** + * Updates tasks with matching taskIds. + * + * @param taskIds + * the taskIds that are used to select the tasks. + * @param customFieldsToUpdate + * a {@link Map} that contains as key the identification of the custom field and as value the + * corresponding new value of that custom field. The key for identification of the custom field must be a + * String with value "1", "2" ... "16" as in the setCustomAttribute or getCustomAttribute method of + * {@link Task} + * @return a list of the Ids of all modified tasks + * @throws InvalidArgumentException + * If the customFieldsToUpdate map contains an invalid key or if the selectionCriteria is invalid + */ + List updateTasks(List taskIds, + Map customFieldsToUpdate) throws InvalidArgumentException; } diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java index 2907e9528..ea60cc944 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/TaskServiceImpl.java @@ -1127,6 +1127,60 @@ public class TaskServiceImpl implements TaskService { } + @Override + public List updateTasks(List taskIds, + Map customFieldsToUpdate) throws InvalidArgumentException { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("entry to updateTasks(taskIds = {}, customFieldsToUpdate = {})", taskIds, + customFieldsToUpdate); + } + + if (customFieldsToUpdate == null || customFieldsToUpdate.isEmpty()) { + throw new InvalidArgumentException("The customFieldsToUpdate argument to updateTasks must not be empty."); + } + + Set 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 entry : customFieldsToUpdate.entrySet()) { + String key = entry.getKey(); + if (!allowedKeys.contains(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 taskSummaries = createTaskQuery() + .idIn(taskIds.toArray(new String[taskIds.size()])) + .list(); + + List changedTasks = new ArrayList<>(); + if (!taskSummaries.isEmpty()) { + changedTasks = taskSummaries.stream().map(TaskSummary::getTaskId).collect(Collectors.toList()); + taskMapper.updateTasks(changedTasks, newTask, fieldSelector); + LOGGER.debug("updateTasks() updated the following tasks: {} ", + LoggerUtils.listToString(changedTasks)); + } else { + LOGGER.debug("updateTasks() found no tasks for update "); + } + return changedTasks; + } finally { + LOGGER.debug("exit from deleteTasks()."); + taskanaEngine.returnConnection(); + } + + } + private void validateObjectReference(ObjectReference objRef, String objRefType, String objName) throws InvalidArgumentException { // check that all values in the ObjectReference are set correctly diff --git a/lib/taskana-core/src/test/java/acceptance/task/UpdateTaskAccTest.java b/lib/taskana-core/src/test/java/acceptance/task/UpdateTaskAccTest.java index 04b1a5e1a..35e16bf38 100644 --- a/lib/taskana-core/src/test/java/acceptance/task/UpdateTaskAccTest.java +++ b/lib/taskana-core/src/test/java/acceptance/task/UpdateTaskAccTest.java @@ -12,6 +12,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.time.Instant; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -289,6 +290,37 @@ public class UpdateTaskAccTest extends AbstractAccTest { } + @WithAccessId( + userName = "teamlead_1", + groupNames = {"group_1"} + ) + @Test + public void testUpdateTasksById() + throws InvalidArgumentException, TaskNotFoundException, NotAuthorizedException { + List taskIds = Arrays.asList( + "TKI:000000000000000000000000000000000008", + "TKI:000000000000000000000000000000000009", + "TKI:000000000000000000000000000000000010"); + Map customProperties = new HashMap<>(); + customProperties.put("1", "This is modifiedValue 1"); + customProperties.put("5", "This is modifiedValue 5"); + customProperties.put("10", "This is modifiedValue 10"); + customProperties.put("12", "This is modifiedValue 12"); + TaskService taskService = taskanaEngine.getTaskService(); + + List changedTasks = taskService.updateTasks(taskIds, customProperties); + assertEquals(3, changedTasks.size()); + for (String taskId : changedTasks) { + Task task = taskService.getTask(taskId); + assertEquals("This is modifiedValue 1", task.getCustomAttribute("1")); + assertEquals("This is modifiedValue 5", task.getCustomAttribute("5")); + assertEquals("This is modifiedValue 10", task.getCustomAttribute("10")); + assertEquals("This is modifiedValue 12", task.getCustomAttribute("12")); + assertNull(task.getCustomAttribute("2")); + } + + } + @WithAccessId( userName = "user_1_1", groupNames = {"group_1"})