Skip to main content
summaryrefslogtreecommitdiffstats
blob: ede3bdf5ba172083d2cfecf1bef621fd9555c98e (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.tasks.core;

import java.util.Date;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.mylar.core.net.HtmlStreamTokenizer;

/**
 * Virtual proxy for a repository task.
 * 
 * @author Mik Kersten
 * @author Rob Elves
 */
public abstract class AbstractRepositoryTask extends Task {

	private static final String CONTEXT_HANDLE_DELIM = "-";

	private static final String MISSING_REPOSITORY_HANDLE = "norepository" + CONTEXT_HANDLE_DELIM;

	/** The last time this task's bug report was in a synchronized (read?) state. */
	protected String lastSynchronizedDateStamp;

	protected transient RepositoryTaskData taskData;

	protected boolean currentlySynchronizing;

	protected boolean isNotifiedIncoming = true;

	/**
	 * Value is <code>true</code> if the bug report has saved changes that
	 * need synchronizing with the repository.
	 */
	protected boolean isDirty;

	public enum RepositoryTaskSyncState {
		OUTGOING, SYNCHRONIZED, INCOMING, CONFLICT
	}

	protected RepositoryTaskSyncState syncState = RepositoryTaskSyncState.SYNCHRONIZED;

	public static final String HANDLE_DELIM = "-";

	protected IStatus status = null;
	
	public AbstractRepositoryTask(String handle, String label, boolean newTask) {
		super(handle, label, newTask);
	}

	@Override
	public abstract String getRepositoryKind();

	public boolean isDownloaded() {
		return taskData != null;
	}

	public String getLastSyncDateStamp() {
		return lastSynchronizedDateStamp;
	}

	public void setLastSyncDateStamp(String lastSyncDateStamp) {
		this.lastSynchronizedDateStamp = lastSyncDateStamp;
	}

	public void setSyncState(RepositoryTaskSyncState syncState) {
		this.syncState = syncState;
	}

	public RepositoryTaskSyncState getSyncState() {
		return syncState;
	}

	public String getRepositoryUrl() {
		return AbstractRepositoryTask.getRepositoryUrl(getHandleIdentifier());
	}

	@Override
	public boolean isLocal() {
		return false;
	}

	public static long getLastRefreshTimeInMinutes(Date lastRefresh) {
		Date timeNow = new Date();
		if (lastRefresh == null)
			lastRefresh = new Date();
		long timeDifference = (timeNow.getTime() - lastRefresh.getTime()) / 60000;
		return timeDifference;
	}

	public boolean isSynchronizing() {
		return currentlySynchronizing;
	}

	public void setCurrentlySynchronizing(boolean currentlySychronizing) {
		this.currentlySynchronizing = currentlySychronizing;
	}

	/**
	 * Human readable identifier for this task.  Override if different than ID, can return
	 * null if no such label exists.
	 */
	public String getIdLabel() {
		return getTaskId(handleIdentifier);
	}
	
	public static String getTaskId(String taskHandle) {
		int index = taskHandle.lastIndexOf(AbstractRepositoryTask.HANDLE_DELIM);
		if (index != -1) {
			String id = taskHandle.substring(index + 1);
			return id;
		}
		return null;
	}

	public static String getRepositoryUrl(String taskHandle) {
		int index = taskHandle.lastIndexOf(AbstractRepositoryTask.HANDLE_DELIM);
		String url = null;
		if (index != -1) {
			url = taskHandle.substring(0, index);
		}
		return url;
	}

	public static String getHandle(String repositoryUrl, String taskId) {
		if (repositoryUrl == null) {
			return MISSING_REPOSITORY_HANDLE + taskId;
		} else if (taskId.contains(CONTEXT_HANDLE_DELIM)) {
			throw new RuntimeException("invalid handle for task, can not contain: " + CONTEXT_HANDLE_DELIM + ", was: "
					+ taskId);
		} else {
			return repositoryUrl + CONTEXT_HANDLE_DELIM + taskId;
		}
	}

	public static String getHandle(String repositoryUrl, int taskId) {
		return AbstractRepositoryTask.getHandle(repositoryUrl, "" + taskId);
	}

	public boolean isDirty() {
		return isDirty;
	}

	public void setDirty(boolean isDirty) {
		this.isDirty = isDirty;
	}

	public RepositoryTaskData getTaskData() {
		return taskData;
	}

	public void setTaskData(RepositoryTaskData taskData) {
		this.taskData = taskData;
		// TODO: remove?
		if (taskData != null) {
			setDescription(HtmlStreamTokenizer.unescape(taskData.getSummary()));
		}
	}

	public boolean isNotified() {
		return isNotifiedIncoming;
	}

	public void setNotified(boolean notified) {
		isNotifiedIncoming = notified;
	}

	public String getOwner() {
		if (taskData != null && taskData.getAssignedTo() != null) {
			return taskData.getAssignedTo();
		} else {
			return "<unknown>";
		}
	}
	
	public IStatus getStatus() {
		return status;
	}
	
	public void setStatus(IStatus status) {
		this.status = status;
	}
	
	/**
	 * Need to update URL since it is derived from handle identifier.
	 */
	@Override
	public void setHandleIdentifier(String newHandleIdentifier) {
		String oldHandleIdentifier = getHandleIdentifier();
		String url = getUrl();
		if (oldHandleIdentifier != null && url != null) {
			String oldRepositoryUrl = AbstractRepositoryTask.getRepositoryUrl(oldHandleIdentifier);
			String newRepositoryUrl = AbstractRepositoryTask.getRepositoryUrl(newHandleIdentifier);

			if (url.startsWith(oldRepositoryUrl)) {
				setUrl(newRepositoryUrl + url.substring(oldRepositoryUrl.length()));
			}
		}		
		super.setHandleIdentifier(newHandleIdentifier);		
	}
	
}

Back to the top