001package com.box.sdkgen.managers.foldermetadata;
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.metadatafull.MetadataFull;
015import com.box.sdkgen.schemas.metadatas.Metadatas;
016import com.box.sdkgen.serialization.json.JsonManager;
017import java.util.List;
018import java.util.Map;
019
020public class FolderMetadataManager {
021
022  public Authentication auth;
023
024  public NetworkSession networkSession;
025
026  public FolderMetadataManager() {
027    this.networkSession = new NetworkSession();
028  }
029
030  protected FolderMetadataManager(Builder builder) {
031    this.auth = builder.auth;
032    this.networkSession = builder.networkSession;
033  }
034
035  /**
036   * Retrieves all metadata for a given folder. This can not be used on the root folder with ID `0`.
037   *
038   * @param folderId The unique identifier that represent a folder.
039   *     <p>The ID for any folder can be determined by visiting this folder in the web application
040   *     and copying the ID from the URL. For example, for the URL
041   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
042   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
043   */
044  public Metadatas getFolderMetadata(String folderId) {
045    return getFolderMetadata(
046        folderId, new GetFolderMetadataQueryParams(), new GetFolderMetadataHeaders());
047  }
048
049  /**
050   * Retrieves all metadata for a given folder. This can not be used on the root folder with ID `0`.
051   *
052   * @param folderId The unique identifier that represent a folder.
053   *     <p>The ID for any folder can be determined by visiting this folder in the web application
054   *     and copying the ID from the URL. For example, for the URL
055   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
056   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
057   * @param queryParams Query parameters of getFolderMetadata method
058   */
059  public Metadatas getFolderMetadata(String folderId, GetFolderMetadataQueryParams queryParams) {
060    return getFolderMetadata(folderId, queryParams, new GetFolderMetadataHeaders());
061  }
062
063  /**
064   * Retrieves all metadata for a given folder. This can not be used on the root folder with ID `0`.
065   *
066   * @param folderId The unique identifier that represent a folder.
067   *     <p>The ID for any folder can be determined by visiting this folder in the web application
068   *     and copying the ID from the URL. For example, for the URL
069   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
070   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
071   * @param headers Headers of getFolderMetadata method
072   */
073  public Metadatas getFolderMetadata(String folderId, GetFolderMetadataHeaders headers) {
074    return getFolderMetadata(folderId, new GetFolderMetadataQueryParams(), headers);
075  }
076
077  /**
078   * Retrieves all metadata for a given folder. This can not be used on the root folder with ID `0`.
079   *
080   * @param folderId The unique identifier that represent a folder.
081   *     <p>The ID for any folder can be determined by visiting this folder in the web application
082   *     and copying the ID from the URL. For example, for the URL
083   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
084   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
085   * @param queryParams Query parameters of getFolderMetadata method
086   * @param headers Headers of getFolderMetadata method
087   */
088  public Metadatas getFolderMetadata(
089      String folderId, GetFolderMetadataQueryParams queryParams, GetFolderMetadataHeaders headers) {
090    Map<String, String> queryParamsMap =
091        prepareParams(mapOf(entryOf("view", convertToString(queryParams.getView()))));
092    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
093    FetchResponse response =
094        this.networkSession
095            .getNetworkClient()
096            .fetch(
097                new FetchOptions.Builder(
098                        String.join(
099                            "",
100                            this.networkSession.getBaseUrls().getBaseUrl(),
101                            "/2.0/folders/",
102                            convertToString(folderId),
103                            "/metadata"),
104                        "GET")
105                    .params(queryParamsMap)
106                    .headers(headersMap)
107                    .responseFormat(ResponseFormat.JSON)
108                    .auth(this.auth)
109                    .networkSession(this.networkSession)
110                    .build());
111    return JsonManager.deserialize(response.getData(), Metadatas.class);
112  }
113
114  /**
115   * Retrieves the instance of a metadata template that has been applied to a folder. This can not
116   * be used on the root folder with ID `0`.
117   *
118   * @param folderId The unique identifier that represent a folder.
119   *     <p>The ID for any folder can be determined by visiting this folder in the web application
120   *     and copying the ID from the URL. For example, for the URL
121   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
122   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
123   * @param scope The scope of the metadata template. Example: "global"
124   * @param templateKey The name of the metadata template. Example: "properties"
125   */
126  public MetadataFull getFolderMetadataById(
127      String folderId, GetFolderMetadataByIdScope scope, String templateKey) {
128    return getFolderMetadataById(folderId, scope, templateKey, new GetFolderMetadataByIdHeaders());
129  }
130
131  /**
132   * Retrieves the instance of a metadata template that has been applied to a folder. This can not
133   * be used on the root folder with ID `0`.
134   *
135   * @param folderId The unique identifier that represent a folder.
136   *     <p>The ID for any folder can be determined by visiting this folder in the web application
137   *     and copying the ID from the URL. For example, for the URL
138   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
139   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
140   * @param scope The scope of the metadata template. Example: "global"
141   * @param templateKey The name of the metadata template. Example: "properties"
142   * @param headers Headers of getFolderMetadataById method
143   */
144  public MetadataFull getFolderMetadataById(
145      String folderId,
146      GetFolderMetadataByIdScope scope,
147      String templateKey,
148      GetFolderMetadataByIdHeaders headers) {
149    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
150    FetchResponse response =
151        this.networkSession
152            .getNetworkClient()
153            .fetch(
154                new FetchOptions.Builder(
155                        String.join(
156                            "",
157                            this.networkSession.getBaseUrls().getBaseUrl(),
158                            "/2.0/folders/",
159                            convertToString(folderId),
160                            "/metadata/",
161                            convertToString(scope),
162                            "/",
163                            convertToString(templateKey)),
164                        "GET")
165                    .headers(headersMap)
166                    .responseFormat(ResponseFormat.JSON)
167                    .auth(this.auth)
168                    .networkSession(this.networkSession)
169                    .build());
170    return JsonManager.deserialize(response.getData(), MetadataFull.class);
171  }
172
173  /**
174   * Applies an instance of a metadata template to a folder.
175   *
176   * <p>In most cases only values that are present in the metadata template will be accepted, except
177   * for the `global.properties` template which accepts any key-value pair.
178   *
179   * <p>To display the metadata template in the Box web app the enterprise needs to be configured to
180   * enable **Cascading Folder Level Metadata** for the user in the admin console.
181   *
182   * @param folderId The unique identifier that represent a folder.
183   *     <p>The ID for any folder can be determined by visiting this folder in the web application
184   *     and copying the ID from the URL. For example, for the URL
185   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
186   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
187   * @param scope The scope of the metadata template. Example: "global"
188   * @param templateKey The name of the metadata template. Example: "properties"
189   * @param requestBody Request body of createFolderMetadataById method
190   */
191  public MetadataFull createFolderMetadataById(
192      String folderId,
193      CreateFolderMetadataByIdScope scope,
194      String templateKey,
195      Map<String, Object> requestBody) {
196    return createFolderMetadataById(
197        folderId, scope, templateKey, requestBody, new CreateFolderMetadataByIdHeaders());
198  }
199
200  /**
201   * Applies an instance of a metadata template to a folder.
202   *
203   * <p>In most cases only values that are present in the metadata template will be accepted, except
204   * for the `global.properties` template which accepts any key-value pair.
205   *
206   * <p>To display the metadata template in the Box web app the enterprise needs to be configured to
207   * enable **Cascading Folder Level Metadata** for the user in the admin console.
208   *
209   * @param folderId The unique identifier that represent a folder.
210   *     <p>The ID for any folder can be determined by visiting this folder in the web application
211   *     and copying the ID from the URL. For example, for the URL
212   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
213   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
214   * @param scope The scope of the metadata template. Example: "global"
215   * @param templateKey The name of the metadata template. Example: "properties"
216   * @param requestBody Request body of createFolderMetadataById method
217   * @param headers Headers of createFolderMetadataById method
218   */
219  public MetadataFull createFolderMetadataById(
220      String folderId,
221      CreateFolderMetadataByIdScope scope,
222      String templateKey,
223      Map<String, Object> requestBody,
224      CreateFolderMetadataByIdHeaders headers) {
225    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
226    FetchResponse response =
227        this.networkSession
228            .getNetworkClient()
229            .fetch(
230                new FetchOptions.Builder(
231                        String.join(
232                            "",
233                            this.networkSession.getBaseUrls().getBaseUrl(),
234                            "/2.0/folders/",
235                            convertToString(folderId),
236                            "/metadata/",
237                            convertToString(scope),
238                            "/",
239                            convertToString(templateKey)),
240                        "POST")
241                    .headers(headersMap)
242                    .data(JsonManager.serialize(requestBody))
243                    .contentType("application/json")
244                    .responseFormat(ResponseFormat.JSON)
245                    .auth(this.auth)
246                    .networkSession(this.networkSession)
247                    .build());
248    return JsonManager.deserialize(response.getData(), MetadataFull.class);
249  }
250
251  /**
252   * Updates a piece of metadata on a folder.
253   *
254   * <p>The metadata instance can only be updated if the template has already been applied to the
255   * folder before. When editing metadata, only values that match the metadata template schema will
256   * be accepted.
257   *
258   * <p>The update is applied atomically. If any errors occur during the application of the
259   * operations, the metadata instance will not be changed.
260   *
261   * @param folderId The unique identifier that represent a folder.
262   *     <p>The ID for any folder can be determined by visiting this folder in the web application
263   *     and copying the ID from the URL. For example, for the URL
264   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
265   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
266   * @param scope The scope of the metadata template. Example: "global"
267   * @param templateKey The name of the metadata template. Example: "properties"
268   * @param requestBody Request body of updateFolderMetadataById method
269   */
270  public MetadataFull updateFolderMetadataById(
271      String folderId,
272      UpdateFolderMetadataByIdScope scope,
273      String templateKey,
274      List<UpdateFolderMetadataByIdRequestBody> requestBody) {
275    return updateFolderMetadataById(
276        folderId, scope, templateKey, requestBody, new UpdateFolderMetadataByIdHeaders());
277  }
278
279  /**
280   * Updates a piece of metadata on a folder.
281   *
282   * <p>The metadata instance can only be updated if the template has already been applied to the
283   * folder before. When editing metadata, only values that match the metadata template schema will
284   * be accepted.
285   *
286   * <p>The update is applied atomically. If any errors occur during the application of the
287   * operations, the metadata instance will not be changed.
288   *
289   * @param folderId The unique identifier that represent a folder.
290   *     <p>The ID for any folder can be determined by visiting this folder in the web application
291   *     and copying the ID from the URL. For example, for the URL
292   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
293   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
294   * @param scope The scope of the metadata template. Example: "global"
295   * @param templateKey The name of the metadata template. Example: "properties"
296   * @param requestBody Request body of updateFolderMetadataById method
297   * @param headers Headers of updateFolderMetadataById method
298   */
299  public MetadataFull updateFolderMetadataById(
300      String folderId,
301      UpdateFolderMetadataByIdScope scope,
302      String templateKey,
303      List<UpdateFolderMetadataByIdRequestBody> requestBody,
304      UpdateFolderMetadataByIdHeaders headers) {
305    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
306    FetchResponse response =
307        this.networkSession
308            .getNetworkClient()
309            .fetch(
310                new FetchOptions.Builder(
311                        String.join(
312                            "",
313                            this.networkSession.getBaseUrls().getBaseUrl(),
314                            "/2.0/folders/",
315                            convertToString(folderId),
316                            "/metadata/",
317                            convertToString(scope),
318                            "/",
319                            convertToString(templateKey)),
320                        "PUT")
321                    .headers(headersMap)
322                    .data(JsonManager.serialize(requestBody))
323                    .contentType("application/json-patch+json")
324                    .responseFormat(ResponseFormat.JSON)
325                    .auth(this.auth)
326                    .networkSession(this.networkSession)
327                    .build());
328    return JsonManager.deserialize(response.getData(), MetadataFull.class);
329  }
330
331  /**
332   * Deletes a piece of folder metadata.
333   *
334   * @param folderId The unique identifier that represent a folder.
335   *     <p>The ID for any folder can be determined by visiting this folder in the web application
336   *     and copying the ID from the URL. For example, for the URL
337   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
338   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
339   * @param scope The scope of the metadata template. Example: "global"
340   * @param templateKey The name of the metadata template. Example: "properties"
341   */
342  public void deleteFolderMetadataById(
343      String folderId, DeleteFolderMetadataByIdScope scope, String templateKey) {
344    deleteFolderMetadataById(folderId, scope, templateKey, new DeleteFolderMetadataByIdHeaders());
345  }
346
347  /**
348   * Deletes a piece of folder metadata.
349   *
350   * @param folderId The unique identifier that represent a folder.
351   *     <p>The ID for any folder can be determined by visiting this folder in the web application
352   *     and copying the ID from the URL. For example, for the URL
353   *     `https://*.app.box.com/folder/123` the `folder_id` is `123`.
354   *     <p>The root folder of a Box account is always represented by the ID `0`. Example: "12345"
355   * @param scope The scope of the metadata template. Example: "global"
356   * @param templateKey The name of the metadata template. Example: "properties"
357   * @param headers Headers of deleteFolderMetadataById method
358   */
359  public void deleteFolderMetadataById(
360      String folderId,
361      DeleteFolderMetadataByIdScope scope,
362      String templateKey,
363      DeleteFolderMetadataByIdHeaders headers) {
364    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
365    FetchResponse response =
366        this.networkSession
367            .getNetworkClient()
368            .fetch(
369                new FetchOptions.Builder(
370                        String.join(
371                            "",
372                            this.networkSession.getBaseUrls().getBaseUrl(),
373                            "/2.0/folders/",
374                            convertToString(folderId),
375                            "/metadata/",
376                            convertToString(scope),
377                            "/",
378                            convertToString(templateKey)),
379                        "DELETE")
380                    .headers(headersMap)
381                    .responseFormat(ResponseFormat.NO_CONTENT)
382                    .auth(this.auth)
383                    .networkSession(this.networkSession)
384                    .build());
385  }
386
387  public Authentication getAuth() {
388    return auth;
389  }
390
391  public NetworkSession getNetworkSession() {
392    return networkSession;
393  }
394
395  public static class Builder {
396
397    protected Authentication auth;
398
399    protected NetworkSession networkSession;
400
401    public Builder() {}
402
403    public Builder auth(Authentication auth) {
404      this.auth = auth;
405      return this;
406    }
407
408    public Builder networkSession(NetworkSession networkSession) {
409      this.networkSession = networkSession;
410      return this;
411    }
412
413    public FolderMetadataManager build() {
414      if (this.networkSession == null) {
415        this.networkSession = new NetworkSession();
416      }
417      return new FolderMetadataManager(this);
418    }
419  }
420}