001package com.box.sdkgen.managers.transfer;
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.folderfull.FolderFull;
015import com.box.sdkgen.serialization.json.JsonManager;
016import java.util.Map;
017
018public class TransferManager {
019
020  public Authentication auth;
021
022  public NetworkSession networkSession;
023
024  public TransferManager() {
025    this.networkSession = new NetworkSession();
026  }
027
028  protected TransferManager(Builder builder) {
029    this.auth = builder.auth;
030    this.networkSession = builder.networkSession;
031  }
032
033  /**
034   * Move all of the items (files, folders and workflows) owned by a user into another user's
035   * account.
036   *
037   * <p>Only the root folder (`0`) can be transferred.
038   *
039   * <p>Folders can only be moved across users by users with administrative permissions.
040   *
041   * <p>All existing shared links and folder-level collaborations are transferred during the
042   * operation. Please note that while collaborations at the individual file-level are transferred
043   * during the operation, the collaborations are deleted when the original user is deleted.
044   *
045   * <p>If the user has a large number of items across all folders, the call will be run
046   * asynchronously. If the operation is not completed within 10 minutes, the user will receive a
047   * 200 OK response, and the operation will continue running.
048   *
049   * <p>If the destination path has a metadata cascade policy attached to any of the parent folders,
050   * a metadata cascade operation will be kicked off asynchronously.
051   *
052   * <p>There is currently no way to check for when this operation is finished.
053   *
054   * <p>The destination folder's name will be in the format `{User}'s Files and Folders`, where
055   * `{User}` is the display name of the user.
056   *
057   * <p>To make this API call your application will need to have the "Read and write all files and
058   * folders stored in Box" scope enabled.
059   *
060   * <p>Please make sure the destination user has access to `Relay` or `Relay Lite`, and has access
061   * to the files and folders involved in the workflows being transferred.
062   *
063   * <p>Admins will receive an email when the operation is completed.
064   *
065   * @param userId The ID of the user. Example: "12345"
066   * @param requestBody Request body of transferOwnedFolder method
067   */
068  public FolderFull transferOwnedFolder(String userId, TransferOwnedFolderRequestBody requestBody) {
069    return transferOwnedFolder(
070        userId,
071        requestBody,
072        new TransferOwnedFolderQueryParams(),
073        new TransferOwnedFolderHeaders());
074  }
075
076  /**
077   * Move all of the items (files, folders and workflows) owned by a user into another user's
078   * account.
079   *
080   * <p>Only the root folder (`0`) can be transferred.
081   *
082   * <p>Folders can only be moved across users by users with administrative permissions.
083   *
084   * <p>All existing shared links and folder-level collaborations are transferred during the
085   * operation. Please note that while collaborations at the individual file-level are transferred
086   * during the operation, the collaborations are deleted when the original user is deleted.
087   *
088   * <p>If the user has a large number of items across all folders, the call will be run
089   * asynchronously. If the operation is not completed within 10 minutes, the user will receive a
090   * 200 OK response, and the operation will continue running.
091   *
092   * <p>If the destination path has a metadata cascade policy attached to any of the parent folders,
093   * a metadata cascade operation will be kicked off asynchronously.
094   *
095   * <p>There is currently no way to check for when this operation is finished.
096   *
097   * <p>The destination folder's name will be in the format `{User}'s Files and Folders`, where
098   * `{User}` is the display name of the user.
099   *
100   * <p>To make this API call your application will need to have the "Read and write all files and
101   * folders stored in Box" scope enabled.
102   *
103   * <p>Please make sure the destination user has access to `Relay` or `Relay Lite`, and has access
104   * to the files and folders involved in the workflows being transferred.
105   *
106   * <p>Admins will receive an email when the operation is completed.
107   *
108   * @param userId The ID of the user. Example: "12345"
109   * @param requestBody Request body of transferOwnedFolder method
110   * @param queryParams Query parameters of transferOwnedFolder method
111   */
112  public FolderFull transferOwnedFolder(
113      String userId,
114      TransferOwnedFolderRequestBody requestBody,
115      TransferOwnedFolderQueryParams queryParams) {
116    return transferOwnedFolder(userId, requestBody, queryParams, new TransferOwnedFolderHeaders());
117  }
118
119  /**
120   * Move all of the items (files, folders and workflows) owned by a user into another user's
121   * account.
122   *
123   * <p>Only the root folder (`0`) can be transferred.
124   *
125   * <p>Folders can only be moved across users by users with administrative permissions.
126   *
127   * <p>All existing shared links and folder-level collaborations are transferred during the
128   * operation. Please note that while collaborations at the individual file-level are transferred
129   * during the operation, the collaborations are deleted when the original user is deleted.
130   *
131   * <p>If the user has a large number of items across all folders, the call will be run
132   * asynchronously. If the operation is not completed within 10 minutes, the user will receive a
133   * 200 OK response, and the operation will continue running.
134   *
135   * <p>If the destination path has a metadata cascade policy attached to any of the parent folders,
136   * a metadata cascade operation will be kicked off asynchronously.
137   *
138   * <p>There is currently no way to check for when this operation is finished.
139   *
140   * <p>The destination folder's name will be in the format `{User}'s Files and Folders`, where
141   * `{User}` is the display name of the user.
142   *
143   * <p>To make this API call your application will need to have the "Read and write all files and
144   * folders stored in Box" scope enabled.
145   *
146   * <p>Please make sure the destination user has access to `Relay` or `Relay Lite`, and has access
147   * to the files and folders involved in the workflows being transferred.
148   *
149   * <p>Admins will receive an email when the operation is completed.
150   *
151   * @param userId The ID of the user. Example: "12345"
152   * @param requestBody Request body of transferOwnedFolder method
153   * @param headers Headers of transferOwnedFolder method
154   */
155  public FolderFull transferOwnedFolder(
156      String userId,
157      TransferOwnedFolderRequestBody requestBody,
158      TransferOwnedFolderHeaders headers) {
159    return transferOwnedFolder(userId, requestBody, new TransferOwnedFolderQueryParams(), headers);
160  }
161
162  /**
163   * Move all of the items (files, folders and workflows) owned by a user into another user's
164   * account.
165   *
166   * <p>Only the root folder (`0`) can be transferred.
167   *
168   * <p>Folders can only be moved across users by users with administrative permissions.
169   *
170   * <p>All existing shared links and folder-level collaborations are transferred during the
171   * operation. Please note that while collaborations at the individual file-level are transferred
172   * during the operation, the collaborations are deleted when the original user is deleted.
173   *
174   * <p>If the user has a large number of items across all folders, the call will be run
175   * asynchronously. If the operation is not completed within 10 minutes, the user will receive a
176   * 200 OK response, and the operation will continue running.
177   *
178   * <p>If the destination path has a metadata cascade policy attached to any of the parent folders,
179   * a metadata cascade operation will be kicked off asynchronously.
180   *
181   * <p>There is currently no way to check for when this operation is finished.
182   *
183   * <p>The destination folder's name will be in the format `{User}'s Files and Folders`, where
184   * `{User}` is the display name of the user.
185   *
186   * <p>To make this API call your application will need to have the "Read and write all files and
187   * folders stored in Box" scope enabled.
188   *
189   * <p>Please make sure the destination user has access to `Relay` or `Relay Lite`, and has access
190   * to the files and folders involved in the workflows being transferred.
191   *
192   * <p>Admins will receive an email when the operation is completed.
193   *
194   * @param userId The ID of the user. Example: "12345"
195   * @param requestBody Request body of transferOwnedFolder method
196   * @param queryParams Query parameters of transferOwnedFolder method
197   * @param headers Headers of transferOwnedFolder method
198   */
199  public FolderFull transferOwnedFolder(
200      String userId,
201      TransferOwnedFolderRequestBody requestBody,
202      TransferOwnedFolderQueryParams queryParams,
203      TransferOwnedFolderHeaders headers) {
204    Map<String, String> queryParamsMap =
205        prepareParams(
206            mapOf(
207                entryOf("fields", convertToString(queryParams.getFields())),
208                entryOf("notify", convertToString(queryParams.getNotify()))));
209    Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders()));
210    FetchResponse response =
211        this.networkSession
212            .getNetworkClient()
213            .fetch(
214                new FetchOptions.Builder(
215                        String.join(
216                            "",
217                            this.networkSession.getBaseUrls().getBaseUrl(),
218                            "/2.0/users/",
219                            convertToString(userId),
220                            "/folders/0"),
221                        "PUT")
222                    .params(queryParamsMap)
223                    .headers(headersMap)
224                    .data(JsonManager.serialize(requestBody))
225                    .contentType("application/json")
226                    .responseFormat(ResponseFormat.JSON)
227                    .auth(this.auth)
228                    .networkSession(this.networkSession)
229                    .build());
230    return JsonManager.deserialize(response.getData(), FolderFull.class);
231  }
232
233  public Authentication getAuth() {
234    return auth;
235  }
236
237  public NetworkSession getNetworkSession() {
238    return networkSession;
239  }
240
241  public static class Builder {
242
243    protected Authentication auth;
244
245    protected NetworkSession networkSession;
246
247    public Builder() {}
248
249    public Builder auth(Authentication auth) {
250      this.auth = auth;
251      return this;
252    }
253
254    public Builder networkSession(NetworkSession networkSession) {
255      this.networkSession = networkSession;
256      return this;
257    }
258
259    public TransferManager build() {
260      if (this.networkSession == null) {
261        this.networkSession = new NetworkSession();
262      }
263      return new TransferManager(this);
264    }
265  }
266}