Skip to main content
summaryrefslogtreecommitdiffstats
blob: ed54c59f37b9ed31fc26d7b3b594524db3322c3a (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*******************************************************************************
 * Copyright (c) 2004, 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.data;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.CoreUtil;
import org.eclipse.mylyn.commons.core.DelegatingProgressMonitor;
import org.eclipse.mylyn.commons.core.IDelegatingProgressMonitor;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.core.ITaskListRunnable;
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.TaskTask;
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.ITask.SynchronizationState;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.ITaskDataManager;
import org.eclipse.mylyn.tasks.core.data.ITaskDataWorkingCopy;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;

/**
 * Encapsulates synchronization policy.
 *
 * @author Mik Kersten
 * @author Rob Elves
 * @author Steffen Pingel
 */
public class TaskDataManager implements ITaskDataManager {

	private static final String ENCODING_UTF_8 = "UTF-8"; //$NON-NLS-1$

	private static final String EXTENSION = ".zip"; //$NON-NLS-1$

	private static final String FOLDER_TASKS = "tasks"; //$NON-NLS-1$

	private static final String FOLDER_DATA = "offline"; //$NON-NLS-1$

	private static final String FOLDER_TASKS_1_0 = "offline"; //$NON-NLS-1$

	private String dataPath;

	private final IRepositoryManager repositoryManager;

	private final TaskDataStore taskDataStore;

	private final TaskList taskList;

	private final TaskActivityManager taskActivityManager;

	private final List<ITaskDataManagerListener> listeners = new CopyOnWriteArrayList<ITaskDataManagerListener>();

	private final SynchronizationManger synchronizationManger;

	public TaskDataManager(TaskDataStore taskDataStore, IRepositoryManager repositoryManager, TaskList taskList,
			TaskActivityManager taskActivityManager, SynchronizationManger synchronizationManger) {
		this.taskDataStore = taskDataStore;
		this.repositoryManager = repositoryManager;
		this.taskList = taskList;
		this.taskActivityManager = taskActivityManager;
		this.synchronizationManger = synchronizationManger;
	}

	public void addListener(ITaskDataManagerListener listener) {
		listeners.add(listener);
	}

	public void removeListener(ITaskDataManagerListener listener) {
		listeners.remove(listener);
	}

	public ITaskDataWorkingCopy createWorkingCopy(final ITask task, final TaskData taskData) {
		Assert.isNotNull(task);
		final TaskDataState state = new TaskDataState(taskData.getConnectorKind(), taskData.getRepositoryUrl(),
				taskData.getTaskId());
		state.setRepositoryData(taskData);
		state.setLastReadData(taskData);
		state.init(TaskDataManager.this, task);
		state.setSaved(false);
		state.revert();
		return state;
	}

	public ITaskDataWorkingCopy getWorkingCopy(final ITask itask) throws CoreException {
		return getWorkingCopy(itask, true);
	}

	public ITaskDataWorkingCopy getWorkingCopy(final ITask itask, final boolean markRead) throws CoreException {
		final AbstractTask task = (AbstractTask) itask;
		Assert.isNotNull(task);
		final String kind = task.getConnectorKind();
		final TaskDataState[] result = new TaskDataState[1];
		final boolean[] changed = new boolean[1];
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				final File file = getMigratedFile(task, kind);
				final TaskDataState state = taskDataStore.getTaskDataState(file);
				if (state == null) {
					throw new CoreException(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Task data at \"" //$NON-NLS-1$
							+ file + "\" not found")); //$NON-NLS-1$
				}
				if (task.isMarkReadPending()) {
					state.setLastReadData(state.getRepositoryData());
				}
				state.init(TaskDataManager.this, task);
				state.revert();
				if (markRead) {
					switch (task.getSynchronizationState()) {
					case INCOMING:
					case INCOMING_NEW:
						task.setSynchronizationState(SynchronizationState.SYNCHRONIZED);
						changed[0] = true;
						break;
					case CONFLICT:
						task.setSynchronizationState(SynchronizationState.OUTGOING);
						changed[0] = true;
						break;
					}
					task.setMarkReadPending(true);
				}
				result[0] = state;
			}
		}, null, true);
		if (changed[0]) {
			taskList.notifyElementChanged(task);
		}
		return result[0];
	}

	public void saveWorkingCopy(final ITask itask, final TaskDataState state) throws CoreException {
		final AbstractTask task = (AbstractTask) itask;
		Assert.isNotNull(task);
		final String kind = task.getConnectorKind();
		final boolean[] changed = new boolean[1];
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				final File file = getFile(task, kind);
				taskDataStore.putTaskData(ensurePathExists(file), state);
				switch (task.getSynchronizationState()) {
				case SYNCHRONIZED:
					task.setSynchronizationState(SynchronizationState.OUTGOING);
					changed[0] = true;
					break;
				}
				taskList.addTask(task);
			}
		});
		if (changed[0]) {
			taskList.notifyElementChanged(task);
		}
	}

	public void putUpdatedTaskData(final ITask itask, final TaskData taskData, final boolean user)
			throws CoreException {
		putUpdatedTaskData(itask, taskData, user, null);
	}

	public void putUpdatedTaskData(final ITask itask, final TaskData taskData, final boolean user, Object token)
			throws CoreException {
		putUpdatedTaskData(itask, taskData, user, null, null);
	}

	public void putUpdatedTaskData(final ITask itask, final TaskData taskData, final boolean user, Object token,
			IProgressMonitor monitor) throws CoreException {
		final AbstractTask task = (AbstractTask) itask;
		Assert.isNotNull(task);
		Assert.isNotNull(taskData);
		final AbstractRepositoryConnector connector = repositoryManager.getRepositoryConnector(task.getConnectorKind());
		final TaskRepository repository = repositoryManager.getRepository(task.getConnectorKind(),
				task.getRepositoryUrl());
		final boolean taskDataChanged = connector.hasTaskChanged(repository, task, taskData);
		final TaskDataManagerEvent event = new TaskDataManagerEvent(this, itask, taskData, token);
		event.setTaskDataChanged(taskDataChanged);
		IDelegatingProgressMonitor delegatingMonitor = DelegatingProgressMonitor.getMonitorFrom(monitor);
		if (delegatingMonitor != null) {
			event.setData(delegatingMonitor.getData());
		}
		final boolean[] synchronizationStateChanged = new boolean[1];
		if (taskDataChanged || user) {
			taskList.run(new ITaskListRunnable() {
				public void execute(IProgressMonitor monitor) throws CoreException {
					TaskDataState state = null;
					if (!taskData.isPartial()) {
						File file = getMigratedFile(task, task.getConnectorKind());
						state = taskDataStore.putTaskData(ensurePathExists(file), taskData, task.isMarkReadPending(),
								user);
						task.setMarkReadPending(false);
						event.setTaskDataUpdated(true);
					}

					boolean taskChanged = updateTaskFromTaskData(taskData, task, connector, repository);
					event.setTaskChanged(taskChanged);

					if (taskDataChanged) {
						String suppressIncoming = null;
						// determine whether to show an incoming
						if (state == null) {
							File file = getMigratedFile(task, task.getConnectorKind());
							state = taskDataStore.getTaskDataState(ensurePathExists(file));
						}
						TaskData lastReadData = (state != null) ? state.getLastReadData() : null;
						TaskDataDiff diff = synchronizationManger.createDiff(taskData, lastReadData, monitor);
						suppressIncoming = Boolean.toString(!diff.hasChanged());

						switch (task.getSynchronizationState()) {
						case OUTGOING:
							task.setSynchronizationState(SynchronizationState.CONFLICT);
							break;
						case SYNCHRONIZED:
							task.setSynchronizationState(SynchronizationState.INCOMING);
							break;
						}

						// if an incoming was previously suppressed it may need to show now
						task.setAttribute(ITasksCoreConstants.ATTRIBUTE_TASK_SUPPRESS_INCOMING, suppressIncoming);
					}
					if (task.isSynchronizing()) {
						task.setSynchronizing(false);
						synchronizationStateChanged[0] = true;
					}
				}
			});
		} else {
			taskList.run(new ITaskListRunnable() {
				public void execute(IProgressMonitor monitor) throws CoreException {
					if (task.isSynchronizing()) {
						task.setSynchronizing(false);
						synchronizationStateChanged[0] = true;
					}
				}
			});
		}
		if (event.getTaskChanged() || event.getTaskDataChanged()) {
			taskList.notifyElementChanged(task);
			fireTaskDataUpdated(event);
		} else {
			if (synchronizationStateChanged[0]) {
				taskList.notifySynchronizationStateChanged(task);
			}
			if (event.getTaskDataUpdated()) {
				fireTaskDataUpdated(event);
			}
		}
	}

	private boolean updateTaskFromTaskData(final TaskData taskData, final AbstractTask task,
			final AbstractRepositoryConnector connector, final TaskRepository repository) {
		task.setChanged(false);
		Date oldDueDate = task.getDueDate();
		connector.updateTaskFromTaskData(repository, task, taskData);
		// XXX move this to AbstractTask or use model listener to notify task activity
		// manager of due date changes
		Date newDueDate = task.getDueDate();
		if (oldDueDate != null && !oldDueDate.equals(newDueDate) || newDueDate != oldDueDate) {
			taskActivityManager.setDueDate(task, newDueDate);
		}
		return task.isChanged();
	}

	private File ensurePathExists(File file) {
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		return file;
	}

	private File getMigratedFile(ITask task, String kind) throws CoreException {
		Assert.isNotNull(task);
		Assert.isNotNull(kind);
		File file = getFile(task, kind);
		if (!file.exists()) {
			File oldFile = getFile10(task, kind);
			if (oldFile.exists()) {
				TaskDataState state = taskDataStore.getTaskDataState(oldFile);
				// save migrated task data right away
				taskDataStore.putTaskData(ensurePathExists(file), state);
			}
		}
		return file;
	}

	public void discardEdits(final ITask itask) throws CoreException {
		final AbstractTask task = (AbstractTask) itask;
		Assert.isNotNull(task);
		final String kind = task.getConnectorKind();
		final TaskDataManagerEvent event = new TaskDataManagerEvent(this, itask);
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				File dataFile = getFile(task, kind);
				if (dataFile.exists()) {
					taskDataStore.discardEdits(dataFile);
				}
				switch (task.getSynchronizationState()) {
				case OUTGOING:
					task.setSynchronizationState(SynchronizationState.SYNCHRONIZED);
					event.setTaskChanged(true);
					break;
				case CONFLICT:
					task.setSynchronizationState(SynchronizationState.INCOMING);
					event.setTaskChanged(true);
					break;
				}
			}
		});
		if (event.getTaskChanged()) {
			taskList.notifyElementChanged(task);
		}
		fireEditsDiscarded(event);
	}

	private File findFile(ITask task, String kind) {
		File file = getFile(task, kind);
		if (file.exists()) {
			return file;
		}
		return getFile10(task, kind);
	}

	public String getDataPath() {
		return dataPath;
	}

	private File getFile(ITask task, String kind) {
		return getFile(task.getRepositoryUrl(), task, kind);
	}

	private File getFile(String repositoryUrl, ITask task, String kind) {
//			String pathName = task.getConnectorKind() + "-"
//					+ URLEncoder.encode(task.getRepositoryUrl(), ENCODING_UTF_8);
//			String fileName = kind + "-" + URLEncoder.encode(task.getTaskId(), ENCODING_UTF_8) + EXTENSION;
		String repositoryPath = task.getConnectorKind() + "-" + CoreUtil.asFileName(repositoryUrl); //$NON-NLS-1$
		String fileName = CoreUtil.asFileName(task.getTaskId()) + EXTENSION;
		File path = new File(dataPath + File.separator + FOLDER_TASKS + File.separator + repositoryPath + File.separator
				+ FOLDER_DATA);
		return new File(path, fileName);
	}

	private File getFile10(ITask task, String kind) {
		try {
			String pathName = URLEncoder.encode(task.getRepositoryUrl(), ENCODING_UTF_8);
			String fileName = task.getTaskId() + EXTENSION;
			File path = new File(dataPath + File.separator + FOLDER_TASKS_1_0, pathName);
			return new File(path, fileName);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}

	}

	public TaskData getTaskData(ITask task) throws CoreException {
		Assert.isNotNull(task);
		final String kind = task.getConnectorKind();
		TaskDataState state = taskDataStore.getTaskDataState(findFile(task, kind));
		if (state == null) {
			return null;
		}
		return state.getRepositoryData();
	}

	public TaskDataState getTaskDataState(ITask task) throws CoreException {
		Assert.isNotNull(task);
		final String kind = task.getConnectorKind();
		// TODO check that repository task data != null for returned task data state
		return taskDataStore.getTaskDataState(findFile(task, kind));
	}

	public TaskData getTaskData(TaskRepository taskRepository, String taskId) throws CoreException {
		Assert.isNotNull(taskRepository);
		Assert.isNotNull(taskId);
		TaskDataState state = taskDataStore.getTaskDataState(
				findFile(new TaskTask(taskRepository.getConnectorKind(), taskRepository.getRepositoryUrl(), taskId),
						taskRepository.getConnectorKind()));
		if (state == null) {
			return null;
		}
		return state.getRepositoryData();
	}

	public boolean hasTaskData(ITask task) {
		Assert.isNotNull(task);
		final String kind = task.getConnectorKind();
		return findFile(task, kind).exists();
	}

	public void putSubmittedTaskData(final ITask itask, final TaskData taskData, IDelegatingProgressMonitor monitor)
			throws CoreException {
		final AbstractTask task = (AbstractTask) itask;
		Assert.isNotNull(task);
		Assert.isNotNull(taskData);
		final AbstractRepositoryConnector connector = repositoryManager.getRepositoryConnector(task.getConnectorKind());
		final TaskRepository repository = repositoryManager.getRepository(task.getConnectorKind(),
				task.getRepositoryUrl());
		final TaskDataManagerEvent event = new TaskDataManagerEvent(this, itask, taskData, null);
		event.setTaskDataChanged(true);
		event.setData(((DelegatingProgressMonitor) monitor).getData());
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				if (!taskData.isPartial()) {
					File file = getMigratedFile(task, task.getConnectorKind());
					taskDataStore.setTaskData(ensurePathExists(file), taskData);
					task.setMarkReadPending(false);
					event.setTaskDataUpdated(true);
				}

				boolean taskChanged = updateTaskFromTaskData(taskData, task, connector, repository);
				event.setTaskChanged(taskChanged);

				task.setSynchronizationState(SynchronizationState.SYNCHRONIZED);
				task.setSynchronizing(false);
			}
		});
		taskList.notifyElementChanged(task);
		fireTaskDataUpdated(event);
	}

	public void deleteTaskData(final ITask itask) throws CoreException {
		Assert.isTrue(itask instanceof AbstractTask);
		final AbstractTask task = (AbstractTask) itask;
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				File file = getFile(task, task.getConnectorKind());
				if (file.exists()) {
					taskDataStore.deleteTaskData(file);
					task.setSynchronizationState(SynchronizationState.SYNCHRONIZED);
				}
			}
		});
		taskList.notifyElementChanged(task);
	}

	public void setDataPath(String dataPath) {
		this.dataPath = dataPath;
	}

	/**
	 * @param itask
	 *            repository task to mark as read or unread
	 * @param read
	 *            true to mark as read, false to mark as unread
	 * @return true if synchronization state has been changed
	 */
	public boolean setTaskRead(final ITask itask, final boolean read) {
		final AbstractTask task = (AbstractTask) itask;
		Assert.isNotNull(task);
		final boolean changed[] = new boolean[1];
		try {
			taskList.run(new ITaskListRunnable() {
				public void execute(IProgressMonitor monitor) throws CoreException {
					if (read) {
						switch (task.getSynchronizationState()) {
						case INCOMING:
						case INCOMING_NEW:
							task.setSynchronizationState(SynchronizationState.SYNCHRONIZED);
							task.setMarkReadPending(true);
							changed[0] = true;
							break;
						case CONFLICT:
							task.setSynchronizationState(SynchronizationState.OUTGOING);
							task.setMarkReadPending(true);
							changed[0] = true;
							break;
						}
					} else {
						// if an incoming was previously suppressed it need to show now
						task.setAttribute(ITasksCoreConstants.ATTRIBUTE_TASK_SUPPRESS_INCOMING,
								Boolean.toString(false));
						switch (task.getSynchronizationState()) {
						case SYNCHRONIZED:
							task.setSynchronizationState(SynchronizationState.INCOMING);
							task.setMarkReadPending(false);
							changed[0] = true;
							break;
						}
					}
				}
			});
		} catch (CoreException e) {
			StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN,
					"Unexpected error while marking task read", e)); //$NON-NLS-1$
		}
		if (changed[0]) {
			taskList.notifyElementChanged(task);
		}
		return changed[0];
	}

	void putEdits(final ITask itask, final TaskData editsData) throws CoreException {
		final AbstractTask task = (AbstractTask) itask;
		Assert.isNotNull(task);
		final String kind = task.getConnectorKind();
		Assert.isNotNull(editsData);
		final boolean[] changed = new boolean[1];
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				taskDataStore.putEdits(getFile(task, kind), editsData);
				switch (task.getSynchronizationState()) {
				case INCOMING:
				case INCOMING_NEW:
					// TODO throw exception instead?
					task.setSynchronizationState(SynchronizationState.CONFLICT);
					changed[0] = true;
					break;
				case SYNCHRONIZED:
					task.setSynchronizationState(SynchronizationState.OUTGOING);
					changed[0] = true;
					break;
				}
			}
		});
		if (changed[0]) {
			taskList.notifySynchronizationStateChanged(task);
		}
	}

	private void fireTaskDataUpdated(final TaskDataManagerEvent event) {
		ITaskDataManagerListener[] array = listeners.toArray(new ITaskDataManagerListener[0]);
		if (array.length > 0) {
			for (final ITaskDataManagerListener listener : array) {
				SafeRunner.run(new ISafeRunnable() {

					public void handleException(Throwable exception) {
						// ignore
					}

					public void run() throws Exception {
						listener.taskDataUpdated(event);
					}

				});
			}
		}
	}

	private void fireEditsDiscarded(final TaskDataManagerEvent event) {
		ITaskDataManagerListener[] array = listeners.toArray(new ITaskDataManagerListener[0]);
		if (array.length > 0) {
			for (final ITaskDataManagerListener listener : array) {
				SafeRunner.run(new ISafeRunnable() {

					public void handleException(Throwable exception) {
						// ignore
					}

					public void run() throws Exception {
						listener.editsDiscarded(event);
					}

				});
			}
		}
	}

	public void refactorRepositoryUrl(final ITask itask, final String newStorageRepositoryUrl,
			final String newRepositoryUrl) throws CoreException {
		Assert.isTrue(itask instanceof AbstractTask);
		final AbstractTask task = (AbstractTask) itask;
		final String kind = task.getConnectorKind();
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				File file = getMigratedFile(task, kind);
				if (file.exists()) {
					TaskDataState oldState = taskDataStore.getTaskDataState(file);
					if (oldState != null) {
						File newFile = getFile(newStorageRepositoryUrl, task, kind);
						TaskDataState newState = new TaskDataState(oldState.getConnectorKind(), newRepositoryUrl,
								oldState.getTaskId());
						newState.merge(oldState);
						taskDataStore.putTaskData(ensurePathExists(newFile), newState);
					}
				}
			}
		});
	}

	public void refactorAttributeValue(final ITask itask, final Map<TaskAttribute, Collection<String>> newValues)
			throws CoreException {
		Assert.isTrue(itask instanceof AbstractTask);
		final AbstractTask task = (AbstractTask) itask;
		final String kind = task.getConnectorKind();
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				File file = getMigratedFile(task, kind);
				if (file.exists()) {
					TaskDataState state = taskDataStore.getTaskDataState(file);
					if (state != null) {
						state.changeAttributeValues(newValues);
						taskDataStore.putTaskData(file, state);
					}
				}
			}
		});
	}

	public void refactorTaskId(final AbstractTask task, final ITask newTask) throws CoreException {
		final String kind = task.getConnectorKind();
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				File file = getMigratedFile(task, kind);
				if (file.exists()) {
					TaskDataState oldState = taskDataStore.getTaskDataState(file);
					if (oldState != null) {
						File newFile = getFile(task.getRepositoryUrl(), newTask, kind);
						TaskDataState newState = new TaskDataState(oldState.getConnectorKind(), task.getRepositoryUrl(),
								newTask.getTaskId());
						newState.merge(oldState);
						taskDataStore.putTaskData(ensurePathExists(newFile), newState);
						taskDataStore.deleteTaskData(file);
					}
				}
			}

		});
	}

	public void refactorAttribute(ITask itask, TaskAttribute attribute) throws CoreException {
		Assert.isTrue(itask instanceof AbstractTask);
		final AbstractTask task = (AbstractTask) itask;
		final String kind = task.getConnectorKind();
		taskList.run(new ITaskListRunnable() {
			public void execute(IProgressMonitor monitor) throws CoreException {
				File file = getMigratedFile(task, kind);
				if (file.exists()) {
					TaskDataState state = taskDataStore.getTaskDataState(file);
					if (state != null) {
						state.refactorAttribute(attribute);
						taskDataStore.putTaskData(file, state);
					}
				}
			}
		});

	}
}

Back to the top