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        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 = (BoxItem.Info) BoxResource.parseInfo(this.getAPI(), jsonObject);
057                if (parsedItemInfo != null) {
058                    results.add(parsedItemInfo);
059                }
060            }
061            return results;
062        }
063    }
064
065    /**
066     * Searches all descendant folders using a given query and query parameters.
067     *
068     * @param offset is the starting position.
069     * @param limit  the maximum number of items to return. The default is 30 and the maximum is 200.
070     * @param bsp    containing query and advanced search capabilities.
071     * @return a PartialCollection containing the search results.
072     */
073    public PartialCollection<BoxSearchSharedLink> searchRangeIncludeSharedLinks(long offset, long limit,
074                                                                                final BoxSearchParameters bsp) {
075        QueryStringBuilder builder = bsp.getQueryParameters()
076            .appendParam("include_recent_shared_links", "true")
077            .appendParam("limit", limit)
078            .appendParam("offset", offset);
079        URL url = SEARCH_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString());
080        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET");
081        try (BoxJSONResponse response = request.send()) {
082            JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
083            String totalCountString = responseJSON.get("total_count").toString();
084            long fullSize = Double.valueOf(totalCountString).longValue();
085            PartialCollection<BoxSearchSharedLink> results = new PartialCollection<>(offset,
086                limit, fullSize);
087            JsonArray jsonArray = responseJSON.get("entries").asArray();
088            for (JsonValue value : jsonArray) {
089                JsonObject jsonObject = value.asObject();
090                BoxSearchSharedLink parsedItem = new BoxSearchSharedLink(jsonObject, this.getAPI());
091                results.add(parsedItem);
092            }
093            return results;
094        }
095    }
096
097    /**
098     * Gets the API connection used by this resource.
099     *
100     * @return the API connection used by this resource.
101     */
102    public BoxAPIConnection getAPI() {
103        return this.api;
104    }
105}