Skip to content

Commit 46b1291

Browse files
committed
Merge pull request #111 from sohtsuka/feature/group_memberships
Add membership related features
2 parents 76606b5 + 5f4cf22 commit 46b1291

File tree

6 files changed

+550
-0
lines changed

6 files changed

+550
-0
lines changed

src/main/java/com/box/sdk/BoxGroup.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.box.sdk;
22

33
import java.net.URL;
4+
import java.util.ArrayList;
5+
import java.util.Collection;
46
import java.util.Iterator;
57

8+
import com.box.sdk.BoxGroupMembership.Role;
9+
import com.eclipsesource.json.JsonArray;
610
import com.eclipsesource.json.JsonObject;
11+
import com.eclipsesource.json.JsonValue;
712

813
/**
914
* Represents a set of Box users.
@@ -15,6 +20,8 @@
1520
public class BoxGroup extends BoxCollaborator {
1621
private static final URLTemplate GROUPS_URL_TEMPLATE = new URLTemplate("groups");
1722
private static final URLTemplate GROUP_URL_TEMPLATE = new URLTemplate("groups/%s");
23+
private static final URLTemplate MEMBERSHIPS_URL_TEMPLATE = new URLTemplate("groups/%s/memberships");
24+
private static final URLTemplate ADD_MEMBERSHIP_URL_TEMPLATE = new URLTemplate("group_memberships");
1825

1926
/**
2027
* Constructs a BoxGroup for a group with a given ID.
@@ -71,6 +78,66 @@ public Info getInfo() {
7178
return new Info(responseJSON);
7279
}
7380

81+
/**
82+
* Gets information about all of the group memberships for this group.
83+
* @return a collection of information about the group memberships for this group.
84+
*/
85+
public Collection<BoxGroupMembership.Info> getMemberships() {
86+
BoxAPIConnection api = this.getAPI();
87+
URL url = MEMBERSHIPS_URL_TEMPLATE.build(api.getBaseURL(), this.getID());
88+
89+
BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
90+
BoxJSONResponse response = (BoxJSONResponse) request.send();
91+
JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
92+
93+
int entriesCount = responseJSON.get("total_count").asInt();
94+
Collection<BoxGroupMembership.Info> memberships = new ArrayList<BoxGroupMembership.Info>(entriesCount);
95+
JsonArray entries = responseJSON.get("entries").asArray();
96+
for (JsonValue entry : entries) {
97+
JsonObject entryObject = entry.asObject();
98+
BoxGroupMembership membership = new BoxGroupMembership(api, entryObject.get("id").asString());
99+
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+
74141
/**
75142
* Deletes this group.
76143
*/
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
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+
}

src/main/java/com/box/sdk/BoxUser.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class BoxUser extends BoxCollaborator {
3131
private static final URLTemplate USER_URL_TEMPLATE = new URLTemplate("users/%s");
3232
private static final URLTemplate GET_ME_URL = new URLTemplate("users/me");
3333
private static final URLTemplate USERS_URL_TEMPLATE = new URLTemplate("users");
34+
private static final URLTemplate USER_MEMBERSHIPS_URL_TEMPLATE = new URLTemplate("users/%s/memberships");
3435
private static final URLTemplate EMAIL_ALIAS_URL_TEMPLATE = new URLTemplate("users/%s/email_aliases/%s");
3536
private static final URLTemplate EMAIL_ALIASES_URL_TEMPLATE = new URLTemplate("users/%s/email_aliases");
3637

@@ -167,6 +168,34 @@ public BoxUser.Info getInfo(String... fields) {
167168
return new Info(jsonObject);
168169
}
169170

171+
/**
172+
* Gets information about all of the group memberships for this user.
173+
*
174+
* <p>Note: This method is only available to enterprise admins.</p>
175+
*
176+
* @return a collection of information about the group memberships for this user.
177+
*/
178+
public Collection<BoxGroupMembership.Info> getMemberships() {
179+
BoxAPIConnection api = this.getAPI();
180+
URL url = USER_MEMBERSHIPS_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
181+
182+
BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
183+
BoxJSONResponse response = (BoxJSONResponse) request.send();
184+
JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
185+
186+
int entriesCount = responseJSON.get("total_count").asInt();
187+
Collection<BoxGroupMembership.Info> memberships = new ArrayList<BoxGroupMembership.Info>(entriesCount);
188+
JsonArray entries = responseJSON.get("entries").asArray();
189+
for (JsonValue entry : entries) {
190+
JsonObject entryObject = entry.asObject();
191+
BoxGroupMembership membership = new BoxGroupMembership(api, entryObject.get("id").asString());
192+
BoxGroupMembership.Info info = membership.new Info(entryObject);
193+
memberships.add(info);
194+
}
195+
196+
return memberships;
197+
}
198+
170199
/**
171200
* Adds a new email alias to this user's account.
172201
* @param email the email address to add as an alias.

0 commit comments

Comments
 (0)