Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9bd519fc1aa635180b89c00859e1b3dc682f3c6e (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, 2008 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
 *     David Green - fixes for bug 265682
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.core;

import java.util.Map;
import java.util.WeakHashMap;

import org.eclipse.mylyn.tasks.core.IRepositoryManager;
import org.eclipse.mylyn.tasks.core.IRepositoryModel;
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
import org.eclipse.mylyn.tasks.core.ITaskComment;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;

/**
 * @author Steffen Pingel
 */
public class RepositoryModel implements IRepositoryModel {

	private final IRepositoryManager repositoryManager;

	private final Map<String, ITask> taskByHandle = new WeakHashMap<String, ITask>();

	private final TaskList taskList;

	public RepositoryModel(TaskList taskList, IRepositoryManager repositoryManager) {
		this.taskList = taskList;
		this.repositoryManager = repositoryManager;
		initialize();
	}

	private void initialize() {
		repositoryManager.addListener(new TaskRepositoryAdapter() {
			@Override
			public void repositoryAdded(TaskRepository repository) {
				taskList.addUnmatchedContainer(new UnmatchedTaskContainer(repository.getConnectorKind(),
						repository.getRepositoryUrl()));
			}

			@Override
			public void repositoryRemoved(TaskRepository repository) {
				// TODO 
				//taskList.removeUnmatchedContainer(taskList.getUnmatchedContainer(repository.getRepositoryUrl()));
			}
		});
	}

	public IRepositoryQuery createRepositoryQuery(TaskRepository taskRepository) {
		String handle = taskList.getUniqueHandleIdentifier();
		RepositoryQuery query = new RepositoryQuery(taskRepository.getConnectorKind(), handle);
		query.setRepositoryUrl(taskRepository.getRepositoryUrl());
		return query;
	}

	public synchronized ITask createTask(TaskRepository taskRepository, String taskId) {
		String handle = getTaskHandle(taskRepository, taskId);
		ITask task = taskByHandle.get(handle);
		if (task == null) {
			task = new TaskTask(taskRepository.getConnectorKind(), taskRepository.getRepositoryUrl(), taskId);
			taskByHandle.put(handle, task);
		}
		return task;
	}

	public ITaskAttachment createTaskAttachment(TaskAttribute taskAttribute) {
		TaskData taskData = taskAttribute.getTaskData();
		TaskRepository taskRepository = repositoryManager.getRepository(taskData.getConnectorKind(),
				taskData.getRepositoryUrl());
		ITask task = getTask(taskRepository, taskData.getTaskId());
		if (task == null) {
			return null;
		}
		TaskAttachment taskAttachment = new TaskAttachment(taskRepository, task, taskAttribute);
		taskData.getAttributeMapper().updateTaskAttachment(taskAttachment, taskAttribute);
		return taskAttachment;
	}

	public ITaskComment createTaskComment(TaskAttribute taskAttribute) {
		TaskData taskData = taskAttribute.getTaskData();
		TaskRepository taskRepository = repositoryManager.getRepository(taskData.getConnectorKind(),
				taskData.getRepositoryUrl());
		ITask task = getTask(taskRepository, taskData.getTaskId());
		if (task == null) {
			return null;
		}
		TaskComment taskComment = new TaskComment(taskRepository, task, taskAttribute);
		taskData.getAttributeMapper().updateTaskComment(taskComment, taskAttribute);
		return taskComment;
	}

	public synchronized ITask getTask(String handleIdentifier) {
		ITask task = taskByHandle.get(handleIdentifier);
		if (task == null) {
			task = taskList.getTask(handleIdentifier);
		}
		return task;
	}

	public synchronized ITask getTask(TaskRepository taskRepository, String taskId) {
		return getTask(getTaskHandle(taskRepository, taskId));
	}

	public synchronized ITask getTaskByKey(TaskRepository repository, String taskKey) {
		return taskList.getTaskByKey(repository.getUrl(), taskKey);
	}

	private String getTaskHandle(TaskRepository taskRepository, String taskId) {
		return RepositoryTaskHandleUtil.getHandle(taskRepository.getRepositoryUrl(), taskId);
	}

	public TaskRepository getTaskRepository(String connectorKind, String repositoryUrl) {
		TaskRepository taskRepository = repositoryManager.getRepository(connectorKind, repositoryUrl);
		if (taskRepository == null) {
			taskRepository = new TaskRepository(connectorKind, repositoryUrl);
			repositoryManager.addRepository(taskRepository);
		}
		return taskRepository;
	}

	public synchronized void clear() {
		taskByHandle.clear();
	}

}

Back to the top