|
| 1 | +package com.box.sdk; |
| 2 | + |
| 3 | +import java.net.URL; |
| 4 | +import java.text.ParseException; |
| 5 | +import java.util.Date; |
| 6 | + |
| 7 | +import com.eclipsesource.json.JsonObject; |
| 8 | +import com.eclipsesource.json.JsonValue; |
| 9 | + |
| 10 | +/** |
| 11 | + * Represents a relationship between a user and a group. |
| 12 | + * |
| 13 | + * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked |
| 14 | + * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error |
| 15 | + * handling for errors related to the Box REST API, you should capture this exception explicitly.</p> |
| 16 | + */ |
| 17 | +public class BoxGroupMembership extends BoxResource { |
| 18 | + private static final URLTemplate MEMBERSHIP_URL_TEMPLATE = new URLTemplate("group_memberships/%s"); |
| 19 | + |
| 20 | + /** |
| 21 | + * Constructs a BoxGroupMembership for a group membership with a given ID. |
| 22 | + * @param api the API connection to be used by the group membership. |
| 23 | + * @param id the ID of the group membership. |
| 24 | + */ |
| 25 | + public BoxGroupMembership(BoxAPIConnection api, String id) { |
| 26 | + super(api, id); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Gets information about this group membership. |
| 31 | + * @return info about this group membership. |
| 32 | + */ |
| 33 | + public Info getInfo() { |
| 34 | + BoxAPIConnection api = this.getAPI(); |
| 35 | + URL url = MEMBERSHIP_URL_TEMPLATE.build(api.getBaseURL(), this.getID()); |
| 36 | + |
| 37 | + BoxAPIRequest request = new BoxAPIRequest(api, url, "GET"); |
| 38 | + BoxJSONResponse response = (BoxJSONResponse) request.send(); |
| 39 | + JsonObject jsonObject = JsonObject.readFrom(response.getJSON()); |
| 40 | + return new Info(jsonObject); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Updates the information about this group membership with any info fields that have been modified locally. |
| 45 | + * @param info the updated info. |
| 46 | + */ |
| 47 | + public void updateInfo(Info info) { |
| 48 | + BoxAPIConnection api = this.getAPI(); |
| 49 | + URL url = MEMBERSHIP_URL_TEMPLATE.build(api.getBaseURL(), this.getID()); |
| 50 | + |
| 51 | + BoxJSONRequest request = new BoxJSONRequest(api, url, "PUT"); |
| 52 | + request.setBody(info.getPendingChanges()); |
| 53 | + BoxJSONResponse response = (BoxJSONResponse) request.send(); |
| 54 | + JsonObject jsonObject = JsonObject.readFrom(response.getJSON()); |
| 55 | + info.update(jsonObject); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Deletes this group membership. |
| 60 | + */ |
| 61 | + public void delete() { |
| 62 | + BoxAPIConnection api = this.getAPI(); |
| 63 | + URL url = MEMBERSHIP_URL_TEMPLATE.build(api.getBaseURL(), this.getID()); |
| 64 | + |
| 65 | + BoxAPIRequest request = new BoxAPIRequest(api, url, "DELETE"); |
| 66 | + BoxAPIResponse response = request.send(); |
| 67 | + response.disconnect(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Contains information about a BoxGroupMembership. |
| 72 | + */ |
| 73 | + public class Info extends BoxResource.Info { |
| 74 | + private BoxUser.Info user; |
| 75 | + private BoxGroup.Info group; |
| 76 | + private Role role; |
| 77 | + private Date createdAt; |
| 78 | + private Date modifiedAt; |
| 79 | + |
| 80 | + /** |
| 81 | + * Constructs an empty Info object. |
| 82 | + */ |
| 83 | + public Info() { |
| 84 | + super(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Constructs an Info object by parsing information from a JSON string. |
| 89 | + * @param json the JSON string to parse. |
| 90 | + */ |
| 91 | + public Info(String json) { |
| 92 | + super(json); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Constructs an Info object using an already parsed JSON object. |
| 97 | + * @param jsonObject the parsed JSON object. |
| 98 | + */ |
| 99 | + Info(JsonObject jsonObject) { |
| 100 | + super(jsonObject); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets the user belonging to the group. |
| 105 | + * |
| 106 | + * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login fields |
| 107 | + * populated.</p> |
| 108 | + * |
| 109 | + * @return the user belonging to the group. |
| 110 | + */ |
| 111 | + public BoxUser.Info getUser() { |
| 112 | + return this.user; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Gets the group the user belongs to. |
| 117 | + * |
| 118 | + * <p>Note: the BoxGroup.Info returned by this method will only have the ID and name fields populated.</p> |
| 119 | + * |
| 120 | + * @return the group the user belongs to. |
| 121 | + */ |
| 122 | + public BoxGroup.Info getGroup() { |
| 123 | + return this.group; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Gets the level of access the user has. |
| 128 | + * @return the level of access the user has. |
| 129 | + */ |
| 130 | + public Role getRole() { |
| 131 | + return this.role; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Sets the level of access the user has. |
| 136 | + * @param role the new level of access to give the user. |
| 137 | + */ |
| 138 | + public void setRole(Role role) { |
| 139 | + this.role = role; |
| 140 | + this.addPendingChange("role", role.toJSONString()); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Gets the time the group membership was created. |
| 145 | + * @return the time the group membership was created. |
| 146 | + */ |
| 147 | + public Date getCreatedAt() { |
| 148 | + return this.createdAt; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Gets the time the group membership was last modified. |
| 153 | + * @return the time the group membership was last modified. |
| 154 | + */ |
| 155 | + public Date getModifiedAt() { |
| 156 | + return this.modifiedAt; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public BoxGroupMembership getResource() { |
| 161 | + return BoxGroupMembership.this; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + protected void parseJSONMember(JsonObject.Member member) { |
| 166 | + super.parseJSONMember(member); |
| 167 | + |
| 168 | + String memberName = member.getName(); |
| 169 | + JsonValue value = member.getValue(); |
| 170 | + |
| 171 | + try { |
| 172 | + if (memberName.equals("user")) { |
| 173 | + JsonObject userJSON = value.asObject(); |
| 174 | + if (this.user == null) { |
| 175 | + String userID = userJSON.get("id").asString(); |
| 176 | + BoxUser user = new BoxUser(getAPI(), userID); |
| 177 | + this.user = user.new Info(userJSON); |
| 178 | + } else { |
| 179 | + this.user.update(userJSON); |
| 180 | + } |
| 181 | + |
| 182 | + } else if (memberName.equals("group")) { |
| 183 | + JsonObject groupJSON = value.asObject(); |
| 184 | + if (this.group == null) { |
| 185 | + String userID = groupJSON.get("id").asString(); |
| 186 | + BoxGroup group = new BoxGroup(getAPI(), userID); |
| 187 | + this.group = group.new Info(groupJSON); |
| 188 | + } else { |
| 189 | + this.group.update(groupJSON); |
| 190 | + } |
| 191 | + |
| 192 | + } else if (memberName.equals("role")) { |
| 193 | + this.role = Role.fromJSONString(value.asString()); |
| 194 | + |
| 195 | + } else if (memberName.equals("created_at")) { |
| 196 | + this.createdAt = BoxDateFormat.parse(value.asString()); |
| 197 | + |
| 198 | + } else if (memberName.equals("modified_at")) { |
| 199 | + this.modifiedAt = BoxDateFormat.parse(value.asString()); |
| 200 | + |
| 201 | + } |
| 202 | + } catch (ParseException e) { |
| 203 | + assert false : "A ParseException indicates a bug in the SDK."; |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Enumerates the possible roles that a user can have within a group. |
| 210 | + */ |
| 211 | + public enum Role { |
| 212 | + /** |
| 213 | + * The user is an administrator in the group. |
| 214 | + */ |
| 215 | + ADMIN ("admin"), |
| 216 | + |
| 217 | + /** |
| 218 | + * The user is a regular member in the group. |
| 219 | + */ |
| 220 | + MEMBER ("member"); |
| 221 | + |
| 222 | + private final String jsonValue; |
| 223 | + |
| 224 | + private Role(String jsonValue) { |
| 225 | + this.jsonValue = jsonValue; |
| 226 | + } |
| 227 | + |
| 228 | + static Role fromJSONString(String jsonValue) { |
| 229 | + return Role.valueOf(jsonValue.toUpperCase()); |
| 230 | + } |
| 231 | + |
| 232 | + String toJSONString() { |
| 233 | + return this.jsonValue; |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments