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