TSK-734: Refactored updateTasks method

This commit is contained in:
julian.schallenmueller 2018-11-08 17:06:15 +01:00 committed by Mustapha Zorgati
parent 9596875310
commit 51af51196e
1 changed files with 64 additions and 59 deletions

View File

@ -1063,7 +1063,7 @@ public class TaskServiceImpl implements TaskService {
} }
return bulkLog; return bulkLog;
} finally { } finally {
LOGGER.debug("exit from deleteTasks()"); LOGGER.debug("exit from updateTasks()");
taskanaEngine.returnConnection(); taskanaEngine.returnConnection();
} }
} }
@ -1075,53 +1075,29 @@ public class TaskServiceImpl implements TaskService {
LOGGER.debug("entry to updateTasks(selectionCriteria = {}, customFieldsToUpdate = {})", selectionCriteria, LOGGER.debug("entry to updateTasks(selectionCriteria = {}, customFieldsToUpdate = {})", selectionCriteria,
customFieldsToUpdate); customFieldsToUpdate);
} }
if (customFieldsToUpdate == null || customFieldsToUpdate.isEmpty()) {
throw new InvalidArgumentException("The customFieldsToUpdate argument to updateTasks must not be empty.");
}
validateObjectReference(selectionCriteria, "ObjectReference", "updateTasks call"); validateObjectReference(selectionCriteria, "ObjectReference", "updateTasks call");
validateCustomFields(customFieldsToUpdate);
Set<String> allowedKeys = new HashSet<>( CustomPropertySelector fieldSelector = new CustomPropertySelector();
Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16")); TaskImpl updated = initUpdatedTask(customFieldsToUpdate, fieldSelector);
try { try {
taskanaEngine.openConnection(); 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)) {
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 // use query in order to find only those tasks that are visible to the current user
List<TaskSummary> taskSummaries = createTaskQuery() List<TaskSummary> taskSummaries = getTasksToChange(selectionCriteria);
.primaryObjectReferenceCompanyIn(selectionCriteria.getCompany())
.primaryObjectReferenceSystemIn(selectionCriteria.getSystem())
.primaryObjectReferenceSystemInstanceIn(selectionCriteria.getSystemInstance())
.primaryObjectReferenceTypeIn(selectionCriteria.getType())
.primaryObjectReferenceValueIn(selectionCriteria.getValue())
.list();
List<String> taskIds = new ArrayList<>(); List<String> changedTasks = new ArrayList<>();
if (!taskSummaries.isEmpty()) { if (!taskSummaries.isEmpty()) {
taskIds = taskSummaries.stream().map(TaskSummary::getTaskId).collect(Collectors.toList()); changedTasks = taskSummaries.stream().map(TaskSummary::getTaskId).collect(Collectors.toList());
taskMapper.updateTasks(taskIds, newTask, fieldSelector); taskMapper.updateTasks(changedTasks, updated, fieldSelector);
LOGGER.debug("updateTasks() updated the following tasks: {} ", LOGGER.debug("updateTasks() updated the following tasks: {} ",
LoggerUtils.listToString(taskIds)); LoggerUtils.listToString(changedTasks));
} else { } else {
LOGGER.debug("updateTasks() found no tasks for update "); LOGGER.debug("updateTasks() found no tasks for update ");
} }
return taskIds; return changedTasks;
} finally { } finally {
LOGGER.debug("exit from deleteTasks()."); LOGGER.debug("exit from updateTasks().");
taskanaEngine.returnConnection(); taskanaEngine.returnConnection();
} }
@ -1135,39 +1111,20 @@ public class TaskServiceImpl implements TaskService {
customFieldsToUpdate); customFieldsToUpdate);
} }
if (customFieldsToUpdate == null || customFieldsToUpdate.isEmpty()) { validateCustomFields(customFieldsToUpdate);
throw new InvalidArgumentException("The customFieldsToUpdate argument to updateTasks must not be empty."); CustomPropertySelector fieldSelector = new CustomPropertySelector();
} TaskImpl updatedTask = initUpdatedTask(customFieldsToUpdate, fieldSelector);
Set<String> allowedKeys = new HashSet<>(
Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"));
try { try {
taskanaEngine.openConnection(); 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)) {
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 // use query in order to find only those tasks that are visible to the current user
List<TaskSummary> taskSummaries = createTaskQuery() List<TaskSummary> taskSummaries = getTasksToChange(taskIds);
.idIn(taskIds.toArray(new String[taskIds.size()]))
.list();
List<String> changedTasks = new ArrayList<>(); List<String> changedTasks = new ArrayList<>();
if (!taskSummaries.isEmpty()) { if (!taskSummaries.isEmpty()) {
changedTasks = taskSummaries.stream().map(TaskSummary::getTaskId).collect(Collectors.toList()); changedTasks = taskSummaries.stream().map(TaskSummary::getTaskId).collect(Collectors.toList());
taskMapper.updateTasks(changedTasks, newTask, fieldSelector); taskMapper.updateTasks(changedTasks, updatedTask, fieldSelector);
LOGGER.debug("updateTasks() updated the following tasks: {} ", LOGGER.debug("updateTasks() updated the following tasks: {} ",
LoggerUtils.listToString(changedTasks)); LoggerUtils.listToString(changedTasks));
} else { } else {
@ -1181,6 +1138,54 @@ public class TaskServiceImpl implements TaskService {
} }
private TaskImpl initUpdatedTask(Map<String, String> customFieldsToUpdate, CustomPropertySelector fieldSelector)
throws InvalidArgumentException {
TaskImpl newTask = new TaskImpl();
newTask.setModified(Instant.now());
for (Map.Entry<String, String> entry : customFieldsToUpdate.entrySet()) {
String key = entry.getKey();
fieldSelector.setCustomProperty(key, true);
newTask.setCustomAttribute(key, entry.getValue());
}
return newTask;
}
private void validateCustomFields(Map<String, String> customFieldsToUpdate) throws InvalidArgumentException {
if (customFieldsToUpdate == null || customFieldsToUpdate.isEmpty()) {
throw new InvalidArgumentException("The customFieldsToUpdate argument to updateTasks must not be empty.");
}
Set<String> allowedKeys = new HashSet<>(
Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"));
for (Map.Entry<String, String> entry : customFieldsToUpdate.entrySet()) {
String key = entry.getKey();
if (!allowedKeys.contains(key)) {
throw new InvalidArgumentException(
"The customFieldsToUpdate argument to updateTasks contains invalid key " + key);
}
}
}
private List<TaskSummary> getTasksToChange(List<String> taskIds) {
List<TaskSummary> taskSummaries = createTaskQuery()
.idIn(taskIds.toArray(new String[taskIds.size()]))
.list();
return taskSummaries;
}
private List<TaskSummary> getTasksToChange(ObjectReference selectionCriteria) {
List<TaskSummary> taskSummaries = createTaskQuery()
.primaryObjectReferenceCompanyIn(selectionCriteria.getCompany())
.primaryObjectReferenceSystemIn(selectionCriteria.getSystem())
.primaryObjectReferenceSystemInstanceIn(selectionCriteria.getSystemInstance())
.primaryObjectReferenceTypeIn(selectionCriteria.getType())
.primaryObjectReferenceValueIn(selectionCriteria.getValue())
.list();
return taskSummaries;
}
private void validateObjectReference(ObjectReference objRef, String objRefType, String objName) private void validateObjectReference(ObjectReference objRef, String objRefType, String objName)
throws InvalidArgumentException { throws InvalidArgumentException {
// check that all values in the ObjectReference are set correctly // check that all values in the ObjectReference are set correctly