Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2e997e657eb818138cf645eeb3ac306c82f28cb7 (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
/*******************************************************************************
 * Copyright (c) 2016 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.operations;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.internal.tasks.core.TaskActivityManager;
import org.eclipse.mylyn.internal.tasks.core.TaskList;
import org.eclipse.mylyn.internal.tasks.core.data.TaskDataManager;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.context.AbstractTaskContextStore;
import org.eclipse.osgi.util.NLS;

public class RefactorTaskIdsOperation extends TaskListOperation {

	private final Map<ITask, String> newTaskIdMap;

	private final TaskActivityManager activityManager;

	private final AbstractTaskContextStore contextStore;

	private final TaskDataManager taskDataManager;

	public RefactorTaskIdsOperation(Map<ITask, String> newTaskIdMap, TaskList taskList,
			TaskActivityManager activityManager, AbstractTaskContextStore contextStore,
			TaskDataManager taskDataManager) {
		super(ITasksCoreConstants.ROOT_SCHEDULING_RULE, taskList);
		this.newTaskIdMap = newTaskIdMap;
		this.activityManager = activityManager;
		this.contextStore = contextStore;
		this.taskDataManager = taskDataManager;
	}

	@Override
	protected void operations(IProgressMonitor monitor)
			throws CoreException, InvocationTargetException, InterruptedException {
		try {
			monitor.beginTask(Messages.RefactorTaskIdsOperation_UpdateTaskId, newTaskIdMap.size() * 2);
			Map<ITask, ITask> map = new HashMap<>();
			MultiStatus status = new MultiStatus(ITasksCoreConstants.ID_PLUGIN, IStatus.OK, null, null);

			for (ITask task : newTaskIdMap.keySet()) {
				AbstractTask newTask = getTaskList().refactorTaskId(task, newTaskIdMap.get(task));
				map.put(task, newTask);

				activityManager.moveActivity(task, newTask);
				if (task instanceof AbstractTask) {
					try {
						taskDataManager.refactorTaskId((AbstractTask) task, newTask);
					} catch (CoreException e) {
						status.add(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN,
								NLS.bind(Messages.RefactorTaskIdsOperation_TaskDataRefactorError, task), e));
					}
				}
				monitor.worked(1);
			}

			contextStore.moveContext(map);
			monitor.worked(newTaskIdMap.size());
			handleFailedMigrations(status);
		} finally {
			monitor.done();
		}
	}

	private void handleFailedMigrations(MultiStatus status) throws CoreException {
		if (!status.isOK()) {
			MultiStatus errorStatus = new MultiStatus(ITasksCoreConstants.ID_PLUGIN, IStatus.ERROR,
					NLS.bind(Messages.RefactorTaskIdsOperation_FailedTaskCount, status.getChildren().length), null);
			errorStatus.merge(status);
			throw new CoreException(errorStatus);
		}
	}

}

Back to the top