Skip to main content
summaryrefslogtreecommitdiffstats
blob: a88ec53869e633ffa85f9292bf703b3ed556a0fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*******************************************************************************
 * Copyright (c) 2004, 2011 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.ui;

import java.text.MessageFormat;
import java.util.Date;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylyn.internal.tasks.ui.util.TaskOpenEvent;
import org.eclipse.mylyn.internal.tasks.ui.util.TaskOpenListener;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;

/**
 * @author Mik Kersten
 * @author Steffen Pingel
 */
public class OpenRepositoryTaskJob extends Job {

	private final String repositoryUrl;

	private final String repositoryKind;

	private final String taskId;

	private final String taskUrl;

	private ITask task;

	private TaskOpenListener listener;

	private final long timestamp;

	private String taskKey;

	private TaskRepository repository;

	/**
	 * Creates a job that searches for a task with the given task <i>key</i> and opens it if found.
	 */
	public OpenRepositoryTaskJob(TaskRepository repository, String taskKey, String taskUrl, IWorkbenchPage page) {
		super(MessageFormat.format(Messages.OpenRepositoryTaskJob_Opening_repository_task_X, taskKey));
		this.repositoryKind = repository.getConnectorKind();
		this.taskId = null;
		this.repositoryUrl = repository.getRepositoryUrl();
		this.taskUrl = taskUrl;
		this.timestamp = 0;
		this.repository = repository;
		this.taskKey = taskKey;
	}

	/**
	 * Creates a job that fetches a task with the given task id and opens it.
	 */
	public OpenRepositoryTaskJob(String repositoryKind, String repositoryUrl, String taskId, String taskUrl,
			IWorkbenchPage page) {
		this(repositoryKind, repositoryUrl, taskId, taskUrl, 0, page);
	}

	/**
	 * Creates a job that fetches a task with the given task id and opens it, expanding all comments made after the
	 * given timestamp.
	 */
	public OpenRepositoryTaskJob(String repositoryKind, String repositoryUrl, String taskId, String taskUrl,
			long timestamp, IWorkbenchPage page) {
		super(MessageFormat.format(Messages.OpenRepositoryTaskJob_Opening_repository_task_X, taskId));

		this.repositoryKind = repositoryKind;
		this.taskId = taskId;
		this.repositoryUrl = repositoryUrl;
		this.taskUrl = taskUrl;
		this.timestamp = timestamp;
	}

	/**
	 * Creates a job that fetches a task with the given task id or key and opens it.
	 */
	public OpenRepositoryTaskJob(IWorkbenchPage page, String repositoryKind, String repositoryUrl, String taskIdOrKey,
			String taskUrl) {
		this(repositoryKind, repositoryUrl, taskIdOrKey, taskUrl, page);
		taskKey = taskIdOrKey;
	}

	/**
	 * Returns the task if it was created when opening
	 *
	 * @return
	 */
	public ITask getTask() {
		return task;
	}

	public void setListener(TaskOpenListener listener) {
		this.listener = listener;
	}

	@Override
	public IStatus run(IProgressMonitor monitor) {
		monitor.beginTask(Messages.OpenRepositoryTaskJob_Opening_Remote_Task, 10);
		if (repository == null) {
			repository = TasksUi.getRepositoryManager().getRepository(repositoryKind, repositoryUrl);
		}
		if (repository == null) {
			PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
				public void run() {
					MessageDialog.openError(null, Messages.OpenRepositoryTaskJob_Repository_Not_Found,
							MessageFormat.format(
									Messages.OpenRepositoryTaskJob_Could_not_find_repository_configuration_for_X,
									repositoryUrl) + "\n" + //$NON-NLS-1$
							MessageFormat.format(Messages.OpenRepositoryTaskJob_Please_set_up_repository_via_X,
									Messages.TasksUiPlugin_Task_Repositories));
					TasksUiUtil.openUrl(taskUrl);
				}

			});
			return Status.OK_STATUS;
		}

		AbstractRepositoryConnector connector = TasksUi.getRepositoryManager().getRepositoryConnector(repositoryKind);
		try {
			final TaskData taskData = getTaskData(connector, monitor);
			if (taskData != null) {
				task = TasksUi.getRepositoryModel().createTask(repository, taskData.getTaskId());
				TasksUiPlugin.getTaskDataManager().putUpdatedTaskData(task, taskData, true);
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

					public void run() {
						TaskOpenEvent event = TasksUiInternal.openTask(task, taskId);
						if (listener != null && event != null) {
							listener.taskOpened(event);
						}
						if (timestamp != 0 && event != null) {
							List<TaskAttribute> commentAttributes = taskData.getAttributeMapper()
									.getAttributesByType(taskData, TaskAttribute.TYPE_COMMENT);
							if (commentAttributes.size() > 0) {
								for (TaskAttribute commentAttribute : commentAttributes) {
									TaskAttribute commentCreateDate = commentAttribute
											.getMappedAttribute(TaskAttribute.COMMENT_DATE);
									if (commentCreateDate != null) {
										Date dateValue = taskData.getAttributeMapper().getDateValue(commentCreateDate);
										if (dateValue.getTime() < timestamp) {
											continue;
										}
										TaskAttribute dn = commentAttribute
												.getMappedAttribute(TaskAttribute.COMMENT_NUMBER);
										TaskEditor editor = (TaskEditor) event.getEditor();
										if (dn != null) {
											editor.selectReveal(TaskAttribute.PREFIX_COMMENT + dn.getValue());
										}
										break;
									}
								}
							}
						}
					}
				});
			} else {
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
					public void run() {
						TasksUiUtil.openUrl(taskUrl);
					}
				});
			}
		} catch (final CoreException e) {
			PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
				public void run() {
					TasksUiInternal.displayStatus(Messages.OpenRepositoryTaskJob_Unable_to_open_task, e.getStatus());
				}
			});
		} finally {
			monitor.done();
		}
		return Status.OK_STATUS;
	}

	TaskData getTaskData(AbstractRepositoryConnector connector, IProgressMonitor monitor) throws CoreException {
		if (taskId != null && taskKey != null && connector.supportsSearchByTaskKey(repository)) {
			try {
				TaskData data = getTaskDataByKey(connector, monitor);
				if (data != null) {
					return data;
				}
			} catch (CoreException | RuntimeException e) {
			}
			try {
				return connector.getTaskData(repository, taskId, monitor);
			} catch (CoreException | RuntimeException e) {
				// do not display connector's message because it may be about using a task key as an ID which is not the real error
				throw new CoreException(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
						NLS.bind("Could not find task with ID \"{0}\" on repository {1}.", taskId, repository), e)); //$NON-NLS-1$
			}
		} else if (taskId != null) {
			return connector.getTaskData(repository, taskId, monitor);
		} else if (taskKey != null && connector.supportsSearchByTaskKey(repository)) {
			return getTaskDataByKey(connector, monitor);
		}
		return null;
	}

	private TaskData getTaskDataByKey(AbstractRepositoryConnector connector, IProgressMonitor monitor)
			throws CoreException {
		TaskData searchTaskData = connector.searchByTaskKey(repository, taskKey, monitor);
		if (searchTaskData != null && searchTaskData.isPartial()) {
			return connector.getTaskData(repository, searchTaskData.getTaskId(), monitor);
		}
		return searchTaskData;
	}

}

Back to the top