Skip to main content
summaryrefslogtreecommitdiffstats
blob: cd2671e891305a785f9d71d4765fa3d1491d703d (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/

package org.eclipse.mylyn.tasks.ui;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiImages;
import org.eclipse.mylyn.monitor.core.StatusHandler;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.core.AbstractTaskDataHandler;
import org.eclipse.mylyn.tasks.core.RepositoryTaskData;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.AbstractTask.RepositoryTaskSyncState;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressConstants;

/**
 * Not API.
 * 
 * @author Mik Kersten
 * @author Rob Elves
 * @author Steffen Pingel
 */
class SynchronizeTaskJob extends Job {

	private static final String LABEL_SYNCHRONIZING = "Synchronizing task ";

	private static final String LABEL_SYNCHRONIZE_TASK = "Task Synchronization";

	private final AbstractRepositoryConnector connector;

	private Set<AbstractTask> repositoryTasks;

	private boolean forced = false;

	private AbstractTaskDataHandler taskDataHandler;

	private Map<TaskRepository, Set<AbstractTask>> repToTasks;

	public SynchronizeTaskJob(AbstractRepositoryConnector connector, Set<AbstractTask> repositoryTasks) {
		super(LABEL_SYNCHRONIZE_TASK + " (" + repositoryTasks.size() + " tasks)");
		this.connector = connector;
		this.repositoryTasks = repositoryTasks;
		this.taskDataHandler = connector.getTaskDataHandler();
	}

	/**
	 * Returns true, if synchronization was triggered manually and not by an automatic background job.
	 */
	public boolean isForced() {
		return forced;
	}

	/**
	 * Indicates a manual synchronization. If set to true, a dialog will be displayed in case of errors.
	 */
	public void setForced(boolean forced) {
		this.forced = forced;
	}

	@Override
	public IStatus run(IProgressMonitor monitor) {
		try {
			repToTasks = new HashMap<TaskRepository, Set<AbstractTask>>();
			monitor.beginTask(LABEL_SYNCHRONIZING, repositoryTasks.size());
			setProperty(IProgressConstants.ICON_PROPERTY, TasksUiImages.REPOSITORY_SYNCHRONIZE);

			// sort tasks by repository
			for (final AbstractTask repositoryTask : repositoryTasks) {

				if (monitor.isCanceled()) {
					break;
				}

				if (taskDataHandler != null && taskDataHandler.canGetMultiTaskData()) {
					// Multi synch supported...
					TaskRepository repository = TasksUiPlugin.getRepositoryManager().getRepository(
							repositoryTask.getConnectorKind(), repositoryTask.getRepositoryUrl());

					if (repository == null) {
						repositoryTask.setSynchronizationStatus(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, 0,
								"Associated repository could not be found. Ensure proper repository configuration of "
										+ repositoryTask.getRepositoryUrl() + " in "
										+ TasksUiPlugin.LABEL_VIEW_REPOSITORIES + ".", null));
						continue;
					}

					Set<AbstractTask> tasks = repToTasks.get(repository);
					if (tasks == null) {
						tasks = new HashSet<AbstractTask>();
						repToTasks.put(repository, tasks);
					}
					repositoryTask.setSynchronizationStatus(null);
					tasks.add(repositoryTask);
					repositoryTask.setSynchronizing(true);
					TasksUiPlugin.getTaskListManager().getTaskList().notifyTaskChanged(repositoryTask, false);
				} else {
					// Single synch supported...
					synchronizeTask(monitor, repositoryTask);
				}
			}

			if (monitor.isCanceled()) {

				for (final AbstractTask repositoryTask : repositoryTasks) {
					repositoryTask.setSynchronizing(false);
					TasksUiPlugin.getTaskListManager().getTaskList().notifyTaskChanged(repositoryTask, false);
				}

				return Status.CANCEL_STATUS;
			}

			// synchronize tasks per repository if multi-sync is supported 
			for (TaskRepository repository : repToTasks.keySet()) {
				Set<AbstractTask> tasksToSynch = repToTasks.get(repository);
				try {
					synchronizeTasks(new SubProgressMonitor(monitor, tasksToSynch.size()), repository, tasksToSynch);
				} catch (final CoreException e) {
					for (AbstractTask task : tasksToSynch) {
						updateStatus(repository, task, e.getStatus());
					}
					if (forced) {
						PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
							public void run() {
								StatusHandler.displayStatus("Task Synchronization Failed", e.getStatus());
							}
						});
					}
				}
			}
		} catch (OperationCanceledException e) {
			return Status.CANCEL_STATUS;
		} catch (Exception e) {
			StatusHandler.fail(e, "Synchronization failed", false);
		} finally {
			monitor.done();
		}

		return Status.OK_STATUS;
	}

	private void synchronizeTask(IProgressMonitor monitor, AbstractTask task) {
		monitor.subTask(task.getSummary());

		task.setSynchronizationStatus(null);

		final TaskRepository repository = TasksUiPlugin.getRepositoryManager().getRepository(task.getConnectorKind(),
				task.getRepositoryUrl());
		try {
			if (repository == null) {
				throw new CoreException(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, 0,
						"Associated repository could not be found. Ensure proper repository configuration of "
								+ task.getRepositoryUrl() + " in " + TasksUiPlugin.LABEL_VIEW_REPOSITORIES + ".", null));
			}

			TasksUiPlugin.getTaskListManager().getTaskList().notifyTaskChanged(task, false);
			if (taskDataHandler != null) {
				String taskId = task.getTaskId();
				RepositoryTaskData downloadedTaskData = taskDataHandler.getTaskData(repository, taskId, monitor);

				if (downloadedTaskData != null) {
					updateTask(monitor, repository, task, downloadedTaskData);
				} else {
					updateFromRepository(repository, task, monitor);
				}
			} else {
				updateFromRepository(repository, task, monitor);
			}
		} catch (final CoreException e) {
			updateStatus(repository, task, e.getStatus());
			if (forced) {
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
					public void run() {
						StatusHandler.displayStatus("Task Synchronization Failed", e.getStatus());
					}
				});
			}
		}

		monitor.worked(1);
	}

	/**
	 * Does not report synchronization failures if repository is offline.
	 */
	private void updateStatus(TaskRepository repository, AbstractTask task, IStatus status) {
		if (!forced && repository != null && repository.isOffline()) {
			task.setSynchronizing(false);
		} else {
			task.setSynchronizationStatus(status);
		}
	}

	private void synchronizeTasks(IProgressMonitor monitor, TaskRepository repository, Set<AbstractTask> tasks)
			throws CoreException {
		monitor.subTask("Synchronizing tasks from: " + repository.getRepositoryLabel());
		Set<String> taskIds = new HashSet<String>();
		Map<String, AbstractTask> idToTask = new HashMap<String, AbstractTask>();
		for (AbstractTask task : tasks) {
			taskIds.add(task.getTaskId());
			idToTask.put(task.getTaskId(), task);
		}
		Set<RepositoryTaskData> newTaskData = taskDataHandler.getMultiTaskData(repository, taskIds,
				new SubProgressMonitor(monitor, tasks.size()));
		if (newTaskData != null && newTaskData.size() > 0) {
			for (RepositoryTaskData taskData : newTaskData) {
				if (monitor.isCanceled())
					throw new OperationCanceledException("Synchronization cancelled by user");
				if (taskData != null) {
					AbstractTask task = idToTask.remove(taskData.getId());
					if (task != null) {
						try {
							updateTask(new SubProgressMonitor(monitor, 1), repository, task, taskData);
						} catch (final CoreException e) {
							updateStatus(repository, task, e.getStatus());
							if (forced) {
								PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
									public void run() {
										StatusHandler.displayStatus("Task Synchronization Failed", e.getStatus());
									}
								});
							}
						}
						monitor.worked(1);
					}
				}
			}
		}

		if (newTaskData != null && newTaskData.size() < tasks.size()) {
			//set error status
		}
	}

	private void updateFromRepository(TaskRepository repository, AbstractTask task, IProgressMonitor monitor)
			throws CoreException {
		connector.updateTaskFromRepository(repository, task, new SubProgressMonitor(monitor, 1));
		task.setStale(false);
		task.setSynchronizing(false);
		if (task.getSynchronizationState() == RepositoryTaskSyncState.INCOMING
				|| task.getSynchronizationState() == RepositoryTaskSyncState.CONFLICT) {
			TasksUiPlugin.getTaskListManager().getTaskList().notifyTaskChanged(task, true);
		} else {
			TasksUiPlugin.getTaskListManager().getTaskList().notifyTaskChanged(task, false);
		}
	}

	private void updateTask(IProgressMonitor monitor, TaskRepository repository, AbstractTask repositoryTask,
			RepositoryTaskData downloadedTaskData) throws CoreException {

		if (downloadedTaskData == null)
			return;

		// HACK: part of hack below
		Date oldDueDate = repositoryTask.getDueDate();

		TaskFactory factory = new TaskFactory(repository, true, forced);
		repositoryTask = factory.createTask(downloadedTaskData, new SubProgressMonitor(monitor, 1));

		// HACK: Remove once connectors can get access to
		// TaskDataManager and do this themselves
		if ((oldDueDate == null && repositoryTask.getDueDate() != null)
				|| (oldDueDate != null && repositoryTask.getDueDate() == null)) {
			TasksUiPlugin.getTaskActivityManager().setDueDate(repositoryTask, repositoryTask.getDueDate());
		} else if (oldDueDate != null && repositoryTask.getDueDate() != null
				&& oldDueDate.compareTo(repositoryTask.getDueDate()) != 0) {
			TasksUiPlugin.getTaskActivityManager().setDueDate(repositoryTask, repositoryTask.getDueDate());
		}

		repositoryTask.setSynchronizing(false);
		repositoryTask.setStale(false);
		if (repositoryTask.getSynchronizationState() == RepositoryTaskSyncState.INCOMING
				|| repositoryTask.getSynchronizationState() == RepositoryTaskSyncState.CONFLICT) {
			TasksUiPlugin.getTaskListManager().getTaskList().notifyTaskChanged(repositoryTask, true);
		} else {
			TasksUiPlugin.getTaskListManager().getTaskList().notifyTaskChanged(repositoryTask, false);
		}
	}
}

Back to the top