From e3f68bf05dab435b2fc84db58de0a431ba1afea4 Mon Sep 17 00:00:00 2001 From: Bob Carroll Date: Mon, 30 May 2016 15:03:09 -0700 Subject: [PATCH] cleaned up search implementation --- README.md | 6 + src/main/java/net/rcarz/jiraclient/Issue.java | 120 +++++------------- .../java/net/rcarz/jiraclient/JiraClient.java | 32 +++-- 3 files changed, 54 insertions(+), 104 deletions(-) diff --git a/README.md b/README.md index 483eeea..d4e5b4c 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,12 @@ public class Example { for (Issue i : sr.issues) 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) { System.err.println(ex.getMessage()); diff --git a/src/main/java/net/rcarz/jiraclient/Issue.java b/src/main/java/net/rcarz/jiraclient/Issue.java index 38dcd81..8d195d4 100644 --- a/src/main/java/net/rcarz/jiraclient/Issue.java +++ b/src/main/java/net/rcarz/jiraclient/Issue.java @@ -544,9 +544,9 @@ public class Issue extends Resource { private List issues; private int total; - public IssueIterator(RestClient restclient, String jql, - String includedFields, String expandFields, Integer maxResults, Integer startAt) - throws JiraException { + public IssueIterator(RestClient restclient, String jql, String includedFields, + String expandFields, Integer maxResults, Integer startAt) + throws JiraException { this.restclient = restclient; this.jql = jql; this.includedFields = includedFields; @@ -578,15 +578,16 @@ public class Issue extends Resource { return result; } - @Override 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 - * 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 * @throws JiraException @@ -624,9 +625,9 @@ public class Issue extends Resource { * @throws JiraException */ private List getNextIssues() throws JiraException { - if (issues == null) { + if (issues == null && startAt == null) { startAt = Integer.valueOf(0); - } else { + } else if (issues != null) { startAt = startAt + issues.size(); } @@ -653,7 +654,6 @@ public class Issue extends Resource { this.issues = Field.getResourceArray(Issue.class, map.get("issues"), restclient); return issues; } - } /** @@ -675,24 +675,20 @@ public class Issue extends Resource { public int max = 0; public int total = 0; public List issues = null; - private RestClient restclient; - private String jql; - private String includedFields; - private String expandFields; - private Integer startAt; private IssueIterator issueIterator; - public SearchResult(RestClient restclient, String jql, - String includedFields, String expandFields, Integer maxResults, Integer startAt) throws JiraException { - this.restclient = restclient; - this.jql = jql; - this.includedFields = includedFields; - this.expandFields = expandFields; - initSearchResult(maxResults, start); - } - - private void initSearchResult(Integer maxResults, Integer start) throws JiraException { - this.issueIterator = new IssueIterator(restclient, jql, includedFields, expandFields, maxResults, startAt); + public SearchResult(RestClient restclient, String jql, String includedFields, + String expandFields, Integer maxResults, Integer startAt) + throws JiraException { + this.issueIterator = new IssueIterator( + restclient, + jql, + includedFields, + expandFields, + maxResults, + startAt + ); + /* backwards compatibility shim - first page only */ this.issueIterator.hasNext(); this.max = issueIterator.maxResults; this.start = issueIterator.startAt; @@ -1277,50 +1273,6 @@ public class Issue extends Resource { 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. - *
Some examples how this parameter works: - *
    - *
  • *all - include all fields
  • - *
  • *navigable - include just navigable fields
  • - *
  • summary,comment - include just the summary and comments
  • - *
  • *all,-comment - include all fields
  • - *
- * - * @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 * 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 */ 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, Integer startAt) throws JiraException { - JSON result = null; - try { - URI searchUri = createSearchURI(restclient, jql, includedFields, - expandFields, maxResults, startAt); - result = restclient.get(searchUri); - } catch (Exception ex) { - throw new JiraException("Failed to search issues", ex); - } - - if (!(result instanceof JSONObject)) { - throw new JiraException("JSON payload is malformed"); - } - return result; + return new SearchResult( + restclient, + jql, + includedFields, + expandFields, + maxResults, + startAt + ); } /** diff --git a/src/main/java/net/rcarz/jiraclient/JiraClient.java b/src/main/java/net/rcarz/jiraclient/JiraClient.java index 9c81d49..276b8d7 100644 --- a/src/main/java/net/rcarz/jiraclient/JiraClient.java +++ b/src/main/java/net/rcarz/jiraclient/JiraClient.java @@ -193,7 +193,7 @@ public class JiraClient { public Issue.SearchResult searchIssues(String jql) 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) 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) 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 */ - public Issue.SearchResult searchIssues(String jql, String includedFields, String expandFields) - throws JiraException { + public Issue.SearchResult searchIssues(String jql, String includedFields, + 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) 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, Integer maxResults, Integer startAt) throws JiraException { - return Issue.search(restclient, jql, includedFields, null, maxResults, - startAt); + return searchIssues(jql, includedFields, null, maxResults, startAt); } /** @@ -361,11 +360,18 @@ public class JiraClient { * * @throws JiraException when the search fails */ - public Issue.SearchResult searchIssues(String jql, String includedFields, String expandFields, - Integer maxResults, Integer startAt) throws JiraException { + public Issue.SearchResult searchIssues(String jql, String includedFields, + String expandFields, Integer maxResults, + Integer startAt) throws JiraException { - return Issue.search(restclient, jql, includedFields, expandFields, maxResults, - startAt); + return Issue.search( + restclient, + jql, + includedFields, + expandFields, + maxResults, + startAt + ); } /**