001package com.box.sdk; 002 003import com.eclipsesource.json.Json; 004import com.eclipsesource.json.JsonArray; 005import com.eclipsesource.json.JsonObject; 006import com.eclipsesource.json.JsonValue; 007import java.net.URL; 008 009/** 010 * Represents search on Box. This class can be used to search through your box instance. In addition 011 * this lets you take advantage of all the advanced search features. 012 * 013 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link 014 * BoxAPIException} (unchecked meaning that the compiler won't force you to handle it) if an error 015 * occurs. If you wish to implement custom error handling for errors related to the Box REST API, 016 * you should capture this exception explicitly. 017 */ 018public class BoxSearch { 019 020 /** Search URL Template. */ 021 public static final URLTemplate SEARCH_URL_TEMPLATE = new URLTemplate("search"); 022 023 private final BoxAPIConnection api; 024 025 /** 026 * Constructs a Search to be used by everything. 027 * 028 * @param api the API connection to be used by the search. 029 */ 030 public BoxSearch(BoxAPIConnection api) { 031 this.api = api; 032 } 033 034 /** 035 * Searches all descendant folders using a given query and query parameters. 036 * 037 * @param offset is the starting position. 038 * @param limit the maximum number of items to return. The default is 30 and the maximum is 200. 039 * @param bsp containing query and advanced search capabilities. 040 * @return a PartialCollection containing the search results. 041 */ 042 public PartialCollection<BoxItem.Info> searchRange( 043 long offset, long limit, final BoxSearchParameters bsp) { 044 QueryStringBuilder builder = 045 bsp.getQueryParameters().appendParam("limit", limit).appendParam("offset", offset); 046 URL url = SEARCH_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString()); 047 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET"); 048 try (BoxJSONResponse response = request.send()) { 049 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 050 String totalCountString = responseJSON.get("total_count").toString(); 051 long fullSize = Double.valueOf(totalCountString).longValue(); 052 PartialCollection<BoxItem.Info> results = new PartialCollection<>(offset, limit, fullSize); 053 JsonArray jsonArray = responseJSON.get("entries").asArray(); 054 for (JsonValue value : jsonArray) { 055 JsonObject jsonObject = value.asObject(); 056 BoxItem.Info parsedItemInfo = 057 (BoxItem.Info) BoxResource.parseInfo(this.getAPI(), jsonObject); 058 if (parsedItemInfo != null) { 059 results.add(parsedItemInfo); 060 } 061 } 062 return results; 063 } 064 } 065 066 /** 067 * Searches all descendant folders using a given query and query parameters. 068 * 069 * @param offset is the starting position. 070 * @param limit the maximum number of items to return. The default is 30 and the maximum is 200. 071 * @param bsp containing query and advanced search capabilities. 072 * @return a PartialCollection containing the search results. 073 */ 074 public PartialCollection<BoxSearchSharedLink> searchRangeIncludeSharedLinks( 075 long offset, long limit, final BoxSearchParameters bsp) { 076 QueryStringBuilder builder = 077 bsp.getQueryParameters() 078 .appendParam("include_recent_shared_links", "true") 079 .appendParam("limit", limit) 080 .appendParam("offset", offset); 081 URL url = SEARCH_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString()); 082 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET"); 083 try (BoxJSONResponse response = request.send()) { 084 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 085 String totalCountString = responseJSON.get("total_count").toString(); 086 long fullSize = Double.valueOf(totalCountString).longValue(); 087 PartialCollection<BoxSearchSharedLink> results = 088 new PartialCollection<>(offset, limit, fullSize); 089 JsonArray jsonArray = responseJSON.get("entries").asArray(); 090 for (JsonValue value : jsonArray) { 091 JsonObject jsonObject = value.asObject(); 092 BoxSearchSharedLink parsedItem = new BoxSearchSharedLink(jsonObject, this.getAPI()); 093 results.add(parsedItem); 094 } 095 return results; 096 } 097 } 098 099 /** 100 * Gets the API connection used by this resource. 101 * 102 * @return the API connection used by this resource. 103 */ 104 public BoxAPIConnection getAPI() { 105 return this.api; 106 } 107}