Skip to main content
summaryrefslogtreecommitdiffstats
blob: 991f3ea26d947d92d758f2519159d414e5c4bd90 (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
/*******************************************************************************
 * Copyright (c) 2009 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.sync;

import java.util.Collection;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.IRepositoryManager;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.sync.TaskJob;
import org.eclipse.osgi.util.NLS;

/**
 * @author Shawn Minto
 */
public class DeleteTasksJob extends TaskJob {
	private final Collection<ITask> tasksToDelete;

	private MultiStatus status;

	private final IRepositoryManager repositoryManager;

	public DeleteTasksJob(String name, Collection<ITask> tasksToDelete, IRepositoryManager repositoryManager) {
		super(name);
		Assert.isNotNull(tasksToDelete);
		Assert.isNotNull(repositoryManager);
		this.repositoryManager = repositoryManager;
		this.tasksToDelete = tasksToDelete;
	}

	@Override
	public IStatus getStatus() {
		return status;
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		status = new MultiStatus(ITasksCoreConstants.ID_PLUGIN, IStatus.OK,
				"Problems occurred while deleting repository tasks", null); //$NON-NLS-1$
		try {
			monitor.beginTask(Messages.DeleteTasksJob_Deleting_tasks, tasksToDelete.size() * 100);
			for (ITask task : tasksToDelete) {
				// delete the task on the server using the repository connector
				AbstractRepositoryConnector repositoryConnector = repositoryManager.getRepositoryConnector(task.getConnectorKind());
				TaskRepository repository = repositoryManager.getRepository(task.getConnectorKind(),
						task.getRepositoryUrl());
				if (repositoryConnector.canDeleteTask(repository, task)) {
					try {
						repositoryConnector.deleteTask(repository, task, subMonitorFor(monitor, 100));
					} catch (OperationCanceledException e) {
						return Status.CANCEL_STATUS;
					} catch (Exception e) {
						String taskId = task.getTaskKey();
						if (taskId == null) {
							taskId = task.getTaskId();
						}
						status.add(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, NLS.bind(
								"Problems occurred while deleting {0} from {1}.", taskId, task.getRepositoryUrl()), e)); //$NON-NLS-1$
					} catch (LinkageError e) {
						String taskId = task.getTaskKey();
						if (taskId == null) {
							taskId = task.getTaskId();
						}
						status.add(new Status(
								IStatus.ERROR,
								ITasksCoreConstants.ID_PLUGIN,
								NLS.bind(
										"Internal Error occurred while deleting {0} from {1}.", taskId, task.getRepositoryUrl()), e)); //$NON-NLS-1$
					}

				}
			}
		} finally {
			monitor.done();
		}
		return Status.OK_STATUS;
	}
}

Back to the top