Skip to content

Commit f711b0b

Browse files
committed
Update JSON handling so child objects get updated
This change allows for child objects of JSON objects to be changed and have those changes tracked by the parent. For example: BoxSharedLink sharedLink = file.createSharedLink(...); sharedLink.getPermissions().setAccess(BoxSharedLink.Access.OPEN); BoxFile.Info info = file.new Info(); info.setSharedLink(sharedLink); file.updateInfo(info); The new permissions will be correctly sent to the API even though they're a child object and weren't explicitly set with `setPermissions()`.
1 parent 1664546 commit f711b0b

File tree

7 files changed

+101
-41
lines changed

7 files changed

+101
-41
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,13 @@ protected void parseJSONMember(JsonObject.Member member) {
145145
switch (memberName) {
146146
case "created_by":
147147
JsonObject userJSON = value.asObject();
148-
String userID = userJSON.get("id").asString();
149-
BoxUser user = new BoxUser(getAPI(), userID);
150-
this.createdBy = user.new Info(userJSON);
148+
if (this.createdBy == null) {
149+
String userID = userJSON.get("id").asString();
150+
BoxUser user = new BoxUser(getAPI(), userID);
151+
this.createdBy = user.new Info(userJSON);
152+
} else {
153+
this.createdBy.update(userJSON);
154+
}
151155
break;
152156
case "created_at":
153157
this.createdAt = BoxDateParser.parse(value.asString());
@@ -164,10 +168,14 @@ protected void parseJSONMember(JsonObject.Member member) {
164168
break;
165169
case "accessible_by":
166170
userJSON = value.asObject();
167-
userID = userJSON.get("id").asString();
168-
user = new BoxUser(getAPI(), userID);
169-
BoxUser.Info userInfo = user.new Info(userJSON);
170-
this.accessibleBy = userInfo;
171+
if (this.accessibleBy == null) {
172+
String userID = userJSON.get("id").asString();
173+
BoxUser user = new BoxUser(getAPI(), userID);
174+
BoxUser.Info userInfo = user.new Info(userJSON);
175+
this.accessibleBy = userInfo;
176+
} else {
177+
this.accessibleBy.update(userJSON);
178+
}
171179
break;
172180
case "role":
173181
this.role = Role.fromJSONString(value.asString());
@@ -177,9 +185,13 @@ protected void parseJSONMember(JsonObject.Member member) {
177185
break;
178186
case "item":
179187
JsonObject folderJSON = value.asObject();
180-
String folderID = folderJSON.get("id").asString();
181-
BoxFolder folder = new BoxFolder(getAPI(), folderID);
182-
this.item = folder.new Info(folderJSON);
188+
if (this.item == null) {
189+
String folderID = folderJSON.get("id").asString();
190+
BoxFolder folder = new BoxFolder(getAPI(), folderID);
191+
this.item = folder.new Info(folderJSON);
192+
} else {
193+
this.item.update(folderJSON);
194+
}
183195
break;
184196
default:
185197
break;

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,18 @@ public BoxUploadEmail getUploadEmail() {
371371
}
372372

373373
public void setUploadEmail(BoxUploadEmail uploadEmail) {
374+
if (this.uploadEmail == uploadEmail) {
375+
return;
376+
}
377+
378+
this.removeChildObject("folder_upload_email");
374379
this.uploadEmail = uploadEmail;
375-
this.addPendingChange("folder_upload_email", uploadEmail);
380+
381+
if (uploadEmail == null) {
382+
this.addPendingChange("folder_upload_email", null);
383+
} else {
384+
this.addChildObject("folder_upload_email", uploadEmail);
385+
}
376386
}
377387

378388
@Override
@@ -388,7 +398,11 @@ protected void parseJSONMember(JsonObject.Member member) {
388398
JsonValue value = member.getValue();
389399
switch (memberName) {
390400
case "folder_upload_email":
391-
this.uploadEmail = new BoxUploadEmail(value.asObject());
401+
if (this.uploadEmail == null) {
402+
this.uploadEmail = new BoxUploadEmail(value.asObject());
403+
} else {
404+
this.uploadEmail.update(value.asObject());
405+
}
392406
break;
393407
default:
394408
break;

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,13 @@ public BoxSharedLink getSharedLink() {
255255
* @param sharedLink the shared link for the item.
256256
*/
257257
public void setSharedLink(BoxSharedLink sharedLink) {
258+
if (this.sharedLink == sharedLink) {
259+
return;
260+
}
261+
262+
this.removeChildObject("shared_link");
258263
this.sharedLink = sharedLink;
259-
this.addPendingChange("shared_link", sharedLink);
264+
this.addChildObject("shared_link", sharedLink);
260265
}
261266

262267
/**
@@ -340,16 +345,24 @@ protected void parseJSONMember(JsonObject.Member member) {
340345
this.ownedBy = this.parseUserInfo(value.asObject());
341346
break;
342347
case "shared_link":
343-
this.sharedLink = new BoxSharedLink(value.asObject());
348+
if (this.sharedLink == null) {
349+
this.setSharedLink(new BoxSharedLink(value.asObject()));
350+
} else {
351+
this.sharedLink.update(value.asObject());
352+
}
344353
break;
345354
case "tags":
346355
this.tags = this.parseTags(value.asArray());
347356
break;
348357
case "parent":
349358
JsonObject jsonObject = value.asObject();
350-
String id = jsonObject.get("id").asString();
351-
BoxFolder parentFolder = new BoxFolder(getAPI(), id);
352-
this.parent = parentFolder.new Info(jsonObject);
359+
if (this.parent == null) {
360+
String id = jsonObject.get("id").asString();
361+
BoxFolder parentFolder = new BoxFolder(getAPI(), id);
362+
this.parent = parentFolder.new Info(jsonObject);
363+
} else {
364+
this.parent.update(jsonObject);
365+
}
353366
break;
354367
default:
355368
break;

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public abstract class BoxJSONObject {
2424
* This allows changes to be made to a child BoxJSONObject and still have those changes reflected in the JSON
2525
* string.
2626
*/
27-
private Map<String, BoxJSONObject> lazyPendingChanges;
27+
private final Map<String, BoxJSONObject> children;
2828

2929
/**
3030
* Constructs an empty BoxJSONObject.
3131
*/
3232
public BoxJSONObject() {
33-
this.lazyPendingChanges = new HashMap<String, BoxJSONObject>();
33+
this.children = new HashMap<String, BoxJSONObject>();
3434
}
3535

3636
/**
@@ -56,7 +56,6 @@ public BoxJSONObject(String json) {
5656
*/
5757
public void clearPendingChanges() {
5858
this.pendingChanges = null;
59-
this.lazyPendingChanges.clear();
6059
}
6160

6261
/**
@@ -106,14 +105,16 @@ void addPendingChange(String key, String value) {
106105
this.addPendingChange(key, JsonValue.valueOf(value));
107106
}
108107

109-
/**
110-
* Adds a pending field change that needs to be sent to the API. It will be included in the JSON string the next
111-
* time {@link #getPendingChanges} is called.
112-
* @param key the name of the field.
113-
* @param value the new BoxJSONObject value of the field.
114-
*/
115-
void addPendingChange(String key, BoxJSONObject value) {
116-
this.lazyPendingChanges.put(key, value);
108+
void addChildObject(String fieldName, BoxJSONObject child) {
109+
if (child == null) {
110+
this.addPendingChange(fieldName, JsonValue.NULL);
111+
} else {
112+
this.children.put(fieldName, child);
113+
}
114+
}
115+
116+
void removeChildObject(String fieldName) {
117+
this.children.remove(fieldName);
117118
}
118119

119120
/**
@@ -151,12 +152,16 @@ void update(JsonObject jsonObject) {
151152
* @return a JsonObject containing the pending changes.
152153
*/
153154
private JsonObject getPendingJSONObject() {
154-
if (this.pendingChanges == null && !this.lazyPendingChanges.isEmpty()) {
155-
this.pendingChanges = new JsonObject();
156-
}
157-
158-
for (Map.Entry<String, BoxJSONObject> entry : this.lazyPendingChanges.entrySet()) {
159-
this.pendingChanges.set(entry.getKey(), entry.getValue().getPendingJSONObject());
155+
for (Map.Entry<String, BoxJSONObject> entry : this.children.entrySet()) {
156+
BoxJSONObject child = entry.getValue();
157+
JsonObject jsonObject = child.getPendingJSONObject();
158+
if (jsonObject != null) {
159+
if (this.pendingChanges == null) {
160+
this.pendingChanges = new JsonObject();
161+
}
162+
163+
this.pendingChanges.set(entry.getKey(), jsonObject);
164+
}
160165
}
161166
return this.pendingChanges;
162167
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ public Permissions getPermissions() {
101101
}
102102

103103
public void setPermissions(Permissions permissions) {
104+
if (this.permissions == permissions) {
105+
return;
106+
}
107+
108+
this.removeChildObject("permissions");
104109
this.permissions = permissions;
105-
this.addPendingChange("permissions", permissions);
110+
this.addChildObject("permissions", permissions);
106111
}
107112

108113
@Override
@@ -137,7 +142,7 @@ void parseJSONMember(JsonObject.Member member) {
137142
break;
138143
case "permissions":
139144
if (this.permissions == null) {
140-
this.permissions = new Permissions(value.asObject());
145+
this.setPermissions(new Permissions(value.asObject()));
141146
} else {
142147
this.permissions.update(value.asObject());
143148
}

src/test/java/com/box/sdk/BoxFileTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public void uploadAndDownloadMultipleVersionsSucceeds() throws UnsupportedEncodi
8989
ProgressListener mockDownloadListener = mock(ProgressListener.class);
9090
previousVersion.download(downloadStream, mockDownloadListener);
9191
String downloadedContent = downloadStream.toString(StandardCharsets.UTF_8.name());
92+
System.out.println(downloadedContent);
9293

9394
assertThat(versions, hasSize(1));
9495
assertThat(previousVersion.getSha1(), is(equalTo(version1Sha)));
@@ -135,9 +136,8 @@ public void updateFileInfoSucceeds() {
135136
BoxFile.Info newInfo = uploadedFile.new Info();
136137
newInfo.setName(newFileName);
137138
uploadedFile.updateInfo(newInfo);
138-
BoxFile.Info refreshedInfo = uploadedFile.getInfo();
139139

140-
assertThat(refreshedInfo.getName(), is(equalTo(newFileName)));
140+
assertThat(newInfo.getName(), is(equalTo(newFileName)));
141141

142142
uploadedFile.delete();
143143
}
@@ -217,10 +217,10 @@ public void copyFileSucceeds() throws UnsupportedEncodingException {
217217

218218
@Test
219219
@Category(IntegrationTest.class)
220-
public void createSharedLinkSucceeds() {
220+
public void createAndUpdateSharedLinkSucceeds() {
221221
BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
222222
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
223-
String fileName = "[createSharedLinkSucceeds] Test File.txt";
223+
String fileName = "[createAndUpdateSharedLinkSucceeds] Test File.txt";
224224
byte[] fileBytes = "Non-empty string".getBytes(StandardCharsets.UTF_8);
225225

226226
InputStream uploadStream = new ByteArrayInputStream(fileBytes);
@@ -232,6 +232,13 @@ public void createSharedLinkSucceeds() {
232232

233233
assertThat(sharedLink.getURL(), not(isEmptyOrNullString()));
234234

235+
sharedLink.getPermissions().setCanDownload(false);
236+
BoxFile.Info info = uploadedFile.new Info();
237+
info.setSharedLink(sharedLink);
238+
uploadedFile.updateInfo(info);
239+
240+
assertThat(info.getSharedLink().getPermissions().getCanDownload(), is(false));
241+
235242
uploadedFile.delete();
236243
}
237244
}

src/test/java/com/box/sdk/BoxFolderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,15 @@ public void setFolderUploadEmailSucceeds() {
271271
BoxFolder.Info info = folder.new Info();
272272
info.setUploadEmail(uploadEmail);
273273
folder.updateInfo(info);
274-
uploadEmail = info.getUploadEmail();
275274

276275
assertThat(uploadEmail.getEmail(), not(isEmptyOrNullString()));
277276
assertThat(uploadEmail.getAccess(), is(equalTo(BoxUploadEmail.Access.OPEN)));
278277

278+
info.setUploadEmail(null);
279+
uploadEmail = info.getUploadEmail();
280+
281+
assertThat(uploadEmail, is(nullValue()));
282+
279283
folder.delete(false);
280284
}
281285
}

0 commit comments

Comments
 (0)