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. 011 * In addition 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 BoxAPIException} (unchecked 014 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error 015 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p> 016 */ 017public class BoxSearch { 018 019 /** 020 * Search URL Template. 021 */ 022 public static final URLTemplate SEARCH_URL_TEMPLATE = new URLTemplate("search"); 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(long offset, long limit, final BoxSearchParameters bsp) { 043 QueryStringBuilder builder = bsp.getQueryParameters() 044 .appendParam("limit", limit) 045 .appendParam("offset", offset); 046 URL url = SEARCH_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString()); 047 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 048 BoxJSONResponse response = (BoxJSONResponse) 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 = (BoxItem.Info) BoxResource.parseInfo(this.getAPI(), jsonObject); 057 if (parsedItemInfo != null) { 058 results.add(parsedItemInfo); 059 } 060 } 061 return results; 062 } 063 064 /** 065 * Searches all descendant folders using a given query and query parameters. 066 * 067 * @param offset is the starting position. 068 * @param limit the maximum number of items to return. The default is 30 and the maximum is 200. 069 * @param bsp containing query and advanced search capabilities. 070 * @return a PartialCollection containing the search results. 071 */ 072 public PartialCollection<BoxSearchSharedLink> searchRangeIncludeSharedLinks(long offset, long limit, 073 final BoxSearchParameters bsp) { 074 QueryStringBuilder builder = bsp.getQueryParameters() 075 .appendParam("include_recent_shared_links", "true") 076 .appendParam("limit", limit) 077 .appendParam("offset", offset); 078 URL url = SEARCH_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString()); 079 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 080 BoxJSONResponse response = (BoxJSONResponse) request.send(); 081 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 082 String totalCountString = responseJSON.get("total_count").toString(); 083 long fullSize = Double.valueOf(totalCountString).longValue(); 084 PartialCollection<BoxSearchSharedLink> results = new PartialCollection<>(offset, 085 limit, fullSize); 086 JsonArray jsonArray = responseJSON.get("entries").asArray(); 087 for (JsonValue value : jsonArray) { 088 JsonObject jsonObject = value.asObject(); 089 BoxSearchSharedLink parsedItem = new BoxSearchSharedLink(jsonObject, this.getAPI()); 090 results.add(parsedItem); 091 } 092 return results; 093 } 094 095 /** 096 * Gets the API connection used by this resource. 097 * 098 * @return the API connection used by this resource. 099 */ 100 public BoxAPIConnection getAPI() { 101 return this.api; 102 } 103}