Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7b7a0720fa0ed13488c96ab88d48a9ecb05e46a7 (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
/*******************************************************************************
 * Copyright (c) 2009 Atlassian 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:
 *     Atlassian - initial API and implementation
 ******************************************************************************/

package com.atlassian.connector.eclipse.internal.crucible.ui;

import com.atlassian.connector.eclipse.internal.crucible.core.CrucibleConstants;
import com.atlassian.connector.eclipse.internal.crucible.core.client.CrucibleClient;
import com.atlassian.connector.eclipse.internal.crucible.core.client.model.IReviewCacheListener;
import com.atlassian.connector.eclipse.internal.crucible.ui.annotations.CrucibleAnnotationModelManager;
import com.atlassian.theplugin.commons.crucible.api.model.Review;
import com.atlassian.theplugin.commons.crucible.api.model.notification.CrucibleNotification;
import com.atlassian.theplugin.commons.util.MiscUtil;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskActivationListener;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.sync.SynchronizationJob;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Class to manage the currently active review for other models
 * 
 * @author sminto
 */
public class ActiveReviewManager implements ITaskActivationListener, IReviewCacheListener {

	/**
	 * All methods are never called from UI thread.
	 * 
	 * @author pniewiadomski
	 */
	public interface IReviewActivationListener {
		void reviewActivated(ITask task, Review review);

		void reviewDeactivated(ITask task, Review review);

		void reviewUpdated(ITask task, Review review, Collection<CrucibleNotification> differences);
	};

	private static final long ACTIVE_REVIEW_POLLING_INTERVAL = 120000L;

	private final JobChangeAdapter refreshJobRescheduler = new JobChangeAdapter() {
		@Override
		public void done(IJobChangeEvent event) {
			synchronizeJob = null;
			createAndScheduleRefreshJob();
		}
	};

	private final List<IReviewActivationListener> activationListeners;

	private Review activeReview;

	private ITask activeTask;

	private SynchronizationJob synchronizeJob;

	private final boolean increasedRefresh;

	private final Map<ITask, Review> reviewByTask = new HashMap<ITask, Review>();

	public ActiveReviewManager(boolean increasedRefresh) {
		this.activationListeners = MiscUtil.buildArrayList();
		this.increasedRefresh = increasedRefresh;
	}

	public synchronized void addReviewActivationListener(IReviewActivationListener l) {
		activationListeners.add(l);
	}

	public synchronized void removeReviewActivationListener(IReviewActivationListener l) {
		activationListeners.remove(l);
	}

	private synchronized void fireReviewActivated(final ITask task, final Review review) {
		for (final IReviewActivationListener l : activationListeners) {
			l.reviewActivated(task, review);
		}
	}

	private synchronized void fireReviewDectivated(final ITask task, final Review review) {
		for (final IReviewActivationListener l : activationListeners) {
			l.reviewDeactivated(task, review);
		}
	}

	private synchronized void fireReviewUpdated(final ITask task, final Review review,
			Collection<CrucibleNotification> differences) {
		for (final IReviewActivationListener l : activationListeners) {
			l.reviewUpdated(task, review, differences);
		}
	}

	public void dispose() {
	}

	public synchronized void taskActivated(ITask task) {
		System.setProperty(CrucibleConstants.REVIEW_ACTIVE_SYSTEM_PROPERTY, "true");

		this.activeTask = task;
		this.activeReview = reviewByTask.get(task);
		if (activeReview != null) {
			scheduleDownloadJob(task);

			if (increasedRefresh) {
				startIncreasedChangePolling();
			}
			fireReviewActivated(task, activeReview);
		}
	}

	public synchronized void taskDeactivated(ITask task) {
		Review oldReview = this.activeReview;
		ITask oldTask = this.activeTask;

		this.activeTask = null;
		this.activeReview = null;
		System.setProperty(CrucibleConstants.REVIEW_ACTIVE_SYSTEM_PROPERTY, "false");
		stopIncreasedChangePolling();

		fireReviewDectivated(oldTask, oldReview);
	}

	public void preTaskActivated(ITask task) {
		// ignore
	}

	public synchronized void preTaskDeactivated(ITask task) {
		// ignore
	}

	private synchronized void activeReviewUpdated(Review cachedReview, ITask task,
			Collection<CrucibleNotification> differences) {
		if (activeTask != null && task != null && activeTask.equals(task)) {
			reviewByTask.put(task, cachedReview);
			if (activeReview == null) {
				this.activeReview = cachedReview;
				fireReviewActivated(activeTask, activeReview);
			} else {
				this.activeReview = cachedReview;
				CrucibleAnnotationModelManager.updateAllOpenEditors(activeReview);
				fireReviewUpdated(activeTask, activeReview, differences);
			}
		}
	}

	public synchronized Review getActiveReview() {
		return activeReview;
	}

	public synchronized ITask getActiveTask() {
		return activeTask;
	}

	private void startIncreasedChangePolling() {
		createAndScheduleRefreshJob();
	}

	private synchronized void createAndScheduleRefreshJob() {
		if (synchronizeJob == null && getActiveTask() != null) {
			Set<ITask> tasks = new HashSet<ITask>();
			tasks.add(getActiveTask());
//			synchronizeJob = TasksUiPlugin.getTaskJobFactory().createSynchronizeTasksJob(
//					CrucibleCorePlugin.getRepositoryConnector(), tasks);
//			synchronizeJob.setUser(false);
//			synchronizeJob.addJobChangeListener(refreshJobRescheduler);
//			synchronizeJob.schedule(ACTIVE_REVIEW_POLLING_INTERVAL);
		}
	}

	private void stopIncreasedChangePolling() {
		if (synchronizeJob != null) {
			synchronizeJob.removeJobChangeListener(refreshJobRescheduler);
			synchronizeJob.cancel();
			synchronizeJob = null;
		}
	}

	private void scheduleDownloadJob(final ITask task) {
		Job downloadJob = new Job("Retrieving review from server") {

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				IStatus errorStatus = null;
				try {
					TaskRepository repository = CrucibleUiUtil.getCrucibleTaskRepository(task.getRepositoryUrl());

					if (repository != null) {
						String taskId = task.getTaskId();

						CrucibleClient client = CrucibleUiPlugin.getClient(repository);
						if (client != null) {
							// This should fire off a listener that we listen to to update the review properly
							client.getReview(repository, taskId, false, monitor);
						} else {
							errorStatus = new Status(IStatus.ERROR, CrucibleUiPlugin.PLUGIN_ID,
									"Unable to get crucible client for repository");

						}
					} else {
						errorStatus = new Status(IStatus.ERROR, CrucibleUiPlugin.PLUGIN_ID,
								"Crucible repository does not exist");

					}
				} catch (CoreException e) {
					errorStatus = e.getStatus();

				} finally {
					if (errorStatus != null && !errorStatus.isOK()) {
						StatusHandler.log(errorStatus);
						TasksUiInternal.asyncDisplayStatus("Unable to retrieve Review", errorStatus);
					}
				}
				return Status.OK_STATUS;
			}
		};
		downloadJob.setUser(true);
		downloadJob.setPriority(Job.INTERACTIVE);
		downloadJob.schedule();
	}

	public synchronized boolean isReviewActive() {
		return activeTask != null && activeReview != null;
	}

	public void reviewAdded(String repositoryUrl, String taskId, Review review) {
		if (activeTask != null) {
			if (activeTask.getRepositoryUrl().equals(repositoryUrl) && activeTask.getTaskId().equals(taskId)) {
				activeReviewUpdated(review, activeTask, Collections.<CrucibleNotification> emptyList());
			}
		}
	}

	public synchronized void reviewUpdated(String repositoryUrl, String taskId, Review review,
			List<CrucibleNotification> differences) {
		if (activeTask != null) {
			if (activeTask.getRepositoryUrl().equals(repositoryUrl) && activeTask.getTaskId().equals(taskId)) {
				activeReviewUpdated(review, activeTask, differences);
			}
		}
	}

	/**
	 * public for testing only!
	 * 
	 * @param review
	 * @param task
	 */
	public void setActiveReview(Review review, ITask task) {
		this.activeTask = task;
		this.activeReview = review;
	}

	public void activeReviewUpdated() {
		activeReviewUpdated(activeReview, activeTask, Collections.<CrucibleNotification> emptyList());
	}
}

Back to the top