001package com.box.sdkgen.managers.search;
002
003import static com.box.sdkgen.internal.utils.UtilsManager.convertToString;
004import static com.box.sdkgen.internal.utils.UtilsManager.entryOf;
005import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
006import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps;
007import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams;
008
009import com.box.sdkgen.networking.auth.Authentication;
010import com.box.sdkgen.networking.fetchoptions.FetchOptions;
011import com.box.sdkgen.networking.fetchoptions.ResponseFormat;
012import com.box.sdkgen.networking.fetchresponse.FetchResponse;
013import com.box.sdkgen.networking.network.NetworkSession;
014import com.box.sdkgen.schemas.metadataquery.MetadataQuery;
015import com.box.sdkgen.schemas.metadataqueryresults.MetadataQueryResults;
016import com.box.sdkgen.schemas.searchresultsresponse.SearchResultsResponse;
017import com.box.sdkgen.serialization.json.JsonManager;
018import java.util.Map;
019
020public class SearchManager {
021
022  public Authentication auth;
023
024  public NetworkSession networkSession;
025
026  public SearchManager() {
027    this.networkSession = new NetworkSession();
028  }
029
030  protected SearchManager(Builder builder) {
031    this.auth = builder.auth;
032    this.networkSession = builder.networkSession;
033  }
034
035  /**
036   * Create a search using SQL-like syntax to return items that match specific metadata.
037   *
038   * <p>By default, this endpoint returns only the most basic info about the items for which the
039   * query matches. To get additional fields for each item, including any of the metadata, use the
040   * `fields` attribute in the query.
041   *
042   * @param requestBody Request body of searchByMetadataQuery method
043   */
044  public MetadataQueryResults searchByMetadataQuery(MetadataQuery requestBody) {
045    return searchByMetadataQuery(requestBody, new SearchByMetadataQueryHeaders());
046  }
047
048  /**
049   * Create a search using SQL-like syntax to return items that match specific metadata.
050   *
051   * <p>By default, this endpoint returns only the most basic info about the items for which the
052   * query matches. To get additional fields for each item, including any of the metadata, use the
053   * `fields` attribute in the query.
054   *
055   * @param requestBody Request body of searchByMetadataQuery method
056   * @param headers Headers of searchByMetadataQuery method
057   */
058  public MetadataQueryResults searchByMetadataQuery(
059      MetadataQuery requestBody, SearchByMetadataQueryHeaders headers) {
060    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
061    FetchResponse response =
062        this.networkSession
063            .getNetworkClient()
064            .fetch(
065                new FetchOptions.Builder(
066                        String.join(
067                            "",
068                            this.networkSession.getBaseUrls().getBaseUrl(),
069                            "/2.0/metadata_queries/execute_read"),
070                        "POST")
071                    .headers(headersMap)
072                    .data(JsonManager.serialize(requestBody))
073                    .contentType("application/json")
074                    .responseFormat(ResponseFormat.JSON)
075                    .auth(this.auth)
076                    .networkSession(this.networkSession)
077                    .build());
078    return JsonManager.deserialize(response.getData(), MetadataQueryResults.class);
079  }
080
081  /**
082   * Searches for files, folders, web links, and shared files across the users content or across the
083   * entire enterprise.
084   */
085  public SearchResultsResponse searchForContent() {
086    return searchForContent(new SearchForContentQueryParams(), new SearchForContentHeaders());
087  }
088
089  /**
090   * Searches for files, folders, web links, and shared files across the users content or across the
091   * entire enterprise.
092   *
093   * @param queryParams Query parameters of searchForContent method
094   */
095  public SearchResultsResponse searchForContent(SearchForContentQueryParams queryParams) {
096    return searchForContent(queryParams, new SearchForContentHeaders());
097  }
098
099  /**
100   * Searches for files, folders, web links, and shared files across the users content or across the
101   * entire enterprise.
102   *
103   * @param headers Headers of searchForContent method
104   */
105  public SearchResultsResponse searchForContent(SearchForContentHeaders headers) {
106    return searchForContent(new SearchForContentQueryParams(), headers);
107  }
108
109  /**
110   * Searches for files, folders, web links, and shared files across the users content or across the
111   * entire enterprise.
112   *
113   * @param queryParams Query parameters of searchForContent method
114   * @param headers Headers of searchForContent method
115   */
116  public SearchResultsResponse searchForContent(
117      SearchForContentQueryParams queryParams, SearchForContentHeaders headers) {
118    Map<String, String> queryParamsMap =
119        prepareParams(
120            mapOf(
121                entryOf("query", convertToString(queryParams.getQuery())),
122                entryOf("scope", convertToString(queryParams.getScope())),
123                entryOf("file_extensions", convertToString(queryParams.getFileExtensions())),
124                entryOf("created_at_range", convertToString(queryParams.getCreatedAtRange())),
125                entryOf("updated_at_range", convertToString(queryParams.getUpdatedAtRange())),
126                entryOf("size_range", convertToString(queryParams.getSizeRange())),
127                entryOf("owner_user_ids", convertToString(queryParams.getOwnerUserIds())),
128                entryOf(
129                    "recent_updater_user_ids",
130                    convertToString(queryParams.getRecentUpdaterUserIds())),
131                entryOf("ancestor_folder_ids", convertToString(queryParams.getAncestorFolderIds())),
132                entryOf("content_types", convertToString(queryParams.getContentTypes())),
133                entryOf("type", convertToString(queryParams.getType())),
134                entryOf("trash_content", convertToString(queryParams.getTrashContent())),
135                entryOf("mdfilters", convertToString(queryParams.getMdfilters())),
136                entryOf("sort", convertToString(queryParams.getSort())),
137                entryOf("direction", convertToString(queryParams.getDirection())),
138                entryOf("limit", convertToString(queryParams.getLimit())),
139                entryOf(
140                    "include_recent_shared_links",
141                    convertToString(queryParams.getIncludeRecentSharedLinks())),
142                entryOf("fields", convertToString(queryParams.getFields())),
143                entryOf("offset", convertToString(queryParams.getOffset())),
144                entryOf("deleted_user_ids", convertToString(queryParams.getDeletedUserIds())),
145                entryOf("deleted_at_range", convertToString(queryParams.getDeletedAtRange()))));
146    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
147    FetchResponse response =
148        this.networkSession
149            .getNetworkClient()
150            .fetch(
151                new FetchOptions.Builder(
152                        String.join(
153                            "", this.networkSession.getBaseUrls().getBaseUrl(), "/2.0/search"),
154                        "GET")
155                    .params(queryParamsMap)
156                    .headers(headersMap)
157                    .responseFormat(ResponseFormat.JSON)
158                    .auth(this.auth)
159                    .networkSession(this.networkSession)
160                    .build());
161    return JsonManager.deserialize(response.getData(), SearchResultsResponse.class);
162  }
163
164  public Authentication getAuth() {
165    return auth;
166  }
167
168  public NetworkSession getNetworkSession() {
169    return networkSession;
170  }
171
172  public static class Builder {
173
174    protected Authentication auth;
175
176    protected NetworkSession networkSession;
177
178    public Builder() {}
179
180    public Builder auth(Authentication auth) {
181      this.auth = auth;
182      return this;
183    }
184
185    public Builder networkSession(NetworkSession networkSession) {
186      this.networkSession = networkSession;
187      return this;
188    }
189
190    public SearchManager build() {
191      if (this.networkSession == null) {
192        this.networkSession = new NetworkSession();
193      }
194      return new SearchManager(this);
195    }
196  }
197}