Skip to main content
summaryrefslogtreecommitdiffstats
blob: 17e855cefb14ef676776465417bce0aba4f9d186 (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
/*******************************************************************************
 * 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.internal.tasks.ui.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.mylyn.context.core.ContextCorePlugin;
import org.eclipse.mylyn.internal.context.core.InteractionContextManager;
import org.eclipse.mylyn.internal.monitor.core.util.ZipFileUtil;
import org.eclipse.mylyn.internal.tasks.ui.ITasksUiConstants;
import org.eclipse.mylyn.internal.tasks.ui.WorkspaceAwareContextStore;
import org.eclipse.mylyn.monitor.core.StatusHandler;
import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.core.TaskRepositoryManager;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;

/**
 * Job that performs exporting (copying or zipping) of Mylar Task List data Assumes that check with user for overwrite
 * already done. Overwrites destination if exists!
 * 
 * @author Wesley Coelho
 * @author Mik Kersten
 * @author Rob Elves
 */
public class TaskDataExportJob implements IRunnableWithProgress {

	private static final String JOB_LABEL = "Exporting Mylar Task Data";

	private boolean zip;

	private boolean exportTaskList;

	private boolean exportActivationHistory;

	private boolean exportTaskContexts;

	private String destinationDirectory;

	private String zipFileName;

	private File destZipFile = null;

	private Collection<AbstractTask> tasks;

	/** export all data */
	public TaskDataExportJob(String destinationDirectory, boolean zipIt, String zipFileName) {
		this(destinationDirectory, true, true, true, zipIt, zipFileName, TasksUiPlugin.getTaskListManager()
				.getTaskList()
				.getAllTasks());
	}

	/** export specified data */
	public TaskDataExportJob(String destinationDirectory, boolean exportTaskList, boolean exportActivationHistory,
			boolean exportTaskContexts, boolean zipIt, String zipFileName, Collection<AbstractTask> taskContextsToExport) {
		this.zipFileName = zipFileName;
		this.zip = zipIt;
		this.exportTaskList = exportTaskList;
		this.exportActivationHistory = exportActivationHistory;
		this.exportTaskContexts = exportTaskContexts;
		this.destinationDirectory = destinationDirectory;
		this.tasks = taskContextsToExport;
	}

	public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
		// Collection<ITask> tasks =
		// MylarTaskListPlugin.getTaskListManager().getTaskList().getAllTasks();
		int jobSize = 1; // 1 for repositories.xml
		if (exportTaskList)
			jobSize++;
		if (exportActivationHistory)
			jobSize++;
		if (exportTaskContexts)
			jobSize += tasks.size();
		monitor.beginTask(JOB_LABEL, jobSize);

		// List of files to add to the zip archive
		List<File> filesToZip = new ArrayList<File>();

		// Map of file paths used to avoid duplicates
		Map<String, String> filesToZipMap = new HashMap<String, String>();

		// Create folders in zip file before contained files
		String sourceContextsPath = TasksUiPlugin.getDefault().getDataDirectory() + File.separator
				+ WorkspaceAwareContextStore.CONTEXTS_DIRECTORY;
		File contextsDirectory = new File(sourceContextsPath);
		// if(contextsDirectory.exists()) {
		// filesToZip.add(contextsDirectory);
		// }

		if (true) {
			// Repositories always exported
			TasksUiPlugin.getRepositoryManager().saveRepositories(TasksUiPlugin.getDefault().getRepositoriesFilePath());

			String sourceRepositoriesPath = TasksUiPlugin.getDefault().getDataDirectory() + File.separator
					+ TaskRepositoryManager.DEFAULT_REPOSITORIES_FILE;
			File sourceRepositoriesFile = new File(sourceRepositoriesPath);
			if (sourceRepositoriesFile.exists()) {
				File destRepositoriesFile = new File(destinationDirectory + File.separator
						+ TaskRepositoryManager.DEFAULT_REPOSITORIES_FILE);

				if (zip) {
					filesToZip.add(sourceRepositoriesFile);
				} else if (!destRepositoriesFile.equals(sourceRepositoriesFile)) {
					if (destRepositoriesFile.exists()) {
						destRepositoriesFile.delete();
					}
					if (!copy(sourceRepositoriesFile, destRepositoriesFile)) {
						StatusHandler.fail(new Exception("Export Exception"), "Could not export repositories file.",
								false);
					}
					monitor.worked(1);
				}
			}

		}

		if (exportTaskList) {
			TasksUiPlugin.getTaskListManager().saveTaskList();

			String sourceTaskListPath = TasksUiPlugin.getDefault().getDataDirectory() + File.separator
					+ ITasksUiConstants.DEFAULT_TASK_LIST_FILE;
			File sourceTaskListFile = new File(sourceTaskListPath);
			if (sourceTaskListFile.exists()) {
				File destTaskListFile = new File(destinationDirectory + File.separator
						+ ITasksUiConstants.DEFAULT_TASK_LIST_FILE);

				if (zip) {
					filesToZip.add(sourceTaskListFile);
				} else if (!destTaskListFile.equals(sourceTaskListFile)) {
					if (destTaskListFile.exists()) {
						destTaskListFile.delete();
					}
					if (!copy(sourceTaskListFile, destTaskListFile)) {
						StatusHandler.fail(new Exception("Export Exception"), "Could not export task list file.", false);
					}
					monitor.worked(1);
				}
			}

		}

		if (exportActivationHistory) {
			try {
				File sourceActivationHistoryFile = new File(contextsDirectory,
						InteractionContextManager.CONTEXT_HISTORY_FILE_NAME
								+ InteractionContextManager.CONTEXT_FILE_EXTENSION);

				if (sourceActivationHistoryFile.exists()) {

					ContextCorePlugin.getContextManager().saveActivityContext();

					File destActivationHistoryFile = new File(destinationDirectory + File.separator
							+ InteractionContextManager.CONTEXT_HISTORY_FILE_NAME
							+ InteractionContextManager.CONTEXT_FILE_EXTENSION);

					if (zip) {
						filesToZip.add(sourceActivationHistoryFile);
					} else if (!destActivationHistoryFile.equals(sourceActivationHistoryFile)) {
						if (destActivationHistoryFile.exists()) {
							destActivationHistoryFile.delete();
						}
						copy(sourceActivationHistoryFile, destActivationHistoryFile);
						monitor.worked(1);
					}
				}
			} catch (RuntimeException e) {
				StatusHandler.fail(e, "Could not export activity history context file", true);
			}
		}

		if (exportTaskContexts) {
			// Prevent many repeated error messages
			boolean errorDisplayed = false;
			for (AbstractTask task : tasks) {

				if (!ContextCorePlugin.getContextManager().hasContext(task.getHandleIdentifier())) {
					continue; // Tasks without a context have no file to
					// copy
				}

				File sourceTaskContextFile = ContextCorePlugin.getContextManager().getFileForContext(
						task.getHandleIdentifier());

				File destTaskFile = new File(destinationDirectory + File.separator + sourceTaskContextFile.getName());

				if (zip) {
					if (!filesToZipMap.containsKey(task.getHandleIdentifier())) {
						filesToZip.add(sourceTaskContextFile);
						filesToZipMap.put(task.getHandleIdentifier(), null);
					}
				} else if (!sourceTaskContextFile.equals(destTaskFile)) {
					if (destTaskFile.exists()) {
						destTaskFile.delete();
					}
					if (!copy(sourceTaskContextFile, destTaskFile) && !errorDisplayed) {
						StatusHandler.fail(new Exception("Export Exception: " + sourceTaskContextFile.getPath()
								+ " -> " + destTaskFile.getPath()), "Could not export one or more task context files.",
								true);
						errorDisplayed = true;
					}
					monitor.worked(1);
				}
			}
		}

		if (zip && filesToZip.size() > 0) {
			try {
				destZipFile = new File(destinationDirectory + File.separator + zipFileName);
				if (destZipFile.exists()) {
					destZipFile.delete();
				}
				ZipFileUtil.createZipFile(destZipFile, filesToZip, TasksUiPlugin.getDefault().getDataDirectory(),
						monitor);
			} catch (Exception e) {
				StatusHandler.fail(e, "Could not create zip file.", true);
			}
		}
		monitor.done();

	}

	private boolean copy(File src, File dst) {
		try {
			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dst);

			// Transfer bytes from in to out
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			return true;
		} catch (IOException ioe) {
			return false;
		}
	}

}

Back to the top