1
0
Fork 0

cleaned up search implementation

master
Bob Carroll 2016-05-30 15:03:09 -07:00
parent c8802583aa
commit e3f68bf05d
3 changed files with 54 additions and 104 deletions

View File

@ -194,6 +194,12 @@ public class Example {
for (Issue i : sr.issues) for (Issue i : sr.issues)
System.out.println("Result: " + i); System.out.println("Result: " + i);
/* Search with paging (optionally 10 issues at a time). There are optional
arguments for including/expanding fields, and page size/start. */
Issue.SearchResult sr = jira.searchIssues("project IN (GOTHAM) ORDER BY id");
while (sr.iterator().hasNext())
System.out.println("Result: " + sr.iterator().next());
} catch (JiraException ex) { } catch (JiraException ex) {
System.err.println(ex.getMessage()); System.err.println(ex.getMessage());

View File

@ -544,9 +544,9 @@ public class Issue extends Resource {
private List<Issue> issues; private List<Issue> issues;
private int total; private int total;
public IssueIterator(RestClient restclient, String jql, public IssueIterator(RestClient restclient, String jql, String includedFields,
String includedFields, String expandFields, Integer maxResults, Integer startAt) String expandFields, Integer maxResults, Integer startAt)
throws JiraException { throws JiraException {
this.restclient = restclient; this.restclient = restclient;
this.jql = jql; this.jql = jql;
this.includedFields = includedFields; this.includedFields = includedFields;
@ -578,15 +578,16 @@ public class Issue extends Resource {
return result; return result;
} }
@Override @Override
public void remove() { public void remove() {
throw new UnsupportedOperationException("Method remove() not support for class " + this.getClass().getName()); throw new UnsupportedOperationException("Method remove() not support for class " +
this.getClass().getName());
} }
/** /**
* Gets the next issue, returning null if none more available * Gets the next issue, returning null if none more available
* Will ask the next set of issues from the server if the end of the current list of issues is reached. * Will ask the next set of issues from the server if the end
* of the current list of issues is reached.
* *
* @return the next issue, null if none more available * @return the next issue, null if none more available
* @throws JiraException * @throws JiraException
@ -624,9 +625,9 @@ public class Issue extends Resource {
* @throws JiraException * @throws JiraException
*/ */
private List<Issue> getNextIssues() throws JiraException { private List<Issue> getNextIssues() throws JiraException {
if (issues == null) { if (issues == null && startAt == null) {
startAt = Integer.valueOf(0); startAt = Integer.valueOf(0);
} else { } else if (issues != null) {
startAt = startAt + issues.size(); startAt = startAt + issues.size();
} }
@ -653,7 +654,6 @@ public class Issue extends Resource {
this.issues = Field.getResourceArray(Issue.class, map.get("issues"), restclient); this.issues = Field.getResourceArray(Issue.class, map.get("issues"), restclient);
return issues; return issues;
} }
} }
/** /**
@ -675,24 +675,20 @@ public class Issue extends Resource {
public int max = 0; public int max = 0;
public int total = 0; public int total = 0;
public List<Issue> issues = null; public List<Issue> issues = null;
private RestClient restclient;
private String jql;
private String includedFields;
private String expandFields;
private Integer startAt;
private IssueIterator issueIterator; private IssueIterator issueIterator;
public SearchResult(RestClient restclient, String jql, public SearchResult(RestClient restclient, String jql, String includedFields,
String includedFields, String expandFields, Integer maxResults, Integer startAt) throws JiraException { String expandFields, Integer maxResults, Integer startAt)
this.restclient = restclient; throws JiraException {
this.jql = jql; this.issueIterator = new IssueIterator(
this.includedFields = includedFields; restclient,
this.expandFields = expandFields; jql,
initSearchResult(maxResults, start); includedFields,
} expandFields,
maxResults,
private void initSearchResult(Integer maxResults, Integer start) throws JiraException { startAt
this.issueIterator = new IssueIterator(restclient, jql, includedFields, expandFields, maxResults, startAt); );
/* backwards compatibility shim - first page only */
this.issueIterator.hasNext(); this.issueIterator.hasNext();
this.max = issueIterator.maxResults; this.max = issueIterator.maxResults;
this.start = issueIterator.startAt; this.start = issueIterator.startAt;
@ -1277,50 +1273,6 @@ public class Issue extends Resource {
return new Issue(restclient, realGet(restclient, key, queryParams)); return new Issue(restclient, realGet(restclient, key, queryParams));
} }
/**
* Search for issues with the given query.
*
* @param restclient REST client instance
*
* @param jql JQL statement
*
* @return a search result structure with results (issues include all
* navigable fields)
*
* @throws JiraException when the search fails
*/
public static SearchResult search(RestClient restclient, String jql)
throws JiraException {
return search(restclient, jql, null, null);
}
/**
* Search for issues with the given query and specify which fields to
* retrieve.
*
* @param restclient REST client instance
*
* @param jql JQL statement
*
* @param includedFields Specifies which issue fields will be included in
* the result.
* <br>Some examples how this parameter works:
* <ul>
* <li>*all - include all fields</li>
* <li>*navigable - include just navigable fields</li>
* <li>summary,comment - include just the summary and comments</li>
* <li>*all,-comment - include all fields</li>
* </ul>
*
* @return a search result structure with results
*
* @throws JiraException when the search fails
*/
public static SearchResult search(RestClient restclient, String jql, String includedFields, Integer maxResults)
throws JiraException {
return search(restclient, jql, includedFields, null, maxResults, null);
}
/** /**
* Search for issues with the given query and specify which fields to * Search for issues with the given query and specify which fields to
* retrieve. If the total results is bigger than the maximum returned * retrieve. If the total results is bigger than the maximum returned
@ -1354,31 +1306,17 @@ public class Issue extends Resource {
* @throws JiraException when the search fails * @throws JiraException when the search fails
*/ */
public static SearchResult search(RestClient restclient, String jql, public static SearchResult search(RestClient restclient, String jql,
String includedFields, String expandFields, Integer maxResults, Integer startAt)
throws JiraException {
SearchResult sr = new SearchResult(restclient, jql, includedFields, expandFields, maxResults, startAt);
return sr;
}
private static JSON executeSearch(RestClient restclient, String jql,
String includedFields, String expandFields, Integer maxResults, String includedFields, String expandFields, Integer maxResults,
Integer startAt) throws JiraException { Integer startAt) throws JiraException {
JSON result = null;
try { return new SearchResult(
URI searchUri = createSearchURI(restclient, jql, includedFields, restclient,
expandFields, maxResults, startAt); jql,
result = restclient.get(searchUri); includedFields,
} catch (Exception ex) { expandFields,
throw new JiraException("Failed to search issues", ex); maxResults,
} startAt
);
if (!(result instanceof JSONObject)) {
throw new JiraException("JSON payload is malformed");
}
return result;
} }
/** /**

View File

@ -193,7 +193,7 @@ public class JiraClient {
public Issue.SearchResult searchIssues(String jql) public Issue.SearchResult searchIssues(String jql)
throws JiraException { throws JiraException {
return Issue.search(restclient, jql, null, null); return searchIssues(jql, null, null, null, null);
} }
/** /**
@ -210,7 +210,7 @@ public class JiraClient {
public Issue.SearchResult searchIssues(String jql, Integer maxResults) public Issue.SearchResult searchIssues(String jql, Integer maxResults)
throws JiraException { throws JiraException {
return Issue.search(restclient, jql, null, maxResults); return searchIssues(jql, null, null, maxResults, null);
} }
/** /**
@ -237,7 +237,7 @@ public class JiraClient {
public Issue.SearchResult searchIssues(String jql, String includedFields) public Issue.SearchResult searchIssues(String jql, String includedFields)
throws JiraException { throws JiraException {
return Issue.search(restclient, jql, includedFields, null); return searchIssues(jql, includedFields, null, null, null);
} }
/** /**
@ -262,10 +262,10 @@ public class JiraClient {
* *
* @throws JiraException when the search fails * @throws JiraException when the search fails
*/ */
public Issue.SearchResult searchIssues(String jql, String includedFields, String expandFields) public Issue.SearchResult searchIssues(String jql, String includedFields,
throws JiraException { String expandFields) throws JiraException {
return Issue.search(restclient, jql, includedFields, expandFields, null, null); return searchIssues(jql, includedFields, expandFields, null, null);
} }
/** /**
@ -293,7 +293,7 @@ public class JiraClient {
public Issue.SearchResult searchIssues(String jql, String includedFields, Integer maxResults) public Issue.SearchResult searchIssues(String jql, String includedFields, Integer maxResults)
throws JiraException { throws JiraException {
return Issue.search(restclient, jql, includedFields, maxResults); return searchIssues(jql, includedFields, null, maxResults, null);
} }
/** /**
@ -327,8 +327,7 @@ public class JiraClient {
public Issue.SearchResult searchIssues(String jql, String includedFields, public Issue.SearchResult searchIssues(String jql, String includedFields,
Integer maxResults, Integer startAt) throws JiraException { Integer maxResults, Integer startAt) throws JiraException {
return Issue.search(restclient, jql, includedFields, null, maxResults, return searchIssues(jql, includedFields, null, maxResults, startAt);
startAt);
} }
/** /**
@ -361,11 +360,18 @@ public class JiraClient {
* *
* @throws JiraException when the search fails * @throws JiraException when the search fails
*/ */
public Issue.SearchResult searchIssues(String jql, String includedFields, String expandFields, public Issue.SearchResult searchIssues(String jql, String includedFields,
Integer maxResults, Integer startAt) throws JiraException { String expandFields, Integer maxResults,
Integer startAt) throws JiraException {
return Issue.search(restclient, jql, includedFields, expandFields, maxResults, return Issue.search(
startAt); restclient,
jql,
includedFields,
expandFields,
maxResults,
startAt
);
} }
/** /**