001package com.box.sdkgen.managers.metadatatemplates;
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.metadatatemplate.MetadataTemplate;
015import com.box.sdkgen.schemas.metadatatemplates.MetadataTemplates;
016import com.box.sdkgen.serialization.json.JsonManager;
017import java.util.List;
018import java.util.Map;
019
020public class MetadataTemplatesManager {
021
022  public Authentication auth;
023
024  public NetworkSession networkSession;
025
026  public MetadataTemplatesManager() {
027    this.networkSession = new NetworkSession();
028  }
029
030  protected MetadataTemplatesManager(Builder builder) {
031    this.auth = builder.auth;
032    this.networkSession = builder.networkSession;
033  }
034
035  /**
036   * Finds a metadata template by searching for the ID of an instance of the template.
037   *
038   * @param queryParams Query parameters of getMetadataTemplatesByInstanceId method
039   */
040  public MetadataTemplates getMetadataTemplatesByInstanceId(
041      GetMetadataTemplatesByInstanceIdQueryParams queryParams) {
042    return getMetadataTemplatesByInstanceId(
043        queryParams, new GetMetadataTemplatesByInstanceIdHeaders());
044  }
045
046  /**
047   * Finds a metadata template by searching for the ID of an instance of the template.
048   *
049   * @param queryParams Query parameters of getMetadataTemplatesByInstanceId method
050   * @param headers Headers of getMetadataTemplatesByInstanceId method
051   */
052  public MetadataTemplates getMetadataTemplatesByInstanceId(
053      GetMetadataTemplatesByInstanceIdQueryParams queryParams,
054      GetMetadataTemplatesByInstanceIdHeaders headers) {
055    Map<String, String> queryParamsMap =
056        prepareParams(
057            mapOf(
058                entryOf(
059                    "metadata_instance_id", convertToString(queryParams.getMetadataInstanceId())),
060                entryOf("marker", convertToString(queryParams.getMarker())),
061                entryOf("limit", convertToString(queryParams.getLimit()))));
062    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
063    FetchResponse response =
064        this.networkSession
065            .getNetworkClient()
066            .fetch(
067                new FetchOptions.Builder(
068                        String.join(
069                            "",
070                            this.networkSession.getBaseUrls().getBaseUrl(),
071                            "/2.0/metadata_templates"),
072                        "GET")
073                    .params(queryParamsMap)
074                    .headers(headersMap)
075                    .responseFormat(ResponseFormat.JSON)
076                    .auth(this.auth)
077                    .networkSession(this.networkSession)
078                    .build());
079    return JsonManager.deserialize(response.getData(), MetadataTemplates.class);
080  }
081
082  /**
083   * Retrieves a metadata template by its `scope` and `templateKey` values.
084   *
085   * <p>To find the `scope` and `templateKey` for a template, list all templates for an enterprise
086   * or globally, or list all templates applied to a file or folder.
087   *
088   * @param scope The scope of the metadata template. Example: "global"
089   * @param templateKey The name of the metadata template. Example: "properties"
090   */
091  public MetadataTemplate getMetadataTemplate(GetMetadataTemplateScope scope, String templateKey) {
092    return getMetadataTemplate(scope, templateKey, new GetMetadataTemplateHeaders());
093  }
094
095  /**
096   * Retrieves a metadata template by its `scope` and `templateKey` values.
097   *
098   * <p>To find the `scope` and `templateKey` for a template, list all templates for an enterprise
099   * or globally, or list all templates applied to a file or folder.
100   *
101   * @param scope The scope of the metadata template. Example: "global"
102   * @param templateKey The name of the metadata template. Example: "properties"
103   * @param headers Headers of getMetadataTemplate method
104   */
105  public MetadataTemplate getMetadataTemplate(
106      GetMetadataTemplateScope scope, String templateKey, GetMetadataTemplateHeaders headers) {
107    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
108    FetchResponse response =
109        this.networkSession
110            .getNetworkClient()
111            .fetch(
112                new FetchOptions.Builder(
113                        String.join(
114                            "",
115                            this.networkSession.getBaseUrls().getBaseUrl(),
116                            "/2.0/metadata_templates/",
117                            convertToString(scope),
118                            "/",
119                            convertToString(templateKey),
120                            "/schema"),
121                        "GET")
122                    .headers(headersMap)
123                    .responseFormat(ResponseFormat.JSON)
124                    .auth(this.auth)
125                    .networkSession(this.networkSession)
126                    .build());
127    return JsonManager.deserialize(response.getData(), MetadataTemplate.class);
128  }
129
130  /**
131   * Updates a metadata template.
132   *
133   * <p>The metadata template can only be updated if the template already exists.
134   *
135   * <p>The update is applied atomically. If any errors occur during the application of the
136   * operations, the metadata template will not be changed.
137   *
138   * @param scope The scope of the metadata template. Example: "global"
139   * @param templateKey The name of the metadata template. Example: "properties"
140   * @param requestBody Request body of updateMetadataTemplate method
141   */
142  public MetadataTemplate updateMetadataTemplate(
143      UpdateMetadataTemplateScope scope,
144      String templateKey,
145      List<UpdateMetadataTemplateRequestBody> requestBody) {
146    return updateMetadataTemplate(
147        scope, templateKey, requestBody, new UpdateMetadataTemplateHeaders());
148  }
149
150  /**
151   * Updates a metadata template.
152   *
153   * <p>The metadata template can only be updated if the template already exists.
154   *
155   * <p>The update is applied atomically. If any errors occur during the application of the
156   * operations, the metadata template will not be changed.
157   *
158   * @param scope The scope of the metadata template. Example: "global"
159   * @param templateKey The name of the metadata template. Example: "properties"
160   * @param requestBody Request body of updateMetadataTemplate method
161   * @param headers Headers of updateMetadataTemplate method
162   */
163  public MetadataTemplate updateMetadataTemplate(
164      UpdateMetadataTemplateScope scope,
165      String templateKey,
166      List<UpdateMetadataTemplateRequestBody> requestBody,
167      UpdateMetadataTemplateHeaders headers) {
168    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
169    FetchResponse response =
170        this.networkSession
171            .getNetworkClient()
172            .fetch(
173                new FetchOptions.Builder(
174                        String.join(
175                            "",
176                            this.networkSession.getBaseUrls().getBaseUrl(),
177                            "/2.0/metadata_templates/",
178                            convertToString(scope),
179                            "/",
180                            convertToString(templateKey),
181                            "/schema"),
182                        "PUT")
183                    .headers(headersMap)
184                    .data(JsonManager.serialize(requestBody))
185                    .contentType("application/json-patch+json")
186                    .responseFormat(ResponseFormat.JSON)
187                    .auth(this.auth)
188                    .networkSession(this.networkSession)
189                    .build());
190    return JsonManager.deserialize(response.getData(), MetadataTemplate.class);
191  }
192
193  /**
194   * Delete a metadata template and its instances. This deletion is permanent and can not be
195   * reversed.
196   *
197   * @param scope The scope of the metadata template. Example: "global"
198   * @param templateKey The name of the metadata template. Example: "properties"
199   */
200  public void deleteMetadataTemplate(DeleteMetadataTemplateScope scope, String templateKey) {
201    deleteMetadataTemplate(scope, templateKey, new DeleteMetadataTemplateHeaders());
202  }
203
204  /**
205   * Delete a metadata template and its instances. This deletion is permanent and can not be
206   * reversed.
207   *
208   * @param scope The scope of the metadata template. Example: "global"
209   * @param templateKey The name of the metadata template. Example: "properties"
210   * @param headers Headers of deleteMetadataTemplate method
211   */
212  public void deleteMetadataTemplate(
213      DeleteMetadataTemplateScope scope,
214      String templateKey,
215      DeleteMetadataTemplateHeaders headers) {
216    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
217    FetchResponse response =
218        this.networkSession
219            .getNetworkClient()
220            .fetch(
221                new FetchOptions.Builder(
222                        String.join(
223                            "",
224                            this.networkSession.getBaseUrls().getBaseUrl(),
225                            "/2.0/metadata_templates/",
226                            convertToString(scope),
227                            "/",
228                            convertToString(templateKey),
229                            "/schema"),
230                        "DELETE")
231                    .headers(headersMap)
232                    .responseFormat(ResponseFormat.NO_CONTENT)
233                    .auth(this.auth)
234                    .networkSession(this.networkSession)
235                    .build());
236  }
237
238  /**
239   * Retrieves a metadata template by its ID.
240   *
241   * @param templateId The ID of the template. Example: "f7a9891f"
242   */
243  public MetadataTemplate getMetadataTemplateById(String templateId) {
244    return getMetadataTemplateById(templateId, new GetMetadataTemplateByIdHeaders());
245  }
246
247  /**
248   * Retrieves a metadata template by its ID.
249   *
250   * @param templateId The ID of the template. Example: "f7a9891f"
251   * @param headers Headers of getMetadataTemplateById method
252   */
253  public MetadataTemplate getMetadataTemplateById(
254      String templateId, GetMetadataTemplateByIdHeaders headers) {
255    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
256    FetchResponse response =
257        this.networkSession
258            .getNetworkClient()
259            .fetch(
260                new FetchOptions.Builder(
261                        String.join(
262                            "",
263                            this.networkSession.getBaseUrls().getBaseUrl(),
264                            "/2.0/metadata_templates/",
265                            convertToString(templateId)),
266                        "GET")
267                    .headers(headersMap)
268                    .responseFormat(ResponseFormat.JSON)
269                    .auth(this.auth)
270                    .networkSession(this.networkSession)
271                    .build());
272    return JsonManager.deserialize(response.getData(), MetadataTemplate.class);
273  }
274
275  /**
276   * Used to retrieve all generic, global metadata templates available to all enterprises using Box.
277   */
278  public MetadataTemplates getGlobalMetadataTemplates() {
279    return getGlobalMetadataTemplates(
280        new GetGlobalMetadataTemplatesQueryParams(), new GetGlobalMetadataTemplatesHeaders());
281  }
282
283  /**
284   * Used to retrieve all generic, global metadata templates available to all enterprises using Box.
285   *
286   * @param queryParams Query parameters of getGlobalMetadataTemplates method
287   */
288  public MetadataTemplates getGlobalMetadataTemplates(
289      GetGlobalMetadataTemplatesQueryParams queryParams) {
290    return getGlobalMetadataTemplates(queryParams, new GetGlobalMetadataTemplatesHeaders());
291  }
292
293  /**
294   * Used to retrieve all generic, global metadata templates available to all enterprises using Box.
295   *
296   * @param headers Headers of getGlobalMetadataTemplates method
297   */
298  public MetadataTemplates getGlobalMetadataTemplates(GetGlobalMetadataTemplatesHeaders headers) {
299    return getGlobalMetadataTemplates(new GetGlobalMetadataTemplatesQueryParams(), headers);
300  }
301
302  /**
303   * Used to retrieve all generic, global metadata templates available to all enterprises using Box.
304   *
305   * @param queryParams Query parameters of getGlobalMetadataTemplates method
306   * @param headers Headers of getGlobalMetadataTemplates method
307   */
308  public MetadataTemplates getGlobalMetadataTemplates(
309      GetGlobalMetadataTemplatesQueryParams queryParams,
310      GetGlobalMetadataTemplatesHeaders headers) {
311    Map<String, String> queryParamsMap =
312        prepareParams(
313            mapOf(
314                entryOf("marker", convertToString(queryParams.getMarker())),
315                entryOf("limit", convertToString(queryParams.getLimit()))));
316    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
317    FetchResponse response =
318        this.networkSession
319            .getNetworkClient()
320            .fetch(
321                new FetchOptions.Builder(
322                        String.join(
323                            "",
324                            this.networkSession.getBaseUrls().getBaseUrl(),
325                            "/2.0/metadata_templates/global"),
326                        "GET")
327                    .params(queryParamsMap)
328                    .headers(headersMap)
329                    .responseFormat(ResponseFormat.JSON)
330                    .auth(this.auth)
331                    .networkSession(this.networkSession)
332                    .build());
333    return JsonManager.deserialize(response.getData(), MetadataTemplates.class);
334  }
335
336  /**
337   * Used to retrieve all metadata templates created to be used specifically within the user's
338   * enterprise.
339   */
340  public MetadataTemplates getEnterpriseMetadataTemplates() {
341    return getEnterpriseMetadataTemplates(
342        new GetEnterpriseMetadataTemplatesQueryParams(),
343        new GetEnterpriseMetadataTemplatesHeaders());
344  }
345
346  /**
347   * Used to retrieve all metadata templates created to be used specifically within the user's
348   * enterprise.
349   *
350   * @param queryParams Query parameters of getEnterpriseMetadataTemplates method
351   */
352  public MetadataTemplates getEnterpriseMetadataTemplates(
353      GetEnterpriseMetadataTemplatesQueryParams queryParams) {
354    return getEnterpriseMetadataTemplates(queryParams, new GetEnterpriseMetadataTemplatesHeaders());
355  }
356
357  /**
358   * Used to retrieve all metadata templates created to be used specifically within the user's
359   * enterprise.
360   *
361   * @param headers Headers of getEnterpriseMetadataTemplates method
362   */
363  public MetadataTemplates getEnterpriseMetadataTemplates(
364      GetEnterpriseMetadataTemplatesHeaders headers) {
365    return getEnterpriseMetadataTemplates(new GetEnterpriseMetadataTemplatesQueryParams(), headers);
366  }
367
368  /**
369   * Used to retrieve all metadata templates created to be used specifically within the user's
370   * enterprise.
371   *
372   * @param queryParams Query parameters of getEnterpriseMetadataTemplates method
373   * @param headers Headers of getEnterpriseMetadataTemplates method
374   */
375  public MetadataTemplates getEnterpriseMetadataTemplates(
376      GetEnterpriseMetadataTemplatesQueryParams queryParams,
377      GetEnterpriseMetadataTemplatesHeaders headers) {
378    Map<String, String> queryParamsMap =
379        prepareParams(
380            mapOf(
381                entryOf("marker", convertToString(queryParams.getMarker())),
382                entryOf("limit", convertToString(queryParams.getLimit()))));
383    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
384    FetchResponse response =
385        this.networkSession
386            .getNetworkClient()
387            .fetch(
388                new FetchOptions.Builder(
389                        String.join(
390                            "",
391                            this.networkSession.getBaseUrls().getBaseUrl(),
392                            "/2.0/metadata_templates/enterprise"),
393                        "GET")
394                    .params(queryParamsMap)
395                    .headers(headersMap)
396                    .responseFormat(ResponseFormat.JSON)
397                    .auth(this.auth)
398                    .networkSession(this.networkSession)
399                    .build());
400    return JsonManager.deserialize(response.getData(), MetadataTemplates.class);
401  }
402
403  /**
404   * Creates a new metadata template that can be applied to files and folders.
405   *
406   * @param requestBody Request body of createMetadataTemplate method
407   */
408  public MetadataTemplate createMetadataTemplate(CreateMetadataTemplateRequestBody requestBody) {
409    return createMetadataTemplate(requestBody, new CreateMetadataTemplateHeaders());
410  }
411
412  /**
413   * Creates a new metadata template that can be applied to files and folders.
414   *
415   * @param requestBody Request body of createMetadataTemplate method
416   * @param headers Headers of createMetadataTemplate method
417   */
418  public MetadataTemplate createMetadataTemplate(
419      CreateMetadataTemplateRequestBody requestBody, CreateMetadataTemplateHeaders headers) {
420    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
421    FetchResponse response =
422        this.networkSession
423            .getNetworkClient()
424            .fetch(
425                new FetchOptions.Builder(
426                        String.join(
427                            "",
428                            this.networkSession.getBaseUrls().getBaseUrl(),
429                            "/2.0/metadata_templates/schema"),
430                        "POST")
431                    .headers(headersMap)
432                    .data(JsonManager.serialize(requestBody))
433                    .contentType("application/json")
434                    .responseFormat(ResponseFormat.JSON)
435                    .auth(this.auth)
436                    .networkSession(this.networkSession)
437                    .build());
438    return JsonManager.deserialize(response.getData(), MetadataTemplate.class);
439  }
440
441  public Authentication getAuth() {
442    return auth;
443  }
444
445  public NetworkSession getNetworkSession() {
446    return networkSession;
447  }
448
449  public static class Builder {
450
451    protected Authentication auth;
452
453    protected NetworkSession networkSession;
454
455    public Builder() {}
456
457    public Builder auth(Authentication auth) {
458      this.auth = auth;
459      return this;
460    }
461
462    public Builder networkSession(NetworkSession networkSession) {
463      this.networkSession = networkSession;
464      return this;
465    }
466
467    public MetadataTemplatesManager build() {
468      if (this.networkSession == null) {
469        this.networkSession = new NetworkSession();
470      }
471      return new MetadataTemplatesManager(this);
472    }
473  }
474}