Skip to main content
summaryrefslogtreecommitdiffstats
blob: e896c44d4d6e5a31a9643dee3de4929b43555a48 (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
/*******************************************************************************
 * 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.internal.bugs;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.mylar.bugzilla.core.BugReport;
import org.eclipse.mylar.core.AbstractRelationProvider;
import org.eclipse.mylar.core.IDegreeOfSeparation;
import org.eclipse.mylar.core.IMylarStructureBridge;
import org.eclipse.mylar.internal.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.internal.bugzilla.core.BugzillaRepositoryUtil;
import org.eclipse.mylar.internal.bugzilla.core.BugzillaTools;
import org.eclipse.mylar.internal.bugzilla.core.search.BugzillaSearchHit;
import org.eclipse.mylar.internal.bugzilla.ui.editor.AbstractBugEditor;
import org.eclipse.mylar.internal.bugzilla.ui.editor.BugzillaOutlineNode;
import org.eclipse.mylar.internal.bugzilla.ui.editor.BugzillaReportSelection;
import org.eclipse.mylar.internal.bugzilla.ui.tasklist.BugzillaReportNode;
import org.eclipse.mylar.internal.core.DegreeOfSeparation;
import org.eclipse.mylar.internal.tasklist.MylarTaskListPlugin;
import org.eclipse.mylar.internal.tasklist.TaskRepository;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;
import org.eclipse.ui.views.markers.internal.ProblemMarker;

/**
 * @author Mik Kersten
 * @author Shawn Minto
 */
public class BugzillaStructureBridge implements IMylarStructureBridge {

	public final static String CONTENT_TYPE = "bugzilla";

	public List<AbstractRelationProvider> providers;

	public String getContentType() {
		return CONTENT_TYPE;
	}

	public BugzillaStructureBridge() {
		super();
		providers = new ArrayList<AbstractRelationProvider>();
		// providers.add(MylarBugsPlugin.getReferenceProvider());
	}

	/**
	 * Handle format: <server-name:port>;<bug-id>;<comment#>
	 * 
	 * Use: BugzillaTools ???
	 */
	public String getHandleIdentifier(Object object) {
		if (object instanceof BugzillaOutlineNode) {
			BugzillaOutlineNode node = (BugzillaOutlineNode) object;
			return BugzillaTools.getHandle(node);
		} else if (object instanceof BugzillaReportSelection) {
			BugzillaReportSelection n = (BugzillaReportSelection) object;
			return BugzillaTools.getHandle(n);
		}
		return null;
	}

	private BugReport result;

	/**
	 * TODO: this will not return a non-cached handle
	 */
	public Object getObjectForHandle(final String handle) {
		result = null;

		// HACK: determine appropriate repository
		final TaskRepository repository = MylarTaskListPlugin.getRepositoryManager().getRepositoryForActiveTask(
				BugzillaPlugin.REPOSITORY_KIND);

		String[] parts = handle.split(";");
		if (parts.length >= 2) {
			String server = parts[0];
			final int id = Integer.parseInt(parts[1]);

			final String bugHandle = server + ";" + id;

			int commentNumber = -1;
			if (parts.length == 3) {
				commentNumber = Integer.parseInt(parts[2]);
			}

			// get the bugzillaOutlineNode for the element
			IEditorPart editorPart = null;
			try {
				editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
			} catch (NullPointerException e) {
				// do nothing, this just means that there is no active page
			}
			if (editorPart != null && editorPart instanceof AbstractBugEditor) {
				AbstractBugEditor abe = ((AbstractBugEditor) editorPart);
				BugzillaOutlineNode node = abe.getModel();
				return findNode(node, commentNumber);
			}

			BugzillaReportNode reportNode = MylarBugsPlugin.getReferenceProvider().getCached(handle);

			// try to get from the cache, if it doesn't exist, startup an
			// operation to get it
			result = MylarBugsPlugin.getDefault().getCache().getFromCache(bugHandle);
			if (result == null && reportNode != null) {
				return reportNode;
			} else if (result == null && reportNode == null) {
				IRunnableWithProgress op = new IRunnableWithProgress() {
					public void run(IProgressMonitor monitor) {
						monitor.beginTask("Downloading Bug# " + id, IProgressMonitor.UNKNOWN);
						try {
							result = BugzillaRepositoryUtil.getCurrentBug(repository.getUrl().toExternalForm(), id);
							if (result != null) {
								MylarBugsPlugin.getDefault().getCache().cache(bugHandle, result);
							}
						} catch (Exception e) {
							result = null;
						}
					}
				};

				IProgressService service = PlatformUI.getWorkbench().getProgressService();
				try {
					service.run(false, false, op);
				} catch (InvocationTargetException e) {
					// Operation was canceled
				} catch (InterruptedException e) {
					// Handle the wrapped exception
				}
				return null;
			}
			// BugzillaOutlineNode node =
			// BugzillaOutlineNode.parseBugReport(result);
			// return findNode(node, commentNumber);
		}
		return null;
	}

	private BugzillaOutlineNode findNode(BugzillaOutlineNode startNode, int commentNumber) {

		if (commentNumber == -1) {
			return startNode;
		} else if (startNode.getComment() != null && startNode.getComment().getNumber() == commentNumber - 1) {
			return startNode;
		} else if (startNode.isCommentHeader() && commentNumber == 1) {
			return startNode;
		} else if (startNode.isDescription() && commentNumber == 0) {
			return startNode;
		}

		BugzillaOutlineNode[] children = startNode.getChildren();
		for (int i = 0; i < children.length; i++) {
			BugzillaOutlineNode n = findNode(children[i], commentNumber);
			if (n != null)
				return n;
		}
		return null;
	}

	public String getParentHandle(String handle) {

		// check so that we don't need to try to get the parent if we are
		// already at the bug report
		if (!handle.matches(".*;.*;.*"))
			return null;

		BugzillaOutlineNode bon = (BugzillaOutlineNode) getObjectForHandle(handle);
		if (bon != null && bon.getParent() != null)
			return BugzillaTools.getHandle(bon.getParent());
		else
			return null;
		// String [] parts = handle.split(";");
		// if (parts.length == 1){
		// return null;
		// }else if (parts.length > 2) {
		// String newHandle = "";
		// for(int i = 0; i < parts.length - 1; i++)
		// newHandle += parts[i] + ";";
		// return newHandle.substring(0, newHandle.length() - 1);
		// // return handle.substring(0, handle.lastIndexOf(";"));
		// }
		// return null;
	}

	public String getName(Object object) {
		if (object instanceof BugzillaOutlineNode) {
			BugzillaOutlineNode b = (BugzillaOutlineNode) object;
			return BugzillaTools.getName(b);
		} else if (object instanceof BugzillaReportNode) {
			BugzillaSearchHit hit = ((BugzillaReportNode) object).getHit();
			return hit.getRepository() + ": Bug#: " + hit.getId() + ": " + hit.getDescription();
		}
		return "";
	}

	public boolean canBeLandmark(String handle) {
		return false;
	}

	public boolean acceptsObject(Object object) {
		return object instanceof BugzillaOutlineNode || object instanceof BugzillaReportSelection;
	}

	public boolean canFilter(Object element) {
		return true;
	}

	public boolean isDocument(String handle) {
		return (handle.indexOf(';') == handle.lastIndexOf(';') && handle.indexOf(";") != -1);
	}

	public String getHandleForMarker(ProblemMarker marker) {
		return null;
	}

	public IProject getProjectForObject(Object object) {
		// bugzilla objects do not yet sit in a project
		return null;
	}

	public String getContentType(String elementHandle) {
		return getContentType();
	}

	public List<AbstractRelationProvider> getRelationshipProviders() {
		return providers;
	}

	public List<IDegreeOfSeparation> getDegreesOfSeparation() {
		List<IDegreeOfSeparation> separations = new ArrayList<IDegreeOfSeparation>();
		separations.add(new DegreeOfSeparation("disabled", 0));
		separations.add(new DegreeOfSeparation("local, fully qualified matches", 1));
		separations.add(new DegreeOfSeparation("local, unqualified matches", 2));
		separations.add(new DegreeOfSeparation("server, fully quaified matches", 3));
		separations.add(new DegreeOfSeparation("server, unqualified matches", 4));

		return separations;
	}

	public String getHandleForOffsetInObject(Object resource, int offset) {
		return null;
	}

	public void setParentBridge(IMylarStructureBridge bridge) {
		// ignore
	}

	public List<String> getChildHandles(String handle) {
		return Collections.emptyList();
	}
}

Back to the top