Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2b07ee1e4b9b348781da95f3f737460db7b39a1f (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*******************************************************************************
 * Copyright (c) 2003 - 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.wizard;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;

import javax.security.auth.login.LoginException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.mylar.bugzilla.BugzillaPlugin;
import org.eclipse.mylar.bugzilla.BugzillaPreferences;
import org.eclipse.mylar.bugzilla.IBugzillaConstants;
import org.eclipse.mylar.bugzilla.core.Attribute;
import org.eclipse.mylar.bugzilla.core.BugPost;
import org.eclipse.mylar.bugzilla.core.BugzillaException;
import org.eclipse.mylar.bugzilla.ui.editor.ExistingBugEditorInput;
import org.eclipse.search.internal.ui.SearchMessages;
import org.eclipse.search.internal.ui.util.ExceptionHandler;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PartInitException;


/**
 * Class that contains shared functions for the wizards that submit bug reports.
 *
 * @author Eric Booth
 */
public abstract class AbstractBugWizard extends Wizard implements INewWizard {

	/** The model used to store all of the data for the wizard */
	protected NewBugModel model;

	/**
	 * Flag to indicate if the wizard can be completed based on the attributes
	 * page
	 */
	protected boolean attributeCompleted = false;

	/** The workbench instance */
	protected IWorkbench workbenchInstance;

	/** The ID of the posted bug report. */
	private String id;

	/**
	 * Constructor for AbstractBugWizard
	 */
	public AbstractBugWizard() {
		super();
		model = new NewBugModel();
		id = null; // Since there is no bug posted yet.
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
	 *      org.eclipse.jface.viewers.IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.workbenchInstance = workbench;
	}

	@Override
	public void addPages() {
		try {
			// check Bugzilla preferences to see if user has supplied a username
			if (BugzillaPreferences.getUserName().equals(""))
				throw new LoginException(
						"A Bugzilla User Name has not been provided."
								+ "  Please check your Bugzilla Preferences information.");
			// Each wizard has its own way of creating and adding the page
			addPagesHelper();
		} catch (LoginException e) {
			MessageDialog.openError(Display.getDefault().getActiveShell(), "Posting Error",
			        "Cannot proceed because your login name or password is incorrect."
			        + "\nPlease check your settings in the Bugzilla preferences. ");
	} catch (Exception e) {
		    BugzillaPlugin.getDefault().logAndShowExceptionDetailsDialog(e, "occurred.", "Bugzilla Error");
		}
	}

	/**
	 * A helper function for "addPages" that creates and adds the first page to
	 * the wizard
	 * 
	 * @throws Exception
	 */
	abstract protected void addPagesHelper() throws Exception;

	/**
	 * A helper function for "canFinish" (implemented in the individual wizard
	 * classes)
	 * 
	 * @param dataPage
	 *            The first page in the wizard, casted as an
	 *            AbstractWizardListPage.
	 * @return true if the wizard could be finished, and false otherwise
	 */
	public boolean canFinishHelper(AbstractWizardListPage dataPage) {
		// cannot complete the wizard from the first page
		if (this.getContainer().getCurrentPage() == dataPage)
			return false;
		else
			return attributeCompleted;
	}

	@Override
	public boolean performFinish() {
		if (getWizardDataPage().serverSelected()) {
			// If the bug report is sent successfully,
			// then close the wizard and open the bug in an editor
			if (postBug()) {
				openBugEditor();
				return true;
			}
			// If the report was not sent, keep the wizard open
			else {
				return false;
			}
		}
		
		if (getWizardDataPage().offlineSelected()) {
			saveBugOffline();
			return true;
		}
		
		// If no action was selected, keep the wizard open.
		return false;
	}

	/**
	 * Attempts to post the bug on the Bugzilla server. If it fails, an error
	 * message pops up.
	 * 
	 * @return true if the bug is posted successfully, and false otherwise
	 */
	protected boolean postBug() {

		BugPost form = new BugPost();
		form.setPrefix("Bug ");
		form.setPostfix1(" posted");
		form.setPostfix2(" Submitted");

		try {
			setURL(form, "post_bug.cgi");
			// go through all of the attributes and add them to the bug post
			Iterator<Attribute> itr = model.getAttributes().iterator();
			while (itr.hasNext()) {
				Attribute a = itr.next();
				if (a != null && a.getParameterName() != null
						&& a.getParameterName().compareTo("") != 0
						&& !a.isHidden()) {
					String key = a.getName();
					String value = null;

					// get the values from the attribute
					if (key.equalsIgnoreCase("OS")) {
						value = a.getValue();
					} else if (key.equalsIgnoreCase("Version")) {
						value = a.getValue();
					} else if (key.equalsIgnoreCase("Severity")) {
						value = a.getValue();
					} else if (key.equalsIgnoreCase("Platform")) {
						value = a.getValue();
					} else if (key.equalsIgnoreCase("Component")) {
						value = a.getValue();
					} else if (key.equalsIgnoreCase("Priority")) {
						value = a.getValue();
					} else if (key.equalsIgnoreCase("URL")) {
						value = a.getValue();
					}

					// add the attribute to the bug post
					if (value == null)
						value = "";

					form.add(a.getParameterName(), value);
				} else if (a != null && a.getParameterName() != null
						&& a.getParameterName().compareTo("") != 0
						&& a.isHidden()) {
					// we have a hidden attribute, add it to the posting
					form.add(a.getParameterName(), a.getValue());

				}

			}

			// set the summary, and description

			// add the summary to the bug post
			form.add("short_desc", model.getSummary());

			// format the description of the bug so that it is roughly in 80
			// character lines
			formatDescription();

			if (model.getDescription().length() != 0) {
				// add the new comment to the bug post if there is some text in
				// it
				form.add("comment", model.getDescription());
			}

			// Flag to indicate if the bug was successfully sent
			boolean sentSuccessfully = false;

			// update the bug on the server
			try {
				id = form.post();

				if (id != null) {
					sentSuccessfully = true;
				}

			} catch (BugzillaException e) {
				MessageDialog
						.openError(
								null,
								"I/O Error",
								"Bugzilla could not post your bug.");
				BugzillaPlugin.log(e);
			} catch (LoginException e) {
				// if we had an error with logging in, display an error
				MessageDialog
						.openError(
								null,
								"Posting Error",
								"Bugzilla could not post your bug since your login name or password is incorrect."
										+ "\nPlease check your settings in the bugzilla preferences. ");
			}

			return sentSuccessfully;
		} catch (MalformedURLException e) {
			MessageDialog
					.openError(
							null,
							"Unsupported Protocol",
							"The server that was specified for Bugzilla is not supported by your JVM."
									+ "\nPlease make sure that you are using a JDK that supports SSL.");
			BugzillaPlugin.log(e);
			return false; // The report was not sent
		}
	}

	/**
	 * Try to open the editor with the newly created bug.
	 */
	protected void openBugEditor() {

		IEditorInput input = null;
		try {
			input = new ExistingBugEditorInput(Integer.parseInt(id));
			BugzillaPlugin.getDefault().getWorkbench()
					.getActiveWorkbenchWindow().getActivePage().openEditor(
							input, IBugzillaConstants.EXISTING_BUG_EDITOR_ID, false);
		} catch (LoginException e) {
			// if we had an error with logging in, display an error
			MessageDialog.openError(null, "Posting Error",
			        "Bugzilla could not access and display your bug in the editor because your login name or password is incorrect."
			        + "\nPlease check your settings in the bugzilla preferences. ");
		} catch (PartInitException e) {
			// if there was a problem, handle it and log it, then get out of
			// here
			ExceptionHandler.handle(e, SearchMessages.Search_Error_search_title, SearchMessages.Search_Error_search_message);
			BugzillaPlugin.log(e.getStatus());
		} catch (IOException e) {
		    BugzillaPlugin.getDefault().logAndShowExceptionDetailsDialog(e, "occurred while opening the bug report.", "Bugzilla Error");
		}
	}

	/**
	 * Saves the bug report offline on the user's hard-drive. All offline bug
	 * reports are saved together in a single file in the plug-in's directory.
	 */
	abstract protected void saveBugOffline();

	/**
	 * Function to set the url to post the bug to.
	 * 
	 * @param form
	 *            A reference to a BugPost that the bug is going to be posted to
	 * @param formName
	 *            The form that we wish to use to submit the bug
	 */
	protected void setURL(BugPost form, String formName)
			throws MalformedURLException {

		String baseURL = BugzillaPlugin.getDefault().getServerName();
		if (!baseURL.endsWith("/"))
			baseURL += "/";
		form.setURL(baseURL + formName);

		// add the login information to the bug post
		form.add("Bugzilla_login", BugzillaPreferences.getUserName());
		form.add("Bugzilla_password", BugzillaPreferences.getPassword());
	}

	/**
	 * Format the description into lines of about 80 characters so that it is
	 * displayed properly in bugzilla
	 */
	protected void formatDescription() {
		String origDesc = model.getDescription();
		String[] descArray = new String[(origDesc.length() / 80 + 1) * 2];
		for (int i = 0; i < descArray.length; i++)
			descArray[i] = null;
		int j = 0;
		while (true) {
			int spaceIndex = origDesc.indexOf(" ", 75);
			if (spaceIndex == origDesc.length() || spaceIndex == -1) {
				descArray[j] = origDesc;
				break;
			}
			descArray[j] = origDesc.substring(0, spaceIndex);
			origDesc = origDesc.substring(spaceIndex + 1, origDesc.length());
			j++;
		}

		String newDesc = "";

		for (int i = 0; i < descArray.length; i++) {
			if (descArray[i] == null)
				break;
			newDesc += descArray[i] + "\n";
		}
		model.setDescription(newDesc);

	}
	
	/**
	 * @return the last page of this wizard
	 */
	abstract protected AbstractWizardDataPage getWizardDataPage();
}

Back to the top