001package com.box.sdk;
002
003import java.net.URL;
004import java.util.Iterator;
005
006import com.eclipsesource.json.JsonObject;
007
008/**
009 * Represents a set of Box users.
010 */
011public class BoxGroup extends BoxCollaborator {
012    private static final URLTemplate GROUPS_URL_TEMPLATE = new URLTemplate("groups");
013    private static final URLTemplate GROUP_URL_TEMPLATE = new URLTemplate("groups/%s");
014
015    /**
016     * Constructs a BoxGroup for a group with a given ID.
017     * @param  api the API connection to be used by the group.
018     * @param  id  the ID of the group.
019     */
020    public BoxGroup(BoxAPIConnection api, String id) {
021        super(api, id);
022    }
023
024    /**
025     * Creates a new group with a specified name.
026     * @param  api  the API connection to be used by the group.
027     * @param  name the name of the new group.
028     * @return      info about the created group.
029     */
030    public static BoxGroup.Info createGroup(BoxAPIConnection api, String name) {
031        JsonObject requestJSON = new JsonObject();
032        requestJSON.add("name", name);
033
034        URL url = GROUPS_URL_TEMPLATE.build(api.getBaseURL());
035        BoxJSONRequest request = new BoxJSONRequest(api, url, "GET");
036        request.setBody(requestJSON.toString());
037        BoxJSONResponse response = (BoxJSONResponse) request.send();
038        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
039
040        BoxGroup group = new BoxGroup(api, responseJSON.get("id").asString());
041        return group.new Info(responseJSON);
042    }
043
044    /**
045     * Gets an iterable of all the groups that the current user is a member of.
046     * @param  api the API connection to be used when retrieving the groups.
047     * @return     an iterable containing info about all the groups.
048     */
049    public static Iterable<BoxGroup.Info> getAllGroups(final BoxAPIConnection api) {
050        return new Iterable<BoxGroup.Info>() {
051            public Iterator<BoxGroup.Info> iterator() {
052                URL url = GROUPS_URL_TEMPLATE.build(api.getBaseURL());
053                return new BoxGroupIterator(api, url);
054            }
055        };
056    }
057
058    /**
059     * Deletes this group.
060     */
061    public void delete() {
062        URL url = GROUP_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
063        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE");
064        BoxAPIResponse response = request.send();
065        response.disconnect();
066    }
067
068    /**
069     * Contains information about a BoxGroup.
070     */
071    public class Info extends BoxCollaborator.Info {
072        /**
073         * Constructs an empty Info object.
074         */
075        public Info() {
076            super();
077        }
078
079        /**
080         * Constructs an Info object by parsing information from a JSON string.
081         * @param  json the JSON string to parse.
082         */
083        public Info(String json) {
084            super(json);
085        }
086
087        /**
088         * Constructs an Info object using an already parsed JSON object.
089         * @param  jsonObject the parsed JSON object.
090         */
091        Info(JsonObject jsonObject) {
092            super(jsonObject);
093        }
094
095        @Override
096        public BoxGroup getResource() {
097            return BoxGroup.this;
098        }
099    }
100}