|
Lines 11-37
Link Here
|
| 11 |
|
11 |
|
| 12 |
package org.eclipse.mylar.internal.jira; |
12 |
package org.eclipse.mylar.internal.jira; |
| 13 |
|
13 |
|
|
|
14 |
import java.io.UnsupportedEncodingException; |
| 15 |
import java.net.URLDecoder; |
| 16 |
import java.net.URLEncoder; |
| 17 |
import java.text.SimpleDateFormat; |
| 18 |
import java.util.ArrayList; |
| 19 |
import java.util.Collections; |
| 20 |
import java.util.Date; |
| 21 |
import java.util.HashMap; |
| 22 |
import java.util.List; |
| 23 |
import java.util.Map; |
| 24 |
|
| 14 |
import org.eclipse.mylar.tasks.core.AbstractRepositoryQuery; |
25 |
import org.eclipse.mylar.tasks.core.AbstractRepositoryQuery; |
| 15 |
import org.eclipse.mylar.tasks.core.TaskList; |
26 |
import org.eclipse.mylar.tasks.core.TaskList; |
|
|
27 |
import org.eclipse.mylar.tasks.core.TaskRepository; |
| 28 |
import org.tigris.jira.core.model.Component; |
| 29 |
import org.tigris.jira.core.model.IssueType; |
| 30 |
import org.tigris.jira.core.model.Priority; |
| 31 |
import org.tigris.jira.core.model.Project; |
| 32 |
import org.tigris.jira.core.model.Resolution; |
| 33 |
import org.tigris.jira.core.model.Status; |
| 34 |
import org.tigris.jira.core.model.Version; |
| 35 |
import org.tigris.jira.core.model.filter.ComponentFilter; |
| 36 |
import org.tigris.jira.core.model.filter.ContentFilter; |
| 37 |
import org.tigris.jira.core.model.filter.CurrentUserFilter; |
| 38 |
import org.tigris.jira.core.model.filter.DateFilter; |
| 39 |
import org.tigris.jira.core.model.filter.DateRangeFilter; |
| 16 |
import org.tigris.jira.core.model.filter.FilterDefinition; |
40 |
import org.tigris.jira.core.model.filter.FilterDefinition; |
|
|
41 |
import org.tigris.jira.core.model.filter.IssueTypeFilter; |
| 42 |
import org.tigris.jira.core.model.filter.NobodyFilter; |
| 43 |
import org.tigris.jira.core.model.filter.PriorityFilter; |
| 44 |
import org.tigris.jira.core.model.filter.ProjectFilter; |
| 45 |
import org.tigris.jira.core.model.filter.ResolutionFilter; |
| 46 |
import org.tigris.jira.core.model.filter.SpecificUserFilter; |
| 47 |
import org.tigris.jira.core.model.filter.StatusFilter; |
| 48 |
import org.tigris.jira.core.model.filter.UserFilter; |
| 49 |
import org.tigris.jira.core.model.filter.UserInGroupFilter; |
| 50 |
import org.tigris.jira.core.model.filter.VersionFilter; |
| 51 |
import org.tigris.jira.core.service.JiraServer; |
| 17 |
|
52 |
|
| 18 |
/** |
53 |
/** |
| 19 |
* A JiraFilter represents a query for issues from a Jira repository. |
54 |
* A JiraCustomQuery represents a custom query for issues from a Jira repository. |
| 20 |
* |
55 |
* |
| 21 |
* @author Mik Kersten |
56 |
* @author Mik Kersten |
|
|
57 |
* @author Eugene Kuleshov |
| 22 |
*/ |
58 |
*/ |
| 23 |
public class JiraCustomQuery extends AbstractRepositoryQuery { |
59 |
public class JiraCustomQuery extends AbstractRepositoryQuery { |
| 24 |
|
60 |
|
|
|
61 |
private static final String PROJECT_KEY = "pid"; |
| 62 |
private static final String COMPONENT_KEY = "component"; |
| 63 |
private static final String TYPE_KEY = "type"; |
| 64 |
private static final String PRIORITY_KEY = "priority"; |
| 65 |
private static final String STATUS_KEY = "status"; |
| 66 |
private static final String RESOLUTION_KEY = "resolution"; |
| 67 |
|
| 68 |
private static final String FIXFOR_KEY = "fixfor"; |
| 69 |
private static final String VERSION_KEY = "version"; |
| 70 |
|
| 71 |
private static final String QUERY_KEY = "query"; |
| 72 |
private static final String ENVIRONMENT_KEY = "environment"; |
| 73 |
private static final String BODY_KEY = "body"; |
| 74 |
private static final String DESCRIPTION_KEY = "description"; |
| 75 |
private static final String SUMMARY_KEY = "summary"; |
| 76 |
|
| 77 |
private static final String ASSIGNEE_KEY = "assignee"; |
| 78 |
private static final String REPORTER_KEY = "reporter"; |
| 79 |
|
| 80 |
private static final String CREATED_KEY = "created"; |
| 81 |
private static final String UPDATED_KEY = "updated"; |
| 82 |
private static final String DUEDATE_KEY = "duedate"; |
| 83 |
|
| 84 |
private static final String ISSUE_SPECIFIC_GROUP = "specificgroup"; |
| 85 |
private static final String ISSUE_SPECIFIC_USER = "specificuser"; |
| 86 |
private static final String ISSUE_CURRENT_USER = "issue_current_user"; |
| 87 |
private static final String ISSUE_NO_REPORTER = "issue_no_reporter"; |
| 88 |
|
| 25 |
private static final int MAX_HITS = 200; |
89 |
private static final int MAX_HITS = 200; |
|
|
90 |
|
| 91 |
|
| 92 |
private final FilterDefinition filter; |
| 93 |
private String encoding; |
| 26 |
|
94 |
|
| 27 |
protected FilterDefinition filter = null; |
|
|
| 28 |
|
95 |
|
| 29 |
public JiraCustomQuery(String repositoryUrl, FilterDefinition filter, TaskList taskList) { |
96 |
public JiraCustomQuery(String repositoryUrl, FilterDefinition filter, TaskList taskList, TaskRepository taskRepository) { |
| 30 |
super(filter.getName(), taskList); |
97 |
super(filter.getName(), taskList); |
| 31 |
setMaxHits(MAX_HITS); |
|
|
| 32 |
this.filter = filter; |
98 |
this.filter = filter; |
| 33 |
super.repositoryUrl = repositoryUrl; |
99 |
this.repositoryUrl = repositoryUrl; |
| 34 |
setUrl(repositoryUrl + MylarJiraPlugin.FILTER_URL_PREFIX + filter.getName()); |
100 |
this.encoding = taskRepository.getCharacterEncoding(); |
|
|
101 |
this.url = repositoryUrl + MylarJiraPlugin.FILTER_URL_PREFIX + "&reset=true&mode=hide" + getQueryParams(filter); |
| 102 |
this.maxHits = MAX_HITS; |
| 103 |
} |
| 104 |
|
| 105 |
public JiraCustomQuery(String name, String queryUrl, String repositoryUrl, |
| 106 |
JiraServer jiraServer, TaskList taskList, TaskRepository taskRepository) { |
| 107 |
super(name, taskList); |
| 108 |
this.repositoryUrl = repositoryUrl; |
| 109 |
this.url = queryUrl; |
| 110 |
this.encoding = taskRepository.getCharacterEncoding(); |
| 111 |
this.filter = createFilter(jiraServer, queryUrl); |
| 112 |
this.filter.setName(name); |
| 113 |
this.maxHits = MAX_HITS; |
| 35 |
} |
114 |
} |
| 36 |
|
115 |
|
| 37 |
public String getRepositoryKind() { |
116 |
public String getRepositoryKind() { |
|
Lines 41-46
Link Here
|
| 41 |
public FilterDefinition getFilterDefinition() { |
120 |
public FilterDefinition getFilterDefinition() { |
| 42 |
return filter; |
121 |
return filter; |
| 43 |
} |
122 |
} |
|
|
123 |
|
| 124 |
|
| 125 |
private FilterDefinition createFilter(JiraServer jiraServer, String url) { |
| 126 |
FilterDefinition filter = new FilterDefinition(); |
| 127 |
|
| 128 |
HashMap<String, List<String>> params = new HashMap<String, List<String>>(); |
| 129 |
|
| 130 |
for (String pair : url.split("&\\?")) { |
| 131 |
String[] tokens = pair.split("="); |
| 132 |
if (tokens.length > 1) { |
| 133 |
try { |
| 134 |
String key = tokens[0]; |
| 135 |
String value = tokens.length>1 ? null : URLDecoder.decode(tokens[1], encoding); |
| 136 |
List<String> values = params.get(key); |
| 137 |
if(values==null) { |
| 138 |
values = new ArrayList<String>(); |
| 139 |
params.put(key, values); |
| 140 |
} |
| 141 |
values.add(value); |
| 142 |
} catch (UnsupportedEncodingException ex) { |
| 143 |
// ignore |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
List<String> projectIds = getIds(params, PROJECT_KEY); |
| 149 |
for (String projectId : projectIds) { |
| 150 |
Project project = jiraServer.getProjectById(projectId); |
| 151 |
filter.setProjectFilter(new ProjectFilter(project)); |
| 152 |
|
| 153 |
List<String> componentIds = getIds(params, COMPONENT_KEY); |
| 154 |
List<Component> components = new ArrayList<Component>(); |
| 155 |
for (String componentId : componentIds) { |
| 156 |
Component[] projectComponents = project.getComponents(); |
| 157 |
for(Component component : projectComponents) { |
| 158 |
if(component.getId().equals(componentId)) { |
| 159 |
components.add(component); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
if(!components.isEmpty()) { |
| 164 |
filter.setComponentFilter(new ComponentFilter(components.toArray(new Component[components.size()]))); |
| 165 |
} |
| 166 |
|
| 167 |
Version[] projectVersions = project.getVersions(); |
| 168 |
|
| 169 |
List<String> fixForIds = getIds(params, FIXFOR_KEY); |
| 170 |
List<Version> fixForversions = new ArrayList<Version>(); |
| 171 |
for (String fixForId : fixForIds) { |
| 172 |
for (Version projectVersion : projectVersions) { |
| 173 |
if(projectVersion.getId().equals(fixForId)) { |
| 174 |
fixForversions.add(projectVersion); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
if(!fixForversions.isEmpty()) { |
| 179 |
filter.setFixForVersionFilter(new VersionFilter(fixForversions.toArray(new Version[fixForversions.size()]))); |
| 180 |
} |
| 181 |
|
| 182 |
List<String> versionIds = getIds(params, VERSION_KEY); |
| 183 |
List<Version> versions = new ArrayList<Version>(); |
| 184 |
for (String versionId : versionIds) { |
| 185 |
for (Version projectVersion : projectVersions) { |
| 186 |
if(projectVersion.getId().equals(versionId)) { |
| 187 |
versions.add(projectVersion); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
if(!versions.isEmpty()) { |
| 192 |
filter.setReportedInVersionFilter(new VersionFilter(versions.toArray(new Version[versions.size()]))); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
List<String> typeIds = getIds(params, TYPE_KEY); |
| 197 |
List<IssueType> issueTypes = new ArrayList<IssueType>(); |
| 198 |
for (String typeId : typeIds) { |
| 199 |
IssueType issueType = jiraServer.getIssueTypeById(typeId); |
| 200 |
if(!IssueType.MISSING_ISSUE_TYPE.equals(issueType)) { |
| 201 |
issueTypes.add(issueType); |
| 202 |
} |
| 203 |
} |
| 204 |
if(!issueTypes.isEmpty()) { |
| 205 |
filter.setIssueTypeFilter(new IssueTypeFilter(issueTypes.toArray(new IssueType[issueTypes.size()]))); |
| 206 |
} |
| 207 |
|
| 208 |
List<String> statusIds = getIds(params, STATUS_KEY); |
| 209 |
List<Status> statuses = new ArrayList<Status>(); |
| 210 |
for (String statusId : statusIds) { |
| 211 |
Status status = jiraServer.getStatusById(statusId); |
| 212 |
if(!Status.MISSING_STATUS.equals(status)) { |
| 213 |
statuses.add(status); |
| 214 |
} |
| 215 |
} |
| 216 |
if(!statuses.isEmpty()) { |
| 217 |
filter.setStatusFilter(new StatusFilter(statuses.toArray(new Status[statuses.size()]))); |
| 218 |
} |
| 219 |
|
| 220 |
List<String> resolutionIds = getIds(params, RESOLUTION_KEY); |
| 221 |
List<Resolution> resolutions = new ArrayList<Resolution>(); |
| 222 |
for (String resolutionId : resolutionIds) { |
| 223 |
Resolution resolution = jiraServer.getResolutionById(resolutionId); |
| 224 |
if(!Resolution.UNKNOWN_RESOLUTION.equals(resolution)) { |
| 225 |
resolutions.add(resolution); |
| 226 |
} |
| 227 |
} |
| 228 |
if(!resolutions.isEmpty()) { |
| 229 |
filter.setResolutionFilter(new ResolutionFilter(resolutions.toArray(new Resolution[resolutions.size()]))); |
| 230 |
} |
| 231 |
|
| 232 |
List<String> queries = getIds(params, QUERY_KEY); |
| 233 |
for (String query : queries) { |
| 234 |
boolean searchSummary = !getIds(params, SUMMARY_KEY).contains("true"); |
| 235 |
boolean searchDescription = !getIds(params, DESCRIPTION_KEY).contains("true"); |
| 236 |
boolean searchEnvironment = !getIds(params, ENVIRONMENT_KEY).contains("true"); |
| 237 |
boolean searchComments = !getIds(params, BODY_KEY).contains("true"); |
| 238 |
filter.setContentFilter(new ContentFilter(query, searchSummary, searchDescription, searchEnvironment, searchComments)); |
| 239 |
} |
| 240 |
|
| 241 |
filter.setReportedByFilter(createUserFilter(params, REPORTER_KEY)); |
| 242 |
filter.setAssignedToFilter(createUserFilter(params, ASSIGNEE_KEY)); |
| 243 |
|
| 244 |
filter.setCreatedDateFilter(createDateFilter(params, CREATED_KEY)); |
| 245 |
filter.setUpdatedDateFilter(createDateFilter(params, UPDATED_KEY)); |
| 246 |
filter.setDueDateFilter(createDateFilter(params, DUEDATE_KEY)); |
| 247 |
|
| 248 |
return filter; |
| 249 |
} |
| 250 |
|
| 251 |
private DateFilter createDateFilter(Map<String, List<String>> params, String key) { |
| 252 |
String after = getId(params, key + ":after"); |
| 253 |
String before = getId(params, key + ":before"); |
| 254 |
|
| 255 |
SimpleDateFormat df = new SimpleDateFormat("d/MMM/yy"); |
| 256 |
Date fromDate; |
| 257 |
try { |
| 258 |
fromDate = df.parse(after); |
| 259 |
} catch (Exception ex) { |
| 260 |
fromDate = null; |
| 261 |
} |
| 262 |
Date toDate; |
| 263 |
try { |
| 264 |
toDate = df.parse(before); |
| 265 |
} catch (Exception ex) { |
| 266 |
toDate = null; |
| 267 |
} |
| 268 |
|
| 269 |
return fromDate==null && toDate==null ? null : new DateRangeFilter(fromDate, toDate); |
| 270 |
} |
| 271 |
|
| 272 |
private UserFilter createUserFilter(Map<String, List<String>> params, String key) { |
| 273 |
String type = getId(params, key + "Select"); |
| 274 |
if(ISSUE_NO_REPORTER.equals(type)) { |
| 275 |
return new NobodyFilter(); |
| 276 |
} else if(ISSUE_CURRENT_USER.equals(type)) { |
| 277 |
return new CurrentUserFilter(); |
| 278 |
} else { |
| 279 |
String reporter = getId(params, key); |
| 280 |
if(reporter!=null) { |
| 281 |
if(ISSUE_SPECIFIC_USER.equals(type)) { |
| 282 |
return new SpecificUserFilter(reporter); |
| 283 |
} else if(ISSUE_SPECIFIC_GROUP.equals(type)) { |
| 284 |
return new UserInGroupFilter(reporter); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
return null; |
| 289 |
} |
| 290 |
|
| 291 |
private String getId(Map<String, List<String>> params, String key) { |
| 292 |
List<String> ids = getIds(params, key); |
| 293 |
return ids.isEmpty() ? null : ids.get(0); |
| 294 |
} |
| 295 |
|
| 296 |
private List<String> getIds(Map<String, List<String>> params, String key) { |
| 297 |
List<String> ids = params.get(key); |
| 298 |
if (ids==null) { |
| 299 |
return Collections.emptyList(); |
| 300 |
} |
| 301 |
return ids; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
private String getQueryParams(FilterDefinition filter) { |
| 306 |
StringBuffer sb = new StringBuffer(); |
| 307 |
|
| 308 |
ProjectFilter projectFilter = filter.getProjectFilter(); |
| 309 |
if(projectFilter!=null) { |
| 310 |
Project project = projectFilter.getProject(); |
| 311 |
// TODO all projects |
| 312 |
addParameter(sb, PROJECT_KEY, project.getId()); |
| 313 |
} |
| 314 |
|
| 315 |
ComponentFilter componentFilter = filter.getComponentFilter(); |
| 316 |
// TODO all components |
| 317 |
if(componentFilter==null || componentFilter.hasNoComponent()) { |
| 318 |
addParameter(sb, COMPONENT_KEY, "-1"); |
| 319 |
} else { |
| 320 |
for (Component component : componentFilter.getComponents()) { |
| 321 |
addParameter(sb, COMPONENT_KEY, component.getId()); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
// TODO |
| 326 |
VersionFilter fixForVersionFilter = filter.getFixForVersionFilter(); |
| 327 |
if (fixForVersionFilter != null) { |
| 328 |
for ( Version fixVersion : fixForVersionFilter.getVersions()) { |
| 329 |
addParameter(sb, FIXFOR_KEY, fixVersion.getId()); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
// TODO |
| 334 |
VersionFilter reportedInVersionFilter = filter.getReportedInVersionFilter(); |
| 335 |
if (reportedInVersionFilter != null) { |
| 336 |
for (Version reportedVersion : reportedInVersionFilter.getVersions()) { |
| 337 |
addParameter(sb, VERSION_KEY, reportedVersion.getId()); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
// TODO |
| 342 |
IssueTypeFilter issueTypeFilter = filter.getIssueTypeFilter(); |
| 343 |
if (issueTypeFilter != null) { |
| 344 |
for (IssueType issueType : issueTypeFilter.getIsueTypes()) { |
| 345 |
addParameter(sb, TYPE_KEY, issueType.getId()); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
// TODO |
| 350 |
StatusFilter statusFilter = filter.getStatusFilter(); |
| 351 |
if(statusFilter!=null) { |
| 352 |
for ( Status status : statusFilter.getStatuses()) { |
| 353 |
addParameter(sb, STATUS_KEY, status.getId()); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
ResolutionFilter resolutionFilter = filter.getResolutionFilter(); |
| 358 |
if(resolutionFilter!=null) { |
| 359 |
for (Resolution resolution : resolutionFilter.getResolutions()) { |
| 360 |
addParameter(sb, RESOLUTION_KEY, resolution.getId()); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
PriorityFilter priorityFilter = filter.getPriorityFilter(); |
| 365 |
if(priorityFilter!=null) { |
| 366 |
for ( Priority priority : priorityFilter.getPriorities()) { |
| 367 |
addParameter(sb, PRIORITY_KEY, priority.getId()); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
ContentFilter contentFilter = filter.getContentFilter(); |
| 372 |
if(contentFilter!=null) { |
| 373 |
String queryString = contentFilter.getQueryString(); |
| 374 |
if(queryString!=null) { |
| 375 |
addParameter(sb, QUERY_KEY, queryString); |
| 376 |
} |
| 377 |
if(contentFilter.isSearchingSummary()) { |
| 378 |
addParameter(sb, SUMMARY_KEY, "true"); |
| 379 |
} |
| 380 |
if(contentFilter.isSearchingDescription()) { |
| 381 |
addParameter(sb, DESCRIPTION_KEY, "true"); |
| 382 |
} |
| 383 |
if(contentFilter.isSearchingComments()) { |
| 384 |
addParameter(sb, BODY_KEY, "true"); |
| 385 |
} |
| 386 |
if(contentFilter.isSearchingEnvironment()) { |
| 387 |
addParameter(sb, ENVIRONMENT_KEY, "true"); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
addUserFilter(sb, filter.getReportedByFilter(), REPORTER_KEY); |
| 392 |
addUserFilter(sb, filter.getAssignedToFilter(), ASSIGNEE_KEY); |
| 393 |
|
| 394 |
addDateFilter(sb, filter.getCreatedDateFilter(), CREATED_KEY); |
| 395 |
addDateFilter(sb, filter.getUpdatedDateFilter(), UPDATED_KEY); |
| 396 |
addDateFilter(sb, filter.getDueDateFilter(), DUEDATE_KEY); |
| 397 |
|
| 398 |
return sb.toString(); |
| 399 |
} |
| 400 |
|
| 401 |
private void addDateFilter(StringBuffer sb, DateFilter filter, String type) { |
| 402 |
if(filter instanceof DateRangeFilter) { |
| 403 |
SimpleDateFormat df = new SimpleDateFormat("d/MMM/yy"); |
| 404 |
DateRangeFilter rangeFilter = (DateRangeFilter) filter; |
| 405 |
addParameter(sb, type + ":after", df.format(rangeFilter.getFromDate())); |
| 406 |
addParameter(sb, type + ":before", df.format(rangeFilter.getToDate())); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
private void addUserFilter(StringBuffer sb, UserFilter filter, String type) { |
| 411 |
if(filter instanceof NobodyFilter) { |
| 412 |
addParameter(sb, type + "Select", ISSUE_NO_REPORTER); |
| 413 |
} else if(filter instanceof CurrentUserFilter) { |
| 414 |
addParameter(sb, type + "Select", ISSUE_CURRENT_USER); |
| 415 |
} else if(filter instanceof SpecificUserFilter) { |
| 416 |
addParameter(sb, type + "Select", ISSUE_SPECIFIC_USER); |
| 417 |
addParameter(sb, type, ((SpecificUserFilter) filter).getUser()); |
| 418 |
} else if(filter instanceof UserInGroupFilter) { |
| 419 |
addParameter(sb, type + "Select", ISSUE_SPECIFIC_GROUP); |
| 420 |
addParameter(sb, type, ((UserInGroupFilter) filter).getGroup()); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
private void addParameter(StringBuffer sb, String name, String value) { |
| 425 |
try { |
| 426 |
sb.append('&').append(name).append('=').append(URLEncoder.encode(value, encoding)); |
| 427 |
} catch (UnsupportedEncodingException ex) { |
| 428 |
// ignore |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 44 |
} |
432 |
} |
| 45 |
|
433 |
|
| 46 |
//public void refreshHits() { |
434 |
//public void refreshHits() { |