001package com.box.sdk; 002 003import java.net.URL; 004import java.util.ArrayList; 005import java.util.Collection; 006import java.util.Iterator; 007 008import com.box.sdk.BoxGroupMembership.Role; 009import com.eclipsesource.json.JsonArray; 010import com.eclipsesource.json.JsonObject; 011import com.eclipsesource.json.JsonValue; 012 013/** 014 * Represents a set of Box users. 015 * 016 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked 017 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error 018 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p> 019 */ 020public class BoxGroup extends BoxCollaborator { 021 private static final URLTemplate GROUPS_URL_TEMPLATE = new URLTemplate("groups"); 022 private static final URLTemplate GROUP_URL_TEMPLATE = new URLTemplate("groups/%s"); 023 private static final URLTemplate MEMBERSHIPS_URL_TEMPLATE = new URLTemplate("groups/%s/memberships"); 024 private static final URLTemplate ADD_MEMBERSHIP_URL_TEMPLATE = new URLTemplate("group_memberships"); 025 026 /** 027 * Constructs a BoxGroup for a group with a given ID. 028 * @param api the API connection to be used by the group. 029 * @param id the ID of the group. 030 */ 031 public BoxGroup(BoxAPIConnection api, String id) { 032 super(api, id); 033 } 034 035 /** 036 * Creates a new group with a specified name. 037 * @param api the API connection to be used by the group. 038 * @param name the name of the new group. 039 * @return info about the created group. 040 */ 041 public static BoxGroup.Info createGroup(BoxAPIConnection api, String name) { 042 JsonObject requestJSON = new JsonObject(); 043 requestJSON.add("name", name); 044 045 URL url = GROUPS_URL_TEMPLATE.build(api.getBaseURL()); 046 BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); 047 request.setBody(requestJSON.toString()); 048 BoxJSONResponse response = (BoxJSONResponse) request.send(); 049 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 050 051 BoxGroup group = new BoxGroup(api, responseJSON.get("id").asString()); 052 return group.new Info(responseJSON); 053 } 054 055 /** 056 * Gets an iterable of all the groups that the current user is a member of. 057 * @param api the API connection to be used when retrieving the groups. 058 * @return an iterable containing info about all the groups. 059 */ 060 public static Iterable<BoxGroup.Info> getAllGroups(final BoxAPIConnection api) { 061 return new Iterable<BoxGroup.Info>() { 062 public Iterator<BoxGroup.Info> iterator() { 063 URL url = GROUPS_URL_TEMPLATE.build(api.getBaseURL()); 064 return new BoxGroupIterator(api, url); 065 } 066 }; 067 } 068 069 /** 070 * Gets information about this group. 071 * @return info about this group. 072 */ 073 public Info getInfo() { 074 URL url = GROUP_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 075 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 076 BoxJSONResponse response = (BoxJSONResponse) request.send(); 077 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 078 return new Info(responseJSON); 079 } 080 081 /** 082 * Gets information about all of the group memberships for this group. 083 * @return a collection of information about the group memberships for this group. 084 */ 085 public Collection<BoxGroupMembership.Info> getMemberships() { 086 BoxAPIConnection api = this.getAPI(); 087 URL url = MEMBERSHIPS_URL_TEMPLATE.build(api.getBaseURL(), this.getID()); 088 089 BoxAPIRequest request = new BoxAPIRequest(api, url, "GET"); 090 BoxJSONResponse response = (BoxJSONResponse) request.send(); 091 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 092 093 int entriesCount = responseJSON.get("total_count").asInt(); 094 Collection<BoxGroupMembership.Info> memberships = new ArrayList<BoxGroupMembership.Info>(entriesCount); 095 JsonArray entries = responseJSON.get("entries").asArray(); 096 for (JsonValue entry : entries) { 097 JsonObject entryObject = entry.asObject(); 098 BoxGroupMembership membership = new BoxGroupMembership(api, entryObject.get("id").asString()); 099 BoxGroupMembership.Info info = membership.new Info(entryObject); 100 memberships.add(info); 101 } 102 103 return memberships; 104 } 105 106 /** 107 * Adds a member to this group with the default role. 108 * @param user the member to be added to this group. 109 * @return info about the new group membership. 110 */ 111 public BoxGroupMembership.Info addMembership(BoxUser user) { 112 return this.addMembership(user, null); 113 } 114 115 /** 116 * Adds a member to this group with the specified role. 117 * @param user the member to be added to this group. 118 * @param role the role of the user in this group. Can be null to assign the default role. 119 * @return info about the new group membership. 120 */ 121 public BoxGroupMembership.Info addMembership(BoxUser user, Role role) { 122 BoxAPIConnection api = this.getAPI(); 123 124 JsonObject requestJSON = new JsonObject(); 125 requestJSON.add("user", new JsonObject().add("id", user.getID())); 126 requestJSON.add("group", new JsonObject().add("id", this.getID())); 127 if (role != null) { 128 requestJSON.add("role", role.toJSONString()); 129 } 130 131 URL url = ADD_MEMBERSHIP_URL_TEMPLATE.build(api.getBaseURL()); 132 BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); 133 request.setBody(requestJSON.toString()); 134 BoxJSONResponse response = (BoxJSONResponse) request.send(); 135 JsonObject responseJSON = JsonObject.readFrom(response.getJSON()); 136 137 BoxGroupMembership membership = new BoxGroupMembership(api, responseJSON.get("id").asString()); 138 return membership.new Info(responseJSON); 139 } 140 141 /** 142 * Deletes this group. 143 */ 144 public void delete() { 145 URL url = GROUP_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 146 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE"); 147 BoxAPIResponse response = request.send(); 148 response.disconnect(); 149 } 150 151 /** 152 * Contains information about a BoxGroup. 153 */ 154 public class Info extends BoxCollaborator.Info { 155 /** 156 * Constructs an empty Info object. 157 */ 158 public Info() { 159 super(); 160 } 161 162 /** 163 * Constructs an Info object by parsing information from a JSON string. 164 * @param json the JSON string to parse. 165 */ 166 public Info(String json) { 167 super(json); 168 } 169 170 /** 171 * Constructs an Info object using an already parsed JSON object. 172 * @param jsonObject the parsed JSON object. 173 */ 174 Info(JsonObject jsonObject) { 175 super(jsonObject); 176 } 177 178 @Override 179 public BoxGroup getResource() { 180 return BoxGroup.this; 181 } 182 } 183}