Skip to main content
summaryrefslogtreecommitdiffstats
blob: 48023040f125ca3eef8ec4e6688556d2a836c5d7 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*******************************************************************************
 * Copyright (c) 2010 Red Hat Inc. 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:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.xmlrpc.XmlRpcException;
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.mylyn.internal.bugzilla.core.service.BugzillaXmlRpcClient;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;

/**
 * @author Charley Wang
 */
public class CustomTransitionManager implements Serializable {
	private static final long serialVersionUID = 3340305752692674487L;

	private static final String DEFAULT_DUPLICATE_STATUS = "RESOLVED"; //$NON-NLS-1$

	/** Default start status -- this uses the Mylyn default from before bug #317729 */
	public static final String DEFAULT_START_STATUS = "NEW"; //$NON-NLS-1$

	/*
	 * Create the default Bugzilla operations
	 */

	//Indexed by the current status
	private final HashMap<String, List<AbstractBugzillaOperation>> operationMapByCurrentStatus;

	//Indexed by the status to be transitioned into
	private final HashMap<String, List<AbstractBugzillaOperation>> operationMapByEndStatus;

	//Statuses that are not marked as is_open by Bugzilla
	private final ArrayList<String> closedStatuses;

	//Whether or not customized names were used in Bugzilla
	private boolean customNames = false;

	private boolean valid = false;

	private String filePath;

	private String duplicateStatus;

	private String startStatus;

	public CustomTransitionManager() {
		operationMapByCurrentStatus = new HashMap<String, List<AbstractBugzillaOperation>>();
		operationMapByEndStatus = new HashMap<String, List<AbstractBugzillaOperation>>();
		closedStatuses = new ArrayList<String>();
		this.valid = false;
		this.filePath = ""; //$NON-NLS-1$
		duplicateStatus = DEFAULT_DUPLICATE_STATUS;
		startStatus = DEFAULT_START_STATUS;
	}

	/**
	 * Searches for a valid transition description file. Returns true if a file exists and was sucessfully parsed, false
	 * otherwise
	 * 
	 * @param filePath
	 * @return true if anything was changed, false otherwise.
	 * @throws CoreException
	 */
	public boolean parse(String filePath) throws CoreException {
		if (filePath == null || filePath.length() < 1) {
			setValid(false);
			return false;
		} else if (filePath.equals(this.filePath)) {
			//Do nothing, already parsed this file
			return false;
		}
		this.filePath = filePath;
		setValid(true);

		operationMapByCurrentStatus.clear();
		operationMapByEndStatus.clear();
		closedStatuses.clear();

		File file = new File(filePath);
		if (!file.exists() || !file.canRead()) {
			setValid(false);
			return isValid();
		}

		BufferedReader br = null;
		try {
			String s;
			boolean checkOptions = true;
			br = new BufferedReader(new FileReader(file));

			while ((s = br.readLine()) != null && isValid()) {
				if (s.equals("<transitions>")) { //$NON-NLS-1$
					checkOptions = false;
					defaultNames();
					continue;
				}

				if (checkOptions) {
					parseOptions(s);
				} else {
					parseTransitions(s);
				}
			}

		} catch (IOException e) {
			setValid(false);
			throw new CoreException(new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, 1,
					"Error parsing transition description file.\n\n" + e.getMessage(), e)); //$NON-NLS-1$
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
				}
			}
		}

		return valid;
	}

	private void parseOptions(String s) throws IOException {
		String[] pieces = s.split("="); //$NON-NLS-1$
		if (pieces.length != 2) {
			throw new IOException(Messages.CustomTransitionManager_InvalidBugzillaOption + s);
		}

		String name = pieces[0];
		String value = pieces[1];

		if (name.equals("CustomStatusNames")) { //$NON-NLS-1$
			if (value.equals("true")) { //$NON-NLS-1$
				customNames = true;
			} else {
				customNames = false;
			}
		} else if (name.equals("DuplicateStatus")) { //$NON-NLS-1$
			duplicateStatus = value;
		} else if (name.equals("ClosedCustomStatus")) { //$NON-NLS-1$
			closedStatuses.add(value);
		} else if (name.equals("StartStatus")) {//$NON-NLS-1$
			startStatus = value;
		}
	}

	private void parseTransitions(String s) throws IOException {
		String[] pieces = s.split(":"); //$NON-NLS-1$
		if (pieces.length < 4) {
			throw new IOException(Messages.CustomTransitionManager_InvalidBugzillaTransition + s);
		}
		String status = pieces[1];
		String[] endStatuses = pieces[3].split(","); //$NON-NLS-1$
		parse(status, endStatuses);
	}

	private void defaultNames() {

		/*
		 * Add the default operations if we are not using custom names
		 */

		//Step 1: Make custom operations for the default operations
		//This is necessary so we can add verifiers to these operations later
		if (operationMapByEndStatus.size() == 0) {

			ArrayList<AbstractBugzillaOperation> list = new ArrayList<AbstractBugzillaOperation>();

			//NONE is currently not used from the map, included for completeness
			list.add(BugzillaOperation.none);
			operationMapByEndStatus.put("NONE", list); //$NON-NLS-1$

			list = new ArrayList<AbstractBugzillaOperation>();
			list.add(BugzillaOperation.accept);
			operationMapByEndStatus.put("ASSIGNED", list); //$NON-NLS-1$

			list = new ArrayList<AbstractBugzillaOperation>();
			list.add(BugzillaOperation.resolve);
			if (duplicateStatus.equals("RESOLVED")) { //$NON-NLS-1$
				list.add(BugzillaOperation.duplicate);
			}
			operationMapByEndStatus.put("RESOLVED", list); //$NON-NLS-1$

			list = new ArrayList<AbstractBugzillaOperation>();
			list.add(BugzillaOperation.reopen);
			operationMapByEndStatus.put("REOPENED", list); //$NON-NLS-1$

			list = new ArrayList<AbstractBugzillaOperation>();
			list.add(BugzillaOperation.reopen);
			operationMapByEndStatus.put("UNCONFIRMED", list); //$NON-NLS-1$

			list = new ArrayList<AbstractBugzillaOperation>();
			list.add(BugzillaOperation.verify);
			if (duplicateStatus.equals("VERIFIED")) { //$NON-NLS-1$
				list.add(BugzillaOperation.duplicate);
			}
			operationMapByEndStatus.put("VERIFIED", list); //$NON-NLS-1$

			list = new ArrayList<AbstractBugzillaOperation>();
			list.add(BugzillaOperation.close);
			list.add(BugzillaOperation.close_with_resolution);
			if (duplicateStatus.equals("CLOSED")) { //$NON-NLS-1$
				list.add(BugzillaOperation.duplicate);
			}
			operationMapByEndStatus.put("CLOSED", list); //$NON-NLS-1$

			list = new ArrayList<AbstractBugzillaOperation>();
			list.add(BugzillaOperation.markNew);
			operationMapByEndStatus.put("NEW", list); //$NON-NLS-1$
		}

	}

	public List<AbstractBugzillaOperation> getOperation(String id) {
		return operationMapByEndStatus.get(id);
	}

	public List<AbstractBugzillaOperation> getValidTransitions(String key) {
		return operationMapByCurrentStatus.get(key);
	}

	/**
	 * Sets whether or not this class is valid. If set to false, the filePath will be set to "" so that subsequent calls
	 * to parse(filePath) will work.
	 * 
	 * @param val
	 */
	public void setValid(boolean val) {
		if (val == false) {
			this.filePath = ""; //$NON-NLS-1$
		}
		this.valid = val;
	}

	public boolean isValid() {
		return this.valid;
	}

	/**
	 * Returns the duplicate status. Standard Bugzilla installations will have a duplicate status of RESOLVED, VERIFIED
	 * or CLOSED. <br>
	 * By default, the duplicate status will be RESOLVED.
	 * 
	 * @return The status to send if a bug is set to Duplicate
	 */
	public String getDuplicateStatus() {
		if (duplicateStatus == null || duplicateStatus.length() == 0) {
			duplicateStatus = DEFAULT_DUPLICATE_STATUS;
		}
		return duplicateStatus;
	}

	/**
	 * Returns the start status. Standard Bugzilla installations have 2 available start statuses, UNCONFIRMED and NEW.
	 * Each user has a permissions setting that determines whether their new bugs start as UNCONFIRMED or NEW. This
	 * permissions setting currently cannot be accessed, so this function does not try to guess and returns either the
	 * default status (NEW) or whichever status was set by a transition file. <br>
	 * Fix for bug #318128, set startStatus to NEW if startStatus is somehow null at this point.
	 * 
	 * @return The valid start status. Default value is NEW.
	 */
	public String getStartStatus() {
		if (startStatus == null || startStatus.length() == 0) {
			startStatus = DEFAULT_START_STATUS;
		}
		return startStatus;
	}

	private void addTransition(String start, String endStatus) {
		if (!customNames
				&& (start.equals("REOPENED") && (endStatus.equals("UNCONFIRMED") || endStatus.equals("REOPENED")))) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			//REOPENED should not retransition to REOPENED
			return;
		}
		List<AbstractBugzillaOperation> list = operationMapByCurrentStatus.get(start);
		if (list == null) {
			list = new ArrayList<AbstractBugzillaOperation>();
		}
		list.addAll(operationMapByEndStatus.get(endStatus));
		operationMapByCurrentStatus.put(start, list);
	}

	private void parse(String start, Object[] transitions) {
		addNewStatus(start);

		//Find valid transitions
		for (Object o : transitions) {
			if ((o instanceof HashMap<?, ?>)) {
				//Used for XMLRPC
				HashMap<?, ?> tran = (HashMap<?, ?>) o;
				String endStatus = (String) tran.get("name"); //$NON-NLS-1$
				addNewStatus(endStatus);
				if (!endStatus.equals(start)) {
					addTransition(start, endStatus);
				}
			} else if (o instanceof String) {
				//Used for files
				String endStatus = (String) o;
				addNewStatus(endStatus);
				if (!endStatus.equals(start)) {
					addTransition(start, endStatus);
				}
			}
		}
	}

	public void parse(IProgressMonitor monitor, BugzillaXmlRpcClient xmlClient) throws CoreException {
		this.filePath = ""; //$NON-NLS-1$
		operationMapByCurrentStatus.clear();
		operationMapByEndStatus.clear();
		closedStatuses.clear();
		defaultNames();
		setValid(false);
		//Assume custom names, we have no way to check
		customNames = true;

		try {
			if (xmlClient.getUserID() == -1) {
				xmlClient.login(monitor);
			}
			String[] fields = new String[1];
			fields[0] = "bug_status"; //$NON-NLS-1$
			for (Object raw : xmlClient.getFieldsWithNames(monitor, fields)) {
				if (raw instanceof HashMap<?, ?>) {
					Object[] values = (Object[]) ((HashMap<?, ?>) raw).get("values"); //$NON-NLS-1$
					if (values == null) {
						continue;
					}
					for (Object status : values) {
						if (status instanceof HashMap<?, ?>) {
							//Get name
							HashMap<?, ?> map = (HashMap<?, ?>) status;
							String start = (String) map.get("name"); //$NON-NLS-1$
							//Get is_open
							Object is_open = map.get("is_open"); //$NON-NLS-1$
							if (is_open.toString().equals("false")) { //$NON-NLS-1$
								closedStatuses.add(start);
							}
							parse(start, (Object[]) map.get("can_change_to")); //$NON-NLS-1$

						}
					}
				} else {
					throw new XmlRpcException(Messages.CustomTransitionManager_UnexpectedResponse);
				}
			}
			//Should check if there are conditions we can use to terminate early
			if (operationMapByCurrentStatus.size() == 0) {
				throw new XmlRpcException(Messages.CustomTransitionManager_UnexpectedResponse);
			}

			setValid(true);
		} catch (XmlRpcException e) {
			setValid(false);
			String message = e.linkedException == null ? e.getMessage() : e.getMessage() + ">" //$NON-NLS-1$
					+ e.linkedException.getMessage();
			throw new CoreException(new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, 1,
					"Error parsing xmlrpc response.\n\n" + message, e)); //$NON-NLS-1$
		}
	}

	/**
	 * Creates a new status with a single operation with the same name as the status itself. Does nothing if a status of
	 * that name already exists.
	 * 
	 * @param status
	 */
	private void addNewStatus(String status) {
		List<AbstractBugzillaOperation> list = operationMapByEndStatus.get(status);
		if (list == null) {
			list = new ArrayList<AbstractBugzillaOperation>();
		} else {
			return;
		}

		if (!closedStatuses.contains(status)) {
			list.add(new BugzillaOperation(AbstractBugzillaOperation.DEFAULT_LABEL_PREFIX + status));
		} else {
			list.add(new BugzillaOperation(AbstractBugzillaOperation.DEFAULT_LABEL_PREFIX + status, "resolution", //$NON-NLS-1$
					TaskAttribute.TYPE_SINGLE_SELECT, status));
		}

		operationMapByEndStatus.put(status, list);
	}

	public ArrayList<String> getClosedStatuses() {
		return closedStatuses;
	}

}

Back to the top