001package com.box.sdk;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.net.URL;
006import java.util.ArrayList;
007import java.util.Collection;
008import java.util.Date;
009import java.util.EnumSet;
010import java.util.Iterator;
011import java.util.Map;
012import java.util.concurrent.TimeUnit;
013
014import com.box.sdk.internal.utils.Parsers;
015import com.eclipsesource.json.JsonArray;
016import com.eclipsesource.json.JsonObject;
017import com.eclipsesource.json.JsonValue;
018
019/**
020 * Represents a folder on Box. This class can be used to iterate through a folder's contents, collaborate a folder with
021 * another user or group, and perform other common folder operations (move, copy, delete, etc.).
022 * <p>
023 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
024 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
025 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
026 */
027@BoxResourceType("folder")
028public class BoxFolder extends BoxItem implements Iterable<BoxItem.Info> {
029    /**
030     * An array of all possible folder fields that can be requested when calling {@link #getInfo()}.
031     */
032    public static final String[] ALL_FIELDS = {"type", "id", "sequence_id", "etag", "name", "created_at", "modified_at",
033        "description", "size", "path_collection", "created_by", "modified_by", "trashed_at", "purged_at",
034        "content_created_at", "content_modified_at", "owned_by", "shared_link", "folder_upload_email", "parent",
035        "item_status", "item_collection", "sync_state", "has_collaborations", "permissions", "tags",
036        "can_non_owners_invite", "collections", "watermark_info", "metadata"};
037
038    /**
039     * Create Folder URL Template.
040     */
041    public static final URLTemplate CREATE_FOLDER_URL = new URLTemplate("folders");
042    /**
043     * Create Web Link URL Template.
044     */
045    public static final URLTemplate CREATE_WEB_LINK_URL = new URLTemplate("web_links");
046    /**
047     * Copy Folder URL Template.
048     */
049    public static final URLTemplate COPY_FOLDER_URL = new URLTemplate("folders/%s/copy");
050    /**
051     * Delete Folder URL Template.
052     */
053    public static final URLTemplate DELETE_FOLDER_URL = new URLTemplate("folders/%s?recursive=%b");
054    /**
055     * Folder Info URL Template.
056     */
057    public static final URLTemplate FOLDER_INFO_URL_TEMPLATE = new URLTemplate("folders/%s");
058    /**
059     * Upload File URL Template.
060     */
061    public static final URLTemplate UPLOAD_FILE_URL = new URLTemplate("files/content");
062    /**
063     * Add Collaboration URL Template.
064     */
065    public static final URLTemplate ADD_COLLABORATION_URL = new URLTemplate("collaborations");
066    /**
067     * Get Collaborations URL Template.
068     */
069    public static final URLTemplate GET_COLLABORATIONS_URL = new URLTemplate("folders/%s/collaborations");
070    /**
071     * Get Items URL Template.
072     */
073    public static final URLTemplate GET_ITEMS_URL = new URLTemplate("folders/%s/items/");
074    /**
075     * Search URL Template.
076     */
077    public static final URLTemplate SEARCH_URL_TEMPLATE = new URLTemplate("search");
078    /**
079     * Metadata URL Template.
080     */
081    public static final URLTemplate METADATA_URL_TEMPLATE = new URLTemplate("folders/%s/metadata/%s/%s");
082    /**
083     * Upload Session URL Template.
084     */
085    public static final URLTemplate UPLOAD_SESSION_URL_TEMPLATE = new URLTemplate("files/upload_sessions");
086
087    /**
088     * Constructs a BoxFolder for a folder with a given ID.
089     *
090     * @param api the API connection to be used by the folder.
091     * @param id  the ID of the folder.
092     */
093    public BoxFolder(BoxAPIConnection api, String id) {
094        super(api, id);
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    protected URL getItemURL() {
102        return FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
103    }
104
105    /**
106     * Gets the current user's root folder.
107     *
108     * @param api the API connection to be used by the folder.
109     * @return the user's root folder.
110     */
111    public static BoxFolder getRootFolder(BoxAPIConnection api) {
112        return new BoxFolder(api, "0");
113    }
114
115    /**
116     * Adds a collaborator to this folder.
117     *
118     * @param collaborator the collaborator to add.
119     * @param role         the role of the collaborator.
120     * @return info about the new collaboration.
121     */
122    public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollaboration.Role role) {
123        JsonObject accessibleByField = new JsonObject();
124        accessibleByField.add("id", collaborator.getID());
125
126        if (collaborator instanceof BoxUser) {
127            accessibleByField.add("type", "user");
128        } else if (collaborator instanceof BoxGroup) {
129            accessibleByField.add("type", "group");
130        } else {
131            throw new IllegalArgumentException("The given collaborator is of an unknown type.");
132        }
133
134        return this.collaborate(accessibleByField, role, null, null);
135    }
136
137    /**
138     * Adds a collaborator to this folder. An email will be sent to the collaborator if they don't already have a Box
139     * account.
140     *
141     * @param email the email address of the collaborator to add.
142     * @param role  the role of the collaborator.
143     * @return info about the new collaboration.
144     */
145    public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role role) {
146        JsonObject accessibleByField = new JsonObject();
147        accessibleByField.add("login", email);
148        accessibleByField.add("type", "user");
149
150        return this.collaborate(accessibleByField, role, null, null);
151    }
152
153    /**
154     * Adds a collaborator to this folder.
155     *
156     * @param collaborator the collaborator to add.
157     * @param role         the role of the collaborator.
158     * @param notify       the user/group should receive email notification of the collaboration or not.
159     * @param canViewPath  the view path collaboration feature is enabled or not.
160     *                     View path collaborations allow the invitee to see the entire ancestral path to the associated
161     *                     folder. The user will not gain privileges in any ancestral folder.
162     * @return info about the new collaboration.
163     */
164    public BoxCollaboration.Info collaborate(BoxCollaborator collaborator, BoxCollaboration.Role role,
165                                             Boolean notify, Boolean canViewPath) {
166        JsonObject accessibleByField = new JsonObject();
167        accessibleByField.add("id", collaborator.getID());
168
169        if (collaborator instanceof BoxUser) {
170            accessibleByField.add("type", "user");
171        } else if (collaborator instanceof BoxGroup) {
172            accessibleByField.add("type", "group");
173        } else {
174            throw new IllegalArgumentException("The given collaborator is of an unknown type.");
175        }
176
177        return this.collaborate(accessibleByField, role, notify, canViewPath);
178    }
179
180    /**
181     * Adds a collaborator to this folder. An email will be sent to the collaborator if they don't already have a Box
182     * account.
183     *
184     * @param email       the email address of the collaborator to add.
185     * @param role        the role of the collaborator.
186     * @param notify      the user/group should receive email notification of the collaboration or not.
187     * @param canViewPath the view path collaboration feature is enabled or not.
188     *                    View path collaborations allow the invitee to see the entire ancestral path to the associated
189     *                    folder. The user will not gain privileges in any ancestral folder.
190     * @return info about the new collaboration.
191     */
192    public BoxCollaboration.Info collaborate(String email, BoxCollaboration.Role role,
193                                             Boolean notify, Boolean canViewPath) {
194        JsonObject accessibleByField = new JsonObject();
195        accessibleByField.add("login", email);
196        accessibleByField.add("type", "user");
197
198        return this.collaborate(accessibleByField, role, notify, canViewPath);
199    }
200
201    private BoxCollaboration.Info collaborate(JsonObject accessibleByField, BoxCollaboration.Role role,
202                                              Boolean notify, Boolean canViewPath) {
203
204        JsonObject itemField = new JsonObject();
205        itemField.add("id", this.getID());
206        itemField.add("type", "folder");
207
208        return BoxCollaboration.create(this.getAPI(), accessibleByField, itemField, role, notify, canViewPath);
209    }
210
211    @Override
212    public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareDate,
213                                          BoxSharedLink.Permissions permissions) {
214
215        BoxSharedLink sharedLink = new BoxSharedLink(access, unshareDate, permissions);
216        Info info = new Info();
217        info.setSharedLink(sharedLink);
218
219        this.updateInfo(info);
220        return info.getSharedLink();
221    }
222
223    /**
224     * Creates new SharedLink for a BoxFolder with a password.
225     *
226     * @param access        The access level of the shared link.
227     * @param unshareDate   A specified date to unshare the Box folder.
228     * @param permissions   The permissions to set on the shared link for the Box folder.
229     * @param password      Password set on the shared link to give access to the Box folder.
230     * @return information about the newly created shared link.
231     */
232    public BoxSharedLink createSharedLink(BoxSharedLink.Access access, Date unshareDate,
233                                          BoxSharedLink.Permissions permissions, String password) {
234
235        BoxSharedLink sharedLink = new BoxSharedLink(access, unshareDate, permissions, password);
236        Info info = new Info();
237        info.setSharedLink(sharedLink);
238
239        this.updateInfo(info);
240        return info.getSharedLink();
241    }
242
243    /**
244     * Gets information about all of the collaborations for this folder.
245     *
246     * @return a collection of information about the collaborations for this folder.
247     */
248    public Collection<BoxCollaboration.Info> getCollaborations() {
249        BoxAPIConnection api = this.getAPI();
250        URL url = GET_COLLABORATIONS_URL.build(api.getBaseURL(), this.getID());
251
252        BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
253        BoxJSONResponse response = (BoxJSONResponse) request.send();
254        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
255
256        int entriesCount = responseJSON.get("total_count").asInt();
257        Collection<BoxCollaboration.Info> collaborations = new ArrayList<BoxCollaboration.Info>(entriesCount);
258        JsonArray entries = responseJSON.get("entries").asArray();
259        for (JsonValue entry : entries) {
260            JsonObject entryObject = entry.asObject();
261            BoxCollaboration collaboration = new BoxCollaboration(api, entryObject.get("id").asString());
262            BoxCollaboration.Info info = collaboration.new Info(entryObject);
263            collaborations.add(info);
264        }
265
266        return collaborations;
267    }
268
269    @Override
270    public BoxFolder.Info getInfo() {
271        URL url = FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
272        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
273        BoxJSONResponse response = (BoxJSONResponse) request.send();
274        return new Info(response.getJSON());
275    }
276
277    @Override
278    public BoxFolder.Info getInfo(String... fields) {
279        String queryString = new QueryStringBuilder().appendParam("fields", fields).toString();
280        URL url = FOLDER_INFO_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), queryString, this.getID());
281
282        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
283        BoxJSONResponse response = (BoxJSONResponse) request.send();
284        return new Info(response.getJSON());
285    }
286
287    /**
288     * Updates the information about this folder with any info fields that have been modified locally.
289     *
290     * @param info the updated info.
291     */
292    public void updateInfo(BoxFolder.Info info) {
293        URL url = FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
294        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
295        request.setBody(info.getPendingChanges());
296        BoxJSONResponse response = (BoxJSONResponse) request.send();
297        JsonObject jsonObject = JsonObject.readFrom(response.getJSON());
298        info.update(jsonObject);
299    }
300
301    @Override
302    public BoxFolder.Info copy(BoxFolder destination) {
303        return this.copy(destination, null);
304    }
305
306    @Override
307    public BoxFolder.Info copy(BoxFolder destination, String newName) {
308        URL url = COPY_FOLDER_URL.build(this.getAPI().getBaseURL(), this.getID());
309        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST");
310
311        JsonObject parent = new JsonObject();
312        parent.add("id", destination.getID());
313
314        JsonObject copyInfo = new JsonObject();
315        copyInfo.add("parent", parent);
316        if (newName != null) {
317            copyInfo.add("name", newName);
318        }
319
320        request.setBody(copyInfo.toString());
321        BoxJSONResponse response = (BoxJSONResponse) request.send();
322        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
323        BoxFolder copiedFolder = new BoxFolder(this.getAPI(), responseJSON.get("id").asString());
324        return copiedFolder.new Info(responseJSON);
325    }
326
327    /**
328     * Creates a new child folder inside this folder.
329     *
330     * @param name the new folder's name.
331     * @return the created folder's info.
332     */
333    public BoxFolder.Info createFolder(String name) {
334        JsonObject parent = new JsonObject();
335        parent.add("id", this.getID());
336
337        JsonObject newFolder = new JsonObject();
338        newFolder.add("name", name);
339        newFolder.add("parent", parent);
340
341        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), CREATE_FOLDER_URL.build(this.getAPI().getBaseURL()),
342                "POST");
343        request.setBody(newFolder.toString());
344        BoxJSONResponse response = (BoxJSONResponse) request.send();
345        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
346
347        BoxFolder createdFolder = new BoxFolder(this.getAPI(), responseJSON.get("id").asString());
348        return createdFolder.new Info(responseJSON);
349    }
350
351    /**
352     * Deletes this folder, optionally recursively deleting all of its contents.
353     *
354     * @param recursive true to recursively delete this folder's contents; otherwise false.
355     */
356    public void delete(boolean recursive) {
357        URL url = DELETE_FOLDER_URL.build(this.getAPI().getBaseURL(), this.getID(), recursive);
358        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE");
359        BoxAPIResponse response = request.send();
360        response.disconnect();
361    }
362
363    @Override
364    public BoxItem.Info move(BoxFolder destination) {
365        return this.move(destination, null);
366    }
367
368    @Override
369    public BoxItem.Info move(BoxFolder destination, String newName) {
370        URL url = FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
371        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
372
373        JsonObject parent = new JsonObject();
374        parent.add("id", destination.getID());
375
376        JsonObject updateInfo = new JsonObject();
377        updateInfo.add("parent", parent);
378        if (newName != null) {
379            updateInfo.add("name", newName);
380        }
381
382        request.setBody(updateInfo.toString());
383        BoxJSONResponse response = (BoxJSONResponse) request.send();
384        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
385        BoxFolder movedFolder = new BoxFolder(this.getAPI(), responseJSON.get("id").asString());
386        return movedFolder.new Info(responseJSON);
387    }
388
389    /**
390     * Renames this folder.
391     *
392     * @param newName the new name of the folder.
393     */
394    public void rename(String newName) {
395        URL url = FOLDER_INFO_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
396        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
397
398        JsonObject updateInfo = new JsonObject();
399        updateInfo.add("name", newName);
400
401        request.setBody(updateInfo.toString());
402        BoxJSONResponse response = (BoxJSONResponse) request.send();
403        response.getJSON();
404    }
405
406    /**
407     * Checks if the file can be successfully uploaded by using the preflight check.
408     *
409     * @param name     the name to give the uploaded file.
410     * @param fileSize the size of the file used for account capacity calculations.
411     */
412    public void canUpload(String name, long fileSize) {
413        URL url = UPLOAD_FILE_URL.build(this.getAPI().getBaseURL());
414        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "OPTIONS");
415
416        JsonObject parent = new JsonObject();
417        parent.add("id", this.getID());
418
419        JsonObject preflightInfo = new JsonObject();
420        preflightInfo.add("parent", parent);
421        preflightInfo.add("name", name);
422
423        preflightInfo.add("size", fileSize);
424
425        request.setBody(preflightInfo.toString());
426        BoxAPIResponse response = request.send();
427        response.disconnect();
428    }
429
430    /**
431     * Uploads a new file to this folder.
432     *
433     * @param fileContent a stream containing the contents of the file to upload.
434     * @param name        the name to give the uploaded file.
435     * @return the uploaded file's info.
436     */
437    public BoxFile.Info uploadFile(InputStream fileContent, String name) {
438        FileUploadParams uploadInfo = new FileUploadParams()
439                .setContent(fileContent)
440                .setName(name);
441        return this.uploadFile(uploadInfo);
442    }
443
444    /**
445     * Uploads a new file to this folder.
446     *
447     * @param callback the callback which allows file content to be written on output stream.
448     * @param name     the name to give the uploaded file.
449     * @return the uploaded file's info.
450     */
451    public BoxFile.Info uploadFile(UploadFileCallback callback, String name) {
452        FileUploadParams uploadInfo = new FileUploadParams()
453                .setUploadFileCallback(callback)
454                .setName(name);
455        return this.uploadFile(uploadInfo);
456    }
457
458    /**
459     * Uploads a new file to this folder while reporting the progress to a ProgressListener.
460     *
461     * @param fileContent a stream containing the contents of the file to upload.
462     * @param name        the name to give the uploaded file.
463     * @param fileSize    the size of the file used for determining the progress of the upload.
464     * @param listener    a listener for monitoring the upload's progress.
465     * @return the uploaded file's info.
466     */
467    public BoxFile.Info uploadFile(InputStream fileContent, String name, long fileSize, ProgressListener listener) {
468        FileUploadParams uploadInfo = new FileUploadParams()
469                .setContent(fileContent)
470                .setName(name)
471                .setSize(fileSize)
472                .setProgressListener(listener);
473        return this.uploadFile(uploadInfo);
474    }
475
476    /**
477     * Uploads a new file to this folder with custom upload parameters.
478     *
479     * @param uploadParams the custom upload parameters.
480     * @return the uploaded file's info.
481     */
482    public BoxFile.Info uploadFile(FileUploadParams uploadParams) {
483        URL uploadURL = UPLOAD_FILE_URL.build(this.getAPI().getBaseUploadURL());
484        BoxMultipartRequest request = new BoxMultipartRequest(getAPI(), uploadURL);
485
486        JsonObject fieldJSON = new JsonObject();
487        JsonObject parentIdJSON = new JsonObject();
488        parentIdJSON.add("id", getID());
489        fieldJSON.add("name", uploadParams.getName());
490        fieldJSON.add("parent", parentIdJSON);
491
492        if (uploadParams.getCreated() != null) {
493            fieldJSON.add("content_created_at", BoxDateFormat.format(uploadParams.getCreated()));
494        }
495
496        if (uploadParams.getModified() != null) {
497            fieldJSON.add("content_modified_at", BoxDateFormat.format(uploadParams.getModified()));
498        }
499
500        if (uploadParams.getSHA1() != null && !uploadParams.getSHA1().isEmpty()) {
501            request.setContentSHA1(uploadParams.getSHA1());
502        }
503
504        request.putField("attributes", fieldJSON.toString());
505
506        if (uploadParams.getSize() > 0) {
507            request.setFile(uploadParams.getContent(), uploadParams.getName(), uploadParams.getSize());
508        } else if (uploadParams.getContent() != null) {
509            request.setFile(uploadParams.getContent(), uploadParams.getName());
510        } else {
511            request.setUploadFileCallback(uploadParams.getUploadFileCallback(), uploadParams.getName());
512        }
513
514        BoxJSONResponse response;
515        if (uploadParams.getProgressListener() == null) {
516            response = (BoxJSONResponse) request.send();
517        } else {
518            response = (BoxJSONResponse) request.send(uploadParams.getProgressListener());
519        }
520        JsonObject collection = JsonObject.readFrom(response.getJSON());
521        JsonArray entries = collection.get("entries").asArray();
522        JsonObject fileInfoJSON = entries.get(0).asObject();
523        String uploadedFileID = fileInfoJSON.get("id").asString();
524
525        BoxFile uploadedFile = new BoxFile(getAPI(), uploadedFileID);
526        return uploadedFile.new Info(fileInfoJSON);
527    }
528
529    /**
530     * Uploads a new weblink to this folder.
531     *
532     * @param linkURL the URL the weblink points to.
533     * @return the uploaded weblink's info.
534     */
535    public BoxWebLink.Info createWebLink(URL linkURL) {
536        return this.createWebLink(null, linkURL,
537                null);
538    }
539
540    /**
541     * Uploads a new weblink to this folder.
542     *
543     * @param name    the filename for the weblink.
544     * @param linkURL the URL the weblink points to.
545     * @return the uploaded weblink's info.
546     */
547    public BoxWebLink.Info createWebLink(String name, URL linkURL) {
548        return this.createWebLink(name, linkURL,
549                null);
550    }
551
552    /**
553     * Uploads a new weblink to this folder.
554     *
555     * @param linkURL     the URL the weblink points to.
556     * @param description the weblink's description.
557     * @return the uploaded weblink's info.
558     */
559    public BoxWebLink.Info createWebLink(URL linkURL, String description) {
560        return this.createWebLink(null, linkURL, description);
561    }
562
563    /**
564     * Uploads a new weblink to this folder.
565     *
566     * @param name        the filename for the weblink.
567     * @param linkURL     the URL the weblink points to.
568     * @param description the weblink's description.
569     * @return the uploaded weblink's info.
570     */
571    public BoxWebLink.Info createWebLink(String name, URL linkURL, String description) {
572        JsonObject parent = new JsonObject();
573        parent.add("id", this.getID());
574
575        JsonObject newWebLink = new JsonObject();
576        newWebLink.add("name", name);
577        newWebLink.add("parent", parent);
578        newWebLink.add("url", linkURL.toString());
579
580        if (description != null) {
581            newWebLink.add("description", description);
582        }
583
584        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(),
585                CREATE_WEB_LINK_URL.build(this.getAPI().getBaseURL()), "POST");
586        request.setBody(newWebLink.toString());
587        BoxJSONResponse response = (BoxJSONResponse) request.send();
588        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
589
590        BoxWebLink createdWebLink = new BoxWebLink(this.getAPI(), responseJSON.get("id").asString());
591        return createdWebLink.new Info(responseJSON);
592    }
593
594    /**
595     * Returns an iterable containing the items in this folder. Iterating over the iterable returned by this method is
596     * equivalent to iterating over this BoxFolder directly.
597     *
598     * @return an iterable containing the items in this folder.
599     */
600    public Iterable<BoxItem.Info> getChildren() {
601        return this;
602    }
603
604    /**
605     * Returns an iterable containing the items in this folder and specifies which child fields to retrieve from the
606     * API.
607     *
608     * @param fields the fields to retrieve.
609     * @return an iterable containing the items in this folder.
610     */
611    public Iterable<BoxItem.Info> getChildren(final String... fields) {
612        return new Iterable<BoxItem.Info>() {
613            @Override
614            public Iterator<BoxItem.Info> iterator() {
615                String queryString = new QueryStringBuilder().appendParam("fields", fields).toString();
616                URL url = GET_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), queryString, getID());
617                return new BoxItemIterator(getAPI(), url);
618            }
619        };
620    }
621
622    /**
623     * Retrieves a specific range of child items in this folder.
624     *
625     * @param offset the index of the first child item to retrieve.
626     * @param limit  the maximum number of children to retrieve after the offset.
627     * @param fields the fields to retrieve.
628     * @return a partial collection containing the specified range of child items.
629     */
630    public PartialCollection<BoxItem.Info> getChildrenRange(long offset, long limit, String... fields) {
631        QueryStringBuilder builder = new QueryStringBuilder()
632                .appendParam("limit", limit)
633                .appendParam("offset", offset);
634
635        if (fields.length > 0) {
636            builder.appendParam("fields", fields).toString();
637        }
638
639        URL url = GET_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), builder.toString(), getID());
640        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
641        BoxJSONResponse response = (BoxJSONResponse) request.send();
642        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
643
644        String totalCountString = responseJSON.get("total_count").toString();
645        long fullSize = Double.valueOf(totalCountString).longValue();
646        PartialCollection<BoxItem.Info> children = new PartialCollection<BoxItem.Info>(offset, limit, fullSize);
647        JsonArray jsonArray = responseJSON.get("entries").asArray();
648        for (JsonValue value : jsonArray) {
649            JsonObject jsonObject = value.asObject();
650            BoxItem.Info parsedItemInfo = (BoxItem.Info) BoxResource.parseInfo(this.getAPI(), jsonObject);
651            if (parsedItemInfo != null) {
652                children.add(parsedItemInfo);
653            }
654        }
655        return children;
656    }
657
658    /**
659     * Returns an iterator over the items in this folder.
660     *
661     * @return an iterator over the items in this folder.
662     */
663    @Override
664    public Iterator<BoxItem.Info> iterator() {
665        URL url = GET_ITEMS_URL.build(this.getAPI().getBaseURL(), BoxFolder.this.getID());
666        return new BoxItemIterator(BoxFolder.this.getAPI(), url);
667    }
668
669    /**
670     * Adds new {@link BoxWebHook} to this {@link BoxFolder}.
671     *
672     * @param address  {@link BoxWebHook.Info#getAddress()}
673     * @param triggers {@link BoxWebHook.Info#getTriggers()}
674     * @return created {@link BoxWebHook.Info}
675     */
676    public BoxWebHook.Info addWebHook(URL address, BoxWebHook.Trigger... triggers) {
677        return BoxWebHook.create(this, address, triggers);
678    }
679
680    /**
681     * Used to retrieve the watermark for the folder.
682     * If the folder does not have a watermark applied to it, a 404 Not Found will be returned by API.
683     *
684     * @param fields the fields to retrieve.
685     * @return the watermark associated with the folder.
686     */
687    public BoxWatermark getWatermark(String... fields) {
688        return this.getWatermark(FOLDER_INFO_URL_TEMPLATE, fields);
689    }
690
691    /**
692     * Used to apply or update the watermark for the folder.
693     *
694     * @return the watermark associated with the folder.
695     */
696    public BoxWatermark applyWatermark() {
697        return this.applyWatermark(FOLDER_INFO_URL_TEMPLATE, BoxWatermark.WATERMARK_DEFAULT_IMPRINT);
698    }
699
700    /**
701     * Removes a watermark from the folder.
702     * If the folder did not have a watermark applied to it, a 404 Not Found will be returned by API.
703     */
704    public void removeWatermark() {
705        this.removeWatermark(FOLDER_INFO_URL_TEMPLATE);
706    }
707
708    /**
709     * Used to retrieve all metadata associated with the folder.
710     *
711     * @param fields the optional fields to retrieve.
712     * @return An iterable of metadata instances associated with the folder
713     */
714    public Iterable<Metadata> getAllMetadata(String... fields) {
715        return Metadata.getAllMetadata(this, fields);
716    }
717
718    /**
719     * This method is deprecated, please use the {@link BoxSearch} class instead.
720     * Searches this folder and all descendant folders using a given queryPlease use BoxSearch Instead.
721     *
722     * @param query the search query.
723     * @return an Iterable containing the search results.
724     */
725    @Deprecated
726    public Iterable<BoxItem.Info> search(final String query) {
727        return new Iterable<BoxItem.Info>() {
728            @Override
729            public Iterator<BoxItem.Info> iterator() {
730                QueryStringBuilder builder = new QueryStringBuilder();
731                builder.appendParam("query", query);
732                builder.appendParam("ancestor_folder_ids", getID());
733
734                URL url = SEARCH_URL_TEMPLATE.buildWithQuery(getAPI().getBaseURL(), builder.toString());
735                return new BoxItemIterator(getAPI(), url);
736            }
737        };
738    }
739
740    @Override
741    public BoxFolder.Info setCollections(BoxCollection... collections) {
742        JsonArray jsonArray = new JsonArray();
743        for (BoxCollection collection : collections) {
744            JsonObject collectionJSON = new JsonObject();
745            collectionJSON.add("id", collection.getID());
746            jsonArray.add(collectionJSON);
747        }
748        JsonObject infoJSON = new JsonObject();
749        infoJSON.add("collections", jsonArray);
750
751        String queryString = new QueryStringBuilder().appendParam("fields", ALL_FIELDS).toString();
752        URL url = FOLDER_INFO_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), queryString, this.getID());
753        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
754        request.setBody(infoJSON.toString());
755        BoxJSONResponse response = (BoxJSONResponse) request.send();
756        JsonObject jsonObject = JsonObject.readFrom(response.getJSON());
757        return new Info(jsonObject);
758    }
759
760    /**
761     * Creates global property metadata on this folder.
762     *
763     * @param metadata the new metadata values.
764     * @return the metadata returned from the server.
765     */
766    public Metadata createMetadata(Metadata metadata) {
767        return this.createMetadata(Metadata.DEFAULT_METADATA_TYPE, metadata);
768    }
769
770    /**
771     * Creates metadata on this folder using a specified template.
772     *
773     * @param templateName the name of the metadata template.
774     * @param metadata     the new metadata values.
775     * @return the metadata returned from the server.
776     */
777    public Metadata createMetadata(String templateName, Metadata metadata) {
778        String scope = Metadata.scopeBasedOnType(templateName);
779        return this.createMetadata(templateName, scope, metadata);
780    }
781
782    /**
783     * Creates metadata on this folder using a specified scope and template.
784     *
785     * @param templateName the name of the metadata template.
786     * @param scope        the scope of the template (usually "global" or "enterprise").
787     * @param metadata     the new metadata values.
788     * @return the metadata returned from the server.
789     */
790    public Metadata createMetadata(String templateName, String scope, Metadata metadata) {
791        URL url = METADATA_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID(), scope, templateName);
792        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "POST");
793        request.addHeader("Content-Type", "application/json");
794        request.setBody(metadata.toString());
795        BoxJSONResponse response = (BoxJSONResponse) request.send();
796        return new Metadata(JsonObject.readFrom(response.getJSON()));
797    }
798
799    /**
800     * Gets the global properties metadata on this folder.
801     *
802     * @return the metadata returned from the server.
803     */
804    public Metadata getMetadata() {
805        return this.getMetadata(Metadata.DEFAULT_METADATA_TYPE);
806    }
807
808    /**
809     * Gets the metadata on this folder associated with a specified template.
810     *
811     * @param templateName the metadata template type name.
812     * @return the metadata returned from the server.
813     */
814    public Metadata getMetadata(String templateName) {
815        String scope = Metadata.scopeBasedOnType(templateName);
816        return this.getMetadata(templateName, scope);
817    }
818
819    /**
820     * Gets the metadata on this folder associated with a specified scope and template.
821     *
822     * @param templateName the metadata template type name.
823     * @param scope        the scope of the template (usually "global" or "enterprise").
824     * @return the metadata returned from the server.
825     */
826    public Metadata getMetadata(String templateName, String scope) {
827        URL url = METADATA_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID(), scope, templateName);
828        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
829        BoxJSONResponse response = (BoxJSONResponse) request.send();
830        return new Metadata(JsonObject.readFrom(response.getJSON()));
831    }
832
833    /**
834     * Updates the global properties metadata on this folder.
835     *
836     * @param metadata the new metadata values.
837     * @return the metadata returned from the server.
838     */
839    public Metadata updateMetadata(Metadata metadata) {
840        URL url = METADATA_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID(), metadata.getScope(),
841                metadata.getTemplateName());
842        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "PUT");
843        request.addHeader("Content-Type", "application/json-patch+json");
844        request.setBody(metadata.getPatch());
845        BoxJSONResponse response = (BoxJSONResponse) request.send();
846        return new Metadata(JsonObject.readFrom(response.getJSON()));
847    }
848
849    /**
850     * Deletes the global properties metadata on this folder.
851     */
852    public void deleteMetadata() {
853        this.deleteMetadata(Metadata.DEFAULT_METADATA_TYPE);
854    }
855
856    /**
857     * Deletes the metadata on this folder associated with a specified template.
858     *
859     * @param templateName the metadata template type name.
860     */
861    public void deleteMetadata(String templateName) {
862        String scope = Metadata.scopeBasedOnType(templateName);
863        this.deleteMetadata(templateName, scope);
864    }
865
866    /**
867     * Deletes the metadata on this folder associated with a specified scope and template.
868     *
869     * @param templateName the metadata template type name.
870     * @param scope        the scope of the template (usually "global" or "enterprise").
871     */
872    public void deleteMetadata(String templateName, String scope) {
873        URL url = METADATA_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID(), scope, templateName);
874        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE");
875        BoxAPIResponse response = request.send();
876        response.disconnect();
877    }
878
879    /**
880     * Adds a metadata classification to the specified file.
881     *
882     * @param classificationType the metadata classification type.
883     * @return the metadata classification type added to the file.
884     */
885    public String addClassification(String classificationType) {
886        Metadata metadata = new Metadata().add(Metadata.CLASSIFICATION_KEY, classificationType);
887        Metadata classification = this.createMetadata(Metadata.CLASSIFICATION_TEMPLATE_KEY,
888                "enterprise", metadata);
889
890        return classification.getString(Metadata.CLASSIFICATION_KEY);
891    }
892
893    /**
894     * Updates a metadata classification on the specified file.
895     *
896     * @param classificationType the metadata classification type.
897     * @return the new metadata classification type updated on the file.
898     */
899    public String updateClassification(String classificationType) {
900        Metadata metadata = new Metadata("enterprise", Metadata.CLASSIFICATION_TEMPLATE_KEY);
901        metadata.replace(Metadata.CLASSIFICATION_KEY, classificationType);
902        Metadata classification = this.updateMetadata(metadata);
903
904        return classification.getString(Metadata.CLASSIFICATION_KEY);
905    }
906
907    /**
908     * Attempts to add classification to a file. If classification already exists then do update.
909     *
910     * @param classificationType the metadata classification type.
911     * @return the metadata classification type on the file.
912     */
913    public String setClassification(String classificationType) {
914        Metadata metadata = new Metadata().add(Metadata.CLASSIFICATION_KEY, classificationType);
915        Metadata classification = null;
916
917        try {
918            classification = this.createMetadata(Metadata.CLASSIFICATION_TEMPLATE_KEY, "enterprise", metadata);
919        } catch (BoxAPIException e) {
920            if (e.getResponseCode() == 409) {
921                metadata = new Metadata("enterprise", Metadata.CLASSIFICATION_TEMPLATE_KEY);
922                metadata.replace(Metadata.CLASSIFICATION_KEY, classificationType);
923                classification = this.updateMetadata(metadata);
924            } else {
925                throw e;
926            }
927        }
928
929        return classification.getString("/Box__Security__Classification__Key");
930    }
931
932    /**
933     * Gets the classification type for the specified file.
934     *
935     * @return the metadata classification type on the file.
936     */
937    public String getClassification() {
938        Metadata metadata = this.getMetadata(Metadata.CLASSIFICATION_TEMPLATE_KEY);
939        return metadata.getString(Metadata.CLASSIFICATION_KEY);
940    }
941
942    /**
943     * Deletes the classification on the file.
944     */
945    public void deleteClassification() {
946        this.deleteMetadata(Metadata.CLASSIFICATION_TEMPLATE_KEY, "enterprise");
947    }
948
949    /**
950     * Creates an upload session to create a new file in chunks.
951     * This will first verify that the file can be created and then open a session for uploading pieces of the file.
952     *
953     * @param fileName the name of the file to be created
954     * @param fileSize the size of the file that will be uploaded
955     * @return the created upload session instance
956     */
957    public BoxFileUploadSession.Info createUploadSession(String fileName, long fileSize) {
958
959        URL url = UPLOAD_SESSION_URL_TEMPLATE.build(this.getAPI().getBaseUploadURL());
960        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST");
961
962        JsonObject body = new JsonObject();
963        body.add("folder_id", this.getID());
964        body.add("file_name", fileName);
965        body.add("file_size", fileSize);
966        request.setBody(body.toString());
967
968        BoxJSONResponse response = (BoxJSONResponse) request.send();
969        JsonObject jsonObject = JsonObject.readFrom(response.getJSON());
970
971        String sessionId = jsonObject.get("id").asString();
972        BoxFileUploadSession session = new BoxFileUploadSession(this.getAPI(), sessionId);
973
974        return session.new Info(jsonObject);
975    }
976
977    /**
978     * Creates a new file.
979     *
980     * @param inputStream the stream instance that contains the data.
981     * @param fileName    the name of the file to be created.
982     * @param fileSize    the size of the file that will be uploaded.
983     * @return the created file instance.
984     * @throws InterruptedException when a thread execution is interrupted.
985     * @throws IOException          when reading a stream throws exception.
986     */
987    public BoxFile.Info uploadLargeFile(InputStream inputStream, String fileName, long fileSize)
988            throws InterruptedException, IOException {
989        URL url = UPLOAD_SESSION_URL_TEMPLATE.build(this.getAPI().getBaseUploadURL());
990        return new LargeFileUpload().
991                upload(this.getAPI(), this.getID(), inputStream, url, fileName, fileSize);
992    }
993
994    /**
995     * Creates a new file using specified number of parallel http connections.
996     *
997     * @param inputStream          the stream instance that contains the data.
998     * @param fileName             the name of the file to be created.
999     * @param fileSize             the size of the file that will be uploaded.
1000     * @param nParallelConnections number of parallel http connections to use
1001     * @param timeOut              time to wait before killing the job
1002     * @param unit                 time unit for the time wait value
1003     * @return the created file instance.
1004     * @throws InterruptedException when a thread execution is interrupted.
1005     * @throws IOException          when reading a stream throws exception.
1006     */
1007    public BoxFile.Info uploadLargeFile(InputStream inputStream, String fileName, long fileSize,
1008                                        int nParallelConnections, long timeOut, TimeUnit unit)
1009            throws InterruptedException, IOException {
1010        URL url = UPLOAD_SESSION_URL_TEMPLATE.build(this.getAPI().getBaseUploadURL());
1011        return new LargeFileUpload(nParallelConnections, timeOut, unit).
1012                upload(this.getAPI(), this.getID(), inputStream, url, fileName, fileSize);
1013    }
1014
1015    /**
1016     * Creates a new Metadata Cascade Policy on a folder.
1017     *
1018     * @param scope         the scope of the metadata cascade policy.
1019     * @param templateKey   the key of the template.
1020     * @return  information about the Metadata Cascade Policy.
1021     */
1022    public BoxMetadataCascadePolicy.Info addMetadataCascadePolicy(String scope, String templateKey) {
1023
1024        return BoxMetadataCascadePolicy.create(this.getAPI(), this.getID(), scope, templateKey);
1025    }
1026
1027    /**
1028     * Retrieves all Metadata Cascade Policies on a folder.
1029     *
1030     * @param fields            optional fields to retrieve for cascade policies.
1031     * @return  the Iterable of Box Metadata Cascade Policies in your enterprise.
1032     */
1033    public Iterable<BoxMetadataCascadePolicy.Info> getMetadataCascadePolicies(String... fields) {
1034        Iterable<BoxMetadataCascadePolicy.Info> cascadePoliciesInfo =
1035                BoxMetadataCascadePolicy.getAll(this.getAPI(), this.getID(), fields);
1036
1037        return cascadePoliciesInfo;
1038    }
1039
1040    /**
1041     * Retrieves all Metadata Cascade Policies on a folder.
1042     *
1043     * @param enterpriseID      the ID of the enterprise to retrieve cascade policies for.
1044     * @param limit             the number of entries of cascade policies to retrieve.
1045     * @param fields            optional fields to retrieve for cascade policies.
1046     * @return  the Iterable of Box Metadata Cascade Policies in your enterprise.
1047     */
1048    public Iterable<BoxMetadataCascadePolicy.Info> getMetadataCascadePolicies(String enterpriseID,
1049                                                                      int limit, String... fields) {
1050        Iterable<BoxMetadataCascadePolicy.Info> cascadePoliciesInfo =
1051                BoxMetadataCascadePolicy.getAll(this.getAPI(), this.getID(), enterpriseID, limit, fields);
1052
1053        return cascadePoliciesInfo;
1054    }
1055
1056    /**
1057     * Contains information about a BoxFolder.
1058     */
1059    public class Info extends BoxItem.Info {
1060        private BoxUploadEmail uploadEmail;
1061        private boolean hasCollaborations;
1062        private SyncState syncState;
1063        private EnumSet<Permission> permissions;
1064        private boolean canNonOwnersInvite;
1065        private boolean isWatermarked;
1066        private boolean isCollaborationRestrictedToEnterprise;
1067        private Map<String, Map<String, Metadata>> metadataMap;
1068
1069        /**
1070         * Constructs an empty Info object.
1071         */
1072        public Info() {
1073            super();
1074        }
1075
1076        /**
1077         * Constructs an Info object by parsing information from a JSON string.
1078         *
1079         * @param json the JSON string to parse.
1080         */
1081        public Info(String json) {
1082            super(json);
1083        }
1084
1085        /**
1086         * Constructs an Info object using an already parsed JSON object.
1087         *
1088         * @param jsonObject the parsed JSON object.
1089         */
1090        public Info(JsonObject jsonObject) {
1091            super(jsonObject);
1092        }
1093
1094        /**
1095         * Gets the upload email for the folder.
1096         *
1097         * @return the upload email for the folder.
1098         */
1099        public BoxUploadEmail getUploadEmail() {
1100            return this.uploadEmail;
1101        }
1102
1103        /**
1104         * Sets the upload email for the folder.
1105         *
1106         * @param uploadEmail the upload email for the folder.
1107         */
1108        public void setUploadEmail(BoxUploadEmail uploadEmail) {
1109            if (this.uploadEmail == uploadEmail) {
1110                return;
1111            }
1112
1113            this.removeChildObject("folder_upload_email");
1114            this.uploadEmail = uploadEmail;
1115
1116            if (uploadEmail == null) {
1117                this.addPendingChange("folder_upload_email", (String) null);
1118            } else {
1119                this.addChildObject("folder_upload_email", uploadEmail);
1120            }
1121        }
1122
1123        /**
1124         * Gets whether or not the folder has any collaborations.
1125         *
1126         * @return true if the folder has collaborations; otherwise false.
1127         */
1128        public boolean getHasCollaborations() {
1129            return this.hasCollaborations;
1130        }
1131
1132        /**
1133         * Gets the sync state of the folder.
1134         *
1135         * @return the sync state of the folder.
1136         */
1137        public SyncState getSyncState() {
1138            return this.syncState;
1139        }
1140
1141        /**
1142         * Sets the sync state of the folder.
1143         *
1144         * @param syncState the sync state of the folder.
1145         */
1146        public void setSyncState(SyncState syncState) {
1147            this.syncState = syncState;
1148            this.addPendingChange("sync_state", syncState.toJSONValue());
1149        }
1150
1151        /**
1152         * Gets the permissions that the current user has on the folder.
1153         *
1154         * @return the permissions that the current user has on the folder.
1155         */
1156        public EnumSet<Permission> getPermissions() {
1157            return this.permissions;
1158        }
1159
1160        /**
1161         * Gets whether or not the non-owners can invite collaborators to the folder.
1162         *
1163         * @return [description]
1164         */
1165        public boolean getCanNonOwnersInvite() {
1166            return this.canNonOwnersInvite;
1167        }
1168
1169        /**
1170         * Gets whether future collaborations should be restricted to within the enterprise only.
1171         *
1172         * @return indicates whether collaboration is restricted to enterprise only.
1173         */
1174        public boolean getIsCollaborationRestrictedToEnterprise() {
1175            return this.isCollaborationRestrictedToEnterprise;
1176        }
1177
1178        /**
1179         * Sets whether future collaborations should be restricted to within the enterprise only.
1180         *
1181         * @param isRestricted indicates whether there is collaboration restriction within enterprise.
1182         */
1183        public void setIsCollaborationRestrictedToEnterprise(boolean isRestricted) {
1184            this.isCollaborationRestrictedToEnterprise = isRestricted;
1185            this.addPendingChange("is_collaboration_restricted_to_enterprise", isRestricted);
1186        }
1187
1188        /**
1189         * Gets flag indicating whether this file is Watermarked.
1190         *
1191         * @return whether the file is watermarked or not
1192         */
1193        public boolean getIsWatermarked() {
1194            return this.isWatermarked;
1195        }
1196
1197        /**
1198         * Gets the metadata on this folder associated with a specified scope and template.
1199         * Makes an attempt to get metadata that was retrieved using getInfo(String ...) method. If no result is found
1200         * then makes an API call to get metadata
1201         *
1202         * @param templateName the metadata template type name.
1203         * @param scope        the scope of the template (usually "global" or "enterprise").
1204         * @return the metadata returned from the server.
1205         */
1206        public Metadata getMetadata(String templateName, String scope) {
1207            try {
1208                return this.metadataMap.get(scope).get(templateName);
1209            } catch (NullPointerException e) {
1210                return null;
1211            }
1212        }
1213
1214        @Override
1215        public BoxFolder getResource() {
1216            return BoxFolder.this;
1217        }
1218
1219        @Override
1220        protected void parseJSONMember(JsonObject.Member member) {
1221            super.parseJSONMember(member);
1222
1223            String memberName = member.getName();
1224            JsonValue value = member.getValue();
1225            if (memberName.equals("folder_upload_email")) {
1226                if (this.uploadEmail == null) {
1227                    this.uploadEmail = new BoxUploadEmail(value.asObject());
1228                } else {
1229                    this.uploadEmail.update(value.asObject());
1230                }
1231
1232            } else if (memberName.equals("has_collaborations")) {
1233                this.hasCollaborations = value.asBoolean();
1234
1235            } else if (memberName.equals("sync_state")) {
1236                this.syncState = SyncState.fromJSONValue(value.asString());
1237
1238            } else if (memberName.equals("permissions")) {
1239                this.permissions = this.parsePermissions(value.asObject());
1240
1241            } else if (memberName.equals("can_non_owners_invite")) {
1242                this.canNonOwnersInvite = value.asBoolean();
1243            } else if (memberName.equals("is_collaboration_restricted_to_enterprise")) {
1244                this.isCollaborationRestrictedToEnterprise = value.asBoolean();
1245
1246            } else if (memberName.equals("watermark_info")) {
1247                JsonObject jsonObject = value.asObject();
1248                this.isWatermarked = jsonObject.get("is_watermarked").asBoolean();
1249            } else if (memberName.equals("metadata")) {
1250                JsonObject jsonObject = value.asObject();
1251                this.metadataMap = Parsers.parseAndPopulateMetadataMap(jsonObject);
1252            }
1253        }
1254
1255        private EnumSet<Permission> parsePermissions(JsonObject jsonObject) {
1256            EnumSet<Permission> permissions = EnumSet.noneOf(Permission.class);
1257            for (JsonObject.Member member : jsonObject) {
1258                JsonValue value = member.getValue();
1259                if (value.isNull() || !value.asBoolean()) {
1260                    continue;
1261                }
1262
1263                String memberName = member.getName();
1264                if (memberName.equals("can_download")) {
1265                    permissions.add(Permission.CAN_DOWNLOAD);
1266                } else if (memberName.equals("can_upload")) {
1267                    permissions.add(Permission.CAN_UPLOAD);
1268                } else if (memberName.equals("can_rename")) {
1269                    permissions.add(Permission.CAN_RENAME);
1270                } else if (memberName.equals("can_delete")) {
1271                    permissions.add(Permission.CAN_DELETE);
1272                } else if (memberName.equals("can_share")) {
1273                    permissions.add(Permission.CAN_SHARE);
1274                } else if (memberName.equals("can_invite_collaborator")) {
1275                    permissions.add(Permission.CAN_INVITE_COLLABORATOR);
1276                } else if (memberName.equals("can_set_share_access")) {
1277                    permissions.add(Permission.CAN_SET_SHARE_ACCESS);
1278                }
1279            }
1280
1281            return permissions;
1282        }
1283    }
1284
1285    /**
1286     * Enumerates the possible sync states that a folder can have.
1287     */
1288    public enum SyncState {
1289        /**
1290         * The folder is synced.
1291         */
1292        SYNCED("synced"),
1293
1294        /**
1295         * The folder is not synced.
1296         */
1297        NOT_SYNCED("not_synced"),
1298
1299        /**
1300         * The folder is partially synced.
1301         */
1302        PARTIALLY_SYNCED("partially_synced");
1303
1304        private final String jsonValue;
1305
1306        private SyncState(String jsonValue) {
1307            this.jsonValue = jsonValue;
1308        }
1309
1310        static SyncState fromJSONValue(String jsonValue) {
1311            return SyncState.valueOf(jsonValue.toUpperCase());
1312        }
1313
1314        String toJSONValue() {
1315            return this.jsonValue;
1316        }
1317    }
1318
1319    /**
1320     * Enumerates the possible permissions that a user can have on a folder.
1321     */
1322    public enum Permission {
1323        /**
1324         * The user can download the folder.
1325         */
1326        CAN_DOWNLOAD("can_download"),
1327
1328        /**
1329         * The user can upload to the folder.
1330         */
1331        CAN_UPLOAD("can_upload"),
1332
1333        /**
1334         * The user can rename the folder.
1335         */
1336        CAN_RENAME("can_rename"),
1337
1338        /**
1339         * The user can delete the folder.
1340         */
1341        CAN_DELETE("can_delete"),
1342
1343        /**
1344         * The user can share the folder.
1345         */
1346        CAN_SHARE("can_share"),
1347
1348        /**
1349         * The user can invite collaborators to the folder.
1350         */
1351        CAN_INVITE_COLLABORATOR("can_invite_collaborator"),
1352
1353        /**
1354         * The user can set the access level for shared links to the folder.
1355         */
1356        CAN_SET_SHARE_ACCESS("can_set_share_access");
1357
1358        private final String jsonValue;
1359
1360        private Permission(String jsonValue) {
1361            this.jsonValue = jsonValue;
1362        }
1363
1364        static Permission fromJSONValue(String jsonValue) {
1365            return Permission.valueOf(jsonValue.toUpperCase());
1366        }
1367
1368        String toJSONValue() {
1369            return this.jsonValue;
1370        }
1371    }
1372}