Skip to main content
summaryrefslogtreecommitdiffstats
blob: 924a7128615aa24d913d6d7bc72331ece26fa41c (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
/*******************************************************************************
 * 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
 *******************************************************************************/
/*
 * Created on Nov 19, 2004
 */
package org.eclipse.mylar.internal.bugs.search;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IType;
import org.eclipse.mylar.internal.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.internal.bugzilla.core.BugzillaRepositoryUtil;
import org.eclipse.mylar.internal.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylar.internal.tasklist.MylarTaskListPlugin;
import org.eclipse.mylar.tasklist.TaskRepository;

/**
 * Utilities methods for the BugzillaMylarBridge
 * 
 * @author Shawn Minto
 * @author Mik Kersten
 */
public class Util {

	/**
	 * List of all of the search operations that can be done <br>
	 * all words, any words, regex
	 */
	private static final String[] patternOperationValues = { "allwordssubstr", "anywordssubstr", "regexp" };

	/**
	 * Sugzilla preferences so that we can get the search params
	 */
	// private static IPreferenceStore prefs =
	// BugzillaPlugin.getDefault().getPreferenceStore();
	// private static String[] resolutionValues =
	// BugzillaRepositoryUtil.convertQueryOptionsToArray(prefs.getString(IBugzillaConstants.VALUES_RESOLUTION));
	//   
	// private static String[] statusValues =
	// BugzillaRepositoryUtil.convertQueryOptionsToArray(prefs.getString(IBugzillaConstants.VALUES_STATUS));
	/**
	 * Get the bugzilla url used for searching for exact matches
	 * 
	 * @param je
	 *            The IMember to create the query string for
	 * @return A url string for the search
	 */
	public static String getExactSearchURL(String repositoryUrl, IMember je) {
		StringBuffer sb = getQueryURLStart(repositoryUrl);

		String long_desc = "";

		// get the fully qualified name of the element
		long_desc += BugzillaMylarSearchOperation.getFullyQualifiedName(je);

		try {
			// encode the string to be used as a url
			sb.append(URLEncoder.encode(long_desc, Charset.defaultCharset().toString()));
		} catch (UnsupportedEncodingException e) {
			// should never get here since we are using the default encoding
		}
		sb.append(getQueryURLEnd(repositoryUrl));

		return sb.toString();
	}

	/**
	 * Get the bugzilla url used for searching for inexact matches
	 * 
	 * @param je
	 *            The IMember to create the query string for
	 * @return A url string for the search
	 */
	public static String getInexactSearchURL(String repositoryUrl, IMember je) {
		StringBuffer sb = getQueryURLStart(repositoryUrl);

		String long_desc = "";

		// add the member, qualified with just its parents name
		if (!(je instanceof IType))
			long_desc += je.getParent().getElementName() + ".";
		long_desc += je.getElementName();

		try {
			// encode the string to be used as a url
			sb.append(URLEncoder.encode(long_desc, Charset.defaultCharset().toString()));
		} catch (UnsupportedEncodingException e) {
			// should never get here since we are using the default encoding
		}
		sb.append(getQueryURLEnd(repositoryUrl));

		return sb.toString();
	}

	/**
	 * Create the end of the bugzilla query URL with all of the status' and
	 * resolutions that we want
	 * 
	 * @return StringBuffer with the end of the query URL in it
	 */
	public static StringBuffer getQueryURLEnd(String repositoryUrl) {

		StringBuffer sb = new StringBuffer();

		String[] resolutionValues = BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_RESOLUTION,
				repositoryUrl);

		String[] statusValues = BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_STATUS, repositoryUrl);

		// add the status and resolutions that we care about
		sb.append("&bug_status=" + statusValues[0]); // UNCONFIRMED
		sb.append("&bug_status=" + statusValues[1]); // NEW
		sb.append("&bug_status=" + statusValues[2]); // ASSIGNED
		sb.append("&bug_status=" + statusValues[3]); // REOPENED
		sb.append("&bug_status=" + statusValues[4]); // RESOLVED
		sb.append("&bug_status=" + statusValues[5]); // VERIFIED
		sb.append("&bug_status=" + statusValues[6]); // CLOSED

		sb.append("&resolution=" + resolutionValues[0]); // FIXED
		sb.append("&resolution=" + resolutionValues[3]); // LATER
		sb.append("&resolution=" + "---"); // ---
		return sb;
	}

	/**
	 * Create the bugzilla query URL start.
	 * 
	 * @return The start of the query url as a StringBuffer <br>
	 *         Example:
	 *         https://bugs.eclipse.org/bugs/buglist.cgi?long_desc_type=allwordssubstr&long_desc=
	 */
	public static StringBuffer getQueryURLStart(String repositoryUrl) {
		StringBuffer sb = new StringBuffer(repositoryUrl);

		if (sb.charAt(sb.length() - 1) != '/') {
			sb.append('/');
		}
		sb.append("buglist.cgi?");

		TaskRepository repository = MylarTaskListPlugin.getRepositoryManager().getRepository(
				BugzillaPlugin.REPOSITORY_KIND, repositoryUrl);
		if (repository != null && repository.hasCredentials()) {
			// if (BugzillaPreferencePage.getUserName() != null
			// && !BugzillaPreferencePage.getUserName().equals("")
			// && BugzillaPreferencePage.getPassword() != null
			// && !BugzillaPreferencePage.getPassword().equals("")) {
			try {
				sb.append("GoAheadAndLogIn=1&Bugzilla_login=" + URLEncoder.encode(repository.getUserName(), // BugzillaPreferencePage.getUserName(),
						Charset.defaultCharset().toString()) + "&Bugzilla_password="
						+ URLEncoder.encode(repository.getPassword(), // BugzillaPreferencePage.getPassword(),
								Charset.defaultCharset().toString()) + "&");
			} catch (UnsupportedEncodingException e) {
				// should never get here since we are using the default encoding
			}
		}
		// add the description search type
		sb.append("long_desc_type=");
		sb.append(patternOperationValues[0]); // search for all words
		sb.append("&long_desc=");

		return sb;
	}

	/**
	 * Search the given string for another string
	 * 
	 * @param elementName
	 *            The name of the element that we are looking for
	 * @param comment
	 *            The text to search for this element name
	 * @return <code>true</code> if the element is found in the text else
	 *         <code>false</code>
	 */
	public static boolean hasElementName(String elementName, String comment) {

		// setup a regex for the element name
		String regexElement = ".*" + elementName + ".*";

		// get all of the individual lines for the string
		String[] lines = comment.split("\n");

		// go through each of the lines of the string
		for (int i = 0; i < lines.length; i++) {

			if (lines[i].matches(regexElement)) {
				return true;
			}
		}
		return false;
	}
}

Back to the top