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    private static final URLTemplate SEARCH_URL_TEMPLATE = new URLTemplate("search");
018    private final BoxAPIConnection api;
019
020    /**
021     * Constructs a Search to be used by everything.
022     * @param  api the API connection to be used by the search.
023     */
024    public BoxSearch(BoxAPIConnection api) {
025        this.api = api;
026    }
027
028    /**
029     * Searches all descendant folders using a given query and query parameters.
030     * @param  offset is the starting position.
031     * @param  limit the number of search results CANNONT Exceed 1000.
032     * @param  bsp containing query and advanced search capabilities.
033     * @return a PartialCollection containing the search results.
034     */
035    public PartialCollection<BoxItem.Info> searchRange(long offset, long limit, final BoxSearchParameters bsp) {
036        QueryStringBuilder builder = bsp.getQueryParameters()
037                .appendParam("limit", limit)
038                .appendParam("offset", offset);
039        URL url = SEARCH_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString());
040        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
041        BoxJSONResponse response = (BoxJSONResponse) request.send();
042        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
043        String totalCountString = responseJSON.get("total_count").toString();
044        long fullSize = Double.valueOf(totalCountString).longValue();
045        PartialCollection<BoxItem.Info> results = new PartialCollection<BoxItem.Info>(offset, limit, fullSize);
046        JsonArray jsonArray = responseJSON.get("entries").asArray();
047        for (JsonValue value : jsonArray) {
048            JsonObject jsonObject = value.asObject();
049            BoxItem.Info parsedItemInfo = (BoxItem.Info) BoxResource.parseInfo(this.getAPI(), jsonObject);
050            if (parsedItemInfo != null) {
051                results.add(parsedItemInfo);
052            }
053        }
054        return results;
055    }
056    /**
057     * Gets the API connection used by this resource.
058     * @return the API connection used by this resource.
059     */
060    public BoxAPIConnection getAPI() {
061        return this.api;
062    }
063}