Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5e00c22572f7b5d54247650dfbc8b89109d60363 (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.tasks.ui;

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.jobs.Job;
import org.eclipse.mylar.core.MylarStatusHandler;
import org.eclipse.mylar.internal.tasks.ui.TaskListImages;
import org.eclipse.mylar.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylar.tasks.core.AbstractRepositoryTask;
import org.eclipse.mylar.tasks.core.ITaskDataHandler;
import org.eclipse.mylar.tasks.core.RepositoryTaskData;
import org.eclipse.mylar.tasks.core.TaskRepository;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressConstants;

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

	private static final String LABEL_SYNCHRONIZING = "Synchronizing ";

	private static final String LABEL_SYNCHRONIZE_TASK = "Task Synchronization";

	private final AbstractRepositoryConnector connector;

	private Set<AbstractRepositoryTask> repositoryTasks;

	private boolean forceSync = false;

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

	public void setForceSynch(boolean forceUpdate) {
		this.forceSync = forceUpdate;
	}

	@Override
	public IStatus run(IProgressMonitor monitor) {
		try {
			monitor.beginTask(LABEL_SYNCHRONIZE_TASK, repositoryTasks.size());
			setProperty(IProgressConstants.ICON_PROPERTY, TaskListImages.REPOSITORY_SYNCHRONIZE);

			for (final AbstractRepositoryTask repositoryTask : repositoryTasks) {
				if (monitor.isCanceled()) {
					throw new OperationCanceledException();
				}

				repositoryTask.setStatus(null);

				try {
					syncTask(monitor, repositoryTask);
				} catch (final CoreException e) {
					repositoryTask.setStatus(e.getStatus());
					if (forceSync) {
						PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
							public void run() {
								MylarStatusHandler.displayStatus("Task synchronization failed", e.getStatus());
							}
						});
					}
				}

				repositoryTask.setCurrentlySynchronizing(false);
				TasksUiPlugin.getTaskListManager().getTaskList().notifyLocalInfoChanged(repositoryTask);
				// TasksUiPlugin.getTaskListManager().getTaskList().notifyRepositoryInfoChanged(repositoryTask);

				monitor.worked(1);
			}
			TasksUiPlugin.getDefault().getTaskDataManager().save();

		} catch (Exception e) {
			MylarStatusHandler.fail(e, "Could not download report", false);
		} finally {
			monitor.done();
		}

		return Status.OK_STATUS;
	}

	private void syncTask(IProgressMonitor monitor, final AbstractRepositoryTask repositoryTask) throws CoreException {
		boolean hasLocalChanges = repositoryTask.isDirty();
		if (forceSync || !hasLocalChanges || !repositoryTask.isDownloaded()) {
			monitor.setTaskName(LABEL_SYNCHRONIZING + repositoryTask.getSummary());

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

			TasksUiPlugin.getTaskListManager().getTaskList().notifyLocalInfoChanged(repositoryTask);
			ITaskDataHandler taskDataHandler = connector.getTaskDataHandler();
			if (taskDataHandler != null) {
				String taskId = AbstractRepositoryTask.getTaskId(repositoryTask.getHandleIdentifier());
				RepositoryTaskData downloadedTaskData = taskDataHandler.getTaskData(repository, taskId);

				if (downloadedTaskData != null) {
					if (TasksUiPlugin.getSynchronizationManager().saveIncoming(repositoryTask,
							downloadedTaskData, forceSync)) {

						TasksUiPlugin.getTaskListManager().getTaskList().notifyRepositoryInfoChanged(repositoryTask);

					}
				} else {
					connector.updateTask(repository, repositoryTask);
				}
			} else {
				connector.updateTask(repository, repositoryTask);
			}
		}
	}
}

Back to the top