Skip to main content
summaryrefslogtreecommitdiffstats
blob: 47649c53a2a000367fd6fdbc7c9a9f740c90b576 (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
/*******************************************************************************
 * 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
 *******************************************************************************/

package org.eclipse.mylar.bugzilla.ui.tasklist;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylar.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylar.bugzilla.core.search.BugzillaSearchHit;
import org.eclipse.mylar.bugzilla.ui.BugzillaImages;
import org.eclipse.mylar.bugzilla.ui.BugzillaUiPlugin;
import org.eclipse.mylar.bugzilla.ui.search.BugzillaResultCollector;
import org.eclipse.mylar.bugzilla.ui.tasklist.BugzillaCategorySearchOperation.ICategorySearchListener;
import org.eclipse.mylar.internal.tasklist.MylarTaskListPlugin;
import org.eclipse.mylar.internal.tasklist.ui.ITaskListElement;
import org.eclipse.mylar.internal.tasklist.ui.TaskListImages;
import org.eclipse.mylar.internal.tasklist.ui.views.TaskListView;
import org.eclipse.mylar.tasklist.IQueryHit;
import org.eclipse.mylar.tasklist.IRepositoryQuery;
import org.eclipse.mylar.tasklist.TaskRepository;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.Workbench;

/**
 * @author Shawn Minto
 * @author Mik Kersten
 */
public class BugzillaQueryCategory implements IRepositoryQuery {

	private String repositoryUrl;
	
	private String queryUrl;

	private int maxHits;

	private List<IQueryHit> hits = new ArrayList<IQueryHit>();

//	private boolean hasBeenRefreshed = false;

	protected Date lastRefresh;

	protected String description = "";

	private String handle = "";

	private ICategorySearchListener listener = new BugzillaQueryCategorySearchListener();

	private boolean maxHitsReached = false;

	public class BugzillaQueryCategorySearchListener implements ICategorySearchListener {

		Map<Integer, BugzillaSearchHit> hits = new HashMap<Integer, BugzillaSearchHit>();

		public void searchCompleted(BugzillaResultCollector collector) {
			for (BugzillaSearchHit hit : collector.getResults()) {

				addHit(new BugzillaQueryHit(
						hit.getId() + ": " + hit.getDescription(), 
						hit.getPriority(), 
						repositoryUrl,
						hit.getId(), 
						null,
						hit.getState()));
			}
		}

	}

	public BugzillaQueryCategory(String repositoryUrl, String queryUrl, String label, String maxHits) {
		this.description = label;
		this.queryUrl = queryUrl;
		this.repositoryUrl = repositoryUrl;
		try {
			this.maxHits = Integer.parseInt(maxHits);
		} catch (Exception e) {
			this.maxHits = -1;
		}
	}

	public String getDescription() {
		return description;
	}

	public Image getIcon() {
		return TaskListImages.getImage(BugzillaImages.CATEGORY_QUERY);
	}

	public String getQueryUrl() {
		return queryUrl;
	}

	public List<IQueryHit> getHits() {
		return hits;
	}

	public void addHit(IQueryHit hit) {
		BugzillaTask task = BugzillaUiPlugin.getDefault().getBugzillaTaskListManager().getFromBugzillaTaskRegistry(
				hit.getHandleIdentifier());
		hit.setCorrespondingTask(task);
		hits.add(hit);
	}

	public void removeHit(BugzillaQueryHit hit) {
		hits.remove(hit);
	}

	public void refreshBugs() {
		hits.clear();
		// refresh the view to show that the results are gone
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				if (TaskListView.getDefault() != null)
					TaskListView.getDefault().getViewer().refresh();
			}
		});

		TaskRepository repository = MylarTaskListPlugin.getRepositoryManager().getRepository(BugzillaPlugin.REPOSITORY_KIND, repositoryUrl);
		if (repository == null) {
            Workbench.getInstance().getDisplay().asyncExec(new Runnable() {
                public void run() {
                	MessageDialog.openInformation(
        					Display.getDefault().getActiveShell(), IBugzillaConstants.TITLE_MESSAGE_DIALOG,
        					"No task repository associated with this query. Open the query to associate it with a repository.");  
                }
            });
//			MylarStatusHandler.fail(null, "could not find repository for url: " + repositoryUrl, true);
		} else {
			final BugzillaCategorySearchOperation catSearch = new BugzillaCategorySearchOperation(
					repository, getQueryUrl(), maxHits);
			catSearch.addResultsListener(listener);
			final IStatus[] status = new IStatus[1];
	
			try {
				// execute the search operation
				catSearch.execute(new NullProgressMonitor());
				maxHitsReached = catSearch.isMaxReached();
//				hasBeenRefreshed = true;
				lastRefresh = new Date();
	
				// get the status of the search operation
				status[0] = catSearch.getStatus();
	
				// determine if there was an error, if it was cancelled, or if it is
				// ok
				if (status[0].getCode() == IStatus.CANCEL) {
					// it was cancelled, so just return
					status[0] = Status.OK_STATUS;
				} else if (!status[0].isOK()) {
					// there was an error, so display an error message
					PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
						public void run() {
							ErrorDialog.openError(null, "Bugzilla Search Error", null, status[0]);
						}
					});
					status[0] = Status.OK_STATUS;
				}
			} catch (LoginException e) {
				// we had a problem while searching that seems like a login info
				// problem
				// thrown in BugzillaSearchOperation
				MessageDialog
						.openError(
								Display.getDefault().getActiveShell(),
								"Login Error",
								"Bugzilla could not log you in to get the information you requested since login name or password is incorrect.\nPlease check your settings in the bugzilla preferences. ");
				BugzillaPlugin.log(new Status(IStatus.ERROR, IBugzillaConstants.PLUGIN_ID, IStatus.OK, "", e));
			}
		}
	}

	public void setQueryUrl(String url) {
		this.queryUrl = url;
	}

	public String getPriority() {
		String highestPriority = MylarTaskListPlugin.PriorityLevel.P5.toString();
		if (hits.isEmpty()) {
			return MylarTaskListPlugin.PriorityLevel.P1.toString();
		}
		for (IQueryHit hit : hits) {
			if (highestPriority.compareTo(hit.getPriority()) > 0) {
				highestPriority = hit.getPriority();
			}
		}
		return highestPriority;
	}

	public boolean isLocal() {
		return true;
	}

	public boolean isActivatable() {
		return false;
	}

	public boolean isDragAndDropEnabled() {
		return false;
	}

	public Font getFont() {
		for (ITaskListElement child : getHits()) {
			if (child instanceof BugzillaQueryHit) {
				BugzillaQueryHit hit = (BugzillaQueryHit) child;
				BugzillaTask task = hit.getCorrespondingTask();
				if (task != null && task.isActive()) {
					return TaskListImages.BOLD;
				}
			}
		}
		return null;
	}

	public boolean isCompleted() {
		return false;
	}

	public String getToolTipText() {
		String tooltip = "";
		if (hits.size() == 1) {
			tooltip += "1 hit";
		} else {
			tooltip += hits.size() + " hits";
		}
		if (maxHitsReached) {
			tooltip += " MAX REACHED";
		}
		tooltip += " (max set to: " + maxHits + ")";
		tooltip += BugzillaTask.getLastRefreshTime(lastRefresh);
		return tooltip;
//		if (hits.size() > 0 || !label) {
//		if (!hasBeenRefreshed && label) {
//			return description + " <needs refresh>";
//		} else if (maxHitsReached && label) {
//			return description + " <first " + maxHits + " hits>";
//		} else {
//			return description;
//		}
//	} else if (!hasBeenRefreshed) {
//		return description + " <needs refresh>";
//	} else {
//		return description + " <no hits>";
//	}
	}

	public int getMaxHits() {
		return maxHits;
	}

	public void setMaxHits(int maxHits) {
		this.maxHits = maxHits;
	}

	public Image getStatusIcon() {
		return null;
	}

	public String getHandleIdentifier() {
		return handle;
	}

	public void setDescription(String description) {
		this.description = description;
	}

//	public String getStringForSortingDescription() {
//		return getDescription(true);
//	}

	public void setHandleIdentifier(String id) {
		this.handle = id;
	}

	public String getRepositoryUrl() {
		return repositoryUrl;
	}

	public void setRepositoryUrl(String repositoryUrl) {
		this.repositoryUrl = repositoryUrl;
	}
}

Back to the top