Skip to main content
summaryrefslogtreecommitdiffstats
blob: e69e45783d21f18ddaabc3de62917c6c60b7a097 (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
/*******************************************************************************
 * Copyright (c) 2004 - 2005 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
 *******************************************************************************/
/*
 * Created on May 2, 2005
  */
package org.eclipse.mylar.bugs;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.mylar.bugzilla.core.BugReport;
import org.eclipse.mylar.bugzilla.core.BugzillaRepository;
import org.eclipse.mylar.bugzilla.core.BugzillaTools;
import org.eclipse.mylar.bugzilla.core.IBugzillaBug;
import org.eclipse.mylar.bugzilla.core.search.BugzillaSearchHit;
import org.eclipse.mylar.bugzilla.ui.editor.AbstractBugEditor;
import org.eclipse.mylar.bugzilla.ui.outline.BugzillaOutlineNode;
import org.eclipse.mylar.bugzilla.ui.outline.BugzillaReportSelection;
import org.eclipse.mylar.bugzilla.ui.tasklist.BugzillaCacheFile;
import org.eclipse.mylar.bugzilla.ui.tasklist.BugzillaReportNode;
import org.eclipse.mylar.core.AbstractRelationshipProvider;
import org.eclipse.mylar.core.IDegreeOfSeparation;
import org.eclipse.mylar.core.IMylarStructureBridge;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.core.internal.DegreeOfSeparation;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.progress.IProgressService;
import org.eclipse.ui.views.markers.internal.ProblemMarker;


public class BugzillaStructureBridge implements IMylarStructureBridge {

    public final static String EXTENSION = "bugzilla";
    
    public List<AbstractRelationshipProvider> providers;
    
    public String getResourceExtension() {
        return EXTENSION;
    }
    
    public BugzillaStructureBridge() {
        super();
		readCacheFile();
		providers = new ArrayList<AbstractRelationshipProvider>();
    	providers.add(MylarBugzillaPlugin.getReferenceProvider());
    }

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

    private BugReport result;
  
    public Object getObjectForHandle(final String handle) {
    	result = null;

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

            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 = MylarBugzillaPlugin.getReferenceProvider().getCached(handle);
            
            // try to get from the cache, if it doesn't exist, startup an operation to get it
            result = getFromCache(bugHandle);
            if(result == null && reportNode != null){
            	return reportNode;
            } else if(result == null && reportNode == null){
            	WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
                	protected void execute(IProgressMonitor monitor) throws CoreException {
                		monitor.beginTask("Downloading Bug# " + id, IProgressMonitor.UNKNOWN);
                        try {
                        	result = BugzillaRepository.getInstance().getCurrentBug(id);
                        }catch(Exception e){
                        	result = null;
                        }
                	}};
                	
                	 // Use the progess service to execute the runnable
                    IProgressService service = PlatformUI.getWorkbench().getProgressService();
                    try {
                    	service.run(false, false, op);
                    } catch (InvocationTargetException e) {
                    	// Operation was canceled
                    } catch (InterruptedException e) {
                    	// Handle the wrapped exception
                    }
                	
            	if(result != null)
            		cache(bugHandle, result);
            }

            BugzillaOutlineNode node = BugzillaOutlineNode.parseBugReport(result);
            return findNode(node, commentNumber);
        }
        else{
            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.getServer() + ": Bug#: " + hit.getId() + ": " + hit.getDescription();
        }
        return "";
    }

    public boolean canBeLandmark(Object element) {
        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 getResourceExtension(String elementHandle) {
        return getResourceExtension();
    }

	/*
	 *
	 * STUFF FOR CACHING BUG REPORTS
	 * 
	 */
    
    // bug report cache
	private Map<String, BugReport> cache = new HashMap<String, BugReport>();

    public void cache(String handle, BugReport report) {
		cache.put(handle, report);
		cacheFile.add(report);
	}
    
    public void clearCache(){
    	cache.clear();
    	cacheFile.removeAll();
    }

	private BugReport getFromCache(String bugHandle) {
		return cache.get(bugHandle);
	}
    
    public Set<String> getCachedHandles(){
    	return cache.keySet();
    }

    private BugzillaCacheFile cacheFile;
	
	private IPath getCacheFile() {
		IPath stateLocation = Platform.getPluginStateLocation(MylarBugzillaPlugin.getDefault());
		IPath configFile = stateLocation.append("offlineReports");
		return configFile;
	}
	
	private void readCacheFile() {
		IPath cachPath = getCacheFile();

		try {
			cacheFile = new BugzillaCacheFile(cachPath.toFile());
			ArrayList<IBugzillaBug> cached = cacheFile.elements();
			for(IBugzillaBug bug: cached){
				if(bug instanceof BugReport)
					cache.put(BugzillaTools.getHandle(bug), (BugReport)bug);
			}
		} catch (Exception e) {
		    MylarPlugin.log(e, "occurred while restoring saved offline Bugzilla reports.");
		}
	}

	public BugReport getCached(String handle) {
		return cache.get(handle);
	}

	public List<AbstractRelationshipProvider> getProviders() {
		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;
	}
}

Back to the top