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