Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Davis2015-11-27 16:39:33 -0500
committerGerrit Code Review @ Eclipse.org2015-11-27 18:02:26 -0500
commit11fb814632a825d3ee291ff6fdd53dd0a3a627ad (patch)
tree5a662c440347d60f7ec178be0e0a1fa54daac9bb /org.eclipse.mylyn.tasks.core/src/org
parentb9b67b4a85c973105e45521c4aa66b51754a0e26 (diff)
downloadorg.eclipse.mylyn.tasks-11fb814632a825d3ee291ff6fdd53dd0a3a627ad.tar.gz
org.eclipse.mylyn.tasks-11fb814632a825d3ee291ff6fdd53dd0a3a627ad.tar.xz
org.eclipse.mylyn.tasks-11fb814632a825d3ee291ff6fdd53dd0a3a627ad.zip
move task lookup logic to core
Diffstat (limited to 'org.eclipse.mylyn.tasks.core/src/org')
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TasksCoreUtil.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TasksCoreUtil.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TasksCoreUtil.java
new file mode 100644
index 000000000..776e2a234
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/util/TasksCoreUtil.java
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.tasks.core.util;
+
+import java.net.URL;
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
+import org.eclipse.mylyn.internal.tasks.core.TaskList;
+import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
+import org.eclipse.mylyn.tasks.core.IRepositoryManager;
+import org.eclipse.mylyn.tasks.core.TaskRepository;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.Ordering;
+
+public class TasksCoreUtil {
+ /**
+ * Searches for a task whose URL matches
+ *
+ * @return first task with a matching URL.
+ */
+ public static AbstractTask getTaskByUrl(TaskList taskList, IRepositoryManager repositoryManager, String taskUrl) {
+ if (!Strings.isNullOrEmpty(taskUrl)) {
+ Collection<AbstractTask> tasks = taskList.getAllTasks();
+ List<AbstractTask> sortedTasks = sortTasksByRepositoryUrl(tasks);
+
+ AbstractRepositoryConnector connector = null;
+ TaskRepository repository = null;
+
+ for (AbstractTask task : sortedTasks) {
+ if (taskUrl.equals(task.getUrl())) {
+ return task;
+ } else {
+ String repositoryUrl = task.getRepositoryUrl();
+ if (repositoryUrl != null) {
+ if (repository == null || !repositoryUrl.equals(repository.getUrl())) {
+ connector = repositoryManager.getRepositoryConnector(task.getConnectorKind());
+ repository = repositoryManager.getRepository(task.getConnectorKind(),
+ task.getRepositoryUrl());
+ }
+
+ if (connector != null) {
+ URL url = connector.getBrowserUrl(repository, task);
+ if (url != null && taskUrl.equals(url.toString())) {
+ return task;
+ }
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ private static List<AbstractTask> sortTasksByRepositoryUrl(Collection<AbstractTask> tasks) {
+ List<AbstractTask> sortedTasks = new Ordering<AbstractTask>() {
+
+ @Override
+ public int compare(AbstractTask left, AbstractTask right) {
+ if (left.getRepositoryUrl() == null) {
+ return 1;
+ }
+ if (right.getRepositoryUrl() == null) {
+ return -1;
+ }
+ return left.getRepositoryUrl().compareTo(right.getRepositoryUrl());
+ }
+
+ }.nullsLast().sortedCopy(tasks);
+ return sortedTasks;
+ }
+}

Back to the top