|
47 | 47 | import org.zendesk.client.v2.model.Ticket;
|
48 | 48 | import org.zendesk.client.v2.model.TicketForm;
|
49 | 49 | import org.zendesk.client.v2.model.TicketImport;
|
| 50 | +import org.zendesk.client.v2.model.TicketPage; |
50 | 51 | import org.zendesk.client.v2.model.TicketResult;
|
51 | 52 | import org.zendesk.client.v2.model.Topic;
|
52 | 53 | import org.zendesk.client.v2.model.Trigger;
|
|
90 | 91 | import java.util.Map;
|
91 | 92 | import java.util.NoSuchElementException;
|
92 | 93 | import java.util.Objects;
|
| 94 | +import java.util.Optional; |
93 | 95 | import java.util.concurrent.ExecutionException;
|
94 | 96 | import java.util.concurrent.TimeUnit;
|
95 | 97 | import java.util.regex.Pattern;
|
@@ -1719,37 +1721,75 @@ public <T extends SearchResultEntity> Iterable<T> getSearchResults(Class<T> type
|
1719 | 1721 |
|
1720 | 1722 |
|
1721 | 1723 | public <T extends SearchResultEntity> Iterable<T> getSearchResults(Class<T> type, String query, Map<String, Object> params) {
|
1722 |
| - String typeName = null; |
1723 |
| - for (Map.Entry<String, Class<? extends SearchResultEntity>> entry : searchResultTypes.entrySet()) { |
1724 |
| - if (type.equals(entry.getValue())) { |
1725 |
| - typeName = entry.getKey(); |
1726 |
| - break; |
1727 |
| - } |
1728 |
| - } |
| 1724 | + String typeName = getTypeName(type); |
| 1725 | + |
1729 | 1726 | if (typeName == null) {
|
1730 | 1727 | return Collections.emptyList();
|
1731 | 1728 | }
|
1732 | 1729 |
|
1733 |
| - StringBuilder uriTemplate = new StringBuilder("/search.json{?query"); //leave off ending curly brace |
1734 |
| - |
1735 |
| - //we have to add each param name to the template so that when we call set() with a map, the entries get put in the uri |
1736 |
| - for (String paramName : params.keySet()) { |
1737 |
| - uriTemplate.append(",") |
1738 |
| - .append(paramName); |
1739 |
| - } |
1740 |
| - |
1741 |
| - uriTemplate.append("}"); |
1742 |
| - |
1743 |
| - TemplateUri templateUri = tmpl(uriTemplate.toString()) |
1744 |
| - .set("query", query + "+type:" + typeName); |
1745 |
| - |
1746 |
| - if(params != null) { |
1747 |
| - templateUri.set(params); |
1748 |
| - } |
| 1730 | + TemplateUri templateUri = getSearchUri(params, query, typeName); |
1749 | 1731 |
|
1750 | 1732 | return new PagedIterable<>(templateUri, handleList(type, "results"));
|
1751 | 1733 | }
|
| 1734 | + |
| 1735 | + /** |
| 1736 | + * Search API implementation with pagination support. |
| 1737 | + * |
| 1738 | + * @param String query string used filter a type given by searchType |
| 1739 | + * @param Map<String, Object> additional parameters other than filter string like per_page, page etc |
| 1740 | + * @param String name of any field of the searchType |
| 1741 | + * @param SortOrder |
| 1742 | + * @param Class<?> type of search entity like Ticket, User etc |
| 1743 | + * @param Class<T> page return type to which the search result will be deserialized |
| 1744 | + */ |
| 1745 | + public <T> Optional<T> getSearchResults( |
| 1746 | + final Class<?> searchType, |
| 1747 | + final Class<T> pageType, |
| 1748 | + final String query, |
| 1749 | + final Map<String, Object> queryParams, |
| 1750 | + final String sortBy, |
| 1751 | + final SortOrder sortOrder |
| 1752 | + ) { |
| 1753 | + |
| 1754 | + String typeName = getTypeName(searchType); |
| 1755 | + |
| 1756 | + if (typeName == null) { |
| 1757 | + return Optional.empty(); |
| 1758 | + } |
| 1759 | + |
| 1760 | + final Map<String, Object> paramsMap = new HashMap<>(); |
| 1761 | + |
| 1762 | + if(queryParams != null) { |
| 1763 | + paramsMap.putAll(queryParams); |
| 1764 | + } |
| 1765 | + |
| 1766 | + if(sortBy!=null && sortOrder!=null) { |
| 1767 | + paramsMap.put("sort_by", sortBy); |
| 1768 | + paramsMap.put("sort_order", sortOrder.getQueryParameter()); |
| 1769 | + } |
1752 | 1770 |
|
| 1771 | + final TemplateUri templateUri = getSearchUri(paramsMap, query, typeName); |
| 1772 | + |
| 1773 | + return Optional.of(complete(submit(req("GET", templateUri.toString()), handle(pageType)))); |
| 1774 | + } |
| 1775 | + |
| 1776 | + /** |
| 1777 | + * Ticket Search API implementation with pagination support. |
| 1778 | + * |
| 1779 | + * @param String query string used filter a type given by searchType |
| 1780 | + * @param Map<String, Object> additional parameters other than filter string like per_page, page etc |
| 1781 | + * @param String name of any field of the searchType |
| 1782 | + * @param SortOrder |
| 1783 | + */ |
| 1784 | + public Optional<TicketPage> getSearchTicketResults( |
| 1785 | + final String query, |
| 1786 | + final Map<String, Object> queryParams, |
| 1787 | + final String sortBy, |
| 1788 | + final SortOrder sortOrder) { |
| 1789 | + |
| 1790 | + return getSearchResults(Ticket.class, TicketPage.class, query, queryParams, sortBy, sortOrder); |
| 1791 | + } |
| 1792 | + |
1753 | 1793 | public void notifyApp(String json) {
|
1754 | 1794 | complete(submit(req("POST", cnst("/apps/notify.json"), JSON, json.getBytes()), handleStatus()));
|
1755 | 1795 | }
|
@@ -2902,7 +2942,40 @@ private static List<String> statusArray(Status... statuses) {
|
2902 | 2942 | }
|
2903 | 2943 | return result;
|
2904 | 2944 | }
|
| 2945 | + |
| 2946 | + private static String getTypeName(final Class<?> type) { |
| 2947 | + String typeName = null; |
| 2948 | + for (final Map.Entry<String, Class<? extends SearchResultEntity>> entry : searchResultTypes.entrySet()) { |
| 2949 | + if (type.equals(entry.getValue())) { |
| 2950 | + typeName = entry.getKey(); |
| 2951 | + break; |
| 2952 | + } |
| 2953 | + } |
| 2954 | + return typeName; |
| 2955 | + } |
| 2956 | + |
| 2957 | + private TemplateUri getSearchUri(Map<String, Object> params, String query , String typeName) { |
| 2958 | + |
| 2959 | + StringBuilder uriTemplate = new StringBuilder("/search.json{?query"); //leave off ending curly brace |
2905 | 2960 |
|
| 2961 | + //we have to add each param name to the template so that when we call set() with a map, the entries get put in the uri |
| 2962 | + for (String paramName : params.keySet()) { |
| 2963 | + uriTemplate.append(",") |
| 2964 | + .append(paramName); |
| 2965 | + } |
| 2966 | + |
| 2967 | + uriTemplate.append("}"); |
| 2968 | + |
| 2969 | + TemplateUri templateUri = tmpl(uriTemplate.toString()) |
| 2970 | + .set("query", query + "+type:" + typeName); |
| 2971 | + |
| 2972 | + if(params != null) { |
| 2973 | + templateUri.set(params); |
| 2974 | + } |
| 2975 | + |
| 2976 | + return templateUri; |
| 2977 | + } |
| 2978 | + |
2906 | 2979 | public static ObjectMapper createMapper() {
|
2907 | 2980 | ObjectMapper mapper = new ObjectMapper();
|
2908 | 2981 | mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
|
|
0 commit comments